【ESP8266之LUA开发】十一、实战DHT11获取温湿度绘制曲线到APP

举报
ReCclay 发表于 2022/02/22 02:03:11 2022/02/22
【摘要】 链接:https://pan.baidu.com/s/1VA8AX7HV5ykdBvLnK-wTTA 密码:ssmz init.lua --[[ GPIO0--3 GPIO1--10 GPIO...

链接:https://pan.baidu.com/s/1VA8AX7HV5ykdBvLnK-wTTA 密码:ssmz


这里写图片描述

init.lua

--[[
GPIO0--3
GPIO1--10
GPIO2--4
GPIO3--9
GPIO4--2
GPIO5--1
GPIO9--11
GPIO10--12
GPIO12--6
GPIO13--7
GPIO14--5
GPIO15--8
GPIO16--0
0--GPIO16   1--GPIO5   2--GPIO4   3--GPIO0   4--GPIO2
5--GPIO14   6--GPIO12  7--GPIO13  8--GPIO15  9--GPIO3
10--GPIO1   11--GPIO9  12--GPIO10
]]

configwifissid = "CMCC_4148";
configwifipwd="61034148";
MqttUserString = "CLAY";
MqttPwdString = "11223344";
MqttIPString = "140.143.4.235";
MqttPort = 1883;

clientid = wifi.sta.getmac()
SubscribeTopic = "/pub"
PublishTopic = "/sub"

RelayPin = 2
wifi.setmode(wifi.STATIONAP)
apcfg={}
apcfg.ssid=configwifissid
apcfg.pwd=configwifipwd
wifi.sta.config(apcfg)
wifi.sta.autoconnect(1)
print("dofile init.lua")

tmr.alarm(0, 3000, 0, function()
   dofile("mqtt.lua");
   print("dofile mqtt.lua")
end)



  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

mqtt.lua

globalSendData ="nil"; 
globalSendData1="nil";
globalSendData2="nil";

UsartReceiveData="";
UsartReceiveDataCnt=0;
UsartReceiveDataCntCopy=0;


if  file.open("relay.lua", "r") then
    if  file.read() == "relay=1" then
        gpio.write(RelayPin,1)
        gpio.mode(RelayPin,gpio.OUTPUT)
        print("relay=1")  
    else
        gpio.write(RelayPin,0)
        gpio.mode(RelayPin,gpio.OUTPUT)
        print("relay=0")
    end
end
file.close() 


Mymqtt = mqtt.Client(clientid, 120,MqttUserString, MqttPwdString);

tmr.alarm(3, 1000, 1, function()
    Mymqtt:connect(MqttIPString, MqttPort, 0,ConnectSuccess,ConnectFailed)
end)

function ConnectSuccess(client)
     client:subscribe(SubscribeTopic, 0, subscribeSuccess)
     print("connected")
     mqttClient = client;
     tmr.stop(3);
     mqttConnectedFlage = 1;
end
function ConnectFailed(client,reason)
   mqttConnectedFlage = 0;
   print("failed reason: " .. reason)
   tmr.start(3)
end
function subscribeSuccess(client)
    print("subscribe success") 
end


Mymqtt:on("message", function(client, topic, data) 
    uart.write(0,data)
    if data == "switch;relay=0" then
       gpio.write(RelayPin,0)
       gpio.mode(RelayPin,gpio.OUTPUT)
       if  file.open("relay.lua", "w+") then
           file.write("relay=0")
           file.close() 
       end
    elseif data == "switch;relay=1"  then    
       gpio.write(RelayPin,1)
       gpio.mode(RelayPin,gpio.OUTPUT)
       if  file.open("relay.lua", "w+") then
           file.write("relay=1")
           file.close() 
       end
    end
end)


tmr.alarm(4, 10, 1, function()
    if  mqttClient ~= nil and mqttConnectedFlage == 1 then
        RelayNowState = gpio.read(RelayPin)
        if  RelayNowState ~= RelayNowStateCopy then
            RelayNowStateCopy = RelayNowState
            globalSendData1 = "relay="..RelayNowState
        end
    
        if  globalSendData1~="nil" then
            globalSendData=globalSendData1;
            globalSendData1="nil"
        elseif globalSendData2 ~="nil" then
            globalSendData=globalSendData2;
            globalSendData2="nil"
        else
            globalSendData="nil";    
        end
        if  globalSendData ~= "nil" then
            mqttClient:publish(PublishTopic,globalSendData, 0, 0, function(client) 
            end)
        end
    end 
    
    if UsartReceiveDataCnt ~= 0 then
       if UsartReceiveDataCntCopy == UsartReceiveDataCnt then
          UsartReceiveDataCnt=0;
          UsartReceiveDataCntCopy = 0;
          globalSendData2 = UsartReceiveData;
          UsartReceiveData="";
       else
          UsartReceiveDataCntCopy = UsartReceiveDataCnt;
       end
    end
end)


uart.setup(0, 115200, 8, uart.PARITY_NONE, uart.STOPBITS_1)

uart.on("data",0,function(data) 
    UsartReceiveData = UsartReceiveData..data;
    UsartReceiveDataCnt = UsartReceiveDataCnt + 1;
end, 0)



DHT11pin = 4--DHT11 GPIO


tmr.alarm(6, 2000, 1, function()--Every other 1S
    status, Temperature, Humidity, temp_dec, humi_dec = dht.read11(DHT11pin)--Gathering temperature and humidity                 
    if  status == dht.OK or status == dht.ERROR_CHECKSUM then
        globalSendData2 = "data;".."Tem="..Temperature..";".."Hum="..Humidity
    elseif status == dht.ERROR_TIMEOUT then
        --print( "DHT timed out." )
    end
end)





  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126

文章来源: recclay.blog.csdn.net,作者:ReCclay,版权归原作者所有,如需转载,请联系作者。

原文链接:recclay.blog.csdn.net/article/details/82026161

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。