2022-12-11 14:54:56 +08:00
|
|
|
import mqtt
|
2022-12-11 19:08:16 +08:00
|
|
|
import PikaStdDevice
|
2022-12-11 14:54:56 +08:00
|
|
|
|
2022-12-18 22:25:27 +08:00
|
|
|
client = mqtt.MQTT('broker.emqx.io', port=1883, clinetID='clientid', username='name_', password='passwd_')
|
2022-12-11 14:54:56 +08:00
|
|
|
|
|
|
|
ret = client.connect()
|
2022-12-18 00:41:57 +08:00
|
|
|
print("connect ret:%d" % ret)
|
2022-12-11 14:54:56 +08:00
|
|
|
|
2022-12-18 22:25:27 +08:00
|
|
|
|
2022-12-11 19:08:16 +08:00
|
|
|
def callback0(signal):
|
2022-12-18 00:41:57 +08:00
|
|
|
recv_msg = client.getMsg(signal)
|
|
|
|
recv_topic = client.getTopic(signal)
|
2022-12-18 21:47:23 +08:00
|
|
|
recv_qos = client.getQos(signal)
|
2022-12-18 22:25:27 +08:00
|
|
|
print("py0 cb: %s-qos:%d-->>%s" % (recv_topic, recv_qos, recv_msg))
|
|
|
|
|
2022-12-18 00:41:57 +08:00
|
|
|
|
|
|
|
def callback1(signal):
|
|
|
|
recv_msg = client.getMsg(signal)
|
|
|
|
recv_topic = client.getTopic(signal)
|
2022-12-18 21:47:23 +08:00
|
|
|
recv_qos = client.getQos(signal)
|
2022-12-18 22:25:27 +08:00
|
|
|
print("py1 cb: %s-qos:%d-->>%s" % (recv_topic, recv_qos, recv_msg))
|
|
|
|
|
2022-12-18 00:41:57 +08:00
|
|
|
|
|
|
|
def callback2(signal):
|
|
|
|
recv_msg = client.getMsg(signal)
|
|
|
|
recv_topic = client.getTopic(signal)
|
2022-12-18 11:52:20 +08:00
|
|
|
recv_qos = client.getQos(signal)
|
2022-12-18 22:25:27 +08:00
|
|
|
print("py2 cb: %s-qos:%d-->>%s" % (recv_topic, recv_qos, recv_msg))
|
2022-12-18 00:41:57 +08:00
|
|
|
|
2022-12-27 15:53:20 +08:00
|
|
|
def reconnect_mq(signal):
|
|
|
|
print('lost mqtt connect and try to reconnect')
|
|
|
|
print('signal:',signal)
|
2022-12-18 22:25:27 +08:00
|
|
|
|
2022-12-27 15:53:20 +08:00
|
|
|
client.setKeepAlive(5)
|
2022-12-18 22:25:27 +08:00
|
|
|
ret = client.subscribe('topic_pikapy_qos0', callback0, 0)
|
2022-12-18 21:47:23 +08:00
|
|
|
print("subscribe ret:%d" % ret)
|
2022-12-18 22:25:27 +08:00
|
|
|
ret = client.subscribe('topic_pikapy_qos1', callback1, 1)
|
2022-12-18 21:47:23 +08:00
|
|
|
print("subscribe ret:%d" % ret)
|
2022-12-18 22:25:27 +08:00
|
|
|
ret = client.subscribe('topic_pikapy_qos2', callback2, 2)
|
2022-12-18 00:41:57 +08:00
|
|
|
print("subscribe ret:%d" % ret)
|
2022-12-11 14:54:56 +08:00
|
|
|
|
2022-12-18 22:25:27 +08:00
|
|
|
# sleep wait for recv data
|
2022-12-11 19:08:16 +08:00
|
|
|
T = PikaStdDevice.Time()
|
2022-12-18 21:47:23 +08:00
|
|
|
T.sleep_s(5)
|
2022-12-18 22:25:27 +08:00
|
|
|
|
2022-12-18 18:48:16 +08:00
|
|
|
out = client.listSubscribeTopic()
|
2022-12-18 22:25:27 +08:00
|
|
|
print('listSubscribeTopic out', out)
|
2022-12-11 19:08:16 +08:00
|
|
|
|
2022-12-11 21:42:55 +08:00
|
|
|
# client.unsubscribe('topic_pikapy_qos0');
|
|
|
|
# client.unsubscribe('topic_pikapy_qos1');
|
|
|
|
# client.unsubscribe('topic_pikapy_qos2');
|
|
|
|
# T.sleep_s(5)
|
2022-12-18 21:47:23 +08:00
|
|
|
# out2 = client.listSubscribeTopic()
|
|
|
|
# print('listSubscribeTopic out2',out2)
|
2022-12-11 14:54:56 +08:00
|
|
|
|
2022-12-27 15:53:20 +08:00
|
|
|
ret = client.setDisconnectHandler(reconnect_mq);
|
|
|
|
print("setDisconnectHandler:%d" % ret)
|
2022-12-11 14:54:56 +08:00
|
|
|
|
2022-12-11 21:42:55 +08:00
|
|
|
# ret = client.setWill(1,'topic_will',1,'lost mqtt connect')
|
2022-12-27 15:53:20 +08:00
|
|
|
T.sleep_s(30)
|
2022-12-11 21:42:55 +08:00
|
|
|
# exit()
|
2022-12-11 19:08:16 +08:00
|
|
|
ret = client.disconnect()
|
2022-12-18 22:25:27 +08:00
|
|
|
print("disconnect ret:%d" % ret)
|