pdf-icon

UIFlow Guide

UIFlow 1.0 Blockly

Event

Unit

UIFlow 1.0 Project

Module Lan

案例程序

通过LAN模块连接到MQTT服务器,订阅和发布消息,并轮询下行消息来控制设备的状态(例如控制继电器的开关)

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)

功能说明

lan.is_available_packet(1)
  • 配置局域网TCP或UDP连接,指定套接字类型(如TCP或UDP)、端口号和设备类型(如服务器或客户端)
lan.tcp_udp_config('', 0, 1, 1)
  • 通过指定的主题和token从服务器获取数据
lan.get_ezdata(ezdata_get_AlUvNcb, 'GCJ3Ic5h2eXnzV3rT3bBXvrncCaJnART', '')
  • uiflow_block_module_lan_ezdata_async_get_value
lan.remove_ezdata('GCJ3Ic5h2eXnzV3rT3bBXvrncCaJnART', '')
  • uiflow_block_module_lan_ezdata_remove
lan.set_ezdata('GCJ3Ic5h2eXnzV3rT3bBXvrncCaJnART', '', '', 0)
  • uiflow_block_module_lan_ezdata_save
req.text
  • uiflow_block_module_lan_get_data
lan.get_if_config()
  • uiflow_block_module_lan_get_if_config
req.status_code
  • uiflow_block_module_lan_get_status_code
try:
  req = lan.http_request(method='GET', url='', headers={})
  gc.collect()
  req.close()
except:
  pass
  • uiflow_block_module_lan_http_request
lan.lan_init(18, 23, 19, 5, 0, 35)
  • uiflow_block_module_lan_init
lan.local_ip()
  • uiflow_block_module_lan_local_ip
lan.mqtt_is_connect()
  • uiflow_block_module_lan_mqtt_check_connection
lan.mqtt_connect()
  • uiflow_block_module_lan_mqtt_connect
lan.mqtt_disconnect()
  • uiflow_block_module_lan_mqtt_disconnect
lan.mqtt_config('mqtt.m5stack.com', 1883, '', '', '', 120)
  • uiflow_block_module_lan_mqtt_init
lan.mqtt_poll_loop()
  • uiflow_block_module_lan_mqtt_poll
lan.mqtt_publish('', '', 0)
  • uiflow_block_module_lan_mqtt_publish
lan.mqtt_subscribe('', module_lan_mqtt_cb, 0)
  • uiflow_block_module_lan_mqtt_sub
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
  • uiflow_block_module_lan_mqtt_sub_cb
lan.remote_ip()
  • uiflow_block_module_lan_remote_ip
lan.set_if_config('192.168.1.100', '255.255.255.0', '192.168.1.1', '8.8.8.8')
  • uiflow_block_module_lan_set_ifconfig
lan.socket_close()
  • uiflow_block_module_lan_socket_close
lan.tcp_receive_packet(0)
  • uiflow_block_module_lan_tcp_receive_packet
lan.tcp_send_packet('1234')
  • uiflow_block_module_lan_tcp_send_packet
lan.udp_receive_packet(0)
  • uiflow_block_module_lan_udp_receive_packet
lan.udp_send_packet('', 0, '')
  • uiflow_block_module_lan_udp_send_packet
On This Page