<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Help Needed: RFID Reader Timeout When Adding Wi-Fi and MQTT Code on M5Stack with MicroPython]]></title><description><![CDATA[<p dir="auto">Hi everyone,</p>
<p dir="auto">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.</p>
<p dir="auto"><strong>Problem Description:</strong></p>
<ul>
<li>
<p dir="auto"><strong>RFID Reader Works Alone:</strong> The RFID reader functions correctly when running only the RFID-related code.</p>
</li>
<li>
<p dir="auto"><strong>Issue When Adding Wi-Fi and MQTT:</strong> Including Wi-Fi connection code and MQTT client causes the RFID reader to throw timeout errors and sometimes leads to the device restarting.</p>
</li>
<li>
<p dir="auto"><strong>Error Messages:</strong></p>
<pre><code>Exception during RFID operation: [Errno 116] ETIMEDOUT
Traceback (most recent call last):
  File "&lt;stdin&gt;", 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
</code></pre>
</li>
</ul>
<p dir="auto"><strong>What I'm Doing:</strong></p>
<ul>
<li><strong>RFID Reader:</strong> Reading RFID tags using the built-in RFID unit connected via the M5Stack PORT.B (I2C).</li>
<li><strong>Wi-Fi Connection:</strong> Connecting to a Wi-Fi network using the built-in Wi-Fi module.</li>
<li><strong>MQTT Client:</strong> Publishing and subscribing to topics using the <code>umqtt.simple</code> library.</li>
<li><strong>Asynchronous Tasks:</strong> Using <code>uasyncio</code> to handle asynchronous operations, including reading RFID tags and maintaining MQTT connections.</li>
</ul>
<p dir="auto"><strong>Code Snippets:</strong></p>
<p dir="auto">Here are the relevant parts of my code:</p>
<ul>
<li>
<p dir="auto"><strong>RFID Reading Loop:</strong></p>
<pre><code class="language-python">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)
</code></pre>
</li>
<li>
<p dir="auto"><strong>Wi-Fi Connection Function:</strong></p>
<pre><code class="language-python">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')
</code></pre>
</li>
<li>
<p dir="auto"><strong>MQTT Connection Function:</strong></p>
<pre><code class="language-python">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')
</code></pre>
</li>
<li>
<p dir="auto"><strong>Initialization Function:</strong></p>
<pre><code class="language-python">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()
</code></pre>
</li>
</ul>
<p dir="auto"><strong>Observations:</strong></p>
<ul>
<li><strong>Possible Resource Conflict:</strong> I suspect there might be a resource conflict between the RFID reader and the Wi-Fi module when both are active.</li>
<li><strong>Task Scheduling Issues:</strong> The asynchronous tasks for Wi-Fi, MQTT, and RFID might be interfering with each other, causing timeouts in RFID operations.</li>
<li><strong>RFID Timeout Only When Wi-Fi and MQTT Are Active:</strong> If I remove the Wi-Fi and MQTT code, the RFID reader works without any issues.</li>
</ul>
<p dir="auto"><strong>What I've Tried:</strong></p>
<ol>
<li>
<p dir="auto"><strong>Using Asyncio Locks:</strong> Implemented <code>asyncio.Lock()</code> around hardware access code to prevent resource conflicts.</p>
<pre><code class="language-python"># 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
</code></pre>
</li>
<li>
<p dir="auto"><strong>Adjusting Task Frequencies:</strong> Increased sleep durations in non-critical tasks to reduce CPU load and potential interference.</p>
</li>
<li>
<p dir="auto"><strong>Ensuring Proper Initialization:</strong> Made sure all variables and hardware components are properly initialized and declared as global where necessary.</p>
</li>
<li>
<p dir="auto"><strong>Simplifying the Code:</strong> Tested the code by removing the Wi-Fi signal strength monitoring task, but the issue still persists when MQTT is active.</p>
</li>
</ol>
<p dir="auto"><strong>Questions:</strong></p>
<ol>
<li>
<p dir="auto"><strong>Resource Contention:</strong> Could the RFID reader and Wi-Fi module be conflicting due to shared hardware resources? If so, how can I effectively manage this conflict?</p>
</li>
<li>
<p dir="auto"><strong>Task Synchronization:</strong> Is there a recommended way to synchronize tasks involving RFID, Wi-Fi, and MQTT to prevent interference?</p>
</li>
<li>
<p dir="auto"><strong>Hardware Limitations:</strong> Are there known limitations with the M5Stack Core2 when using RFID and Wi-Fi/MQTT simultaneously?</p>
</li>
<li>
<p dir="auto"><strong>Alternative Approaches:</strong> Suggest some other methods to handle such scnarios</p>
</li>
</ol>
<p dir="auto"><strong>Additional Information:</strong></p>
<ul>
<li><strong>Device:</strong> M5Stack Core2</li>
<li><strong>Programming Language:</strong> MicroPython</li>
<li><strong>RFID Reader Connection:</strong> Built-in RFID unit connected via I2C (PORT.B)</li>
<li><strong>Wi-Fi Module:</strong> Built-in Wi-Fi module</li>
<li><strong>MQTT Library:</strong> Using <code>umqtt.simple</code></li>
<li><strong>Asyncio Version:</strong> Using <code>uasyncio</code> for asynchronous operations</li>
</ul>
<p dir="auto"><strong>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.</strong></p>
<hr />
<p dir="auto">Thank you!</p>
]]></description><link>https://community.m5stack.com/topic/6858/help-needed-rfid-reader-timeout-when-adding-wi-fi-and-mqtt-code-on-m5stack-with-micropython</link><generator>RSS for Node</generator><lastBuildDate>Wed, 29 Apr 2026 04:48:23 GMT</lastBuildDate><atom:link href="https://community.m5stack.com/topic/6858.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 02 Oct 2024 19:02:19 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Help Needed: RFID Reader Timeout When Adding Wi-Fi and MQTT Code on M5Stack with MicroPython on Thu, 03 Oct 2024 10:34:00 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/felmue" aria-label="Profile: felmue">@<bdi>felmue</bdi></a></p>
<p dir="auto">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.</p>
]]></description><link>https://community.m5stack.com/post/26597</link><guid isPermaLink="true">https://community.m5stack.com/post/26597</guid><dc:creator><![CDATA[edraak]]></dc:creator><pubDate>Thu, 03 Oct 2024 10:34:00 GMT</pubDate></item><item><title><![CDATA[Reply to Help Needed: RFID Reader Timeout When Adding Wi-Fi and MQTT Code on M5Stack with MicroPython on Thu, 03 Oct 2024 09:39:55 GMT]]></title><description><![CDATA[<p dir="auto">Hello <a class="plugin-mentions-user plugin-mentions-a" href="/user/edraak" aria-label="Profile: edraak">@<bdi>edraak</bdi></a></p>
<p dir="auto">one thing I would change. After <code>card_uid = rfid.read_card_uid()</code> check for read being successful: e.g. <code>if card_uid != None:</code></p>
<p dir="auto">Thanks<br />
Felix</p>
]]></description><link>https://community.m5stack.com/post/26595</link><guid isPermaLink="true">https://community.m5stack.com/post/26595</guid><dc:creator><![CDATA[felmue]]></dc:creator><pubDate>Thu, 03 Oct 2024 09:39:55 GMT</pubDate></item><item><title><![CDATA[Reply to Help Needed: RFID Reader Timeout When Adding Wi-Fi and MQTT Code on M5Stack with MicroPython on Thu, 03 Oct 2024 09:26:51 GMT]]></title><description><![CDATA[<p dir="auto">Hi <a class="plugin-mentions-user plugin-mentions-a" href="/user/edraak" aria-label="Profile: edraak">@<bdi>edraak</bdi></a></p>
<p dir="auto">I am running your code with <code>test.mosquitto.org</code> as MQTT broker; but so far I was not able to break it, e.g. no ETIMEDOUT error so far.</p>
<p dir="auto">Thanks<br />
Felix</p>
]]></description><link>https://community.m5stack.com/post/26594</link><guid isPermaLink="true">https://community.m5stack.com/post/26594</guid><dc:creator><![CDATA[felmue]]></dc:creator><pubDate>Thu, 03 Oct 2024 09:26:51 GMT</pubDate></item><item><title><![CDATA[Reply to Help Needed: RFID Reader Timeout When Adding Wi-Fi and MQTT Code on M5Stack with MicroPython on Thu, 03 Oct 2024 08:08:54 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/felmue" aria-label="Profile: felmue">@<bdi>felmue</bdi></a><br />
Hi, Apologies for confusion I am using m5 dial<br />
and below is the link of my code</p>
<p dir="auto"><a href="https://uiflow2.m5stack.com/?pkey=7b06b0d459f14c8ab16e70d5b073bf76" target="_blank" rel="noopener noreferrer nofollow ugc">https://uiflow2.m5stack.com/?pkey=7b06b0d459f14c8ab16e70d5b073bf76</a></p>
]]></description><link>https://community.m5stack.com/post/26591</link><guid isPermaLink="true">https://community.m5stack.com/post/26591</guid><dc:creator><![CDATA[edraak]]></dc:creator><pubDate>Thu, 03 Oct 2024 08:08:54 GMT</pubDate></item><item><title><![CDATA[Reply to Help Needed: RFID Reader Timeout When Adding Wi-Fi and MQTT Code on M5Stack with MicroPython on Wed, 02 Oct 2024 21:16:43 GMT]]></title><description><![CDATA[<p dir="auto">Hello <a class="plugin-mentions-user plugin-mentions-a" href="/user/edraak" aria-label="Profile: edraak">@<bdi>edraak</bdi></a></p>
<p dir="auto">I am confused about whether you are using an M5Core2 or an M5Dial. Could you please clarify?</p>
<p dir="auto">Also it would help if you could share your complete program in the UIFlow2 Project Zone.</p>
<p dir="auto">Thanks<br />
Felix</p>
]]></description><link>https://community.m5stack.com/post/26585</link><guid isPermaLink="true">https://community.m5stack.com/post/26585</guid><dc:creator><![CDATA[felmue]]></dc:creator><pubDate>Wed, 02 Oct 2024 21:16:43 GMT</pubDate></item></channel></rss>