pdf-icon

UIFlow Guide

UiFlow1 Blockly

Event

Unit

UiFlow1 Project

Module13.2 LAN

Example

Connect to the MQTT server through the LAN module, subscribe to and publish messages, and poll downstream messages to control the device status (e.g., controlling the relay switch).

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

setScreenColor(0x810000)

lan_topic = None
lan_msg = None

lan = module.get(module.LAN_MODULE)

label2 = M5TextBox(53, 219, "ON", lcd.FONT_DejaVu18, 0x810000, rotate=0)
label3 = M5TextBox(230, 219, "OFF", lcd.FONT_DejaVu18, 0x810000, rotate=0)

def module_lan_mqtt_cb(lan_mq_topic, lan_mq_payload):
  global lan_topic, lan_msg
  lan_topic = lan_mq_topic
  lan_msg = lan_mq_payload
  print(lan_msg)
  pass

lan.lan_init(18, 23, 19, 5, 0, 35)
print('Lan Init')
wait(1.5)
lan.mqtt_config('mqtt.m5stack.com', 1883, 'm5mqttid', '', '', 120)
print('Mqtt Configured')
wait(1.5)
lan.mqtt_connect()
print('Connect Server')
wait(1.5)
lan.mqtt_subscribe('m5stack/relay', module_lan_mqtt_cb, 0)
while True:
  lan.mqtt_publish('m5stack/relay', 'ON', 0)
  wait(1.5)
  lan.mqtt_poll_loop()
  if False:
    print('ON')
  else:
    print('OFF')
  wait_ms(2)

API

lan.is_available_packet(1)
  • Configure LAN TCP or UDP connection, specify the socket type (TCP or UDP), port number, and device type (server or client).
lan.tcp_udp_config('', 0, 1, 1)
  • Fetch data from the server by specifying a topic and token.
lan.get_ezdata(ezdata_get_AlUvNcb, 'GCJ3Ic5h2eXnzV3rT3bBXvrncCaJnART', '')
  • Asynchronously get EZData (requires token and topic)
lan.remove_ezdata('GCJ3Ic5h2eXnzV3rT3bBXvrncCaJnART', '')
  • Delete EZData cloud data
lan.set_ezdata('GCJ3Ic5h2eXnzV3rT3bBXvrncCaJnART', '', '', 0)
  • Save data to EZData cloud (parameters: token, topic, data, timeout)
req.text
  • Get HTTP response text content
lan.get_if_config()
  • Get network interface configuration (IP/mask/gateway/DNS)
req.status_code
  • Get HTTP status code
try:
  req = lan.http_request(method='GET', url='', headers={})
  gc.collect()
  req.close()
except:
  pass
  • Send HTTP request (auto memory recycle)
lan.lan_init(18, 23, 19, 5, 0, 35)
  • Initialize LAN module (parameters: RST/CS/INT/MOSI/MISO/SCLK pins)
lan.local_ip()
  • Get local IP address
lan.mqtt_is_connect()
  • Check MQTT connection status
lan.mqtt_connect()
  • Establish MQTT connection
lan.mqtt_disconnect()
  • Disconnect MQTT
lan.mqtt_config('mqtt.m5stack.com', 1883, '', '', '', 120)
  • Configure MQTT parameters (server/port/username/password/clientID/keepalive)
lan.mqtt_poll_loop()
  • MQTT message polling (need to call in loop)
lan.mqtt_publish('', '', 0)
  • Publish MQTT message (parameters: topic/message/QoS level)
lan.mqtt_subscribe('', module_lan_mqtt_cb, 0)
  • Subscribe MQTT topic (with callback function)
def module_lan_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
  • MQTT subscription callback template
lan.remote_ip()
  • Get remote host IP address
lan.set_if_config('192.168.1.100', '255.255.255.0', '192.168.1.1', '8.8.8.8')
  • Manually set network interface (IP/mask/gateway/DNS)
lan.socket_close()
  • Close current socket connection
lan.tcp_receive_packet(0)
  • TCP receive packet (parameter: socket handle)
lan.tcp_send_packet('1234')
  • TCP send packet
lan.udp_receive_packet(0)
  • UDP receive packet (parameter: socket handle)
lan.udp_send_packet('', 0, '')
  • UDP send packet (parameters: target IP/port/data)
On This Page