<?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[Handling WLAN and MQTT disconnects]]></title><description><![CDATA[<p dir="auto">Hello.  Before I start coding it up I was wondering if anyone has a good base model for detecting WiFi and mqtt failures and reconnecting?</p>
<p dir="auto">Thanks<br />
Chris</p>
]]></description><link>https://forum.pycom.io/topic/5252/handling-wlan-and-mqtt-disconnects</link><generator>RSS for Node</generator><lastBuildDate>Wed, 15 Jul 2026 06:43:12 GMT</lastBuildDate><atom:link href="https://forum.pycom.io/topic/5252.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 04 Oct 2019 00:24:40 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Handling WLAN and MQTT disconnects on Fri, 04 Oct 2019 00:24:40 GMT]]></title><description><![CDATA[<p dir="auto">Hello.  Before I start coding it up I was wondering if anyone has a good base model for detecting WiFi and mqtt failures and reconnecting?</p>
<p dir="auto">Thanks<br />
Chris</p>
]]></description><link>https://forum.pycom.io/post/29663</link><guid isPermaLink="true">https://forum.pycom.io/post/29663</guid><dc:creator><![CDATA[cmisztur]]></dc:creator><pubDate>Fri, 04 Oct 2019 00:24:40 GMT</pubDate></item><item><title><![CDATA[Reply to Handling WLAN and MQTT disconnects on Fri, 25 Oct 2019 13:26:24 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://forum.pycom.io/uid/2906">@cmisztur</a> To check if you are connected to wifi you can use the isconnected() as show below</p>
<pre><code>from network import WLAN
import time


class WifiMonitor:

    def __init__(self):
        self.conn_timeout = 20
        self.wlan = WLAN(mode=WLAN.STA)

    def _connect(self, ssid, pswd):
        if not self.wlan.isconnected():
            print(&quot;Connecting to Wifi SSID: {} PASS: {}&quot;.format(ssid, pswd))
            self.wlan.connect(ssid, auth=(WLAN.WPA2, pswd))

            count_10ms = 0
            while count_10ms &lt;= self.conn_timeout * 100 and not self.wlan.isconnected():
                count_10ms += 1
                time.sleep(0.01)

        if self.wlan.isconnected():
            print(&quot;WiFi connected: {}&quot;.format(self.wlan.ifconfig()))

    def connect_loop(self, ssid, pswd):
        while True:
            try:
                self._connect(ssid, pswd)
            except Exception as e:
                print(&quot;WIFI connection error: {}&quot;.format(e))
                pass
            time.sleep(10)

WIFI_SSID = 'my ssid'
WIFI_PASS = 'my pass'

wifi = WifiMonitor()
wifi.connect_loop(WIFI_SSID, WIFI_PASS)
</code></pre>
<p dir="auto">For MQTT you can send a ping to the MQTT server</p>
]]></description><link>https://forum.pycom.io/post/30030</link><guid isPermaLink="true">https://forum.pycom.io/post/30030</guid><dc:creator><![CDATA[oligauc]]></dc:creator><pubDate>Fri, 25 Oct 2019 13:26:24 GMT</pubDate></item></channel></rss>