概述
基本流程
刷固件
固件下载
- 在nodemcu官网下载固件点击跳转
输入你的邮箱,注意确认邮箱直接输入,不要复制上方邮箱,此邮箱用于接收构建好的固件。
- 选取需要用到的模块
3.保存默认点击 Start your build
4.成功获取到固件到邮件去下载
第一封 正在构建你的固件
第二封 构建完成 下载相应的固件 一个支持浮点型,一个支持整型
5.打开固件下载固件获取
选择刚刚下载的固件
6.按图配置开始刷固件
完成
阿里云
登录阿里云控制台,进入物联网平台
创建产品
名字随便,数据格式 alink json
创建设备
在产品下创建一个设备
保存好三元组备用
{
“ProductKey”: “a1Ulg1U99vn”,
“DeviceName”: “PhysicalDevice”,
“DeviceSecret”: “2FkXL2JQfyGpnq9GzEZcdljOZyIIWSLw”
}
示例代码
cfg={}
cfg.ssid="802.1x"
cfg.pwd="123456789rg"
pin = 0
gpio.mode(pin,gpio.OUTPUT)
gpio.write(pin,gpio.HIGH)
----[wifi connect]---------------
wifi.setmode(wifi.STATION)
wifi.sta.config(cfg)
wifi.sta.connect()
print("wifi connecting")
----[wifi connected]--------
wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function(T)
print("Connected, IP is "..wifi.sta.getip())
end)
wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(T)
print("wifi disconnect")
end)
---------------------
--------[getTime]--------------------
function GetNetTime()
sntp.sync({"0.nodemcu.pool.ntp.org","1.nodemcu.pool.ntp.org","2.nodemcu.pool.ntp.org","3.nodemcu.pool.ntp.org","www.beijing-time.org"},
function(sec, usec, server, info)
print('sync', sec, usec, server)
end,
function()
print("get time error")
end)
return 0
end
---------------------
----[3yuanzu]------
ProductKey ="a1Ulg1U99vn"
DeviceName ="PhysicalDevice"
DeviceSecret="2FkXL2JQfyGpnq9GzEZcdljOZyIIWSLw"
RegionId="cn-shanghai"
topic0="/sys/a1Ulg1U99vn/PhysicalDevice/thing/service/property/set"
topic1="/sys/a1Ulg1U99vn/PhysicalDevice/thing/event/property/post"
myMQTTport=1883
--port
myMQTT=nil
--client
ClientId = wifi.sta.getmac()
myMQTTtimes='6666'
---------------------
myMQTThost=ProductKey..".iot-as-mqtt."..RegionId..".aliyuncs.com"
--host
myMQTTusername=DeviceName.."&"..ProductKey
--username
--------MQTT------------------
myMQTTtimes='6666'
hmacdata="clientId"..ClientId.."deviceName"..DeviceName.."productKey"..ProductKey.."timestamp"..myMQTTtimes
myMQTTpassword=crypto.toHex(crypto.hmac("sha1",hmacdata,DeviceSecret))
myMQTTClientId=ClientId.."|securemode=3,signmethod=hmacsha1,timestamp="..myMQTTtimes.."|"
----[Create MQTT
client]
myMQTT=mqtt.Client(myMQTTClientId, 120,myMQTTusername,myMQTTpassword)
----[Start Timing Connection]-----
MQTTconnectFlag=0
time =tmr.create()
time:alarm(10000,tmr.ALARM_AUTO,function()
if myMQTT~=nil then
print("Attempting client connect...")
myMQTT:connect(myMQTThost, myMQTTport,0,MQTTSuccess,MQTTFailed)
end
end)
--[Connect Successful]---
function MQTTSuccess(client)
print("MQTT connected")
client:subscribe(topic0,0, function(conn)
--topic0
print("subscribe topic0 success")
end)
myMQTT=client
MQTTconnectFlag=1
time:stop()
end
---[Connect Fail---
function MQTTFailed(client,reson)
print("Fail reson:"..reson)
MQTTconnectFlag=0
time:start()
end
--[Device offline Event]-----
myMQTT:on("offline", function(client)
print ("offline")
time:start()
end)
---------------------
--[Registered Device Receives Subscribed Topic Events]-----
myMQTT:on("message", function(client, topic, data)
print("Recieved Topic:/"..topic .. ":" )
if data ~= nil then
print(data)
end
end)
--[topic Timely upload of data]
time1 = tmr.create()
temp =10.1
ok,json = pcall(sjson.encode, {method="thing.service.property.set",id="978050866",params={Temperature=temp,version="1.0.0"}})
--ok,json = pcall(sjson.encode, {id="1",params={Temperature=30.1,Data="test1"}})
data= "this is my first time connect alibaba cloud"
time1:alarm(10000, 1, function()
--watting connect
tmr.wdclr()
else
temp = 10
end
if MQTTconnectFlag==1 and myMQTT~=nil then
myMQTT:publish(topic0,json,0,0,function(client)
print("send ok")
end)
gpio.write(pin,gpio.LOW)
tmr.delay(1000000)
gpio.write(pin,gpio.HIGH)
end
end)
最后
以上就是儒雅金针菇为你收集整理的ESP8266基于NodeMCU接入阿里云基本流程阿里云的全部内容,希望文章能够帮你解决ESP8266基于NodeMCU接入阿里云基本流程阿里云所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复