diff --git a/adafruit_io/adafruit_io.py b/adafruit_io/adafruit_io.py index 07f833e..fff5c23 100755 --- a/adafruit_io/adafruit_io.py +++ b/adafruit_io/adafruit_io.py @@ -99,6 +99,16 @@ def __enter__(self): def __exit__(self, exception_type, exception_value, traceback): self.disconnect() + def reconnect(self): + """Attempts to reconnect to the Adafruit IO MQTT Broker. + + """ + try: + self._client.reconnect() + except: + raise AdafruitIO_MQTTError("Unable to reconnect to Adafruit IO.") + + def connect(self): """Connects to the Adafruit IO MQTT Broker. Must be called before any other API methods are called. diff --git a/examples/mqtt/adafruit_io_groups.py b/examples/mqtt/adafruit_io_groups.py index 5297393..0623a0a 100755 --- a/examples/mqtt/adafruit_io_groups.py +++ b/examples/mqtt/adafruit_io_groups.py @@ -11,7 +11,7 @@ import adafruit_esp32spi.adafruit_esp32spi_socket as socket import neopixel from adafruit_io.adafruit_io import IO_MQTT -from adafruit_minimqtt import MQTT +import adafruit_minimqtt as MQTT ### WiFi ### @@ -76,18 +76,18 @@ def message(client, feed_id, payload): # the new value. print("Feed {0} received new value: {1}".format(feed_id, payload)) - # 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( - socket=socket, - broker="io.adafruit.com", - username=secrets["aio_user"], - password=secrets["aio_key"], - network_manager=wifi, -) +mqtt_client = MQTT.MQTT(broker="https://io.adafruit.com", + username=secrets["aio_user"], + password=secrets["aio_key"]) # Initialize an Adafruit IO MQTT Client io = IO_MQTT(mqtt_client) @@ -105,6 +105,7 @@ def message(client, feed_id, payload): humid_feed = "weatherstation.humidity" # Connect to Adafruit IO +print("Connecting to Adafruit IO...") io.connect() print("Publishing new messages to group feeds every 5 seconds...") diff --git a/examples/mqtt/adafruit_io_location.py b/examples/mqtt/adafruit_io_location.py index 4f9700a..f77eef9 100755 --- a/examples/mqtt/adafruit_io_location.py +++ b/examples/mqtt/adafruit_io_location.py @@ -1,6 +1,6 @@ # Example of tagging data with location values # and sending it to an Adafruit IO feed. - +import time import board import busio from digitalio import DigitalInOut @@ -10,7 +10,7 @@ import neopixel -from adafruit_minimqtt import MQTT +import adafruit_minimqtt as MQTT from adafruit_io.adafruit_io import IO_MQTT ### WiFi ### @@ -75,18 +75,18 @@ def message(client, feed_id, payload): # the new value. print("Feed {0} received new value: {1}".format(feed_id, payload)) - # 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( - socket=socket, - broker="io.adafruit.com", - username=secrets["aio_user"], - password=secrets["aio_key"], - network_manager=wifi, -) +mqtt_client = MQTT.MQTT(broker="https://io.adafruit.com", + username=secrets["aio_user"], + password=secrets["aio_key"]) # Initialize an Adafruit IO MQTT Client io = IO_MQTT(mqtt_client) @@ -112,5 +112,15 @@ def message(client, feed_id, payload): print("Data sent!") -# Listen forever... -io.loop_blocking() +# Start a blocking message loop... +# NOTE: NO code below this loop will execute +# NOTE: Network reconnection is handled within this loop +while True: + try: + io.loop() + except (ValueError, RuntimeError) as e: + print("Failed to get data, retrying\n", e) + wifi.reset() + io.reconnect() + continue + time.sleep(1) diff --git a/examples/mqtt/adafruit_io_simpletest.py b/examples/mqtt/adafruit_io_simpletest.py index 56326ee..ec6c28c 100755 --- a/examples/mqtt/adafruit_io_simpletest.py +++ b/examples/mqtt/adafruit_io_simpletest.py @@ -16,7 +16,7 @@ import adafruit_esp32spi.adafruit_esp32spi_socket as socket import neopixel from adafruit_io.adafruit_io import IO_MQTT -from adafruit_minimqtt import MQTT +import adafruit_minimqtt as MQTT ### WiFi ### @@ -89,18 +89,19 @@ def message(client, feed_id, payload): # the new value. print("Feed {0} received new value: {1}".format(feed_id, payload)) - # 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( - socket=socket, - broker="io.adafruit.com", - username=secrets["aio_user"], - password=secrets["aio_key"], - network_manager=wifi, -) +mqtt_client = MQTT.MQTT(broker="https://io.adafruit.com", + username=secrets["aio_user"], + password=secrets["aio_key"]) + # Initialize an Adafruit IO MQTT Client io = IO_MQTT(mqtt_client) @@ -113,6 +114,7 @@ def message(client, feed_id, payload): io.on_message = message # Connect to Adafruit IO +print("Connecting to Adafruit IO...") io.connect() # Below is an example of manually publishing a new value to Adafruit IO. @@ -127,8 +129,3 @@ def message(client, feed_id, payload): print("Publishing {0} to DemoFeed.".format(value)) io.publish("DemoFeed", value) last = time.monotonic() - - -# You can also call loop_blocking if you only want to receive values. -# NOTE: If uncommented, no code below this line will run. -# io.loop_blocking() diff --git a/examples/mqtt/adafruit_io_simpletest_eth.py b/examples/mqtt/adafruit_io_simpletest_eth.py new file mode 100755 index 0000000..e5fc421 --- /dev/null +++ b/examples/mqtt/adafruit_io_simpletest_eth.py @@ -0,0 +1,96 @@ +# Example of using the Adafruit IO CircuitPython MQTT client +# to subscribe to an Adafruit IO feed and publish random data +# to be received by the feed. +# +# Example by Tony DiCola for Adafruit Industries +# Modified by Brent Rubell for Adafruit Industries, 2019 +import time +from random import randint + +import board +import busio +from digitalio import DigitalInOut + +from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K +import adafruit_wiznet5k.adafruit_wiznet5k_socket as socket +from adafruit_io.adafruit_io import IO_MQTT +import adafruit_minimqtt as MQTT + +# Get MQTT details and more from a secrets.py file +try: + from secrets import secrets +except ImportError: + print("MQTT secrets are kept in secrets.py, please add them there!") + raise + +cs = DigitalInOut(board.D10) +spi_bus = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO) + +# Initialize ethernet interface with DHCP +eth = WIZNET5K(spi_bus, cs) + +# 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. + # This is a good place to subscribe to feed changes. The client parameter + # passed to this function is the Adafruit IO MQTT client so you can make + # calls against it easily. + print("Connected to Adafruit IO! Listening for DemoFeed changes...") + # Subscribe to changes on a feed named DemoFeed. + client.subscribe("DemoFeed") + +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)) + +def unsubscribe(client, userdata, topic, pid): + # This method is called when the client unsubscribes from a feed. + print('Unsubscribed from {0} with PID {1}'.format(topic, pid)) + +# pylint: disable=unused-argument +def disconnected(client): + # Disconnected function will be called when the client disconnects. + print("Disconnected from Adafruit IO!") + +# pylint: disable=unused-argument +def message(client, feed_id, payload): + # Message function will be called when a subscribed feed has a new value. + # The feed_id parameter identifies the feed, and the payload parameter has + # the new value. + print("Feed {0} received new value: {1}".format(feed_id, payload)) + +# Initialize MQTT interface with the ethernet interface +MQTT.set_socket(socket, eth) + +# Initialize a new MQTT Client object +mqtt_client = MQTT.MQTT(broker="http://io.adafruit.com", + username=secrets["aio_user"], + password=secrets["aio_key"]) + +# 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 +io.on_unsubscribe = unsubscribe +io.on_message = message + +# Connect to Adafruit IO +print("Connecting to Adafruit IO...") +io.connect() + +# Below is an example of manually publishing a new value to Adafruit IO. +last = 0 +print("Publishing a new message every 10 seconds...") +while True: + # Explicitly pump the message loop. + io.loop() + # Send a new message every 10 seconds. + if (time.monotonic() - last) >= 5: + value = randint(0, 100) + print("Publishing {0} to DemoFeed.".format(value)) + io.publish("DemoFeed", value) + last = time.monotonic() diff --git a/examples/mqtt/adafruit_io_time.py b/examples/mqtt/adafruit_io_time.py index 4af325a..546e71a 100755 --- a/examples/mqtt/adafruit_io_time.py +++ b/examples/mqtt/adafruit_io_time.py @@ -1,7 +1,7 @@ # Adafruit IO provides some built-in MQTT topics # for obtaining the current server time, if you don't have # access to a RTC module. - +import time import board import busio from digitalio import DigitalInOut @@ -10,7 +10,7 @@ import adafruit_esp32spi.adafruit_esp32spi_socket as socket import neopixel -from adafruit_minimqtt import MQTT +import adafruit_minimqtt as MQTT from adafruit_io.adafruit_io import IO_MQTT ### WiFi ### @@ -91,16 +91,17 @@ def message(client, feed_id, payload): # 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( - socket=socket, - broker="io.adafruit.com", - username=secrets["aio_user"], - password=secrets["aio_key"], - network_manager=wifi, -) +mqtt_client = MQTT.MQTT(broker="https://io.adafruit.com", + username=secrets["aio_user"], + password=secrets["aio_key"]) # Initialize an Adafruit IO MQTT Client io = IO_MQTT(mqtt_client) @@ -113,5 +114,16 @@ def message(client, feed_id, payload): # Connect to Adafruit IO io.connect() -# Listen forever... -io.loop_blocking() + +# Start a blocking message loop... +# NOTE: NO code below this loop will execute +# NOTE: Network reconnection is handled within this loop +while True: + try: + io.loop() + except (ValueError, RuntimeError) as e: + print("Failed to get data, retrying\n", e) + wifi.reset() + io.reconnect() + continue + time.sleep(1)