Python搭建物聯(lián)網(wǎng),一招搞定所有代理商

搭建整套物聯(lián)網(wǎng)系統(tǒng)的方法有很多,最近四處搗鼓,使用python + 阿里云搭建一套最簡單的物聯(lián)系統(tǒng),可以將單片機(jī)上的數(shù)據(jù)通過阿里云傳輸?shù)絇C端。

小編推薦大家可以加我的扣扣群 735934841 ,免費(fèi)領(lǐng)取Python學(xué)習(xí)資料

一、基本結(jié)構(gòu)

先看架構(gòu)圖

框架.png

二、設(shè)備端接入

物聯(lián)網(wǎng)終端是用的ESP32,是一款自帶藍(lán)牙和Wifi的單片機(jī)。利用它可以直接接入互聯(lián)網(wǎng),無需其他模塊。當(dāng)然你可以將現(xiàn)在流行的NB-Iot模塊來聯(lián)網(wǎng),不過需要自己寫一下驅(qū)動(dòng)程序。我買的模塊是支持micropython開發(fā)的,在淘寶上可以搜索到,用起來很方便。有時(shí)間我會(huì)補(bǔ)上這一塊的初步教程。

ESP32模塊.jpg

Micropython是可以在低端硬件上運(yùn)行的python,可以使用python語言直接操作IO 和MCU的外設(shè)比如UART、I2C等,用起來非常方便,不要搭建復(fù)雜的開發(fā)環(huán)境,也不需要學(xué)習(xí)寄存器配置。作為一個(gè)對(duì)傳統(tǒng)MCU開發(fā)非常熟悉的硬件工程師來說,感覺操作起來非常簡單。目前Micropython已經(jīng)支持很多硬件了,應(yīng)該用比較廣泛的STM32部分系列也被支持。Micropython也已經(jīng)支持很多常用的庫,比如藍(lán)牙,telnet,mqtt等。下面這個(gè)鏈接是micropython的中文論壇。

http://www.micropython.org.cn/bbs/forum.php

ESP32 通過wifi 接入互聯(lián)網(wǎng),使用mqtt協(xié)議接入阿里云,將溫度數(shù)據(jù)上傳至阿里云。在云端通過消息訂閱可以直接查看溫度信息。在PC端使用python調(diào)用MQTT協(xié)議,接入到阿里云。但是PC端和ESP32在阿里云上是兩個(gè)不同的設(shè)備,需要通過阿里云來轉(zhuǎn)發(fā)信息,這樣PC就可以拿到ESP32上傳的數(shù)據(jù)了。

ESP32 上的代碼如下:

from umqtt.simple import MQTTClient

import usocket as socket

import time

import wifi

wifi.connect()

#Demo_01

ProductKey = "*********"#使用你自己的

ClientId = "1234|securemode=3,signmethod=hmacsha1|"

DeviceName = "Demo_01"

DeviceSecret = "*******************************"#使用你自己的

strBroker = ProductKey + ".iot-as-mqtt.cn-shanghai.aliyuncs.com"

Brokerport = 1883

user_name = "Demo_01&*********"#使用你自己的

user_password = "**********************************************"#使用你自己的

print("clientid:",ClientId," ","Broker:",strBroker," ","User Name:",user_name," ","Password:",user_password," ")

def connect():

client = MQTTClient(client_id = ClientId,server= strBroker,port=Brokerport,user=user_name, password=user_password,keepalive=60)

#please make sure keepalive value is not 0

client.connect()

temperature =25.00

while temperature < 30:

temperature += 0.5

send_mseg = '{"params": {"IndoorTemperature": %s},"method": "thing.event.property.post"}' % (temperature)

client.publish(topic="/sys/*************/Demo_01/thing/event/property/post", msg=send_mseg,qos=1, retain=False)#*號(hào)處為product id

time.sleep(3)

while True:

pass

#client.disconnect()

有幾點(diǎn)需要說明:

1.代碼中的wifi.connect()函數(shù)需要自己編寫,網(wǎng)上能搜到類似的,也可以打賞私信我所要源碼。

2.阿里云物聯(lián)網(wǎng)平臺(tái)的接入需要進(jìn)行三元組認(rèn)證,會(huì)根據(jù)一定的規(guī)則生成登錄名和密碼,這個(gè)網(wǎng)上信息還是比較全面的。

3.向阿里云物聯(lián)網(wǎng)平臺(tái)發(fā)布消息的格式一定要按照代碼中所寫,網(wǎng)上很多代碼,但是對(duì)這一塊的描述都不清楚。

Micropython使用的umqtt.simple庫,一定要設(shè)置keepalive時(shí)間,否則無法連接。這一點(diǎn)我是摸索了好久,最終通過查看庫的源碼才發(fā)現(xiàn)的問題。

三、云端設(shè)置

在云端建立一個(gè)高級(jí)產(chǎn)品,并創(chuàng)建兩個(gè)設(shè)備,以供ESP32 和PC連接。

device.JPG

需要在產(chǎn)品中定義一下功能。

device_define.JPG

云端和設(shè)備端都建立好了之后,可以查看設(shè)備運(yùn)行狀態(tài)看到數(shù)據(jù)上傳

云端數(shù)據(jù)查看.JPG

這是查看數(shù)據(jù)記錄得到的結(jié)果

云端數(shù)據(jù)記錄.JPG

當(dāng)你看到正確的數(shù)據(jù)之后,就說明你的成功接入物聯(lián)網(wǎng)并上傳了數(shù)據(jù)。

接下來就是最重要的部分——設(shè)置是使用規(guī)則引擎來進(jìn)行數(shù)據(jù)轉(zhuǎn)發(fā),將設(shè)備demo_01的數(shù)據(jù)轉(zhuǎn)發(fā)到demo_02。這一步的語法很重要,雖然有官網(wǎng)有詳細(xì)教程,但是當(dāng)時(shí)還是搞了好久才完全正確。

規(guī)則查詢語句:

SELECT items.IndoorTemperature.value as IndoorTemperature FROM "/sys/use-your-productkey-here/Demo_01/thing/event/property/post" WHERE items.IndoorTemperature.value > 0

四、PC端接入

PC 端使用python模擬MQTT設(shè)備登陸阿里云訂閱消息就行了,只要裝好python很快就可以實(shí)現(xiàn),網(wǎng)上也有很多代碼。代碼的很大一部分就是在做三元組認(rèn)證,可以將這部分稍微修改一下來計(jì)算ESP32 登陸時(shí)所需的

PC端python代碼如下:

# coding=utf-8

import datetime

import time

import hmac

import hashlib

import math

try:

import paho.mqtt.client as mqtt

except ImportError:

print("MQTT client not find. Please install as follow:")

print("pip install paho-mqtt")

# 設(shè)置連接信息

#Demo_02

ProductKey = "*********"#使用你自己的

ClientId = "2234" # 自定義clientId

DeviceName = "Demo_02"

DeviceSecret ="************************************8**"#使用你自己的

# 獲取時(shí)間戳(當(dāng)前時(shí)間毫秒值)

us = math.modf(time.time())[0]

ms = int(round(us * 1000))

timestamp = str(ms)

# 計(jì)算密碼(簽名值)

def calculation_sign(signmethod):

data = "".join(("clientId", ClientId, "deviceName", DeviceName,

"productKey", ProductKey, "timestamp", timestamp))

if "hmacsha1" == signmethod:

# ret = hmac.new(bytes(DeviceSecret),

# bytes(data), hashlib.sha1).hexdigest()

ret = hmac.new(bytes(DeviceSecret, encoding="utf-8"),

bytes(data, encoding="utf-8"),

hashlib.sha1).hexdigest()

elif "hmacmd5" == signmethod:

# ret = hmac.new(bytes(DeviceSecret, encoding="utf-8"),

# bytes(data, encoding="utf-8"), hashlib.md5).hexdigest()

ret = hmac.new(bytes(DeviceSecret, encoding="utf-8"),

bytes(data, encoding="utf-8"),

hashlib.md5).hexdigest()

else:

raise ValueError

return ret

# ======================================================

strBroker = ProductKey + ".iot-as-mqtt.cn-shanghai.aliyuncs.com"

port = 1883

client_id = "".join((ClientId,

"|securemode=3",

",signmethod=", "hmacsha1",

",timestamp=", timestamp,

"|"))

username = "".join((DeviceName, "&", ProductKey))

password = calculation_sign("hmacsha1")

print("="*60)

print(strBroker)

print("client_id:", client_id)

print("username:", username)

print("password:", password)

print("="*60)

# 成功連接后的操作

def on_connect(client, userdata, flags, rc):

print("OnConnetc, rc: " + str(rc))

# 成功發(fā)布消息的操作

def on_publish(client, msg, rc):

if rc == 0:

print("publish success, msg = " + msg)

# 成功訂閱消息的操作

def on_subscribe(mqttc, obj, mid, granted_qos):

print("Subscribed: " + str(mid) + " " + str(granted_qos))

def on_log(mqttc, obj, level, string):

print("Log:" + string)

def on_message(mqttc, obj, msg):

curtime = datetime.datetime.now()

strcurtime = curtime.strftime("%Y-%m-%d %H:%M:%S")

print(strcurtime + ": " + msg.topic + " " + str(msg.qos) + " " + str(msg.payload))

on_exec(str(msg.payload))

def on_exec(strcmd):

print("Exec:", strcmd)

strExec = strcmd

if __name__ == '__main__':

mqttc = mqtt.Client(client_id)

mqttc.username_pw_set(username, password)

mqttc.on_message = on_message

mqttc.on_connect = on_connect

mqttc.on_publish = on_publish

mqttc.on_subscribe = on_subscribe

mqttc.on_log = on_log

mqttc.connect(strBroker, port, 120)

# mqttc.loop_start()

time.sleep(1)

temperature =27.55

mqttc.subscribe("/sys/************/Demo_02/thing/service/property/set", qos=1) # 換成自己的

#send_mseg = '{"pm_25": %s,"area":"%s","time":"%s"}' % (0, 0, datetime.datetime.now())

#send_mseg = '{"id": "1234", "version": "1.0","params": {"IndoorTemperature": %s},"method": "thing.event.property.post"}'%(temperature)

send_mseg = '{"params": {"IndoorTemperature": %s},"method": "thing.event.property.post"}' % (temperature)

print('send_mseg is : ',send_mseg)

mqttc.loop_forever()

五、總結(jié)

工作之余了解了一下物聯(lián)網(wǎng)的發(fā)展,看到有意思的東西打算學(xué)一下,剛好看到了microPython,震驚之余,決心做點(diǎn)小東西玩玩。

這套框架全部使用python實(shí)現(xiàn),比我了解到的絕大多數(shù)物聯(lián)網(wǎng)方案要簡單太多,雖然有些開發(fā)首先,但是用來實(shí)現(xiàn)一些簡單設(shè)計(jì)應(yīng)該是不成問題的,只要你會(huì)python,這套系統(tǒng)可以很快構(gòu)建。當(dāng)然python也是非常好學(xué)的,長期使用C語言的人根本不需要什么學(xué)習(xí)就可以上手。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容