Post your best Lua script examples here

User avatar
By jankop
#8684 Using api.thingspeak.com is easier than I thought. A little adjustment program and graphs are here. The client behaves very well and stably. Get your account and give it a try. This is the path that allows to make a battery powered sensor .
Image
Load program ts_fpdht.lua and dht.lua to ESP8266 with LuaLoader
Code: Select all--    Demo thingspeak.com client with sensor DHT11/22
--    Tested with Lua NodeMCU 0.9.5 build 20150127 floating point !!!
-- 1. Flash Lua NodeMCU to ESP module.
-- 2. Set in program ts_fpmdht.lua humidity sensor type. This is parameter typeSensor="dht11" or "dht22".
-- 3. Set in program ts_fpmdht.lua your thingspeak.com write API key
-- 4. You can rename the program ts_fpmdht.lua to init.lua
-- 5. Load program ts_fpmdht.lua and dht.lua to ESP8266 with LuaLoader
-- 6. HW reset module
-- 7. Login module to your AP - wifi.setmode(wifi.STATION),wifi.sta.config("yourSSID","yourPASSWORD")
-- 8. Run program ts_fpmdht.lua - dofile(ts_fpmdht.lua)
-- 9. The sensor is repeatedly read and data are send to api.thingspeak.com every minute.
--10. Minimal period for data send to api.thingspeak.com is 15s
--    The author of the program module dht.lua for reading DHT sensor is Javier Yanez
--    The author of the http client part is Peter Jennings

sensorType="dht22"          -- set sensor type dht11 or dht22
WRITEKEY="YourWriteApiKey"    -- set your thingspeak.com key
PIN = 4                --  data pin, GPIO2
   wifi.setmode(wifi.STATION)
   --wifi.sta.config("yourSSID","yourPASSWORD")
   wifi.sta.connect()
   tmr.delay(1000000)
   humi=0
   temp=0
   fare=0
--load DHT module and read sensor
function ReadDHT()
   dht=require("dht")
   dht.read(PIN)
   if sensorType=="dht11"then
   humi=dht.getHumidity()/256
   temp=dht.getTemperature()/256
   else
   humi=dht.getHumidity()/10
   temp=dht.getTemperature()/10
   end
   fare=(temp*9/5+32)
   print("Humidity:    "..humi.."%")
   print("Temperature: "..temp.." deg C")
   print("Temperature: "..fare.." deg F")
   -- release module
   dht=nil
   package.loaded["dht"]=nil
end
-- send to https://api.thingspeak.com
function sendTS(humi,temp)
conn = nil
conn = net.createConnection(net.TCP, 0)
conn:on("receive", function(conn, payload)success = true print(payload)end)
conn:on("connection",
   function(conn, payload)
   print("Connected")
   conn:send('GET /update?key='..WRITEKEY..'&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("disconnection", function(conn, payload) print('Disconnected') end)
conn:connect(80,'184.106.153.149')
end
ReadDHT()
sendTS(humi,temp)
tmr.alarm(1,60000,1,function()ReadDHT()sendTS(humi,temp)end)
User avatar
By MaxSchulz2
#8696 Hi, thank you very much for uploading the code.

Your code works for me except for the humidity reading with dht11. I tried 3 different dht11 and it always returns -1; is there anything that I missed? Temperature reading is good but a bit jumpy (maybe due to dth11).
User avatar
By jankop
#8699 I am change sensor and set "dht11" string only, reading/sending is without problem now.
DHT11 are very unstable and far worse than DHT22. I have five of them, but I would not buy them. But my program works well with them.
Humidity -1, it is attribute for bad data checksum.
What have you got resistor on sensor? It has a sensor blocking capacitor?
User avatar
By John W
#9504
MaxSchulz2 wrote:Hi, thank you very much for uploading the code.

Your code works for me except for the humidity reading with dht11. I tried 3 different dht11 and it always returns -1; is there anything that I missed? Temperature reading is good but a bit jumpy (maybe due to dth11).


Hi, i had a lot of trouble with my ESP-03 for a while with the DHT11. Got negative values for Humidity and wrong Temperature...

Tried a few script here with different results. The problem was that i was running an old version of nodeMCU, the one that was included with ESP8266Flasher. After upgrading to the latest everything began to work! Noob error!