Here is the document translated into English with the formatting intact:
DTU Cat1
Example Program
This program communicates with a server using the MQTT protocol. After connecting, it subscribes to the cat_topic
and publishes a message "hello". When a message is received on this topic, the program updates the RGB light color and prints the message content. Each time a message is received, the cat_msg
variable is cleared, and it waits 1 second before the next loop.
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)
API
cat = DTU_CAT1()
- Initializes the CAT1 DTU module to start using it for communication.
modbus = cat.modbus_init(23, 33, 115200, 1, 1)
- Sets the Modbus communication baud rate to 115200, in master mode, and specifies the slave address as 1.
modbus.function_init(1, 0, 0)
- Initializes the Modbus function code to read coil status. You can specify the start address and quantity to read.
cat.get_gprs_network_registration()
- Checks if the CAT1 DTU module is registered on the GPRS network.
cat.get_network_registration()
- Checks the network registration status to confirm if the module is registered to the network.
cat.get_single_quality()
- Checks the current signal quality, typically to assess signal strength and stability.
cat.check_status()
- Checks the current status of the module to ensure it is functioning correctly or to retrieve fault information.
cat.enable_PDP_context()
- Enables the PDP (Packet Data Protocol) context for the CAT1 DTU module to connect to the mobile network and begin data transmission.
cat.get_ezdata(ezdata_get_nOfcOcb, 'GCJ3Ic5h2eXnzV3rT3bBXvrncCaJnART', '')
- Retrieves a value from a specified topic, using the provided token to access the data.
cat.remove_ezdata('GCJ3Ic5h2eXnzV3rT3bBXvrncCaJnART', '')
- Deletes data from a specified topic, identified by the token.
cat.set_ezdata('GCJ3Ic5h2eXnzV3rT3bBXvrncCaJnART', '', '', 0)
- Saves data to a specified topic. You can choose the storage mode, such as Single mode, using the token to identify the topic.
cat.get_CCID()
- Retrieves the CCID (Integrated Circuit Card Identifier) of the SIM card, used to identify the inserted SIM card.
cat.get_IMEI()
- Retrieves the IMEI (International Mobile Equipment Identity) of the CAT1 DTU module, used for device identification.
cat.http_get('')
- Sends an HTTP(S) GET request to a specified URL to retrieve data from a server.
cat.http_post('', 'application/json', '')
- Sends an HTTP(S) POST request, specifying the URL and data type (e.g., JSON), to send data to a server.
cat.http_terminate()
- Terminates the current HTTP(S) connection.
Continuing with the translation:
modbus.read_coils(1, 1, 0)
- Reads the coil status from a Modbus slave, providing the slave address, start address, and the number of coils.
modbus.read_discrete_inputs(1, 1, 0)
- Reads discrete input signals from a Modbus slave, providing the slave address, start address, and the number of inputs.
modbus.read_holding_registers(1, 1, 0, True)
- Reads holding registers from a Modbus slave, specifying the slave address, start address, and the number of registers. You can also choose whether the data is signed.
modbus.read_input_registers(1, 1, 0, True)
- Reads input registers from a Modbus slave, similar to reading holding registers, but typically used for sensor data.
modbus.write_multiple_coils(1, 1, 0)
- Writes multiple coil states to a specified Modbus slave address. Set the slave address, start address, and output values.
modbus.write_multiple_registers(1, 1, 0, True)
- Writes multiple holding registers to a specified Modbus slave address. Set the slave address, start address, register values, and choose if the data is signed.
modbus.write_single_coil(1, 1, 0)
- Writes a single coil state to a specified Modbus slave address. Set the slave address, output address, and output value.
modbus.write_single_register(1, 1, 0, True)
- Writes a single holding register to a specified Modbus slave address. Set the slave address, register address, register value, and choose if the data is signed.
1-6,15-16
- Initializes the Modbus function codes used to read coil status.
modbus.find_address
- Retrieves the current Modbus slave address in communication.
modbus.find_function
- Retrieves the Modbus function code currently in use.
modbus.find_quantity
- Retrieves the quantity of data requested in the Modbus query, typically associated with the number of coils or registers.
modbus.receive_req_create_pdu()
- Receives the Application Data Unit (ADU) request from Modbus RTU. This is the data frame received from the Modbus master device.
modbus.create_slave_response(1)
- Sends the ADU data response back to the Modbus master device. Here, the response is set to the value 1.
modbus.update_process(1, 0, 0, [0, 0, 0])
- Updates the Modbus function code execution result. This function reads coil status and specifies the start address, quantity, and values in a list format.
cat.is_connect_mqtt()
- Checks the MQTT connection status to ensure it is connected to the MQTT server.
cat.mqtt_to_connect('mqtt.m5stack.com', 1883, '', '', '', 120)
- Initializes a connection to the MQTT server at mqtt.m5stack.com, port 1883. You need to specify the client id, username, and password for authentication. The keepalive is set to 120 seconds to maintain the heartbeat interval.
cat.mqtt_disconnect()
- Disconnects from the MQTT server.
cat.mqtt_poll()
- Polls the MQTT server to check if there is any downlink message for the client.
cat.mqtt_publish('', '', 0)
- Publishes a message to a specified MQTT topic. You need to provide the topic, payload, and QoS (Quality of Service level, default is 0).
cat.mqtt_subscribe('', cat_mqtt_cb, 0)
- Subscribes to a specified MQTT topic,
providing the topic name and QoS level.
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
- Sets a callback function for the subscribed MQTT topic, which is triggered when a message is received for the specified topic.
cat.mqtt_unsubscribe('')
- Unsubscribes from a specified MQTT topic.
cat.poweroff()
- Powers off the module to enter a low-power mode.
cat.reset()
- Resets and restarts the module.
cat.set_command_echo_mode(0)
- Sets the command echo mode, allowing you to enable or disable it (OFF).
modbus._mdbus_uart.any()
- Reads the UART buffer to check if there is any data available.
modbus._mdbus_uart.read()
- Reads all available data from UART.
modbus._mdbus_uart.readline()
- Reads data from UART until a newline character is encountered.
modbus._mdbus_uart.read(10)
- Reads a specified number of characters from UART, here set to 10 characters.
modbus._mdbus_uart.write('')
- Sends the specified data through UART.
modbus._mdbus_uart.write(''+"\r\n")
- Writes a complete line of data to UART, usually ending with a newline character, suitable for sending string data.
modbus._mdbus_uart.write(bytes([0, 0, 0]))
- Writes raw data in the form of a byte list to UART, suitable for sending binary data or non-string formats.