Post your best Lua script examples here

User avatar
By jankop
#30271 Here's new functional sketch, tested with DHT11:

Code: Select all--     thingspeak.com client with sensor DHT11,DHT22
--    Tested with Lua NodeMCU 0.9.6 (nodemcu_float_0.9.6-dev_20150704.bin)
--    Minimal period for data send to api.thingspeak.com is 15s
--
   DEBUGPRINT = true -- true: enable debugg print, false: disable debugg print
--****************************************************************
   WRITEKEY="yourAPIkey" -- set your thingspeak.com key
--****************************************************************
   ssid="yourSSID"    -- your router SSID
    pass="yourPass"   -- your router password
--****************************************************************
   pin=3   -- number of pin (GPIO0), where is DHTXX connected
   prs=15  -- period reading and sending sensors *s*
   wifi.setmode(wifi.STATION)
   wifi.sta.config(ssid,pass,1)

function debugprint(...)
   if DEBUGPRINT then
      print(...)
   end
end

--Read DHTXX sensor
function ReadDHT()
     status,temp,humi,temp_decimial,humi_decimial = dht.read(pin)
   if( status == dht.OK ) then
   debugprint("DHT Temperature:"..temp..";".."Humidity:"..humi)
   elseif( status == dht.ERROR_CHECKSUM ) then
   debugprint( "DHT Checksum error." );
   elseif( status == dht.ERROR_TIMEOUT ) then
   debugprint( "DHT Time out." );
   end
end

-- send data to https://api.thingspeak.com
function SendTS()
conn = net.createConnection(net.TCP, 0)
conn:connect(80,'api.thingspeak.com')   -- This row is good, but only for newer firmware
--conn:connect(80,'184.106.153.149')    -- This is worse, but it also works well with the older firmware.
conn:on("connection",
   function(conn) debugprint("Connection!")
   conn:send('GET /update?key='..WRITEKEY..
   '&headers=false'..
   '&field1='..humi..
   '&field2='..temp..
   ' HTTP/1.1\r\n'..
   'Host: api.thingspeak.com\r\nAccept: */*\r\nUser-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n\r\n')
   end)
conn:on("sent",
   function(conn)
   debugprint("Sent!")
      if DEBUGPRINT == false
      then
      conn:close()
      end
   end)
conn:on("receive",
   function(conn, payload)
   debugprint("Receive!")
   debugprint(payload)
   conn:close()
   end)
conn:on("disconnection",
   function(conn)
   debugprint("Disconnection!")
   end)
end

-- first reading sensors
  ReadDHT()
  SendTS()
-- Periodic reading of the sensor
tmr.alarm(0,prs*1000,1,
 function()
 if wifi.sta.getip()==nil
   then
   print("IP refresh")
   print("!!!!!!!!!!!!!!")
   node.restart()
   end
  ReadDHT()
  SendTS()
end)
Last edited by jankop on Thu Oct 01, 2015 1:50 pm, edited 1 time in total.
User avatar
By kniazio
#30283 > DHT Time out.
Connection!
Sent!
Receive!
HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
X-Cnection: close
Status: 200 OK
ETag: "ef003c7754ea2e6e7b977467cf19bc21"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: 1bdfed8c-3fe0-48f8-8231-ee172ddc6cf9
X-Runtime: 0.018335
X-Powered-By: Phusion Passenger 4.0.57
Date: Thu, 01 Oct 2015 19:07:01 GMT
Server: nginx/1.9.3 + Phusion Passenger 4.0.57

5
18849

Disconnection!
temp= -999