rsuinux

test wifi connexio esp8266

Jul 24th, 2016
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.97 KB | None | 0 0
  1. -- Test de connexion sur mon serveur et d'envoi de données
  2. -- Pour le moment, tests non concluants ...
  3. -- version nodemcu sur esp8266:
  4.  
  5. conf = {
  6.   wifi   = { ssid='<my ssid>', pwd='<my pass>' },
  7.   server = { port=80, ip='10.20.30.40', uri='/phytoepuration/formulaire-simple.php'},
  8.   valeur = { temperature="00,00", humidite="00,00", compteur=0 }
  9. }
  10.  
  11. id = 0
  12.  
  13. function Requete_Post( Securite )
  14.  
  15.     -- ici, appel pour lire la temperature et l'humidite */
  16.     lecture()
  17.  
  18.     mac=wifi.sta.getmac()
  19.     t="num=" .. mac .. "&temperature=" .. conf.valeur.temperature .. "&humidite=" .. conf.valeur.humidite
  20.     connexion = nil
  21.     connexion = net.createConnection(net.TCP, Securite)
  22.     connexion:on("receive", function(connexion, payloadout)
  23.         if (string.find(payloadout, "Status: 200 OK") ~= nil) then
  24.             print("Posted OK")
  25.         end
  26.     end)
  27.     connexion:on("connection", function(connexion, payloadout)
  28.         print ("Posting...")
  29.         connexion:send("POST ".. conf.server.uri
  30.         .. " HTTP/1.1\r\n"
  31.         .. "Host: ".. conf.server.ip .. "\r\n"
  32.         .. "Connection: close\r\n"
  33.         .. "User-Agent: ESP8266-01 (compatible; esp8266 Lua)\r\n"
  34.         .. "Accept: */*\r\n"
  35.         .. "Content-Type: application/x-www-form-urlencoded\r\n"
  36.         .. "Content-Length: " .. string.len(t) .. "\r\n\r\n"
  37.         .. t)
  38.     end)
  39.     connexion:on("disconnection", function(connexion, payloadout)
  40.         connexion:close()
  41.         print("Requette close\n")
  42.         collectgarbage()
  43.         requete_ok=false
  44.     end)
  45.     connexion:connect(conf.server.port , conf.server.ip)
  46. end
  47.  
  48.  
  49. function reseau_wifi()
  50.     wifi.setmode(wifi.STATION)
  51.     wifi.sta.config(conf.wifi.ssid, conf.wifi.pwd)
  52. end
  53.  
  54. function lecture()
  55.     conf.valeur.temperature = string.format("%.2f", 26.23)
  56.     conf.valeur.humidite  = string.format("%.2f", 60.95)
  57.     print("temp:"..conf.valeur.temperature.." / hum.:"..conf.valeur.humidite..".")
  58. end
  59.  
  60. reseau_wifi()
  61. Requete_Post( 0 )
Advertisement
Add Comment
Please, Sign In to add comment