Skip to content

Commit 670c575

Browse files
committed
Updated to use is_connected
1 parent f11eddf commit 670c575

File tree

1 file changed

+7
-11
lines changed
  • CircuitPython_Templates/adafruit_io_cpu_temp_neopixel_color

1 file changed

+7
-11
lines changed

CircuitPython_Templates/adafruit_io_cpu_temp_neopixel_color/code.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
# SPDX-FileCopyrightText: 2022 Kattni Rembor for Adafruit Industries
33
# SPDX-License-Identifier: MIT
44
import time
5+
import microcontroller
56
import ssl
67
from random import randint
7-
import microcontroller
88
import socketpool
99
import wifi
1010
import board
@@ -71,21 +71,17 @@ def message(client, feed_id, payload): # pylint: disable=unused-argument
7171
io.on_connect = connected
7272
io.on_message = message
7373

74-
# Connect to Adafruit IO
75-
try:
76-
io.connect()
77-
# connect() fails with an internal error type, so this except is broad.
78-
except Exception as e: # pylint: disable=broad-except
79-
print("Failed to connect to Adafruit IO. Error:", e, "\nBoard will hard reset in 30 seconds.")
80-
time.sleep(30)
81-
microcontroller.reset()
82-
8374
timestamp = 0
8475
while True:
8576
try:
77+
# If Adafruit IO is not connected...
78+
if not io.is_connected:
79+
# Connect the client to the MQTT broker.
80+
print("Connecting to Adafruit IO...")
81+
io.connect()
82+
8683
# Explicitly pump the message loop.
8784
io.loop()
88-
8985
# Obtain the "random" value, print it and publish it to Adafruit IO every 10 seconds.
9086
if (time.monotonic() - timestamp) >= 10:
9187
random_number = "{}".format(randint(0, 255))

0 commit comments

Comments
 (0)