Black Lives Matter - Action and Equality.
0

MQTT connection and client.check_msg() problem
Moderators: adafruit_support_bill, adafruit

Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
Please be positive and constructive with your questions and comments.

MQTT connection and client.check_msg() problem

by rodrigo1120 on Thu May 14, 2020 2:36 pm

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)


Code: Select all | TOGGLE FULL SIZE
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)
Image

rodrigo1120
 
Posts: 4
Joined: Thu May 14, 2020 2:15 pm

Re: MQTT connection and client.check_msg() problem

by brubell on Fri May 15, 2020 10:08 am

Does the monitor page: io.adafruit.com/monitor, show a disconnection after five minutes?

brubell
 
Posts: 891
Joined: Fri Jul 17, 2015 10:33 pm

Re: MQTT connection and client.check_msg() problem

by rodrigo1120 on Sat May 16, 2020 10:09 pm

brubell wrote:Does the monitor page: io.adafruit.com/monitor, show a disconnection after five minutes?


Yes! after using for some time the switch, this happens: (client_16645810)

Firstly I connected at 22:55:14, then it got disconnected out of nowhere, In the picture you can see i'm using the switches until it stopped working
Attachments
adafruit problemmmmm.png
adafruit problemmmmm.png (90.36 KiB) Viewed 35 times

rodrigo1120
 
Posts: 4
Joined: Thu May 14, 2020 2:15 pm

Re: MQTT connection and client.check_msg() problem

by brubell on Mon May 18, 2020 10:18 am

After you initialize your client, could you enable uMQTT's debugging messages:

Code: Select all | TOGGLE FULL SIZE
client = MQTTClient(client_id= mqtt_client_id,
                    server= ADAFRUIT_LINK,
                    user=ADAFRUIT_USERNAME,
                    password=ADAFRUIT_KEY)
# Print diagnostic messages when retries/reconnects happens
c.DEBUG = True


and paste the output here?

brubell
 
Posts: 891
Joined: Fri Jul 17, 2015 10:33 pm

Re: MQTT connection and client.check_msg() problem

by rodrigo1120 on Mon May 18, 2020 2:38 pm

Here is the debug as needed
Attachments
adafruit com4 error.png
adafruit com4 error.png (24.66 KiB) Viewed 29 times

rodrigo1120
 
Posts: 4
Joined: Thu May 14, 2020 2:15 pm

Re: MQTT connection and client.check_msg() problem

by rodrigo1120 on Tue May 26, 2020 1:24 pm

anyone got the same issue?

rodrigo1120
 
Posts: 4
Joined: Thu May 14, 2020 2:15 pm

Please be positive and constructive with your questions and comments.