概述
链接: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)
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)
最后
以上就是甜美犀牛为你收集整理的【ESP8266之LUA开发】十一、实战DHT11获取温湿度绘制曲线到APP的全部内容,希望文章能够帮你解决【ESP8266之LUA开发】十一、实战DHT11获取温湿度绘制曲线到APP所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复