I’m trying to send data to Ubidots, using Micropython on an ESP32.
I’m using code based on the Ubidots help here - Connect your ESP32 to Ubidots over MQTT using MicroPython | Ubidots Help Center
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 Connect your ESP32 to Ubidots over MQTT using MicroPython | Ubidots Help Center to Ubidots, using Micropython on an ESP32.
I’m using code based on the Ubidots help here - Connect your ESP32 to Ubidots over MQTT using MicroPython | Ubidots Help Center
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()