Ubidots Community

ESP32 + Micropython - problem with mqtt client connect after 3-4 successful publish requests

I’m trying to send data to Ubidots, using Micropython on an ESP32.

I’m using code based on the Ubidots help here - https://help.ubidots.com/en/articles/1956065-connect-your-esp32-to-ubidots-over-mqtt-using-micropython

I find I’m able to successfully send a few values (3-4) to Ubidots, but then my program always stops at the client.connect() with “OS error -202”.I’m trying to send https://help.ubidots.com/en/articles/1956065-connect-your-esp32-to-ubidots-over-mqtt-using-micropythondata to Ubidots, using Micropython on an ESP32.

I’m using code based on the Ubidots help here - https://help.ubidots.com/en/articles/1956065-connect-your-esp32-to-ubidots-over-mqtt-using-micropython

I find I’m able to successfully send a few values (3-4) to Ubidots, but then my program always stops at the client.connect() with “OS error -202”.

I’ve tried various things such as using “keep alive=120”, and disconnecting the client each time after publishing. But still the same error message.

Can you help please?

I’ve tried various things such as using “keep alive=120”, and disconnecting the client each time after publishing. But still the same error message after a few successful publish requests.

Can anyone help please?

ERROR MESSAGE:

/home/pi> repl
Entering REPL. Use Control-X to exit.

MicroPython v1.13 on 2020-09-02; ESP32 module with ESP32
Type “help()” for more information.

import main2
15.4 63.9
{“ambient-temperature”:15.4}
15.4 64.20001
{“ambient-temperature”:15.4}
15.5 64.1
{“ambient-temperature”:15.5}
OS error: -202

CODE:

import network

import machine as m

import socket

import time

from umqtt.robust import MQTTClient

import dht

d = dht.DHT22(m.Pin(4))

sta_if = network.WLAN(network.STA_IF); sta_if.active(True)

sta_if.scan() # Scan for available access points

sta_if.connect("xxx", "xxx") # Connect to an AP

time.sleep(3)

ubidotsToken = "xxx" 

clientID = 'ESP32a' # "RANDOM-ALPHA-NUMERIC-NAME_OR_IMEI DEVICE ID"

#client = MQTTClient(clientID, "industrial.api.ubidots.com", 1883, user = ubidotsToken, password = ubidotsToken,keepalive=120)
client = MQTTClient(clientID, "industrial.api.ubidots.com", 1883, user = ubidotsToken, password = ubidotsToken)


def checkwifi():

    while not sta_if.isconnected():

        time.sleep_ms(500)

        print(".")

        sta_if.connect()

        time.sleep(3)

        text = sta_if.isconnected()

        print('connected',text)

def publish():
    
    while True:

        checkwifi()
        
        try:
            client.connect()
        except OSError as err:
            print("OS error: {0}".format(err))
        else:
            d.measure()
            var = d.temperature()
            hum = d.humidity()
            print(var, hum)

            msg = '{"ambient-temperature":%s}' %var

            print(msg)

            client.publish(b"/v1.6/devices/dht22-1", msg)

#        client.disconnect()

        time.sleep(60)

publish()

Apologies for the garbled style of the post – I’ve tried to correct it, but, whatever edits I try to save give a message “forbidden 403”.

Greetings, a 403 error code means that you are using a non-valid token, please check that you are getting a valid token key from your account. For more information, please refer to our docs.

All the best

Thanks, but the 403 code is coming from the software I’m using to log the problem in this forum. That was the message I got when I tried to correct the garbled paragraphs in my original Ubidots Community post above.

The error I get from the ESP32 / Micropython is “OS error -202”.

I believe the token is correct, as I’m able to successfully log data values 3+ times in Ubidots before it invariably fails with the -202 error.

Greetings, OS-errors are related to your device rather than to the example. I would advise you to use the official micropython umqtt library example to discard any issues with your device, you would just need to modify the broker URL and the password using your token.

All the best