🤖Have you ever tried Chat.M5Stack.com before asking??😎
    M5Stack Community
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Register
    • Login

    Help Needed: RFID Reader Timeout When Adding Wi-Fi and MQTT Code on M5Stack with MicroPython

    UiFlow 2.0
    2
    6
    631
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • E
      edraak
      last edited by

      Hi everyone,

      I'm working on a project using the M5 Dial with MicroPython and UIFlow 2.0, and I'm encountering an issue where adding Wi-Fi and MQTT code causes my RFID reader to timeout and throw exceptions. When I run the RFID code alone, it works perfectly, but when I include the Wi-Fi connection and MQTT functionalities, the RFID reader starts experiencing problems.

      Problem Description:

      • RFID Reader Works Alone: The RFID reader functions correctly when running only the RFID-related code.

      • Issue When Adding Wi-Fi and MQTT: Including Wi-Fi connection code and MQTT client causes the RFID reader to throw timeout errors and sometimes leads to the device restarting.

      • Error Messages:

        Exception during RFID operation: [Errno 116] ETIMEDOUT
        Traceback (most recent call last):
          File "<stdin>", line XX, in main_loop
          File "unit/rfid.py", line X, in is_new_card_present
          File "driver/mfrc522/__init__.py", line XXX, in picc_request_a
          ... (additional traceback lines)
        OSError: [Errno 116] ETIMEDOUT
        

      What I'm Doing:

      • RFID Reader: Reading RFID tags using the built-in RFID unit connected via the M5Stack PORT.B (I2C).
      • Wi-Fi Connection: Connecting to a Wi-Fi network using the built-in Wi-Fi module.
      • MQTT Client: Publishing and subscribing to topics using the umqtt.simple library.
      • Asynchronous Tasks: Using uasyncio to handle asynchronous operations, including reading RFID tags and maintaining MQTT connections.

      Code Snippets:

      Here are the relevant parts of my code:

      • RFID Reading Loop:

        async def main_loop():
            global rfid
            while True:
                try:
                    if rfid:
                        # Attempt to detect a card
                        card_present = rfid.is_new_card_present()
                        if card_present:
                            # Read card UID
                            card_uid = rfid.read_card_uid()
                            # Process the card UID
                    else:
                        print("RFID object is None, attempting to reinitialize")
                        try:
                            rfid = RFID()
                            print("RFID reinitialized")
                        except Exception as e:
                            print(f"Error reinitializing RFID: {e}")
                except Exception as e:
                    print(f"Exception in main_loop: {e}")
                await asyncio.sleep(0.1)
        
      • Wi-Fi Connection Function:

        async def connect_wifi():
            global wlan
            wlan = network.WLAN(network.STA_IF)
            wlan.active(True)
            await asyncio.sleep(1)
            wlan.connect('Your_SSID', 'Your_Password')
            while not wlan.isconnected():
                print('Waiting for Wi-Fi connection...')
                await asyncio.sleep(1)
            print('Wi-Fi Connected')
        
      • MQTT Connection Function:

        async def connect_mqtt():
            global mqtt_client
            mqtt_client = MQTTClient('client_id', MQTT_BROKER, port=MQTT_PORT, user=MQTT_USER, password=MQTT_PASSWORD)
            mqtt_client.connect()
            print('MQTT Connected')
        
      • Initialization Function:

        async def setup():
            global rfid
            M5.begin()
            # Initialize RFID
            try:
                rfid = RFID()
                print("RFID initialized")
            except Exception as e:
                print(f"Error initializing RFID: {e}")
                rfid = None
            # Connect to Wi-Fi
            await connect_wifi()
            # Connect to MQTT
            await connect_mqtt()
        

      Observations:

      • Possible Resource Conflict: I suspect there might be a resource conflict between the RFID reader and the Wi-Fi module when both are active.
      • Task Scheduling Issues: The asynchronous tasks for Wi-Fi, MQTT, and RFID might be interfering with each other, causing timeouts in RFID operations.
      • RFID Timeout Only When Wi-Fi and MQTT Are Active: If I remove the Wi-Fi and MQTT code, the RFID reader works without any issues.

      What I've Tried:

      1. Using Asyncio Locks: Implemented asyncio.Lock() around hardware access code to prevent resource conflicts.

        # Initialize a lock
        lock = asyncio.Lock()
        
        # In RFID operation
        async with lock:
            # RFID read code
        
        # In Wi-Fi and MQTT operations
        async with lock:
            # Wi-Fi and MQTT code
        
      2. Adjusting Task Frequencies: Increased sleep durations in non-critical tasks to reduce CPU load and potential interference.

      3. Ensuring Proper Initialization: Made sure all variables and hardware components are properly initialized and declared as global where necessary.

      4. Simplifying the Code: Tested the code by removing the Wi-Fi signal strength monitoring task, but the issue still persists when MQTT is active.

      Questions:

      1. Resource Contention: Could the RFID reader and Wi-Fi module be conflicting due to shared hardware resources? If so, how can I effectively manage this conflict?

      2. Task Synchronization: Is there a recommended way to synchronize tasks involving RFID, Wi-Fi, and MQTT to prevent interference?

      3. Hardware Limitations: Are there known limitations with the M5Stack Core2 when using RFID and Wi-Fi/MQTT simultaneously?

      4. Alternative Approaches: Suggest some other methods to handle such scnarios

      Additional Information:

      • Device: M5Stack Core2
      • Programming Language: MicroPython
      • RFID Reader Connection: Built-in RFID unit connected via I2C (PORT.B)
      • Wi-Fi Module: Built-in Wi-Fi module
      • MQTT Library: Using umqtt.simple
      • Asyncio Version: Using uasyncio for asynchronous operations

      Any insights or suggestions would be greatly appreciated! I'm looking for guidance on how to resolve this issue so that I can use RFID, Wi-Fi, and MQTT together without conflicts.


      Thank you!

      felmueF 1 Reply Last reply Reply Quote 0
      • felmueF
        felmue @edraak
        last edited by

        Hello @edraak

        I am confused about whether you are using an M5Core2 or an M5Dial. Could you please clarify?

        Also it would help if you could share your complete program in the UIFlow2 Project Zone.

        Thanks
        Felix

        GPIO translation table M5Stack / M5Core2
        Information about various M5Stack products.
        Code examples

        E 1 Reply Last reply Reply Quote 0
        • E
          edraak @felmue
          last edited by

          @felmue
          Hi, Apologies for confusion I am using m5 dial
          and below is the link of my code

          https://uiflow2.m5stack.com/?pkey=7b06b0d459f14c8ab16e70d5b073bf76

          felmueF 1 Reply Last reply Reply Quote 0
          • felmueF
            felmue @edraak
            last edited by

            Hi @edraak

            I am running your code with test.mosquitto.org as MQTT broker; but so far I was not able to break it, e.g. no ETIMEDOUT error so far.

            Thanks
            Felix

            GPIO translation table M5Stack / M5Core2
            Information about various M5Stack products.
            Code examples

            felmueF 1 Reply Last reply Reply Quote 0
            • felmueF
              felmue @felmue
              last edited by

              Hello @edraak

              one thing I would change. After card_uid = rfid.read_card_uid() check for read being successful: e.g. if card_uid != None:

              Thanks
              Felix

              GPIO translation table M5Stack / M5Core2
              Information about various M5Stack products.
              Code examples

              E 1 Reply Last reply Reply Quote 0
              • E
                edraak @felmue
                last edited by edraak

                @felmue

                This error occurs inconsistently—sometimes very frequently, and other times, it only happens after 4,000 or 5,000 scans. In some cases, I encounter the issue after just 20 scans.

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post