第一封 正在构建你的固件
第二封 构建完成 下载相应的固件 一个支持浮点型,一个支持整型
5.打开固件下载固件获取
选择刚刚下载的固件
登录阿里云控制台,进入物联网平台
名字随便,数据格式 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)