Post your best Lua script examples here

User avatar
By krzysztofk
#35562 Hello!

First, I wanted to thank jankop and all for sharing their experiences with feeding ESP with DHTxx sensor data. I also have made tiny "weather station" with DHT11 onboard. In my project ESP goes to deep sleep for 15 minutes and then reads temp and humidity and sends it along with vdd to thingspeak.

As some have mentioned before, I have also noticed that readings were somewhat unstable. It occurred that in fact having RF circuitry ON and reading DHT at the same time caused this glitchy readout (although checksums were OK!). The solution was to:
1. read DHT before invoking wifi.sta.connect()
2. read DHT twice for having current data

Obligatory before/after image in the attachment :)

Best regards,
Krzysztof
Attachments
DHT11_before_after.png
DHT11 readout - before and after the fix
User avatar
By krzysztofk
#38167 Hey Glitch,

I'm posting the code to the whole community, maybe someone will need it too. It is a mix of few scripts I've seen on the forum and the net plus my own tweaks. Please note that the code will work only on modified modules with deep sleep enabled.

init.lua:
Code: Select allloaded_chunk = assert(loadfile("program.lua"))
loaded_chunk()

DSLEEPTIME=15*60*1000000 --15 minutes

wifi.sta.disconnect() --read DHT&ADC data without WIFI noise
--read DHT data two times; as written in DHT's datasheet:
--Note: The host reads the temperature and humidity data from DHT11 always the last measured
--value, such as twice the measured interval of time is very long, continuous read twice to the second
--value of real-time temperature and humidity values.
readDHT()
tmr.delay(1000000) --wait 1 second
readDHT()
vdd=adc.readvdd33()/1000
wifi.setmode(wifi.STATION)
wifi.sta.config("name","password") --now connect AP

function Failure()
   tmr.stop(0)
   node.dsleep(DSLEEPTIME,4)
   return 0
end
   
tmr.alarm(0,30000,0, function() Failure() end)
tmr.alarm(1, 1000, 1, function()
  ip = wifi.sta.getip()
  if ip ~= nil then --wait for proper IP
      tmr.stop(0)                   
      tmr.stop(1)
      sendTS(humi,temp,vdd,status)
  end
end)


program.lua:
Code: Select allWRITEKEY="key"    -- set your thingspeak.com key
--PIN = 4                --  data pin, GPIO2
PIN = 3                --  data pin, GPIO0
function readDHT()
    status,temp,humi,temp_decimial,humi_decimial = dht.read(PIN)
    if( status == dht.OK ) then
      print("DHT Temperature:"..temp..";".."Humidity:"..humi)
    else
      humi=0
      temp=0
        if( status == dht.ERROR_CHECKSUM ) then
            print( "DHT Checksum error." );
        elseif( status == dht.ERROR_TIMEOUT ) then
            print( "DHT Time out." );
        end
    end
end
-- send to https://api.thingspeak.com
function sendTS(humi,temp,vdd,status)
conn = nil
conn = net.createConnection(net.TCP, 0)
conn:on("receive",
    function(conn, payload)
    conn:close()
    print(payload)
    end)
conn:on("connection",
    function(conn, payload)
    print("Connected")
    conn:send('GET /update?key='..WRITEKEY..
    '&field1='..temp..
    '&field2='..humi..
    '&field3='..vdd..
    '&field4='..status..
    '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')
    node.dsleep(DSLEEPTIME,4)
    end)
conn:connect(80,'api.thingspeak.com')
end