NodeMCU阿里云平臺的遠程溫度查看實現

參考文章

NodeMCU 通過MQTT 連接阿里云物聯網 https://blog.csdn.net/weixin_43368807/article/details/82984796
謝謝驾孔。

概要

本文與參考文章有一點的重復张肾,但有幾點會不同,一,更加具體的添加了一個實際功能框舔,本文監(jiān)控了溫度與濕度信息;二,對代碼進行了重新的組織,更加清晰簡單蒂胞。

阿里云平臺方面的準備工作

創(chuàng)建產品、創(chuàng)建設備

可以參考最前邊列出的參考文章条篷。另外骗随,阿里云平臺也有很好的引導蛤织,按著引導下一步下一步就可以了。

添加功能

這個是與參考文章不同的地方鸿染。對了讓讀者更容易理解為什么要添加功能指蚜,我把產品、設備牡昆、功能的關系簡單描述下姚炕。產品就像一個抽象的概念摊欠,例如電視機丢烘,只有把電視機變成具體的東西,我們才能具體觸摸感知些椒,這個就是設備播瞳,很類似于類與對象的概念。而一個產品對普通人來說免糕,主要就是關注與它能做什么赢乓,也就是它的功能。功能是在產品中定義的石窑,每個設備都會具備這個功能牌芋,這個就類似與類方法。

首先松逊,選擇產品躺屁,進入產品查看頁面


選擇產品
進入產品查看信息頁面

然后,選擇功能定義经宏,在功能定義中選擇自定義功能的添加


添加自定義功能

最后犀暑,進行自定義功能的設置


自定義功能設置

信息復制

后期設備端連接阿里云服務器時有幾個信息需要設置,所以現在就要把對應的信息準備好:

  1. 在產品查看頁面獲取ProductKey與ProductSecret兩個信息烁兰;
  2. 在功能定義頁面獲取溫度耐亏、濕度的標識名,例如:


    功能標識選擇

    上圖中沪斟,溫度標識符:CurrentTemperature广辰,濕度標識符:humidity,這兩個是讀者在前邊自己設置主之。

  3. 在設備查看頁面獲取DeviceName信息轨域;
  4. 在設備查看頁面獲取Topic鏈接信息,是一個類似/sys/*********/DeviceA/thing/event/property/post的字符串杀餐。

到此為止干发,阿里云端的設置就已經完成了。

設備端操作

固件燒寫

請參考本文最前的參考文章的說明史翘,以及本人其他關于NodeMCU操作的文章枉长。但需要注意的是冀续,固件注意選擇溫度傳感器模塊,例如我使用了DHT溫度傳感器模塊必峰。
我的固件包含了如下模塊:This was built against the master branch and includes the following modules: crypto, dht, encoder, enduser_setup, file, gpio, http, i2c, mqtt, net, node, pwm, sntp, spi, tmr, uart, wifi, wifi_monitor.

代碼編寫邏輯

在代碼設計上洪唐,分為了五個文件,一個是初始化wifi邏輯文件吼蚁,init.lua,另一個是mqtt的連接處理文件凭需,再一個就是配置文件,用來記錄不同使用者信息的文件肝匆,config.lua粒蜈,再一個是mqtt的邏輯處理文件,這個是針對一個物聯網項目需要編寫的邏輯旗国,mqtt_app.lua枯怖。最后一個文件是應用模塊管理文件application.lua。未來能曾,可能會有其他功能添加度硝,例如添加一個HTTP服務器支持,那么很可能會需要添加新的模塊實現寿冕,那么就在application.lua中加載新的模塊實現蕊程。

配置文件config.lua

-- WIFI 設置
SSID = "HiWiFi_**********"
PASSWORD = "**********"


-- MQTT設置
ProductKey ="************"   
DeviceName ="*******"  
DeviceSecret="*********************************************"

RegionId="cn-shanghai"     --華東2
-- MQTT發(fā)送消息的topic
publish_topic = "/sys/"..ProductKey.."/"..DeviceName.."/thing/event/property/post"

Wifi初始化邏輯文件init.lua

dofile("config.lua")


function startup()
    if file.open("init.lua") == nil then
        print("init.lua deleted or renamed")
    else
        print("Running")
        file.close("init.lua")
        -- the actual application is stored in 'application.lua'
        dofile("application.lua")
    end
end

-- Define WiFi station event callbacks
wifi_connect_event = function(T)
  print("Connection to AP("..T.SSID..") established!")
  print("Waiting for IP address...")
  if disconnect_ct ~= nil then disconnect_ct = nil end
end

wifi_got_ip_event = function(T)
  -- Note: Having an IP address does not mean there is internet access!
  -- Internet connectivity can be determined with net.dns.resolve().
  print("Wifi connection is ready! IP address is: "..T.IP)
  print("Startup will resume momentarily, you have 3 seconds to abort.")
  print("Waiting...")
  tmr.create():alarm(3000, tmr.ALARM_SINGLE, startup)
end

wifi_disconnect_event = function(T)
  if T.reason == wifi.eventmon.reason.ASSOC_LEAVE then
    --the station has disassociated from a previously connected AP
    return
  end
  -- total_tries: how many times the station will attempt to connect to the AP. Should consider AP reboot duration.
  local total_tries = 75
  print("\nWiFi connection to AP("..T.SSID..") has failed!")

  --There are many possible disconnect reasons, the following iterates through
  --the list and returns the string corresponding to the disconnect reason.
  for key,val in pairs(wifi.eventmon.reason) do
    if val == T.reason then
      print("Disconnect reason: "..val.."("..key..")")
      break
    end
  end

  if disconnect_ct == nil then
    disconnect_ct = 1
  else
    disconnect_ct = disconnect_ct + 1
  end
  if disconnect_ct < total_tries then
    print("Retrying connection...(attempt "..(disconnect_ct+1).." of "..total_tries..")")
  else
    wifi.sta.disconnect()
    print("Aborting connection to AP!")
    disconnect_ct = nil
  end
end

-- Register WiFi Station event callbacks
wifi.eventmon.register(wifi.eventmon.STA_CONNECTED, wifi_connect_event)
wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, wifi_got_ip_event)
wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, wifi_disconnect_event)

print("Connecting to WiFi access point...")
wifi.setmode(wifi.STATION)
wifi.sta.config({ssid=SSID, pwd=PASSWORD})
-- wifi.sta.connect() not necessary because config() uses auto-connect=true by default

應用模塊管理文件application.lua

-- MQTT 模塊 ----
dofile("mqtt.lua")
--print("Hello ...")

mqtt的連接處理模塊mqtt.lua

-- 加載配置信息
dofile("config.lua")
-- 加載MQTT應用邏輯
mqtt_app = require("mqtt_app")

-- 連接阿里云
ClientId =wifi.sta.getmac()  

myMQTTport=1883    
myMQTT=nil      

myMQTThost=ProductKey..".iot-as-mqtt."..RegionId..".aliyuncs.com"   
myMQTTusername=DeviceName.."&"..ProductKey         


myMQTTtimes='1234567890'
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.."|"      


myMQTT=mqtt.Client(myMQTTClientId, 120,myMQTTusername,myMQTTpassword) 

mqtt_app.MQTT_Init(myMQTT)

print("Attempting client connect...")
local re = myMQTT:connect(myMQTThost, myMQTTport,0, mqtt_app.MQTTSuccess,mqtt_app.MQTTFailed)

mqtt的應用邏輯實現模塊mqtt_app.lua

dofile("config.lua")

local myqq_app = {};

MQTTconnectFlag = 0;
mClient = nil;

function myqq_app.MQTT_Init(client)
    --[設備offline 事件]-----
    myMQTT:on("offline", function(client) 
        print ("offline") 
        --tmr.start(0)
    end)

    --[注冊 設備接收到訂閱的topic 事件]-----
    myMQTT:on("message", function(client, topic, data) 
        print(topic ..":") 
        if data ~= nil then
            print(data)
        end
    end)
end

---[連接成功]---
function myqq_app.MQTTSuccess(client)
    print("MQTT connected")
    
    -- 注冊通道
    --client:subscribe(topic0,0, function(conn)     --注冊topic0
    --    print("subscribe success") 
    --end) 

    mClient = client;
    MQTTconnectFlag=1
    --tmr.stop(0)        --關閉定時連接
    
    --[topic 定時上傳數據]
    tmr.create():alarm(5000, 1, function()    --等待連接上
        if MQTTconnectFlag==1 and mClient~=nil then   
            re,t,s,x = dht.read11(1);
            msg = "{\"id\":\"bc:dd:c2:30:cb:dc\", \"params\":{\"CurrentTemperature\":"..tostring(t)..",\"humidity\":"..tostring(s).."},\"method\":\"thing.event.property.post\"}"
            mClient:publish(publish_topic,msg,0,0,function(client)
                            print("send ok" ) 
            end)
        end
    end)
end

---[連接失敗]---
function myqq_app.MQTTFailed(client,reson)
    print("Fail reson:"..reson)
    MQTTconnectFlag=0
    --tmr.start(0)     --重新啟動連接
end

return myqq_app;
?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市驼唱,隨后出現的幾起案子藻茂,更是在濱河造成了極大的恐慌,老刑警劉巖曙蒸,帶你破解...
    沈念sama閱讀 219,490評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件捌治,死亡現場離奇詭異,居然都是意外死亡纽窟,警方通過查閱死者的電腦和手機肖油,發(fā)現死者居然都...
    沈念sama閱讀 93,581評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來臂港,“玉大人森枪,你說我怎么就攤上這事∩竽酰” “怎么了县袱?”我有些...
    開封第一講書人閱讀 165,830評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長佑力。 經常有香客問我式散,道長,這世上最難降的妖魔是什么打颤? 我笑而不...
    開封第一講書人閱讀 58,957評論 1 295
  • 正文 為了忘掉前任暴拄,我火速辦了婚禮漓滔,結果婚禮上,老公的妹妹穿的比我還像新娘乖篷。我一直安慰自己响驴,他們只是感情好,可當我...
    茶點故事閱讀 67,974評論 6 393
  • 文/花漫 我一把揭開白布撕蔼。 她就那樣靜靜地躺著豁鲤,像睡著了一般。 火紅的嫁衣襯著肌膚如雪鲸沮。 梳的紋絲不亂的頭發(fā)上琳骡,一...
    開封第一講書人閱讀 51,754評論 1 307
  • 那天,我揣著相機與錄音诉探,去河邊找鬼日熬。 笑死棍厌,一個胖子當著我的面吹牛肾胯,可吹牛的內容都是我干的。 我是一名探鬼主播耘纱,決...
    沈念sama閱讀 40,464評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼敬肚,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了束析?” 一聲冷哼從身側響起艳馒,我...
    開封第一講書人閱讀 39,357評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎员寇,沒想到半個月后弄慰,有當地人在樹林里發(fā)現了一具尸體,經...
    沈念sama閱讀 45,847評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡蝶锋,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,995評論 3 338
  • 正文 我和宋清朗相戀三年陆爽,在試婚紗的時候發(fā)現自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片扳缕。...
    茶點故事閱讀 40,137評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡慌闭,死狀恐怖,靈堂內的尸體忽然破棺而出躯舔,到底是詐尸還是另有隱情驴剔,我是刑警寧澤,帶...
    沈念sama閱讀 35,819評論 5 346
  • 正文 年R本政府宣布粥庄,位于F島的核電站丧失,受9級特大地震影響,放射性物質發(fā)生泄漏惜互。R本人自食惡果不足惜布讹,卻給世界環(huán)境...
    茶點故事閱讀 41,482評論 3 331
  • 文/蒙蒙 一科侈、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧炒事,春花似錦臀栈、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,023評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至睡扬,卻和暖如春盟蚣,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背卖怜。 一陣腳步聲響...
    開封第一講書人閱讀 33,149評論 1 272
  • 我被黑心中介騙來泰國打工屎开, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人马靠。 一個月前我還...
    沈念sama閱讀 48,409評論 3 373
  • 正文 我出身青樓奄抽,卻偏偏與公主長得像,于是被迫代替她去往敵國和親甩鳄。 傳聞我的和親對象是個殘疾皇子逞度,可洞房花燭夜當晚...
    茶點故事閱讀 45,086評論 2 355

推薦閱讀更多精彩內容