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

參考文章

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

概要

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

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

創建產品、創建設備

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

添加功能

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

首先,選擇產品,進入產品查看頁面


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

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


添加自定義功能

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


自定義功能設置

信息復制

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

  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發送消息的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閱讀 230,578評論 6 544
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,701評論 3 429
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,691評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,974評論 1 318
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,694評論 6 413
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 56,026評論 1 329
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 44,015評論 3 450
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 43,193評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,719評論 1 336
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,442評論 3 360
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,668評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,151評論 5 365
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,846評論 3 351
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,255評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,592評論 1 295
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,394評論 3 400
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,635評論 2 380

推薦閱讀更多精彩內容