Updated to use is_connected

This commit is contained in:
Kattni Rembor 2022-03-24 15:57:30 -04:00
parent f11eddf545
commit 670c575046

View file

@ -2,9 +2,9 @@
# SPDX-FileCopyrightText: 2022 Kattni Rembor for Adafruit Industries # SPDX-FileCopyrightText: 2022 Kattni Rembor for Adafruit Industries
# SPDX-License-Identifier: MIT # SPDX-License-Identifier: MIT
import time import time
import microcontroller
import ssl import ssl
from random import randint from random import randint
import microcontroller
import socketpool import socketpool
import wifi import wifi
import board import board
@ -71,21 +71,17 @@ io = IO_MQTT(mqtt_client)
io.on_connect = connected io.on_connect = connected
io.on_message = message io.on_message = message
# Connect to Adafruit IO
try:
io.connect()
# connect() fails with an internal error type, so this except is broad.
except Exception as e: # pylint: disable=broad-except
print("Failed to connect to Adafruit IO. Error:", e, "\nBoard will hard reset in 30 seconds.")
time.sleep(30)
microcontroller.reset()
timestamp = 0 timestamp = 0
while True: while True:
try: try:
# If Adafruit IO is not connected...
if not io.is_connected:
# Connect the client to the MQTT broker.
print("Connecting to Adafruit IO...")
io.connect()
# Explicitly pump the message loop. # Explicitly pump the message loop.
io.loop() io.loop()
# Obtain the "random" value, print it and publish it to Adafruit IO every 10 seconds. # Obtain the "random" value, print it and publish it to Adafruit IO every 10 seconds.
if (time.monotonic() - timestamp) >= 10: if (time.monotonic() - timestamp) >= 10:
random_number = "{}".format(randint(0, 255)) random_number = "{}".format(randint(0, 255))