ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

nodeMCU-esp8266 lua---DHT11采集温湿度上报阿里云

2020-06-28 13:41:57  阅读:497  来源: 互联网

标签:function tmr end esp8266 .. 温湿度 lua print data


引脚接的是PD5

wifitab={}
wifitab.ssid = "lei"
wifitab.pwd = "leizhe123"
wifi.setmode(wifi.STATION)
wifi.sta.config(wifitab)
wifi.sta.connect()

timer1 = tmr.create()
timer2 = tmr.create()


ProductKey = "a1BDinky4Et"
DeviceName = "demo_voice"
DeviceSecret = "KaFwazd5x4AjHdsFQ2HFluIjOZkUg0ZE"
RegionId = "cn-shanghai"
ESP8266ClientId = 20200510

SubTopic="/a1BDinky4Et/demo_voice/user/get"
Pubtopic="/sys/a1BDinky4Et/demo_voice/thing/event/property/post"

BrokerAddress = ProductKey..".iot-as-mqtt."..RegionId..".aliyuncs.com"
BrokerPort = 1883

HmacData = "clientId"..ESP8266ClientId.."deviceName"..DeviceName.."productKey"..ProductKey
MQTTClientId = ESP8266ClientId.."|securemode=3,signmethod=hmacsha1|"
MQTTUserName = DeviceName.."&"..ProductKey
MQTTPassword = crypto.toHex(crypto.hmac("sha1",HmacData,DeviceSecret))


function ConnectWifi()
    if wifi.sta.getip() == nil then
        print("Connecting...")
    else
        timer1:stop()
        print("Connect AP success")
        print(wifi.sta.getip())
        MQTTClient = mqtt.Client(MQTTClientId, 120, MQTTUserName, MQTTPassword, false)
        MQTTClient:connect(BrokerAddress, BrokerPort, 0, function(client)
        
            timer1:stop()
            print("MQTT connect success")

            MQTTClient:subscribe(SubTopic,0,function(conm)
                 print("MQTT subscribe success")
                 MQTTOn()
            end)

        end,

        function(client,reason)
            print("MQTT connect fail:"..reason)
        end)
    end
end

function MQTTOn()
    print("MQTT listen...")

           timer1:alarm(1000, tmr.ALARM_AUTO, MQTTPublish)
end

function MQTTPublish()

    tmr.delay(1000)
    tmr.delay(1000)
    
    pin = 5
    status, temp, humi, temp_dec, humi_dec = dht.read11(pin)
    
    data = {}
    data.voice =  1
    data.CurrentTemperature = temp -- DHT_11(temp1)
    data.CurrentHumidity = humi --DHT_11(humi1)
  --  if data.voice == ture or data.voice ==ture then
    ok,json = pcall(sjson.encode, {params=data})
    MQTTClient:publish(Pubtopic, json, 0, 0, function(client)
        print("Publish weather success")
    end)
   -- end
end



timer1:alarm(500, tmr.ALARM_AUTO, ConnectWifi)



标签:function,tmr,end,esp8266,..,温湿度,lua,print,data
来源: https://blog.csdn.net/qq_42530422/article/details/106979240

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有