ESP8266基于NodeMCU接入阿里云

基本流程

刷固件

固件下载

  1. 在nodemcu官网下载固件点击跳转
    输入你的邮箱,注意确认邮箱直接输入,不要复制上方邮箱,此邮箱用于接收构建好的固件。
    ESP8266基于NodeMCU接入阿里云_第1张图片
  2. 选取需要用到的模块
    ESP8266基于NodeMCU接入阿里云_第2张图片
    3.保存默认点击 Start your build
    ESP8266基于NodeMCU接入阿里云_第3张图片
    4.成功获取到固件到邮件去下载
    ESP8266基于NodeMCU接入阿里云_第4张图片

第一封 正在构建你的固件
ESP8266基于NodeMCU接入阿里云_第5张图片
第二封 构建完成 下载相应的固件 一个支持浮点型,一个支持整型
ESP8266基于NodeMCU接入阿里云_第6张图片
5.打开固件下载固件获取
选择刚刚下载的固件
ESP8266基于NodeMCU接入阿里云_第7张图片

6.按图配置开始刷固件
ESP8266基于NodeMCU接入阿里云_第8张图片

完成
ESP8266基于NodeMCU接入阿里云_第9张图片

阿里云

登录阿里云控制台,进入物联网平台

创建产品

名字随便,数据格式 alink json

ESP8266基于NodeMCU接入阿里云_第10张图片

创建设备

在产品下创建一个设备ESP8266基于NodeMCU接入阿里云_第11张图片

保存好三元组备用

{
“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接入阿里云_第12张图片

你可能感兴趣的:(嵌入式,阿里云)