Onenet物聯網Mqtt初探(micropython_mqtt登陸與資料收發)

請先去去看Onenet 物聯網Mqtt初探(MQTT。fx模擬登陸與資料收發)和Onenet物聯網Mqtt初探(python_mqtt登陸與資料收發)的文章,否則可能不明被我輸入的是什麼訊息。這裡直接做micropython程式碼的mqtt連線:

1、上電燒錄好micropython的ESP32

2、在控制檯下輸入arp -a查一下ESP32的IP地址,因為之前已經記錄ESP32的MAC地址,所以看到有這個地址就對應上IP了:

Onenet物聯網Mqtt初探(micropython_mqtt登陸與資料收發)

3、,開啟webrepl。html頁面,輸入IP連線,輸入密碼回車,連線成功了:

Onenet物聯網Mqtt初探(micropython_mqtt登陸與資料收發)

4、先看看有哪些支援的模組,輸入:help(‘modules’)

Onenet物聯網Mqtt初探(micropython_mqtt登陸與資料收發)

暈~~~我的esp32-cam自定義camera的韌體沒有mqtt的模組,

沒有的話就用upip在ESP32上安裝MQTT庫

>>> import upip

>>> upip。install(‘micropython-umqtt。simple’)

Onenet物聯網Mqtt初探(micropython_mqtt登陸與資料收發)

安裝完成,再次輸入:help(‘modules’)

Onenet物聯網Mqtt初探(micropython_mqtt登陸與資料收發)

沒變化,是什麼鬼。

找了一網上解決辦法都不沒有答案,就要放棄了,再嘗試輸入一下:

from umqtt。simple import MQTTClient

Onenet物聯網Mqtt初探(micropython_mqtt登陸與資料收發)

居然能呼叫了…。我~~~~~擦~,難道是upip安裝後沒給help(‘modules’)這東西標記,導致沒顯示?還是本來就有的?一臉懵逼。

直接上程式碼,功能:傳送6秒發一次溫溼度資料,永遠等待開關led訊息,控制led燈亮滅。

import timeimport dht11from umqtt。simple import MQTTClientfrom machine import Pinled = Pin(4,Pin。OUT)#led引腳輸出模式client_id=‘pc_esp32’ #裝置的IDserver = ‘mqtts。heclouds。com’ #onenet地址port = 1883 #連線的埠user = ‘449990’ #產品的數字IDpassword = ‘version=2018-10-31&res=products%2F449990%2Fdevices%2Fpc_esp32&et=1735660800&method=md5&sign=i1CJFxX6Od4vtdtXCgjp9w%3D%3D’# Publish test messages e。g。 with:# mosquitto_pub -t foo_topic -m hello# Received messages from subscriptions will be delivered to this callbackc = MQTTClient(client_id, server,port,user,password,60) #(self, client_id, server, port=0, user=None, password=None, keepalive=0,ssl=False, ssl_params={}):def sub_cb(topic, msg): print((topic, msg)) msg_str = msg。decode()#訊息轉換為二進位制轉換字串 if(msg_str == “TurnOn”): led。value(1) topic_str = topic。decode() #二進位制轉換字串,轉換“request”,“response”變成訊息 #b‘$sys/449990/pc_esp32/cmd/request/90651f67-14fc-431c-97b7-6321911728ed’ #b‘$sys/449990/pc_esp32/cmd/response/90651f67-14fc-431c-97b7-6321911728ed’ topic = topic_str。replace(“request”,“response”)。encode() if(led。value()):c。publish(topic,b“light is turn on”) if(msg_str == “TurnOff”): led。value(0) topic_str = topic。decode() #二進位制轉換字串,轉換“request”,“response”變成訊息 topic = topic_str。replace(“request”,“response”)。encode() if(led。value() == 0):c。publish(topic,b“light is turn off”)def main(): # test server : iot。eclipse。org c。set_callback(sub_cb) c。connect() c。subscribe(b“$sys/449990/pc_esp32/cmd/#”)# subscribe foo_topic tipic while True: try:#防止dht11讀取錯誤異常退出(leb燈開啟後會有超時情況:File “dht。py”, line 17, in measure ——>> OSError: [Errno 116] ETIMEDOUT) _,temperature,Humidity = dht11。dht11(15)#傳入引腳號 #print(temperature,Humidity) c。publish(b“$sys/449990/pc_esp32/dp/post/json”, “{ ‘id’: 123, ‘dp’: { ‘temperatrue’: [{ ‘v’:” + str(temperature)+ “}],‘humidity’: [{ ‘v’:” + str(Humidity) +“, }]}}”)#傳送資料 except OSError: print(OSError) # Non-blocking wait for message c。check_msg() # Then need to sleep to avoid 100% CPU usage (in a real # app other useful actions would be performed instead) time。sleep(6) c。disconnect()if __name__ == “__main__”: main()

Onenet物聯網Mqtt初探(micropython_mqtt登陸與資料收發)

執行後,看onenet控制檯/裝置/資料流,可以看到訊息已經發送到了

Onenet物聯網Mqtt初探(micropython_mqtt登陸與資料收發)

下發開燈命令測試:

Onenet物聯網Mqtt初探(micropython_mqtt登陸與資料收發)

Onenet物聯網Mqtt初探(micropython_mqtt登陸與資料收發)

Esp32收到訊息:

Onenet物聯網Mqtt初探(micropython_mqtt登陸與資料收發)

Onenet物聯網Mqtt初探(micropython_mqtt登陸與資料收發)

傳送關燈命令:

Onenet物聯網Mqtt初探(micropython_mqtt登陸與資料收發)

Onenet物聯網Mqtt初探(micropython_mqtt登陸與資料收發)

Ok,本次測試完成。

補充:

Import dht11 是帶入這個檔案dht11。py,裡面的內容是:

from machine import Pinimport dhtdef dht11(pin): d = dht。DHT11(Pin(pin,Pin。IN)) d。measure() #print(d。temperature(),d。humidity()) return d,d。temperature(),d。humidity()