facing connecting ESP32 with my adafruit IO using umqtt #10360
Unanswered
vgtronics23012004
asked this question in
ESP32
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
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
Beta Was this translation helpful? Give feedback.
All reactions