← index #386Issue #10357
Related · medium · value 1.257
QUERY · ISSUE

MQTT connection and client.check_msg() problem

openby rodrigo1120opened 2020-05-12updated 2024-08-25

I'm using Adafruit IO switches to turn my nodeMCU lights ON and OFF. The following code works but for a limited period of time. 5 minutes after the connection into Adafruit IO it seems the client tries to reconnect and if this happens I lose my switch funcionality at my adafruit dashboard. Either if I try to turn on or off the LED on my dashboard, the nodeMCU does not responds accordingly. Here is the code used (I'm using micropython, pycharm and a Lolin v3 nodemcu)

from machine import Pin
import sys
import os
import network
from umqtt.robust import MQTTClient

random_num = int.from_bytes(os.urandom(3), 'little')
mqtt_client_id = bytes('client_' + str(random_num), 'utf-8')

ADAFRUIT_LINK = b'io.adafruit.com'
ADAFRUIT_USERNAME = b'rodrigo1120'
ADAFRUIT_KEY = b'AAAAAAAAAAAAAAAAAAAAAAAAAAAA'
ADAFRUIT_FEEDNAME1 = b'gpio16'
ADAFRUIT_FEEDNAME2 = b'gpio2'

ssid = "WIFIUSERNAME"
password = "WIFIPASSWORD"

client = MQTTClient(client_id= mqtt_client_id,
                    server= ADAFRUIT_LINK,
                    user=ADAFRUIT_USERNAME,
                    password=ADAFRUIT_KEY)

mqtt_feedname1 = bytes('{:s}/feeds/{:s}'.format(ADAFRUIT_USERNAME, ADAFRUIT_FEEDNAME1), 'utf-8')
mqtt_feedname2 = bytes('{:s}/feeds/{:s}'.format(ADAFRUIT_USERNAME, ADAFRUIT_FEEDNAME2), 'utf-8')


def connect_to_ap():
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    if not wlan.isconnected():
        print('Connecting to {}'.format(ssid))
        wlan.connect(ssid, password)
        while not wlan.isconnected():
            pass
    print('Credentials: ', wlan.ifconfig())
    try:
        client.connect()
    except Exception as e:
        print('Could not connect to MQTT Server {}{}'.format(type(e).__name__, e))


def callback_routine(feed, msg):
    print('Received Data from: feed={}, msg={}'.format(feed, msg))
    if ADAFRUIT_FEEDNAME1 in feed:
        action = str(msg, 'utf-8')
        if action == 'ON':
            pin16.value(0)
        else:
            pin16.value(1)
        print('Action: {}'.format(action))
    if ADAFRUIT_FEEDNAME2 in feed:
        action = str(msg, 'utf-8')
        if action == 'ON':
            pin2.value(0)
        else:
            pin2.value(1)
        print('Action: {}'.format(action))


pin16 = Pin(16, Pin.OUT)
pin2 = Pin(2, Pin.OUT)
pin16.value(1)
pin2.value(1)


def main():
    connect_to_ap()
    while True:
        client.set_callback(callback_routine)
        client.subscribe(mqtt_feedname1)
        client.subscribe(mqtt_feedname2)
        try:
            client.wait_msg()
        except KeyboardInterrupt:
            print('Ctrl+C pressed, exiting')
            client.disconnect()
            sys.exit()


if __name__ == '__main__':
    main()

Here is a picture of my serial monitor with this problem: (I took this picture 15 minutes after I stopped using the adafruit IO switches)
problemmp023

7 comments
EddieParis · 2020-05-15

Try to pull the change referenced here in PR #385 I had same issue, this is the solution.

rodrigo1120 · 2020-05-15

Try to pull the change referenced here in PR #385 I had same issue, this is the solution.

In order for this to work, I just need to upload this robust.py inside my nodemcu and use import robust.py?

EddieParis · 2020-05-15

Yes, Ugly but easy way to do that is to copy/paste the file from my fork, you can also clone my fork or get the patch and apply it on you clone.

rodrigo1120 · 2020-05-16

Yes, Ugly but easy way to do that is to copy/paste the file from my fork, you can also clone my fork or get the patch and apply it on you clone.

I tried to copy/paste your code, but unfortunately I got this error:
problem github 001

in pycharm they say I can install package simple, but if I do this I got an install package error.
I'm sorry, I guess I'm struggling with a simple relative import problem

rodrigo1120 · 2020-05-16

I did the following changes in the code, but I got no fix for that initial problem:

import utime
from umqtt.robust import simple
# micropython-lib/umqtt.robust/umqtt/robust.py


class MQTTClient(simple.MQTTClient):

    DELAY = 2
    DEBUG = False

    def delay(self, i):
...

(Inside robust.py)

from machine import Pin
import sys
import os
import network
# from umqtt.robust import MQTTClient
from robust import MQTTClient


random_num = int.from_bytes(os.urandom(3), 'little')
mqtt_client_id = bytes('client_' + str(random_num), 'utf-8')

ADAFRUIT_LINK = b'io.adafruit.com'
ADAFRUIT_USERNAME = b'rodrigo1120'
ADAFRUIT_KEY = b'AAAAAAAAAAAAAAAAAAAAAAAAAA'
ADAFRUIT_FEEDNAME1 = b'gpio16'
ADAFRUIT_FEEDNAME2 = b'gpio2'

ssid = "WIFI-USER"
password = "WIFI-PASSW"

client = MQTTClient(client_id= mqtt_client_id,
                    server= ADAFRUIT_LINK,
                    user=ADAFRUIT_USERNAME,
                    password=ADAFRUIT_KEY,
                    ssl=False)
...

(Inside main.py)

EddieParis · 2020-05-17

Sorry, I was mislead by the title of your PR. You don't use check_msg(), but wait_msg(). My patch is specifically for check_msg() that can get stuck, so you may have a different issue.

It might be the well known loosing subscriptions issue. If the robust layer reconnects, you loose all subscriptions. Try connecting with a non clean session so that you broker keep track of them.

I recommend reading this chapter :
https://www.hivemq.com/blog/mqtt-essentials-part-7-persistent-session-queuing-messages/

And the whole essentials articles from this website are worthy too.

jonnor · 2024-08-25

Is this still an issue on the latest MicroPython and mqtt.simple version?

CANDIDATE · ISSUE

facing connecting ESP32 with my adafruit IO using umqtt

closedby vgtronics23012004opened 2022-12-28updated 2022-12-28
bug

i am using micropython for ESP32
using umqtt for subscribing a feed on Adafruit IO
but i am getting an error :

this is my code :

`from machine import Pin
import network
import time
from umqtt.robust import MQTTClient
import sys

led=Pin(2,Pin.OUT) # Onboard LED on Pin 2 of ESP32

WIFI_SSID = 'DLINK214_1'
WIFI_PASSWORD = 'vishal@270898'

mqtt_client_id = bytes('client_'+'1233', 'utf-8') # Just a random client ID

ADAFRUIT_IO_URL = 'io.adafruit.com'
ADAFRUIT_USERNAME = 'vgtronics2301xxxx'
ADAFRUIT_IO_KEY = 'aio_YLRt18bsCnXXXXXXXXXXXXXXX' # hide for security reason

TOGGLE_FEED_ID = 'homeautomation'

def connect_wifi():
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.disconnect()
wifi.connect(WIFI_SSID,WIFI_PASSWORD)
if not wifi.isconnected():
print('connecting..')
timeout = 0
while (not wifi.isconnected() and timeout < 5):
print(5 - timeout)
timeout = timeout + 1
time.sleep(1)
if(wifi.isconnected()):
print('connected')
else:
print('not connected')
sys.exit()

connect_wifi() # Connecting to WiFi Router

client = MQTTClient(client_id=mqtt_client_id,
server=ADAFRUIT_IO_URL,
user=ADAFRUIT_USERNAME,
password=ADAFRUIT_IO_KEY,
ssl=False)

def cb(topic, msg): # Callback function
print('Received Data: Topic = {}, Msg = {}'.format(topic, msg))
recieved_data = str(msg,'utf-8') # Recieving Data
if recieved_data=="1":
led.value(1)
if recieved_data=="0":
led.value(0)

client.connect()
toggle_feed = bytes('{:s}/feeds/{:s}'.format(ADAFRUIT_USERNAME, TOGGLE_FEED_ID), 'utf-8') # format - techiesms/feeds/relay1
client.set_callback(cb) # Callback function
client.subscribe(toggle_feed) # Subscribing to particular topic

while True:
try:
client.check_msg() # non blocking function
except :
client.disconnect()
sys.exit()`

And my error is :

sometimes this -

>>> %Run -c $EDITOR_CONTENT connected Traceback (most recent call last): File "<stdin>", line 63, in <module> File "umqtt/simple.py", line 108, in connect IndexError: bytes index out of range

and sometimes -

>>> %Run -c $EDITOR_CONTENT connected Traceback (most recent call last): File "<stdin>", line 54, in <module> File "umqtt/simple.py", line 68, in connect OSError: [Errno 118] EHOSTUNREACH

when i am runnign this same code with nodemcu it is working fine
but showing error with esp32
dont know why...
😞

https://github.com/micropython/micropython-lib/blob/6d2c0019e7d109e8ca18d9eab7133f2a49f2144f/umqtt.simple/umqtt/simple.py#L108

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied