daily pastebin goal
50%
SHARE
TWEET

30.07.2016 ESP8266 LUA Si7021 send to web with deepsleep

a guest Jul 30th, 2016 174 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. starttime = tmr.now()
  2. -- setup Wifi only once after power cycle
  3. local val = rtcmem.read32(0)
  4. if val ~= 32767 then
  5.   wifi.setmode(wifi.STATION)
  6.   wifi.setphymode(wifi.PHYMODE_N)
  7.   wifi.sta.config("<SSID>", "<Passwort>")
  8.   -- save more power by using static IP instead of DHCP
  9.   wifi.sta.setip({ip="<localIP>",netmask="255.255.255.0",gateway="<Gateway IP>"})
  10.   wifi.sta.connect()
  11.  
  12.   rtcmem.write32(0,32767)
  13. end
  14.  
  15. -- read AM3220 / Si7021 on GPIO2/0 = Pin 4 / 3 -
  16. sda_pin = 4
  17. scl_pin = 3
  18. si7021 = require("si7021")
  19. si7021.init(sda_pin, scl_pin)
  20. si7021.read(status)
  21.  
  22. wifi.sta.status()
  23. --print(wifi.sta.getip())
  24.  
  25.  
  26. -- Deep sleep time in ms (300s)
  27. deepsleep_time = 300000
  28.  
  29. local sensor = "mobile"
  30. local host_ip = "<Empfänger-IP>"
  31.  
  32. function loop()
  33.   if wifi.sta.status() == 5 then
  34.     -- Stop the wifi connection wait-loop
  35.     tmr.stop(0)
  36.    
  37.     conn=net.createConnection(net.TCP, 0)
  38.     conn:on("receive", function(conn, payload)
  39. --      print(payload);
  40.     end)
  41.     conn:connect(80,host_ip)
  42.     conn:on("connection", function(conn, payload)
  43. --      print("Connected")
  44.       -- read battery voltage
  45.       local vbat = 5.555*(adc.read(0)/1024)
  46.       local humi = si7021.getHumidity()
  47.       local temp = si7021.getTemperature()
  48. --        buf = temp.."\n"..humi.."\n"
  49.       local my_table = {}
  50.       my_table[#my_table + 1]="GET /send.py?sensor="
  51.       my_table[#my_table + 1]=sensor
  52.       my_table[#my_table + 1]="&temp="
  53.       my_table[#my_table + 1]=string.format("%.2f",(temp/100))
  54.       my_table[#my_table + 1]="&hum="
  55.       my_table[#my_table + 1]=string.format("%.2f",(humi/100))
  56.       my_table[#my_table + 1]="&vbat="
  57.       my_table[#my_table + 1]=string.format("%.3f",vbat)
  58.       my_table[#my_table + 1]="&time="
  59.       my_table[#my_table + 1]=string.format("%.3f",((tmr.now() - starttime) / 1000000))
  60.       my_table[#my_table + 1]=" HTTP/1.1\r\nHost: "
  61.       my_table[#my_table + 1]=host_ip
  62.       my_table[#my_table + 1]="\r\nAccept: */*\r\n"
  63.       my_table[#my_table + 1]="User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n\r\n"
  64.       local my_string=table.concat(my_table)
  65.       conn:send(my_string)
  66.     end)
  67.     conn:on("disconnection", function(conn, payload)
  68. --      print('Disconnected')
  69.       conn:close()
  70. --      print(buf)
  71. --      collectgarbage()
  72. --    print("Going to deep sleep for "..(deepsleep_time/1000).." seconds")
  73.       node.dsleep(deepsleep_time*1000)
  74. --      tmr.delay(1000)
  75.     end)
  76.   else
  77. --    print("Waiting for Wifi ...")
  78.   end
  79. end
  80.  
  81. tmr.alarm(0, 100, 1, function() loop() end)
  82.  
  83. -- Watchdog loop, will force deep sleep if the operation somehow takes to long
  84. tmr.alarm(1,15000,1,function() node.dsleep(deepsleep_time*1000) end)
  85. -- eof
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top