ESP01s DHT11 温度湿度传感器

warning: 这篇文章距离上次修改已过323天,其中的内容可能已经有所变动。

前面说到了如何烧录ESP01s的固件

这回我们试试做个小玩具(1/3)

基于ESP01s和DHT11物联网传感器节点

359436538.jpg359436538.jpg

其实本来应该定时发送数据到指定的API那里。比如 thingspeak。

但是,奈何网络不稳定。

所以这回这里采用的是向ESP01s发送请求,ESP01s把温湿度以json格式返回。我们也不需要在局域网内配置一个接收服务了。

如果还希望随时随地的查看这些信息的话,还得配置内网穿透。

其实主要还是懒

当然,如果希望记录温度、湿度变化曲线的话,还是别偷懒了。

好的,那我们开始吧!


烧录lua固件并连接到工作模式

这里我们不再啰嗦一遍,如何烧录上传固件了,需要的话可以看这里


代码

在ESP01s这个Lua固件中,每次初始化完成都会运行init.lua。这个文件就是它的程序入口

init.lua

这里,设置了ESP01s工作在STATION模式,作为一个wifi的客户端。

等连接成功获取到IP就执行dht11.lua,否则重新尝试连接,重试10次后重启设备。

getTemp()函数用来在IO2上获取DHT11的数据。


--init.lua
-- tcp client
print("ESP8266 DHT11 Client1 Starting...")
wifi.sta.disconnect()
tmr.delay(100)
wifi.setmode(wifi.STATION) 
wifi.sta.config("360 N7 Pro","123456780") -- connecting to server
wifi.sta.connect() 
i=0
tmr.alarm(0,10000, 1, function() 
    if wifi.sta.getip()== nil then 
        print("Connect WiFi, Waiting...")
        i=i+1
        if(i>10) then
            print("Restart nodeMCU !")
            node.restart()
        end
        wifi.sta.disconnect()
        wifi.sta.connect()
    else 
        tmr.stop(0)
        print("WiFI Done, IP:"..wifi.sta.getip())
        print("Runing dht11.lua")
        dofile("dht11.lua")
    end 
end)

dht11.lua

这里提供了一个简易web服务器,把温度、湿度以json格式返回。

-- Measure temperature, humidity 
---Sensor DHT11 is conntected to GPIO2
pin = 4
Humidity = 0
HumidityDec=0
Temperature = 0
TemperatureDec=0
Checksum = 0
ChecksumTest=0


srv=net.createServer(net.TCP)
srv:listen(80,function(conn) 
    conn:on("receive", function(client,request)
        local buf = "";
        buf = buf.."HTTP/1.1 200 OK \t\n"
        buf = buf.."Access-Control-Allow-Origin: * \t\n"
        buf = buf.."Content-Type: application/json  \n\n"
        local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP");
        if(method == nil)then
            _, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP");
        end
        local _GET = {}
        if (vars ~= nil)then
            for k, v in string.gmatch(vars, "(%w+)=(%w+)&*") do
                _GET[k] = v
            end
        end
        
        if(_GET.token == "tcp")then
              getTemp()
              --buf = buf.."T="..Temperature.."."..TemperatureDec.."  H="..Humidity.."\r\n"
              buf = buf.."[{\"T\":\""..Temperature.."."..TemperatureDec.."\",\"H\":\""..Humidity.."\"}]"
              
        else
              buf = buf.."[{\"token\":\"error\"}]"
        end
        client:send(buf);
        client:close();
        collectgarbage();
    end)
end)


function getTemp()
Humidity = 0
HumidityDec=0
Temperature = 0
TemperatureDec=0
Checksum = 0
ChecksumTest=0

--Data stream acquisition timing is critical. There's
--barely enough speed to work with to make this happen.
--Pre-allocate vars used in loop.

bitStream = {}
for j = 1, 40, 1 do
     bitStream[j]=0
end
bitlength=0

gpio.mode(pin, gpio.OUTPUT)
gpio.write(pin, gpio.LOW)
tmr.delay(20000)   --default 20000
--Use Markus Gritsch trick to speed up read/write on GPIO
gpio_read=gpio.read
gpio_write=gpio.write

gpio.mode(pin, gpio.INPUT)

--bus will always let up eventually, don't bother with timeout
while (gpio_read(pin)==0 ) do end

c=0
while (gpio_read(pin)==1 and c<100) do c=c+1 end

--bus will always let up eventually, don't bother with timeout
while (gpio_read(pin)==0 ) do end

c=0
while (gpio_read(pin)==1 and c<100) do c=c+1 end

--acquisition loop
for j = 1, 40, 1 do
     while (gpio_read(pin)==1 and bitlength<10 ) do
          bitlength=bitlength+1
     end
     bitStream[j]=bitlength
     bitlength=0
     --bus will always let up eventually, don't bother with timeout
     while (gpio_read(pin)==0) do end
end

--DHT data acquired, process.

for i = 1, 8, 1 do
     if (bitStream[i+0] > 2) then
          Humidity = Humidity+2^(8-i)
     end
end
for i = 1, 8, 1 do
     if (bitStream[i+8] > 2) then
          HumidityDec = HumidityDec+2^(8-i)
     end
end
for i = 1, 8, 1 do
     if (bitStream[i+16] > 2) then
          Temperature = Temperature+2^(8-i)
     end
end
for i = 1, 8, 1 do
     if (bitStream[i+24] > 2) then
          TemperatureDec = TemperatureDec+2^(8-i)
     end
end
for i = 1, 8, 1 do
     if (bitStream[i+32] > 2) then
          Checksum = Checksum+2^(8-i)
     end
end
ChecksumTest=(Humidity+HumidityDec+Temperature+TemperatureDec) % 0xFF

print ("Temperature: "..Temperature.."."..TemperatureDec)
print ("Humidity: "..Humidity.."."..HumidityDec)
print ("ChecksumReceived: "..Checksum)
print ("ChecksumTest: "..ChecksumTest)
end

测试&效果

2019-12-12_214154.png2019-12-12_214154.png

添加新评论

* 已开启反垃圾保护,《隐私政策》