Post your best Lua script examples here

User avatar
By Magnus
#24417 My setup is ESP8266 (ESP-12) with DHT22.
To get valid values out of DHT22 I must have NodeMCU version 0.9.5_20150318 (later version will get invalid values).
I use the latest version of DHT22.lua (https://github.com/javieryanez/nodemcu- ... /dht22.lua) BUT I needed to modify two lines of code in "Step 1: send out start signal to DHT22)".
First: change delay from 40 to 20000
Code: Select alltmr.delay(20000)

Second:
After the delay, set gpio to HIGH once again
Code: Select allgpio.write(pin, gpio.HIGH)


So it will look like this:
Code: Select all-- Step 1:  send out start signal to DHT22
  gpio.mode(pin, gpio.OUTPUT)
  gpio.write(pin, gpio.HIGH)
  tmr.delay(150)
  gpio.write(pin, gpio.LOW)
  tmr.delay(20000)
  --tmr.delay(40)
  --delayMicroseconds(40)
  gpio.write(pin, gpio.HIGH)
  gpio.mode(pin, gpio.INPUT)


Hope it helps!
User avatar
By aventrax
#24446
jankop wrote:Test it!
1. Is your sensor really connected to GPIO2 ?
2. Is sensor properly connected to ground?
3. Is your terminals wiring okay?
Save test program wots.lua and modul testdht.lua
Load it to esp8266 and run wots.lua.
Bring result.
Then we can talk of an additional cases.

This is proper communication with DHT11. DHT22 is very similar.
Code: Select alldofile("wots.lua")
checksum    58
checksumTest    58
humidity - timing of bits      0 1 5 1 1 5 1 4 1 1 1 1 1 1 1 1
temperat - timing of bits      1 1 1 5 1 5 1 4 1 1 1 1 1 1 1 1
checksum - timing of bits      1 1 5 5 5 1 5 1
Humidity:    37%
Temperature: 21 deg C


And here the sensor is disconnected .
Code: Select alldofile("wots.lua")
checksum    255
checksumTest    252
humidity - timing of bits      10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10
temperat - timing of bits      10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10
checksum - timing of bits      10 10 10 10 10 10 10 10
Humidity:    0%
Temperature: -127.99609375 deg C



Here's mine:

Code: Select allNodeMCU 0.9.5 build 20150318  powered by Lua 5.1.4
lua: cannot open init.lua
>
dofile(wots.lua)  martedì 28 luglio 2015  10:02:15

dofile("wots.lua")
checksum    239
checksumTest    239
humidity - timing of bits      0 1 2 1 1 2 1 5 5 5 5 2 1 5 1 1
temperat - timing of bits      2 2 1 1 1 1 1 5 1 1 1 1 5 1 1 5
checksum - timing of bits      5 5 5 2 5 5 5 5
Humidity:    1.890625%
Temperature: 1.03515625 deg C


With the ufficial code I'm able to get the right temperature but the humidity is always "-0.1", "-0.1.9" or "-1".... I tried another DHT22 (working on the GPIO of the raspberryPI) without any luck. I tried with or without the pullup between the data and vcc (3.3v).

As you can see the nodeMCU version is 0.9.5-20150318 but I tried 20150212, 20150213 with the same results. My ESP is version ESP-01 (tried two of them).

How can I get the right humidity?



[UPDATE]: I missed to set the sensortype to DHT22. It works!! Using your dhttest.lua even my scripts are working, not only temperature but also humidity! Can you explain what can be different from the "ufficial" library https://github.com/javieryanez/nodemcu- ... /dht22.lua compared to yours? Thanks Again!
Last edited by aventrax on Wed Jul 29, 2015 9:09 am, edited 1 time in total.
User avatar
By aventrax
#24574
jankop wrote:Different is one constant only.

Rows with comparing:
Code: Select all bitStrem[  ] > 4 or 2


Change, not 4 to 2, but to 3.


Does it makes sense to you? Why my dht "needs" a different value on that line? Does it depends on differences on the sensor itself?

I will test it with this modification and I will be back...