|
|
@@ -5,8 +5,7 @@ from machine import Pin
|
|
|
from machine import Timer
|
|
|
import ustruct as struct
|
|
|
from umqtt.simple import MQTTClient
|
|
|
-
|
|
|
-led = Pin(2, Pin.OUT, value=0)
|
|
|
+import re
|
|
|
|
|
|
SERVER = 'mqtts.heclouds.com' # 服务器地址
|
|
|
PRO_ID = '522417' # 产品ID
|
|
|
@@ -25,30 +24,43 @@ class MqttOneNet():
|
|
|
# 暂未用到
|
|
|
self.accesskey = ""
|
|
|
self.device_id = ""
|
|
|
- self.client = self.client_init()
|
|
|
+ self.client = None
|
|
|
+ self.client_init()
|
|
|
self.data_point_id = 0
|
|
|
- self.upload_init()
|
|
|
+ # self.upload_start(2000)
|
|
|
|
|
|
- def upload_init(self):
|
|
|
- self.data_point_id = 0
|
|
|
+ def upload_start(self, ms):
|
|
|
self.tim = Timer(-1)
|
|
|
# 每 2000ms 定时回调一次 self.upload_data()
|
|
|
- self.tim.init(period=2000, mode=Timer.PERIODIC,
|
|
|
+ self.tim.init(period=ms, mode=Timer.PERIODIC,
|
|
|
callback=self.publish_timer_callback)
|
|
|
- print("init finish, start upload_data")
|
|
|
+ print("start upload_data")
|
|
|
|
|
|
+ def upload_stop(self):
|
|
|
+ self.tim.deinit()
|
|
|
+
|
|
|
def client_init(self):
|
|
|
- client = MQTTClient(client_id=self.device_name, server=self.broker_addr, port=self.broker_port, user=self.product_id,
|
|
|
+ self.client = MQTTClient(client_id=self.device_name, server=self.broker_addr, port=self.broker_port, user=self.product_id,
|
|
|
password=self.password, keepalive=360) # create a mqtt client
|
|
|
- client.connect() # connect mqtt
|
|
|
+ self.client.connect() # connect mqtt
|
|
|
print("mqtt connect")
|
|
|
- client.set_callback(self.client_msg_callback)
|
|
|
+ self.client.set_callback(self.client_msg_callback)
|
|
|
+
|
|
|
+ # 每 50ms 定时回调一次检查消息是否到来
|
|
|
+ self.tim = Timer(-2)
|
|
|
+ self.tim.init(period=50, mode=Timer.PERIODIC,
|
|
|
+ callback=self.check_mag_callback)
|
|
|
# 订阅所有消息
|
|
|
topic = f"$sys/{self.product_id}/{self.device_name}/#"
|
|
|
- client.subscribe(topic)
|
|
|
+ self.client.subscribe(topic, qos=1)
|
|
|
print("subscribed to %s topic" % (topic))
|
|
|
- return client
|
|
|
+
|
|
|
+ print("client init finish")
|
|
|
+
|
|
|
+ def check_mag_callback(self, timer):
|
|
|
+ self.client.check_msg()
|
|
|
|
|
|
+
|
|
|
# timer 是定时器回调函数需要的参数,实际上是定时器本身(self),如果没有这个参数,定时器会一直打印一个错误
|
|
|
# http://docs.micropython.org/en/latest/library/machine.Timer.html#machine-timer
|
|
|
def publish_timer_callback(self, timer):
|
|
|
@@ -59,13 +71,35 @@ class MqttOneNet():
|
|
|
print("------------- publish ------------- \n")
|
|
|
print("topic:%s\n" % topic_publish)
|
|
|
print("payload:%s\n" % payload)
|
|
|
- self.client.publish(topic=topic_publish, msg=payload, qos=1)
|
|
|
+ self.publish(topic_publish, payload, qos=1)
|
|
|
+
|
|
|
+ def response_cmd(self, topic_list, msg):
|
|
|
+ if topic_list[4] == 'request':
|
|
|
+ print("receive cmd:", msg)
|
|
|
+
|
|
|
+ cmdid = topic_list[5]
|
|
|
+ response_topic = f"$sys/{self.product_id}/{self.device_name}/cmd/response/{cmdid}"
|
|
|
+ payload = "12gfok"
|
|
|
+ print("------------- response ------------- \n")
|
|
|
+ print("topic:%s\n" % response_topic)
|
|
|
+ print("payload:%s\n" % payload)
|
|
|
+ self.publish(response_topic, payload)
|
|
|
|
|
|
def client_msg_callback(self, topic, msg):
|
|
|
print("\n------------- receive ------------- \n")
|
|
|
print("topic:%s\n" % topic)
|
|
|
print("payload:%s\n" % msg)
|
|
|
+
|
|
|
+ topic_list = str(topic, 'utf8').split('/')
|
|
|
+ # 收到服务器命令:$sys/{pid}/{device-name}/cmd/request/{cmdid}
|
|
|
+ if topic_list[3] == 'cmd':
|
|
|
+ self.response_cmd(topic_list, msg)
|
|
|
|
|
|
+ def publish(self, topic, msg, retain=False, qos=0):
|
|
|
+ # 必须转换成bytes类型发送
|
|
|
+ msg = bytes("{}".format(msg), 'utf8')
|
|
|
+ self.client.publish(topic=topic, msg=msg, retain=retain, qos=qos)
|
|
|
+
|
|
|
# 获取板子数据,用于上传到服务器
|
|
|
def get_upload_data(self):
|
|
|
self.data_point_id += 1
|
|
|
@@ -83,7 +117,6 @@ class MqttOneNet():
|
|
|
}]
|
|
|
}
|
|
|
}
|
|
|
- message = bytes("{}".format(message), 'utf8')
|
|
|
return message
|
|
|
|
|
|
def get_temperature(self):
|
|
|
@@ -105,6 +138,6 @@ class MqttOneNet():
|
|
|
# device = MqttOneNet()
|
|
|
if __name__ == '__main__':
|
|
|
try:
|
|
|
- MqttOneNet()
|
|
|
+ mq = MqttOneNet()
|
|
|
except Exception as e:
|
|
|
print(e)
|