|
| 1 | +import time |
| 2 | + |
| 3 | +import board |
| 4 | +import busio |
| 5 | +from digitalio import DigitalInOut |
| 6 | +from adafruit_esp32spi import adafruit_esp32spi |
| 7 | +from adafruit_esp32spi import adafruit_esp32spi_wifimanager |
| 8 | +import adafruit_esp32spi.adafruit_esp32spi_socket as socket |
| 9 | +import neopixel |
| 10 | +import adafruit_minimqtt.adafruit_minimqtt as MQTT |
| 11 | +from adafruit_io.adafruit_io import IO_MQTT |
| 12 | + |
| 13 | +### WiFi ### |
| 14 | + |
| 15 | +# Get wifi details and more from a secrets.py file |
| 16 | +try: |
| 17 | + from secrets import secrets |
| 18 | +except ImportError: |
| 19 | + print("WiFi secrets are kept in secrets.py, please add them there!") |
| 20 | + raise |
| 21 | + |
| 22 | +# If you are using a board with pre-defined ESP32 Pins: |
| 23 | +esp32_cs = DigitalInOut(board.ESP_CS) |
| 24 | +esp32_ready = DigitalInOut(board.ESP_BUSY) |
| 25 | +esp32_reset = DigitalInOut(board.ESP_RESET) |
| 26 | + |
| 27 | +# If you have an externally connected ESP32: |
| 28 | +# esp32_cs = DigitalInOut(board.D9) |
| 29 | +# esp32_ready = DigitalInOut(board.D10) |
| 30 | +# esp32_reset = DigitalInOut(board.D5) |
| 31 | + |
| 32 | +spi = busio.SPI(board.SCK, board.MOSI, board.MISO) |
| 33 | +esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) |
| 34 | +"""Use below for Most Boards""" |
| 35 | +status_light = neopixel.NeoPixel( |
| 36 | + board.NEOPIXEL, 1, brightness=0.2 |
| 37 | +) # Uncomment for Most Boards |
| 38 | +"""Uncomment below for ItsyBitsy M4""" |
| 39 | +# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2) |
| 40 | +# Uncomment below for an externally defined RGB LED |
| 41 | +# import adafruit_rgbled |
| 42 | +# from adafruit_esp32spi import PWMOut |
| 43 | +# RED_LED = PWMOut.PWMOut(esp, 26) |
| 44 | +# GREEN_LED = PWMOut.PWMOut(esp, 27) |
| 45 | +# BLUE_LED = PWMOut.PWMOut(esp, 25) |
| 46 | +# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED) |
| 47 | +wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light) |
| 48 | + |
| 49 | +# Define callback functions which will be called when certain events happen. |
| 50 | +# pylint: disable=unused-argument |
| 51 | +def connected(client): |
| 52 | + # Connected function will be called when the client is connected to Adafruit IO. |
| 53 | + # This is a good place to subscribe to feed changes. The client parameter |
| 54 | + # passed to this function is the Adafruit IO MQTT client so you can make |
| 55 | + # calls against it easily. |
| 56 | + print("Connected to Adafruit IO! Listening for DemoFeed changes...") |
| 57 | + |
| 58 | + |
| 59 | +def subscribe(client, userdata, topic, granted_qos): |
| 60 | + # This method is called when the client subscribes to a new feed. |
| 61 | + print("Subscribed to {0} with QOS level {1}".format(topic, granted_qos)) |
| 62 | + |
| 63 | + |
| 64 | +def unsubscribe(client, userdata, topic, pid): |
| 65 | + # This method is called when the client unsubscribes from a feed. |
| 66 | + print("Unsubscribed from {0} with PID {1}".format(topic, pid)) |
| 67 | + |
| 68 | + |
| 69 | +# pylint: disable=unused-argument |
| 70 | +def disconnected(client): |
| 71 | + # Disconnected function will be called when the client disconnects. |
| 72 | + print("Disconnected from Adafruit IO!") |
| 73 | + |
| 74 | + |
| 75 | +# pylint: disable=unused-argument |
| 76 | +def on_message(client, feed_id, payload): |
| 77 | + # Message function will be called when a subscribed feed has a new value. |
| 78 | + # The feed_id parameter identifies the feed, and the payload parameter has |
| 79 | + # the new value. |
| 80 | + print("Feed {0} received new value: {1}".format(feed_id, payload)) |
| 81 | + |
| 82 | + |
| 83 | +def on_battery_msg(client, topic, message): |
| 84 | + # Method called whenever user/feeds/battery has a new value |
| 85 | + print("Battery level: {}v".format(message)) |
| 86 | + |
| 87 | + |
| 88 | +# 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) |
| 95 | + |
| 96 | +# Initialize a new MQTT Client object |
| 97 | +mqtt_client = MQTT.MQTT( |
| 98 | + broker="io.adafruit.com", |
| 99 | + username=secrets["aio_username"], |
| 100 | + password=secrets["aio_key"], |
| 101 | +) |
| 102 | + |
| 103 | +# Initialize an Adafruit IO MQTT Client |
| 104 | +io = IO_MQTT(mqtt_client) |
| 105 | + |
| 106 | +# Connect the callback methods defined above to Adafruit IO |
| 107 | +io.on_connect = connected |
| 108 | +io.on_disconnect = disconnected |
| 109 | +io.on_subscribe = subscribe |
| 110 | +io.on_unsubscribe = unsubscribe |
| 111 | +io.on_message = on_message |
| 112 | + |
| 113 | +# Connect to Adafruit IO |
| 114 | +print("Connecting to Adafruit IO...") |
| 115 | +io.connect() |
| 116 | + |
| 117 | +# Set up a message handler for the battery feed |
| 118 | +io.add_feed_callback("battery", on_battery_msg) |
| 119 | + |
| 120 | +# Subscribe to all messages on the battery feed |
| 121 | +io.subscribe("battery") |
| 122 | + |
| 123 | +# Start a blocking loop to check for new messages |
| 124 | +while True: |
| 125 | + try: |
| 126 | + io.loop() |
| 127 | + except (ValueError, RuntimeError) as e: |
| 128 | + print("Failed to get data, retrying\n", e) |
| 129 | + wifi.reset() |
| 130 | + io.reconnect() |
| 131 | + continue |
| 132 | + time.sleep(0.5) |
0 commit comments