mirror of
https://gitee.com/beecue/fastbee.git
synced 2025-12-18 00:45:55 +08:00
树莓派sdk更新
This commit is contained in:
@@ -20,34 +20,35 @@ import threading #导入线程模块,用作定时器
|
|||||||
|
|
||||||
# 作为python的AES的iv,应该为16位,字节型数据
|
# 作为python的AES的iv,应该为16位,字节型数据
|
||||||
wumei_iv = b"wumei-smart-open"
|
wumei_iv = b"wumei-smart-open"
|
||||||
|
#发布监测数据的最大次数
|
||||||
|
monitorCount =5
|
||||||
|
# 发布监测数据的间隔,默认5秒。 使用esp8266单片机时,服务器传来的间隔单位为毫秒,本程序由于定时运行需要的是秒,将转化为秒,如需毫秒运行,自行更改程序
|
||||||
|
monitorInterval =5
|
||||||
# NTP地址(用于获取时间,可选的修改为自己部署项目的地址)
|
# NTP地址(用于获取时间,可选的修改为自己部署项目的地址)
|
||||||
ntpServer = "http://120.24.218.158:8080/iot/tool/ntp?deviceSendTime="
|
ntpServer = "http://120.24.218.158:8080/iot/tool/ntp?deviceSendTime="
|
||||||
|
# 连接成功标志位
|
||||||
|
g_rc=-1
|
||||||
|
#全局变量,管理定时监测
|
||||||
|
global t2
|
||||||
|
|
||||||
# 设备信息配置
|
# 设备信息配置
|
||||||
deviceNum = "DW43CI6RM8GMG23H"
|
deviceNum = "DW43CI6RM8GMG23H"
|
||||||
userId = "1"
|
userId = "1"
|
||||||
productId = "4"
|
productId = "4"
|
||||||
firmwareVersion = "1.0"
|
firmwareVersion = "1.0"
|
||||||
|
# 经度和纬度可选,如果产品使用设备定位,则必须传
|
||||||
|
latitude=0
|
||||||
|
longitude=0
|
||||||
|
|
||||||
# Mqtt配置
|
# Mqtt配置
|
||||||
mqttHost = "120.24.218.158"
|
mqttHost = "120.24.218.158"
|
||||||
|
|
||||||
mqttPort = 1883
|
mqttPort = 1883
|
||||||
mqttUserName = "wumei-smart"
|
mqttUserName = "wumei-smart"
|
||||||
mqttPwd = "P261I5G3RY3MCIGG"
|
mqttPwd = "P261I5G3RY3MCIGG"
|
||||||
# 作为python的AES的key,应该为16位,字节型数据
|
# 作为python的AES的key,应该为16位,字节型数据
|
||||||
mqttSecret = b"K2IB784BM0O01GG6"
|
mqttSecret = b"K2IB784BM0O01GG6"
|
||||||
|
# 产品启用授权码则authCode不能为空
|
||||||
monitorCount =5 #发布监测数据的最大次数
|
authCode=""
|
||||||
# 使用esp8266单片机时,服务器传来的间隔单位为毫秒,本程序由于定时运行需要的是秒,将转化为秒,如需毫秒运行,自行更改程序
|
|
||||||
monitorInterval =5 # 发布监测数据的间隔,默认5秒
|
|
||||||
|
|
||||||
g_rc=-1 # 连接成功标志位
|
|
||||||
|
|
||||||
global t2 #全局变量,管理定时监测
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# 订阅的主题
|
# 订阅的主题
|
||||||
prefix = "/" + productId + "/" + deviceNum
|
prefix = "/" + productId + "/" + deviceNum
|
||||||
@@ -66,9 +67,8 @@ pFunctionTopic = prefix + "/function/post"
|
|||||||
pMonitorTopic = prefix + "/monitor/post"
|
pMonitorTopic = prefix + "/monitor/post"
|
||||||
pEventTopic = prefix + "/event/post"
|
pEventTopic = prefix + "/event/post"
|
||||||
|
|
||||||
# 初始化
|
# 初始化,连接 设备mqtt客户端Id格式为:认证类型(E=加密、S=简单) & 设备编号 & 产品ID & 用户ID
|
||||||
# 连接(客户端ID = 设备编号 & 产品ID)
|
clientId = "E&" + deviceNum + "&" + productId +"&" + userId
|
||||||
clientId = deviceNum + "&" + productId
|
|
||||||
client=mqtt.Client(clientId)
|
client=mqtt.Client(clientId)
|
||||||
|
|
||||||
#加密 (AES-CBC-128-pkcs5padding)
|
#加密 (AES-CBC-128-pkcs5padding)
|
||||||
@@ -194,7 +194,7 @@ def publishInfo():
|
|||||||
# rssi值 树莓派中暂时不处理wifi信号问题
|
# rssi值 树莓派中暂时不处理wifi信号问题
|
||||||
# 信号强度(信号极好4格[-55— 0],信号好3格[-70— -55],信号一般2格[-85— -70],信号差1格[-100— -85])
|
# 信号强度(信号极好4格[-55— 0],信号好3格[-70— -55],信号一般2格[-85— -70],信号差1格[-100— -85])
|
||||||
# status值 (1-未激活,2-禁用,3-在线,4-离线)
|
# status值 (1-未激活,2-禁用,3-在线,4-离线)
|
||||||
doc={"rssi":1,"firmwareVersion":firmwareVersion,"status":3,"userId":userId}
|
doc={"rssi":1,"firmwareVersion":firmwareVersion,"status":3,"userId":userId,"longitude":longitude,"latitude":latitude}
|
||||||
# client.publish('raspberry/topic',payload=i,qos=0,retain=False)
|
# client.publish('raspberry/topic',payload=i,qos=0,retain=False)
|
||||||
jsonData=json.dumps(doc)
|
jsonData=json.dumps(doc)
|
||||||
printMsg("发布设备信息:"+pInfoTopic+" "+jsonData)
|
printMsg("发布设备信息:"+pInfoTopic+" "+jsonData)
|
||||||
@@ -251,21 +251,18 @@ def randomPropertyData():
|
|||||||
#连接mqtt
|
#连接mqtt
|
||||||
def connectMqtt():
|
def connectMqtt():
|
||||||
printMsg("连接Mqtt服务器")
|
printMsg("连接Mqtt服务器")
|
||||||
# 生成mqtt认证密码(密码 = mqtt密码 & 用户ID & 过期时间)
|
# 生成mqtt认证密码(设备加密认证,密码加密格式为:mqtt密码 & 过期时间 & 授权码,其中授权码为可选)
|
||||||
password = generationPwd()
|
password = generationPwd()
|
||||||
encryptPassword=encrypt(password,mqttSecret,wumei_iv)
|
encryptPassword=encrypt(password,mqttSecret,wumei_iv)
|
||||||
# 生成mqtt认证密码(密码 = mqtt密码 & 用户ID & 过期时间)
|
|
||||||
client.username_pw_set(mqttUserName,encryptPassword)
|
client.username_pw_set(mqttUserName,encryptPassword)
|
||||||
client.on_connect=on_connect
|
client.on_connect=on_connect
|
||||||
client.on_message=on_message
|
client.on_message=on_message
|
||||||
client.connect(mqttHost,mqttPort,10)
|
client.connect(mqttHost,mqttPort,10)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#打印提示信息
|
#打印提示信息
|
||||||
def printMsg(msg):
|
def printMsg(msg):
|
||||||
print("[{}] {}".format(time.strftime("%Y-%m-%d %H:%M:%S"),msg))
|
print("[{}] {}".format(time.strftime("%Y-%m-%d %H:%M:%S"),msg))
|
||||||
|
|
||||||
# 生成密码
|
# 生成密码
|
||||||
def generationPwd():
|
def generationPwd():
|
||||||
try:
|
try:
|
||||||
@@ -279,9 +276,15 @@ def generationPwd():
|
|||||||
deviceRecvTime = round(time.time()*1000)
|
deviceRecvTime = round(time.time()*1000)
|
||||||
now = (serverSendTime + serverRecvTime + deviceRecvTime - deviceSendTime) / 2
|
now = (serverSendTime + serverRecvTime + deviceRecvTime - deviceSendTime) / 2
|
||||||
expireTime = int(now + 1 * 60 * 60 * 1000)
|
expireTime = int(now + 1 * 60 * 60 * 1000)
|
||||||
password = mqttPwd + "&" + userId + "&" + str(expireTime)
|
# 密码加密格式为:mqtt密码 & 过期时间 & 授权码(可选),如果产品启用了授权码就必须加上
|
||||||
|
password=""
|
||||||
|
if(authCode == ""):
|
||||||
|
password = mqttPwd + "&" + str(expireTime, 0)
|
||||||
|
else:
|
||||||
|
password = mqttPwd + "&" + str(expireTime, 0) + "&" + authCode
|
||||||
printMsg("密码(未加密):" + password)
|
printMsg("密码(未加密):" + password)
|
||||||
return password
|
return password
|
||||||
|
|
||||||
# HTTP获取时间
|
# HTTP获取时间
|
||||||
def getTime():
|
def getTime():
|
||||||
try:
|
try:
|
||||||
@@ -295,7 +298,7 @@ def getTime():
|
|||||||
except:
|
except:
|
||||||
printMsg("连接Http失败")
|
printMsg("连接Http失败")
|
||||||
|
|
||||||
|
# 定时上报属性
|
||||||
def timing_publishProperty():
|
def timing_publishProperty():
|
||||||
printMsg("执行定时上报")
|
printMsg("执行定时上报")
|
||||||
#发布事件
|
#发布事件
|
||||||
@@ -307,6 +310,8 @@ def timing_publishProperty():
|
|||||||
publishProperty(msg)
|
publishProperty(msg)
|
||||||
t1=threading.Timer(60,timing_publishProperty)
|
t1=threading.Timer(60,timing_publishProperty)
|
||||||
t1.start()
|
t1.start()
|
||||||
|
|
||||||
|
# 定时上报监测数据
|
||||||
def timing_publishMonitor():
|
def timing_publishMonitor():
|
||||||
global monitorCount
|
global monitorCount
|
||||||
monitorCount=monitorCount-1
|
monitorCount=monitorCount-1
|
||||||
|
|||||||
Reference in New Issue
Block a user