【NodeMCU_LUA系列】NodeMCU连接阿里云

序号其它NodeMCU LUA开发文档
NodeMCU/ESP8266 DHT11使用
NodeMCU/ESP8266 HTTP POST数据至中国移动OneNET
NodeMCU/ESP8266 MQTT接入中国移动OneNET
NodeMCU/ESP8266 远程升级(OTA)
微信小程序获取中移OneNET数据
NodeMCU/ESP8266 NodeMCU连接阿里云

这篇文章主要简单介绍一下ESP8266使用LUA开发,连接阿里云的一些大概流程。详细操作可以看视频教程
做这个之前,自己也是百度查来查去,所以再次感谢所有开源玩家。那么这里就主要做个总结。

1.阿里云创建产品

①.先进到阿里云物联网平台的控制台
②.如下图创建产品,完成之后保存。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2.创建设备

如下图填写设备名称和备注(随便写)。
在这里插入图片描述
在这里插入图片描述
那么现在设备就已经创建好了。

3.添加物模型,如下图操作

一定要记得添加物模型,也就是数据模板,否则即使上传了数据,平台也不会显示,并且记得在最后要点击发布。
在这里插入图片描述
在这里插入图片描述
下图是创建温度模型
在这里插入图片描述
下图是创建湿度模型
在这里插入图片描述
最后一定要记得点击发布
在这里插入图片描述

4.主题介绍

可以看到自定义Topic列表中,图中红框框里面的主题,就是等会ESP8266要订阅的主题,等会测试可以在此控制台发布消息。
在这里插入图片描述
那么再看到物模型Topic列表,如下图,等会ESP8266将温湿度信息上传到这个主题,然后我们就可以在控制台上查看了。
在这里插入图片描述

5.上代码

那么现在就可以测试了,代码不会详细讲,大家可以去看我录制的视频
主要是将自己的wifi信息,ProductKey,DeviceName, DeviceSecret和主题替换到代码中。
在这里插入图片描述
代码:
要注意的是,在向主题发布数据时,代码里的temp和humi的名字要改成和你创建物模型的一样。

wifitab={}
wifitab.ssid = "2.4G-603"
wifitab.pwd = "CX071500"
wifi.setmode(wifi.STATION)
wifi.sta.config(wifitab)
wifi.sta.connect()

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


ProductKey = "a1ti9YS1tqQ"
DeviceName = "esp8266"
DeviceSecret = "kXlF1pZHdAUn1K2ixotJQXBsoy5l5h3y"
RegionId = "cn-shanghai"
ESP8266ClientId = 20200510

SubTopic="/a1ti9YS1tqQ/esp8266/user/get"
Pubtopic="/sys/a1ti9YS1tqQ/esp8266/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...")
    MQTTClient:on("message",function(client,topic,data)
        print("\n")
        print(topic..":")
        print(data)
        if data == "1" then
            MQTTPublish()
        end
    end)
end

function MQTTPublish()
    data = {}
    data.temp = 88.8
    data.humi = 88.8
    ok,json = pcall(sjson.encode, {params=data})
    MQTTClient:publish(Pubtopic, json, 0, 0, function(client)
        print("Publish weather success")
    end)
end

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

6.测试

在这里插入图片描述
在这里插入图片描述

7.总结

①.添加产品。
②.添加设备。
③.添加物模型(记得发布上线)。
④.修改代码。
⑤.测试。

自建交流Q群:612683349
源码和相应的固件都在群里,资源多多哦,欢迎进来一起交流学习!

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值