ESP8266基于NodeMCU接入阿里云

基本流程

刷固件

固件下载

  1. 在nodemcu官网下载固件点击跳转
    输入你的邮箱,注意确认邮箱直接输入,不要复制上方邮箱,此邮箱用于接收构建好的固件。
  2. 选取需要用到的模块
    在这里插入图片描述
    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)






在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

都学点

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值