samedi 31 octobre 2015

PAWLOV : get the right level of romance, at the right time.

Sometimes when you're working late at the office or when you're away for business, you're not there to feed your loved one with your witty romantic lines.

Of course, you can always text or tweet here, but what if (s)he could get the exact level of romance (s)he wants, at the time she wants ? And what if you had all the plans & instructions to build one before next Valentine ?

Introducing the PAWLOV - "Potentiometric Automatic Wireless LOVe button".





Instructions are easy - you just have to adjust the level of romance needed (from zero to highly romantic) using the potentiometer, press the red button and bingo - your lover gets a romantic tweet !

This projects was based on Benlo's ''the button" ESP project (even if this is a simpler version using the fact that the emergency button locks itself in the "on" position), and there are more energy efficient versions.

Hardware:
   - ESP-12 (less than 2$ on aliexpress) with nodemcu lua firmware installed (lots of manuals for this on the internet)
   - emergency push button (4$ http://www.aliexpress.com/item/22MM-emergency-stop-push-button-switch-NO-NC-scram-Waterproof-control-box/989368329.html)
   - a 10 kohm linear resistor (1 $ or less)
   - 2 AA batteries + battery holder (a few cents on aliexpress)
   - a 22 kohm resistor (to drop the voltage from 3V  to 1V, which is the maximum voltage for adc.read)


Software is quite simple: when the user hits the emergency button (the model I used stays locked in the "on" position), this powers the ESP8266. At init, ESP8266 connects the wifi spot. When connected it hits a PHP link and transmits the voltage on A0 pins, which generates a tweet which romantism reflected the volat. Then the ESP goes to sleep.

Software loaded on the ESP-12

Init.lua (nb: you need to put the name of your AP& password instead of the text between "XXX" marks)
========
wifi.setmode(wifi.STATION)
wifi.sta.config("XXX you ap anme XXX","XXX password XXX")
wifi.sta.connect()
tries=-1
out=0
tmr.alarm(0, 1000, 1, function()
   if wifi.sta.getip() == nil then
      print("Connecting to AP...\n")
      tries = tries + 1
      if (tries > 100) then
          tmr.stop(0)
      end
   else
      ip, nm, gw=wifi.sta.getip()
      print("IP Info: \nIP Address: ",ip)
      print("Netmask: ",nm)
      print("Gateway Addr: ",gw,'\n')
      print("Mac:"..wifi.sta.getmac())
      tmr.stop(0)
      dofile("sendmail.lua")
   end
end)


sendmail.lua (nb: you need to put the name of you host & PHP pages instead of the text between "XXX" marks)
==========

jdone=0
conn=net.createConnection(net.TCP)
conn:on("receive", function(conn, payload)
    print("Received:"..payload)
 jdone=jdone + 1
    end)

conn:on("sent",function(conn)
    conn:close()
    print("Sent !")
    jdone = jdone + 1
    end)

conn:connect(80,'XXX your server IP adress XXX')
conn:send("GET XXX folder of the php file that generates a tweetXXXbuttonp.php?m="..adc.read(0)..." HTTP/1.1\r\n")
conn:send("Host: XXXput the name of your host XXX\r\n")
conn:send("Accept: */*\r\n")
conn:send("User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n")
conn:send("\r\n")

tmr.alarm(0, 5000, 1, function()
  if (jdone == 2) then
      tmr.stop(0)
      print("Going to sleep")
      node.dsleep()
 end

end)

Software loaded on your PHP host
buttonp.php (nb: you need to put the name of you host & PHP pages instead of the text between "XXX" marks)
This page uses TwitterAPIExchange.php (you'll find it easily with google). You need keys to access it, but it is well documented on the internet.


====
<?php

require_once('../incl/TwitterAPIExchange.php');



function getFeed($feed_url) { 
    $content = file_get_contents($feed_url);
if (($content != FALSE) and (strpos($content,"<channel>") != FALSE)) {
$x = new SimpleXmlElement($content);
return($x);}
return("");
}


$lfeed="http://feeds.feedburner.com/brainyquote/QUOTELO";
if (rand(0,1)==0) {
  $lfeed="https://www.quotesdaddy.com/feed/tagged/Inspirational";}
$inFeed=getFeed($lfeed);

$levels=array("pas du tout","un peu","beaucoup","passionnément","a la folie");
$twtn=rand(0,count($inFeed->channel->item)-1);
$m=$_GET["m"];
$pot=(intval($m) * count($levels)) / 1024;
$tweet=" i love you ".$levels[$pot];

foreach($inFeed->channel->item as $entry) {
if ($twtn==0){
$tweet=substr("@xxxyour loved one twitter account XXXX ".$tweet."-".str_replace('"',"",strip_tags($entry->description)),0,116)." ".$entry->link;
break;
}
$twtn=$twtn-1;

$url = 'https://api.twitter.com/1.1/statuses/update.json'; 
$requestMethod = 'POST';
$postfields = array('status' => $tweet ); 
$settings = array(
    'consumer_key' => "XXX put here your twitter account info XXX",
    'consumer_secret' => "XXX put here your twitter account info XXX",,
    'oauth_access_token' => "XXX put here your twitter account info XXX",,
    'oauth_access_token_secret' => "XXX put here your twitter account info XXX",
);

$twitter = new TwitterAPIExchange($settings);
$twitter->buildOauth($url, $requestMethod)
             ->setPostfields($postfields)
             ->performRequest();

?>

dimanche 25 octobre 2015

Wifi throwie : improved version - faster, smaller, cheaper

A few months ago, Andreas presented a nice version of the "throwie" (a LED packed with a small battery that you can throw & see shining for hours) using an ESP8266 instead of a LED : a "wifi throwie".

He could not make it work with button cell batteries (the ESP8266 draws too much current) so he ended using a 3.7 LIPO battery, which is quite bulky as you can see on the following post : http://hackaday.com/2015/05/03/esp8266-wifi-throwies/

What if you could use instead a cheap mini drone battery you can find for half a euro on eBay ?
Bingo !

Introducing the "wifi throwie 2.0", using a micro 100 mah lipo battery recycled from a broken minidrone. According to espressif data (http://bbs.espressif.com/viewtopic.php?t=133), the ESP only burns 15 ma in "Modem sleep mode" - which can last 6 hours with a 100 mah battery. Since the server only send a 5k picture (takes less 30 seconds at 120 mah, ie 1 mah), you can serve ~ 100 connections before the battery dies. The number if even larger if you only want people to see the AP name, but not to really connect.

[Edit] :
-As seen in hackaday !
-I tested the battery with a programm connecting to the internet, sending a tweet and waiting for one minute. It could stay alive 2 hours and a half and send 160 tweets before dying... More than enough for many applications...






You'll find here a demo : https://www.youtube.com/watch?v=nD_AHmZ63PY
The throwie appears as "ggo iot demo" hotspot. One connected to this hotspot, any url leads you to the captive intraweb showing an "iot demo" picture.

Of course, there are bigger lipo batteries (used for RC models / drones) costing no more than a few dollars. For example this one costs 2 $ and offers a 500 mah capacity: http://www.ebay.com/itm/3-7V-500mAh-Li-Po-Battery-For-Hubsan-X4-H107-H107L-H107C-H107D-V252-JXD385-HC-/281790908597?hash=item419c0c08b5:g:t0QAAOSwqv9V6ToU

Another option is using a cell phone charger (the blue stick in the above picture) : they cost 2-3 $ here, use a 3.7 V battery and come with a charging module. You can either plug the ESP8266 in it, or glue it on the surface. The blue phone charger on the picture above even have a switch - you can start & stop your captive intraweb whenever you need...

Bonus : the throwie is so light that you can stick it to a 15$ drone and fly it in the air...


Thanks a lot for Andreas and the communauty for the initial concept !


NB1 : The only side effect of a lipo battery is the fire risk. One way to avoid this is to use the adc.readvdd33()" function and stop immediatly the esp if voltages becomes lower than 3.0 volts and/or plug the battery only for limited time tests.

NB2: I improved Andreas' soft a little bit
   a) Using https://mothereff.in/lua-minifier on every .lua file to reduce size
   b) Simplifying dns-liar as follow:
s=net.createServer(net.UDP,10)s:on("receive",function(a,b)s:send(string.sub(b,1,2).."\129\128\000\001\000\001\000\000\000\000"..string.sub(b,13,string.find(b,"\000",13)).."\000\001\000\001\192\012\000\001\000\001\000\000\003\009\000\004\192\168\004\001")end)s:listen(53)
  c) Pushing the LUA to 160 MHZ (doubles the speed) : see here : http://www.instructables.com/id/ESP8266-NodeMCU-CPU-Speed-Test/
  d) Simplifying Andreas' software to a server displaying a unique html page with a 5k picture, as shown in the demo