Now, I am dealing with the MQTT which publishes data of “ON” or “OFF” to Adafruit.io feeds and got an error.
What I expected from the program, the on/off buttons will; switches every second on the adafruit.io dashboard.
What I saw on the other forum provided ways to solve the problem:
- unassigned UART0 from the REPL
- blocking stdin to read the line which related UART.
This is my code:
from MQTT import MQTTClient from machine import Pin import machine import time import Main2 from machine import UART import os uart = UART(0, 115200) os.dupterm(uart) def sub_cb(topic, msg): print(msg) wlan = Main2.get_connection() if wlan is None: print("Could not initialize the network connection.") while True: pass client = MQTTClient("Id(i think this random)", "io.adafruit.com",user="Myaccountid", password= "AIO-key", port=1881) client.set_callback(sub_cb) client.connect() client.subscribe(topic="Myaccountid∕feeds∕afeed") while True: print("Sending ON") client.publish(topic="Myaccountid∕feeds∕afeed", msg="ON") time.sleep(2) print("Sending OFF") client.publish(topic="Myaccountid∕feeds∕afeed", msg="OFF") time.sleep(2)
Traceback (most recent call last): File "<stdin>", line 14, in <module> ValueError: UART(0) is disabled (dedicated to REPL) These are libraries link of MQTT,Main2(for connecting WIFI).
- Main2: github.com/tayfunulu/WiFiManager/blob/master/wifimgr.py
- MQTT: github.com/pycom/pycom-libraries/blob/master/examples/mqtt/mqtt.py
Why do you need to import UART? If you comment that line what happens?
from machine import UART import os uart = UART(0, 115200) os.dupterm(uart)
I want to import UART for setting the esp32 in UART(0) with a baudrate at 115200 to access REPL
to create a serial communication way (i think it is a only way, is it ?)for the esp32 to communicate with adafruit.io.
I’m sorry for taking so long to get back to you.
I’ve never tried adafruit io with micropython.
But I’ve found this example: https://github.com/miketeachman/micropython-adafruit-mqtt-esp8266/blob/master/publishAdafruit.py
And it doesn’t use uart. It is compatible with the ESP32 and ESP8266.