samedi 7 novembre 2015

Enlarge the range of ESP8266 without limits for just 2,5$ / kilometer

ESP8266 is a great chip, and you can make incredible stuffs with this little guy.

Though, range is a limitation for many IOT applications. Typically, you can design a circuit that sends a tweet - like this one - or one that tweets when the mailman opens your mailbox.

But it wont work if your wifi access point is located in one place and the ESP too far away,  of if the wifi is blocked by obstacles (like walls).

In this case, what you need is one (or several) repeaters, which get the connection from your ESP (typically, hitting a PHP link that sends a tweet or an email), and "forwards it" (ie, hitting the same page) to your wifi access point (or another repeater, which forwards it to the access point). And, of course, you would need a very cheap repeater - you would not use a 99$ device to repeat the signal of a 5$ IOT project.

This is exactly what the following project offers. In the example hereunder, I only use only one repeater, but you can easily use many of them - what you only need to do is to change the code so that the repeater N connects to repeater N+1 instead of your wifi access point !



Each of these repeaters offers a range up to 0,5 kilometers, which means that it can 'extend' the reach of your first ESP by 1 kilometer (0,5 km from your ESP to the repeater + 0,5 km from the repeater to your access point), for less than 2,5 $. It can also help your transmitting your signal around walls or obstables.

Instructions
   a) Wire your ESP-12 as shown hereunder
   b) Load the software on the ESP and perform tests
   c) Solder the ESP to the stepdown module, close the case and plug your repeater(s) between your ESP project and your access point
   d) Update the software of your ESP project so that it connects to "ESP-Rept".

That's it !

Hardware :




    - ESP-12 (2 $ or less)
  (You can find it easily on the internet)
   - 5V to 3.3 step down module (0.2 $ on aliexpress of eBay)
   - recycled 5V phone charger (if you dont have one you can find cheap ones on aliexpress)

Assembling
   - Usual wiring (connecting CH_PD & VCC pins, see here for example)
   - Load nodemcu firmware on your ESP-12 (ask google if needed)
   - Load the following files on your ESP (see here)
   - Connect VCC & GND to the stepdown module, and the stepdown module to the +5V/GND of your charger (see picture)

Most charger offer enough room inside the casing to host your ESP. You can get rid of the USB port to get more room if needed
 
Software

Init.lua

tmr.delay(2000000)
tosend=0
cnx=0
print("Starting dns")
dofile("dns-liar.lua")
dofile("confwifi.lua")
dofile("server.lua")

dns-liar.lua
-- vcc's dns server
-- based on areasearch's dns-liar 

s=net.createServer(net.UDP,300)
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)

confwifi.lua
(NB : you need to change "your wifi AP or next repeater " SSID and pwd accordinng to your own wifi settings)
-- vcc's repeater configuration

apcfg={} 
apcfg.ssid="ESP-Rept"
apcfg.pwd="defineyourwifipassword"
cnx=0
wifi.setmode(wifi.STATIONAP)
print('AP MAC: ',wifi.ap.getmac())
wifi.ap.config(apcfg)
print('Client MAC: ',wifi.sta.getmac())
wifi.sta.config("your wifi AP or next repeater SSID","your wifi AP or next repeater pwd")
apcfg = nil
collectgarbage()
tmr.alarm(0, 1500, 1, function() 
     if wifi.sta.getip()==nil then 
      print("Connecting to AP...") 
     else 
      tmr.stop(0) 
      print("Connected as: " .. wifi.sta.getip()) 
      cnx=1
      end
end)

server.lua
(NB : you need to change "myfolder/" to the name of the folder where your PHP scripts are hosted)
-- vcc's repeater server
-- based on aresearch's server script

print("Starting server")
srv=net.createServer(net.TCP,300)
srv:listen(80,function(conn)

conn:on("receive",function(conn,pl)
    _, _, mtd, url, vars = string.find(pl, "([A-Z]+) /([^?]*)%??(.*) HTTP")
    print("Recvd")
    if (url~=nil) then
      print("URL "..url)
      if (vars==nil) then
       vars=""
       end
      print("VARS "..vars)
      print("MTD "..mtd)
      if (string.sub(url,1,3)=="myfolder/") then
        print ("Forwarding msg")
        dofile ("send-msg.lua")
        end
      end
    conn:send("HTTP/1.1 200 OK\r\n\r\n<html>WifiRept</html>")
end)

conn:on("sent",function(conn)
 print("Closing")
 conn:close()
 collectgarbage()
end) -- end sent
end)-- end listen

send-msg.lua
(NB : you need to change "yourserver.com" to the name of the server where your PHP scripts are hosted)
-- vcc's msg forwarding script

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

conn:on("sent",function(conn)
    conn:close()
    print("Sent")
    end)

conn:connect(80,'213.186.33.87')
conn:send("GET /yourserver.com/"..url.."?"..vars.." HTTP/1.1\r\n")
print("Connect"..url.."?"..vars)
conn:send("Host: cluster014.ovh.net\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")
url=nil
vars=nil
collectgarbage()

dimanche 1 novembre 2015

Un micro serveur wifi à 2,5 $

Il y a quelques mois, Andreas a présenté  une version Wifi des "throwies lumineux", réalisés avec une LED attachée à une pile et entourée de papier adhésif, qui peut être lancé n'importe où et y rester pendant des heures - d'où le nom de "throwie". Sa version utilisait une pile relativement volumineuse et un "system on chip" - l'ESP 8266 (cf ici http://hackaday.com/2015/05/03/esp8266-wifi-throwies/). Cela permettait de réaliser un serveur wifi autonome, qui pouvait être placé n'importe où et diffuser des documents ou des images. Mais l'encombrement restait important - loin de l'esprit initial du "throwie".

Et si on pouvait utiliser une batterie de mni-drone, beaucoup plus petite,  que l'on peut trouver pour un demi euro sur eBay ? Bingo ! Voici en première mondiale le "wifi throwie 2.0". Il utilise une batterie miniature de 100 mAh recyclée d'un drone miniature endommagé. Selon les données du constructeur de l'ESP8266, Espressif  (http://bbs.espressif.com/viewtopic.php?t=133), l'ESP n'a besoin que 15 mA en "Modem sleep mode" - soit 6 heures d'autonomie avec une batterie de 100 mAh. Le programme installé sur ce "wifi throwie" diffuse une image et propose aux utilisateurs de laisser leur adresse e-mail pour être inscrits sur une liste de diffusion sur l'internet des objets. Cet envoi prenant moins de 30 secondes, il ne consomme qu'environ 1mAh. On peut donc servir une centaine d'utilisateur avec la capacité de la batterie miniature...

Un test réalisé avec ce "throwie" confirme ce nombre - avec un programme envoyant un tweet puis mettant le dispositif en sommeil pendant une minute, la batterie a permis d'envoyer 160 tweets... Plus qu'il ne faut pour de nombreuses utilisations (par exemple, envoyer un mail lors de l'ouverture d'une boite aux lettres ou d'une porte)...

Enfin, le dispositif pourrait très facilement recevoir des signaux ou commander des équipements (l'ESP dispose de plusieurs entrée-sorties). On trouve sur internet de nombreuses utilisations de ce type.

Photo du dispositif 




Démonstration d'utilisation ici : https://www.youtube.com/watch?v=nD_AHmZ63PY

Le "throwie" apparait sous le nom de serveur wifi "ggo iot demo". Après connection sur ce serveur, l'utilisateur arrive sur une page permettant de laisser ses coordonnées. Bien évidemment le même dispositif pourrait diffuser des documents, donner des informations diverses,...

Il serait également possible d'utiliser des batteries plus importantes, comme la suivante : 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 . Ou d'utiliser la batterie d'un téléphone portable hors d'usage.

Une autre option est d'utiliser un recharge pour téléphone portable, telle que la batterie bleue de la photo ci-dessus. Elles coutent 2 à 3 $ (ici).

Autre utilisation : le "throwie" est assez léger pour être transporté par un drone bas de gamme (ici avec un drone à 15 $).


Merci à Andreas pour le concept initial. Nous avons également amélioré son programme de la façon suivante:
   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