Current Lua downloadable firmware will be posted here

User avatar
By Sleyar
#43716 I'm trying to read the temperature and humidity from a DHT22 sensor with the ESP8266-12. So far I tried many different versions of the firmware (with DHT compiled into the firmware and with the dht22.lua included in script).
Current firmware: http://nodemcu-build.com/ default settings + DHT

It all results in the same. Humidity is either 1 or nill. When I breath at the sensor, I get some results between 30 en 99. Time between measurement is at least 3 seconds since it can become a problem.

Can someone take a look at the connections / code below. Anything I can try to make it work?


Connections:

ESP8266-12:
VCC ----> 3.3V of power supply
GND ----> Ground of power supply
CH_PD ----> HIGH (3.3V)
GPIO2 ----> HIGH (3.3V)
GPIO15 ----> LOW (GND)
GPIO0 ----> HIGH (3.3V)
GPIO16 ----> RST

DHT22
PIN1: --> 3.3V of power supply
PIN2: --> GPIO12 of ESP8266-12
PIN4: --> Ground of power supply


Code:

init.lua

Code: Select allfunction startup()
    if abort == true then
        print('startup aborted...')
        return
        end
    print('starting user.lua...')
    dofile('user.lua')
    end

abort = false
print("1 seconds to abort user.lua...")
tmr.alarm(0,1000,0,startup)


user.lua

Code: Select all--- WiFi Connect
wifi.setmode(wifi.STATION)
wifi.sta.config("ssid","password")
tmr.alarm(1, 1000, 1, function()
    if wifi.sta.getip()== nil then
        print("IP unavaiable, Waiting...")
    else
        tmr.stop(1)
        print("ESP8266 mode is: " .. wifi.getmode())
        print("Connected...")
        print("MAC: " .. wifi.ap.getmac())
        print("IP :".. wifi.sta.getip())

        -- Read sensor and post data

        -- Read DHT22 Sensor on GPIO12 (pin 6)
        status, temp, humi, temp_dec, humi_dec = dht.read(6)
        --todo: add some errorchecking for dht.read

        -- Post data
        print("Sending data:"..temp.." "..humi..".")
        conn=net.createConnection(net.TCP, 0)
        conn:on("receive", function(conn, payload) print(payload) end )
        conn:on("connection", function(c)
            conn:send("GET /update?api_key=notsosupersecretkey="..temp.."&field2="..humi..""
            .. " HTTP/1.1\r\n"
            .. "Host: api.thingspeak.com\r\n"
            .. "Connection: close\r\n"
            .. "Accept: */*\r\n"
            .. "User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n"
            .. "\r\n")
        end)
        conn:connect(80,"54.88.155.198")

        tmr.alarm(2, 1000, 1, function()
            if conn == nil then -- is this the right way to make the program wait before deepsleep?
                print("Still sending data, please wait...")
            else
                tmr.stop(2)
                print("Going to sleep for 55secs...")
                node.dsleep(55000000)
            end
        end)
    end
end)
User avatar
By TerryE
#43743 I think that you will find that this is a hardware issue rather than a firmware one. Make sure that your have the right pull-ups etc.
User avatar
By Sleyar
#43867 Thought that too :(...
Already tried all kinds of pullup resistor values, but they all gave the same result.
User avatar
By xraynaud
#44204 with my dht22, I have a 4.7k resistor between +3.3 and pin2.

I read the sensor with the following function which is identical to yours:
Code: Select allfunction readDHT(pin)   
   status,tempdht,humidht,temp_decimal,humi_decimal = dht.read(pin)
   if( status == dht.ERROR_CHECKSUM or status == dht.ERROR_TIMEOUT  ) then
        tempdht = nil
        humidht = nil
     end   
     return tempdht,humidht
end