Micropython Support for M5Stack Core2 + LAN W5500 Module



  • I'm using the LAN Module with my M5Stack Core2 to connect to an MQTT server on the Internet through Ethernet. I was able to connect seamlessly via Wi-Fi, but the switch to Ethernet has been rough. None of the examples provided for Micropython work on my device. I continue to get a 'failed to establish connection error' when I run any programs with the LAN Module. Any help is appreciated, and I do not want to switch to the Arduino IDE.

    Here is my code so far:
    from m5stack import *
    from m5stack_ui import *
    from uiflow import *
    import wifiCfg
    from machine import I2C, Pin, Timer, SPI
    import time
    import module
    #from libs.ethernet.wiznet5k import WIZNET5K
    #import libs.ethernet.wiznet5k_socket as socket
    from m5mqtt import M5mqtt
    import random
    import network

    power.setBusPowerMode(0)
    screen = M5Screen()
    screen.clean_screen()
    screen.set_screen_bg_color(0x00ffff)
    lcd.font(lcd.FONT_DejaVu24)

    lan = module.get(module.LANBASE)
    eth0ip = lan.get_if_config()[0]
    #nic = network.LAN(0)

    #WIFI COMMUNICATION
    #wifiCfg.doConnect(...)

    #while not (wifiCfg.wlan_sta.isconnected()):
    #time.sleep(1.0)

    #ETHERNET COMMUNICATION

    lan.tcp_udp_config(str(eth0ip), 4711, 1, 2) <- connection error
    time.sleep(1.0)
    lcd.text(0, 50, 'TCP Connected', lcd.ORANGE)



  • I am able to ping my M5Stack Core2 through Command Prompt, and Wireshark shows the device attempting to communicate before the error occurs.



  • Hello @AdamMillionP

    have you tried the UIFlow MQTT blocks under Modules - LAN Base - Init MQTT Server etc.? This should give you something like below:

    from m5stack import *
    from m5stack_ui import *
    from uiflow import *
    import module
    
    screen = M5Screen()
    screen.clean_screen()
    screen.set_screen_bg_color(0xFFFFFF)
    
    lan = module.get(module.LANBASE)
    lan.mqtt_config('mqtt.m5stack.com', 1883, '', '', '', 120)
    lan.mqtt_connect()
    

    Thanks
    Felix



  • Thank you for your reply, Felix. Those blocks are not available to me in UIFlow. Was there a recent update that made them available? I used the m5mqtt library to connect to my HiveMQ server over WiFi. I was not aware that there were specific MQTT functions for the LAN Module. What functions are available to me in that respect?



  • I was using the downloadable version of UIFlow, and I did not realize that the web version had more functions available. I see what you are talking about now, and I will investigate it further.



  • @felmue
    I was able to set up one Core2 to publish to a local Mosquitto server successfully. It connects, and I am able to see the data being published. This was all done with the LAN Module and the code you directed me to. Now I have a second Core2 connected to another LAN Module that I want to use as a Subscriber on the same Mosquitto server. It connects to the server, but it closes its connection when the data that it is subscribing to gets published. Is there something else that I have to do to properly configure a subscriber? I noticed that both devices have the same IP address. Is that messing things up?
    Here is the new code:

    from m5stack import *
    from m5stack_ui import *
    from uiflow import *
    import wifiCfg
    from m5mqtt import M5mqtt![alt text](image url)
    from machine import I2C, Pin, Timer, SPI
    import time
    import module
    import network

    def subscribe_to_STUFF_(topic, payload):
    global topic_data, payload_data
    topic_data = topic
    payload_data = payload
    lcd.textClear(0, 10, 'TOPIC DATA: AAAAAAAAAAAAAAAAAAA', 0x00ffff)
    lcd.text(0, 10, 'TOPIC DATA: ' + str(payload_data), lcd.ORANGE)
    pass

    power.setBusPowerMode(0)
    screen = M5Screen()
    screen.clean_screen()
    screen.set_screen_bg_color(0x00ffff)
    lcd.font(lcd.FONT_DejaVu24)

    lan = module.get(module.LANBASE)
    eth0ip = lan.get_if_config()[0]
    #lan.mqtt_disconnect()
    #nic = network.LAN(0)

    #WIFI COMMUNICATION
    #wifiCfg.doConnect('MARS', 'M.A.R.S.')

    #while not (wifiCfg.wlan_sta.isconnected()):
    #time.sleep(1.0)

    #ETHERNET COMMUNICATION
    lan.mqtt_config('192.168.2.146', 1883, 'ADAMS', '', '', 300)
    lan.mqtt_subscribe('STUFF', subscribe_to_STUFF_, 0)
    lan.mqtt_connect()
    while not (lan.mqtt_is_connect()):
    time.sleep(1.0)
    lcd.text(0, 180, 'SUBSCRIBED', lcd.ORANGE)

    Here is the Mosquitto log:
    0_1692907839975_c0f730e9-f7ec-4bd8-a0fe-db84102998ea-image.png



  • Hello @AdamMillionP

    having two devices using the same IP address is never a good thing in any network.

    The other thing which needs to be different between clients is the MQTT client id as it is used by the Mosquitto broker to distinguish between clients.

    Thanks
    Felix