Skip to content

Commit 839a1a5

Browse files
author
brentru
committed
add groups, simpletest, fix regular example
1 parent 277e3b3 commit 839a1a5

File tree

3 files changed

+32
-23
lines changed

3 files changed

+32
-23
lines changed

adafruit_io/adafruit_io.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,16 @@ def __enter__(self):
9999
def __exit__(self, exception_type, exception_value, traceback):
100100
self.disconnect()
101101

102+
def reconnect(self):
103+
"""Attempts to reconnect to the Adafruit IO MQTT Broker.
104+
105+
"""
106+
try:
107+
self._client.reconnect()
108+
except:
109+
raise AdafruitIO_MQTTError("Unable to reconnect to Adafruit IO.")
110+
111+
102112
def connect(self):
103113
"""Connects to the Adafruit IO MQTT Broker.
104114
Must be called before any other API methods are called.

examples/mqtt/adafruit_io_groups.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from digitalio import DigitalInOut
1212
import neopixel
1313
from adafruit_io.adafruit_io import IO_MQTT
14-
from adafruit_minimqtt import MQTT
14+
import adafruit_minimqtt as MQTT
1515

1616
### WiFi ###
1717

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

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

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

9091
# Initialize an Adafruit IO MQTT Client
9192
io = IO_MQTT(mqtt_client)
@@ -103,6 +104,7 @@ def message(client, feed_id, payload):
103104
humid_feed = "weatherstation.humidity"
104105

105106
# Connect to Adafruit IO
107+
print("Connecting to Adafruit IO...")
106108
io.connect()
107109

108110
print("Publishing new messages to group feeds every 5 seconds...")

examples/mqtt/adafruit_io_simpletest.py

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from digitalio import DigitalInOut
1717
import neopixel
1818
from adafruit_io.adafruit_io import IO_MQTT
19-
from adafruit_minimqtt import MQTT
19+
import adafruit_minimqtt as MQTT
2020

2121
### WiFi ###
2222

@@ -85,18 +85,19 @@ def message(client, feed_id, payload):
8585
# the new value.
8686
print("Feed {0} received new value: {1}".format(feed_id, payload))
8787

88-
8988
# Connect to WiFi
89+
print("Connecting to WiFi...")
90+
wifi.connect()
91+
print("Connected!")
92+
93+
# Initialize MQTT interface with the esp interface
94+
MQTT.set_socket(socket, esp)
9095
wifi.connect()
9196

9297
# Initialize a new MQTT Client object
93-
mqtt_client = MQTT(
94-
socket=socket,
95-
broker="io.adafruit.com",
96-
username=secrets["aio_user"],
97-
password=secrets["aio_key"],
98-
network_manager=wifi
99-
)
98+
mqtt_client = MQTT.MQTT(broker="https://io.adafruit.com",
99+
username=secrets["aio_user"],
100+
password=secrets["aio_key"])
100101

101102
# Initialize an Adafruit IO MQTT Client
102103
io = IO_MQTT(mqtt_client)
@@ -109,6 +110,7 @@ def message(client, feed_id, payload):
109110
io.on_message = message
110111

111112
# Connect to Adafruit IO
113+
print("Connecting to Adafruit IO...")
112114
io.connect()
113115

114116
# Below is an example of manually publishing a new value to Adafruit IO.
@@ -123,8 +125,3 @@ def message(client, feed_id, payload):
123125
print("Publishing {0} to DemoFeed.".format(value))
124126
io.publish("DemoFeed", value)
125127
last = time.monotonic()
126-
127-
128-
# You can also call loop_blocking if you only want to receive values.
129-
# NOTE: If uncommented, no code below this line will run.
130-
# io.loop_blocking()

0 commit comments

Comments
 (0)