pdf-icon

UIFlow Guide

UIFlow 1.0 Blockly

Event

Unit

UIFlow 1.0 Project

Module Lan Base

Example

Initialize a TCP server, establish a connection with a specified IP and port, and continuously send randomly generated data packets. Also, monitor for available packets and print the received packet size.

from m5stack import *
from m5ui import *
from uiflow import *
import module
import time

setScreenColor(0x222222)

counter = None

lan = module.get(module.LANBASE)

import random

counter = 0
print('Start the TCP Server')
print((str('Local IP address: ') + str((lan.local_ip()))))
lan.tcp_udp_config('192.168.1.97', 55005, 1, 1)
print('TCP Connected')
print((str('Remote IP address: ') + str((lan.remote_ip()))))
while True:
  counter = random.randint(100000, 999999)
  lan.tcp_send_packet(str(counter))
  wait(1)
  if lan.is_available_packet(1):
    print((str('TCP receive packet size: ') + str((lan.tcp_receive_packet(0)))) )
  wait(1)
  wait_ms(2)

API

lan.is_available_packet(1)
  • This block indicates that the currently available packet protocol is TCP.
 lan.get_ezdata(ezdata_get_fOkSZcb, 'GCJ3Ic5h2eXnzV3rT3bBXvrncCaJnART', '')
  • Get values from a specified topic and verify using the provided token.
lan.remove_ezdata('GCJ3Ic5h2eXnzV3rT3bBXvrncCaJnART', '')
  • Remove data associated with the specified topic and verify using the token.
lan.set_ezdata('GCJ3Ic5h2eXnzV3rT3bBXvrncCaJnART', '', '', 0)
  • Save a value to the specified topic, also using the token for verification. A save mode (like Single) can be chosen.
req.text
  • Fetch data from the LAN.
lan.get_if_config()
  • Check if the configuration exists or fetch the current configuration.
req.status_code
  • Fetch the status code and return an integer value.
try:
    req = lan.http_request(method='GET', url='', headers={})
    gc.collect()
    req.close()
  except:
    pass
  • Method: Specify the HTTP request method, such as GET.
  • URL: The URL address to request.
  • Headers: Set request headers using a map.
  • Data: Send data via a map.
  • Success and Fail: Define actions for successful or failed requests.
lan.tcp_udp_config('', 0, 1, 1)
  • remote IP: The remote device's IP address.
  • port: Communication port.
  • socket type: Choose the communication protocol (TCP or UDP).
  • machine type: Specify whether it's a server or client.
lan.local_ip()
  • Fetch the local device's IP address.
lan.modbus_init(15, 5, 115200, 1, 1)
  • bandrate: Set the baud rate, here it's 115200.
  • mode: Choose between master or slave device mode.
  • slave addr: Set the slave device's address.
modbus.read_coils(1, 1, 0)
  • slave address: Read from the slave device's address.
  • starting address: Start reading at the register address.
  • coil qty: Quantity of coils to read.
modbus.read_discrete_inputs(1, 1, 0)
  • Read data from the slave device's discrete input registers.
    • slave address: The slave device's address.
    • starting address: The register address to start reading from.
    • input qty: The number of inputs to read.
modbus.read_holding_registers(1, 1, 0, True)
  • Read data from the slave device's holding registers.
    • slave address: The slave device's address.
    • starting address: The register address to start reading from.
    • register qty: The number of registers to read.
    • signed: Specify whether the data is signed.
modbus.read_input_registers(1, 1, 0, True)
  • Read data from the slave device's input registers.
    • slave address: The slave device's address.
    • starting address: The register address to start reading from.
    • register qty: The number of registers to read.
    • signed: Specify whether the data is signed.
modbus.write_multiple_coils(1, 1, 0)
  • Write data to multiple coil registers on the slave device.
    • slave address: The slave device's address.
    • starting address: The register address to start writing to.
    • output value: The output value to write.
modbus.write_multiple_registers(1, 1, 0, True)
  • Write data to multiple holding registers on the slave device.
    • slave address: The slave device's address.
    • starting address: The

register address to start writing to. - register value: The register value to write. - signed: Specify whether the data is signed.

modbus.write_single_coil(1, 1, 0)
  • Write data to a single coil register on the slave device.
    • slave address: The slave device's address.
    • output address: The coil register's address.
    • output value: The value to write.
modbus.write_single_register(1, 1, 0, True)
  • Write data to a single holding register on the slave device.
    • slave address: The slave device's address.
    • register address: The register's address.
    • register value: The register value to write.
    • signed: Specify whether the data is signed.
lan.modbus_init(15, 5, 115200, 1, 1)
  • Set the function code to specify the Modbus command.
    • READ_COILS_STATUS: The function code to read coil status.
modbus.find_address
  • Fetch the slave device's address.
modbus.find_function
  • Fetch the current function code.
modbus.find_quantity
  • Fetch the quantity of data, used to query the number of registers to read/write.
modbus.function_init(1, 0, 0)
  • Initialize the function code and related parameters.
    • READ_COILS_STATUS: The function code to read coil status.
    • start addr: Start address.
    • quantity: The number of coils to read or operate.
modbus.receive_req_create_pdu()
  • Receive an ADU (Application Data Unit) request, used for data exchange between Modbus master and slave.
modbus.create_slave_response(1)
  • Send ADU response data, providing response data to the Modbus master.
modbus.update_process(1, 0, 0, [0, 0, 0])
  • Update the function's data information.
    • start addr: The start address to operate on.
    • quantity: The quantity of data to update.
    • value: The actual value to update.
lan.mqtt_is_connect()
  • Check the connection status with the MQTT server.
lan.mqtt_connect()
  • Connect to the MQTT server.
lan.mqtt_disconnect()
  • Disconnect from the MQTT server.
lan.mqtt_config('mqtt.m5stack.com', 1883, '', '', '', 120)
  • Initialize the MQTT connection.
    • mqtt.m5stack.com: Server address.
    • port 1883: Communication port.
    • client id: Client identifier.
    • username: Username.
    • password: Password.
    • keepalive 120: Heartbeat time (seconds) to maintain the connection.
  lan.mqtt_poll_loop()
  • Poll for downstream messages received from the MQTT server, checking for new messages sent to the device.
lan.mqtt_publish('', '', 0)
  • Publish a message to the specified topic. The device can send messages to other devices subscribed to the same topic.
    • topic: The topic to publish the message to.
    • payload: The message content to send.
    • QoS: Quality of Service level (typically 0 means the message is sent at most once, with no acknowledgment required).
lan.mqtt_subscribe('', lan_base_mqtt_cb, 0)
  • Subscribe to a topic to receive messages published under that topic.
    • topic: The topic to subscribe to. Once subscribed, the device will receive messages from that topic.
    • QoS: Control the Quality of Service level for the subscription.
def lan_base_mqtt_cb(lan_mq_topic, lan_mq_payload):
  global ezdata_value1, lan_topic, lan_msg
  lan_topic = lan_mq_topic
  lan_msg = lan_mq_payload
  pass
  • When the device receives a message from the subscribed topic, this statement triggers a callback function and displays the received topic and message content.
    • lan_topic: The topic of the received message.
    • lan_msg: The content of the received message.
lan.remote_ip()
  • Set the IP address of the remote server.
lan.set_if_config('192.168.1.100', '255.255.255.0', '192.168.1.1', '8.8.8.8')
  • Configure the device's IP address, subnet mask, gateway, and DNS information.

<img class="blockly_svg" src=" https://m5stack.oss-cn-shenzhen.aliyuncs

.com/resource/docs/static/assets/img/uiflow/blockly/modules/lan_base/uiflow_block_lan_base_socket_close.svg">

lan.socket_close()
  • Close the current TCP or UDP connection.
lan.tcp_receive_packet(0)
  • Set the size of the TCP packet to receive.
 lan.tcp_send_packet('1234')
  • Send the specified packet content using the TCP protocol.
modbus._mdbus_uart.any()
  • Check and retain data in the UART buffer.
modbus._mdbus_uart.read()
  • Read all data in the UART buffer.
modbus._mdbus_uart.readline()
  • Read an entire line of data in the UART buffer until a newline character is encountered.
modbus._mdbus_uart.read(10)
  • Read a specified number of characters.
modbus._mdbus_uart.write('')
  • Send the specified string data via UART.
modbus._mdbus_uart.write(''+"\r\n")
  • Send a line of string data via UART, usually appending a newline character at the end.
modbus._mdbus_uart.write(bytes([0, 0, 0]))
  • Send a raw data list via UART, commonly used for transmitting binary data.
lan.udp_receive_packet(0)
  • Set the size of the UDP packet to receive.
lan.udp_send_packet('', 0, '')
  • Send a UDP packet using the specified IP address, port, and payload.
On This Page