这个程序使用MQTT协议与服务器进行通信,连接后会订阅cat_topic
主题并发布消息“hello”。当接收到来自该主题的消息时,程序会更新RGB灯的颜色并打印消息内容。每次接收消息后,cat_msg
变量会被清空,并在下一次循环前等待1秒。
from m5stack import *
from m5ui import *
from uiflow import *
from base.DTU_CAT1 import DTU_CAT1
import time
cat_topic = None
cat_msg = None
def cat_mqtt_cb(cat_mq_topic, cat_mq_payload):
global cat_topic, cat_msg
cat_topic = cat_mq_topic
cat_msg = cat_mq_payload
rgb.setColorAll(0xffff00)
pass
cat = DTU_CAT1()
cat.mqtt_to_connect('mqtt.m5stack.com', 1883, 'cat1_4399', 'm5', 'm5', 120)
if cat.is_connect_mqtt():
rgb.setColorAll(0x33cc00)
print('MQTT Connected')
if cat.mqtt_subscribe('cat_topic', cat_mqtt_cb, 0):
print('topic subscribed')
cat.mqtt_publish('cat_topic', 'hello', 0)
while True:
cat.mqtt_poll()
if cat_msg != None:
print((str(((str(((str('message from topic: ') + str(cat_topic)))) + str(' : ')))) + str(cat_msg)))
cat_msg = None
rgb.setColorAll(0x33cc00)
wait(1)
wait_ms(2)
cat = DTU_CAT1()
modbus = cat.modbus_init(23, 33, 115200, 1, 1)
modbus.function_init(1, 0, 0)
cat.get_gprs_network_registration()
cat.get_network_registration()
cat.get_single_quality()
cat.check_status()
cat.enable_PDP_context()
cat.get_ezdata(ezdata_get_nOfcOcb, 'GCJ3Ic5h2eXnzV3rT3bBXvrncCaJnART', '')
cat.remove_ezdata('GCJ3Ic5h2eXnzV3rT3bBXvrncCaJnART', '')
cat.set_ezdata('GCJ3Ic5h2eXnzV3rT3bBXvrncCaJnART', '', '', 0)
cat.get_CCID()
cat.get_IMEI()
cat.http_get('')
cat.http_post('', 'application/json', '')
cat.http_terminate()
modbus.read_coils(1, 1, 0)
modbus.read_discrete_inputs(1, 1, 0)
modbus.read_holding_registers(1, 1, 0, True)
modbus.read_input_registers(1, 1, 0, True)
modbus.write_multiple_coils(1, 1, 0)
modbus.write_multiple_registers(1, 1, 0, True)
modbus.write_single_coil(1, 1, 0)
modbus.write_single_register(1, 1, 0, True)
1-6,15-16
modbus.find_address
modbus.find_function
modbus.find_quantity
modbus.receive_req_create_pdu()
modbus.create_slave_response(1)
modbus.update_process(1, 0, 0, [0, 0, 0])
cat.is_connect_mqtt()
cat.mqtt_to_connect('mqtt.m5stack.com', 1883, '', '', '', 120)
cat.mqtt_disconnect()
cat.mqtt_poll()
cat.mqtt_publish('', '', 0)
cat.mqtt_subscribe('', cat_mqtt_cb, 0)
def cat_mqtt_cb(cat_mq_topic, cat_mq_payload):
global ezdata_value1, cat_topic, cat_msg
cat_topic = cat_mq_topic
cat_msg = cat_mq_payload
pass
cat.mqtt_unsubscribe('')
cat.poweroff()
cat.reset()
cat.set_command_echo_mode(0)
modbus._mdbus_uart.any()
modbus._mdbus_uart.read()
modbus._mdbus_uart.readline()
modbus._mdbus_uart.read(10)
modbus._mdbus_uart.write('')
modbus._mdbus_uart.write(''+"\r\n")
modbus._mdbus_uart.write(bytes([0, 0, 0]))