By Brent Rubell
https://learn.adafruit.com/quickstart-r ... dafruit-io
I have set up the above project and am having issues with the code.py on the above linked page.
I have attached a copy of the slightly modified code.py that I used. I added 2 print statements to see where the code is working and not working.
The code.py makes it to the print Tom Henry.1 as you can see below.
The code problem area is under the While True section just after printing Tom Henry.1.
code.py output:
Connecting to WiFi...
Connected!
Connecting to Adafruit IO...
Connected to Adafruit IO!
Subscribed to Tom_Henry/f/led with QOS level 0
Tom Henry.1
Traceback (most recent call last):
File "code.py", line 112, in <module>
File "code.py", line 106, in <module>
File "adafruit_io/adafruit_io.py", line 215, in loop
File "adafruit_minimqtt/adafruit_minimqtt.py", line 813, in loop
File "adafruit_minimqtt/adafruit_minimqtt.py", line 831, in _wait_for_msg
File "adafruit_minimqtt/adafruit_minimqtt.py", line 826, in _wait_for_msg
File "adafruit_minimqtt/adafruit_minimqtt.py", line 923, in _sock_exact_recv
MMQTTException: Unable to receive 1 bytes within 60 seconds.
Code done running.
I have also attached a screenshot of the CIRCUITPY drive showing the lib.
I would most appreciate any suggestions on what you think might need to be fixed or changed
.
Code: Select all
# SPDX-FileCopyrightText: Brent Rubell for Adafruit Industries
# SPDX-License-Identifier: MIT
import time
from microcontroller import cpu
import board
import busio
from digitalio import DigitalInOut
from adafruit_esp32spi import adafruit_esp32spi
from adafruit_esp32spi import adafruit_esp32spi_wifimanager
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
import adafruit_minimqtt.adafruit_minimqtt as MQTT
from adafruit_io.adafruit_io import IO_MQTT
### WiFi ###
# Get wifi details and more from a secrets.py file
try:
from secrets import secrets
except ImportError:
print("WiFi secrets are kept in secrets.py, please add them there!")
raise
# Raspberry Pi RP2040
esp32_cs = DigitalInOut(board.GP13)
esp32_ready = DigitalInOut(board.GP14)
esp32_reset = DigitalInOut(board.GP15)
spi = busio.SPI(board.GP10, board.GP11, board.GP12)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets)
# Configure the RP2040 Pico LED Pin as an output
led_pin = DigitalInOut(board.LED)
led_pin.switch_to_output()
# Define callback functions which will be called when certain events happen.
# pylint: disable=unused-argument
def connected(client):
# Connected function will be called when the client is connected to Adafruit IO.
print("Connected to Adafruit IO! ")
def subscribe(client, userdata, topic, granted_qos):
# This method is called when the client subscribes to a new feed.
print("Subscribed to {0} with QOS level {1}".format(topic, granted_qos))
# pylint: disable=unused-argument
def disconnected(client):
# Disconnected function will be called when the client disconnects.
print("Disconnected from Adafruit IO!")
def on_led_msg(client, topic, message):
# Method called whenever user/feeds/led has a new value
print("New message on topic {0}: {1} ".format(topic, message))
if message == "ON":
led_pin.value = True
elif message == "OFF":
led_pin.value = False
else:
print("Unexpected message on LED feed.")
# Connect to WiFi
print("Connecting to WiFi...")
wifi.connect()
print("Connected!")
# Initialize MQTT interface with the esp interface
MQTT.set_socket(socket, esp)
# Initialize a new MQTT Client object
mqtt_client = MQTT.MQTT(
broker="io.adafruit.com",
username="Tom_Henry",
password="aio_jfOu45Ii5G1NVxQPMg2M5Y4p2YMv",
)
# Initialize an Adafruit IO MQTT Client
io = IO_MQTT(mqtt_client)
# Connect the callback methods defined above to Adafruit IO
io.on_connect = connected
io.on_disconnect = disconnected
io.on_subscribe = subscribe
# Set up a callback for the led feed
io.add_feed_callback("led", on_led_msg)
# Connect to Adafruit IO
print("Connecting to Adafruit IO...")
io.connect()
# Subscribe to all messages on the led feed
io.subscribe("led")
print("Tom Henry.1")
prv_refresh_time = 0.0
while True:
# Poll for incoming messages
try:
io.loop()
print("Tom Henry.2")
except (ValueError, RuntimeError) as e:
print("Failed to get data, retrying\n", e)
wifi.reset()
io.reconnect()
continue
# Send a new temperature reading to IO every 30 seconds
if (time.monotonic() - prv_refresh_time) > 30:
# take the cpu's temperature
cpu_temp = cpu.temperature
print("cpu.temperature")
# truncate to two decimal points
cpu_temp = str(cpu_temp)[:5]
print("CPU temperature is %s degrees C" % cpu_temp)
# publish it to io
print("Publishing %s to temperature feed..." % cpu_temp)
io.publish("temperature", cpu_temp)
print("Published!")
prv_refresh_time = time.monotonic()