Skip to content

Commit 6195133

Browse files
author
brentru
committed
fix location for mmqtt breaking
1 parent 839a1a5 commit 6195133

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

examples/mqtt/adafruit_io_location.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Example of tagging data with location values
22
# and sending it to an Adafruit IO feed.
3-
3+
import time
44
import board
55
import busio
66
from digitalio import DigitalInOut
@@ -10,7 +10,7 @@
1010
import neopixel
1111

1212

13-
from adafruit_minimqtt import MQTT
13+
import adafruit_minimqtt as MQTT
1414
from adafruit_io.adafruit_io import IO_MQTT
1515

1616
### WiFi ###
@@ -73,18 +73,19 @@ def message(client, feed_id, payload):
7373
# the new value.
7474
print("Feed {0} received new value: {1}".format(feed_id, payload))
7575

76-
7776
# Connect to WiFi
77+
print("Connecting to WiFi...")
78+
wifi.connect()
79+
print("Connected!")
80+
81+
# Initialize MQTT interface with the esp interface
82+
MQTT.set_socket(socket, esp)
7883
wifi.connect()
7984

8085
# Initialize a new MQTT Client object
81-
mqtt_client = MQTT(
82-
socket=socket,
83-
broker="io.adafruit.com",
84-
username=secrets["aio_user"],
85-
password=secrets["aio_key"],
86-
network_manager=wifi
87-
)
86+
mqtt_client = MQTT.MQTT(broker="https://io.adafruit.com",
87+
username=secrets["aio_user"],
88+
password=secrets["aio_key"])
8889

8990
# Initialize an Adafruit IO MQTT Client
9091
io = IO_MQTT(mqtt_client)
@@ -110,5 +111,15 @@ def message(client, feed_id, payload):
110111
print("Data sent!")
111112

112113

113-
# Listen forever...
114-
io.loop_blocking()
114+
# Start a blocking message loop...
115+
# NOTE: NO code below this loop will execute
116+
# NOTE: Network reconnection is handled within this loop
117+
while True:
118+
try:
119+
io.loop()
120+
except (ValueError, RuntimeError) as e:
121+
print("Failed to get data, retrying\n", e)
122+
wifi.reset()
123+
io.reconnect()
124+
continue
125+
time.sleep(1)

0 commit comments

Comments
 (0)