|
| 1 | +import time |
| 2 | +import board |
| 3 | +import busio |
| 4 | +from digitalio import DigitalInOut |
| 5 | +import neopixel |
| 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 adafruit_minimqtt.adafruit_minimqtt as MQTT |
| 10 | + |
| 11 | +### WiFi ### |
| 12 | + |
| 13 | +# Get wifi details and more from a secrets.py file |
| 14 | +try: |
| 15 | + from secrets import secrets |
| 16 | +except ImportError: |
| 17 | + print("WiFi secrets are kept in secrets.py, please add them there!") |
| 18 | + raise |
| 19 | + |
| 20 | +# If you are using a board with pre-defined ESP32 Pins: |
| 21 | +esp32_cs = DigitalInOut(board.ESP_CS) |
| 22 | +esp32_ready = DigitalInOut(board.ESP_BUSY) |
| 23 | +esp32_reset = DigitalInOut(board.ESP_RESET) |
| 24 | + |
| 25 | +# If you have an externally connected ESP32: |
| 26 | +# esp32_cs = DigitalInOut(board.D9) |
| 27 | +# esp32_ready = DigitalInOut(board.D10) |
| 28 | +# esp32_reset = DigitalInOut(board.D5) |
| 29 | + |
| 30 | +spi = busio.SPI(board.SCK, board.MOSI, board.MISO) |
| 31 | +esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) |
| 32 | +"""Use below for Most Boards""" |
| 33 | +status_light = neopixel.NeoPixel( |
| 34 | + board.NEOPIXEL, 1, brightness=0.2 |
| 35 | +) # Uncomment for Most Boards |
| 36 | +"""Uncomment below for ItsyBitsy M4""" |
| 37 | +# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2) |
| 38 | +# Uncomment below for an externally defined RGB LED |
| 39 | +# import adafruit_rgbled |
| 40 | +# from adafruit_esp32spi import PWMOut |
| 41 | +# RED_LED = PWMOut.PWMOut(esp, 26) |
| 42 | +# GREEN_LED = PWMOut.PWMOut(esp, 27) |
| 43 | +# BLUE_LED = PWMOut.PWMOut(esp, 25) |
| 44 | +# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED) |
| 45 | +wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light) |
| 46 | + |
| 47 | +### Code ### |
| 48 | + |
| 49 | +# Define callback methods which are called when events occur |
| 50 | +# pylint: disable=unused-argument, redefined-outer-name |
| 51 | +def connected(client, userdata, flags, rc): |
| 52 | + # This function will be called when the client is connected |
| 53 | + # successfully to the broker. |
| 54 | + print("Connected to MQTT Broker!") |
| 55 | + |
| 56 | + |
| 57 | +def disconnected(client, userdata, rc): |
| 58 | + # This method is called when the client is disconnected |
| 59 | + print("Disconnected from MQTT Broker!") |
| 60 | + |
| 61 | + |
| 62 | +def subscribe(client, userdata, topic, granted_qos): |
| 63 | + # This method is called when the client subscribes to a new feed. |
| 64 | + print("Subscribed to {0} with QOS level {1}".format(topic, granted_qos)) |
| 65 | + |
| 66 | + |
| 67 | +def unsubscribe(client, userdata, topic, pid): |
| 68 | + # This method is called when the client unsubscribes from a feed. |
| 69 | + print("Unsubscribed from {0} with PID {1}".format(topic, pid)) |
| 70 | + |
| 71 | + |
| 72 | +def on_battery_msg(client, topic, message): |
| 73 | + # Method called when device/batteryLife has a new value |
| 74 | + print("Battery level: {}v".format(message)) |
| 75 | + |
| 76 | + # client.remove_topic_callback("device/batteryLevel") |
| 77 | + |
| 78 | + |
| 79 | +def on_message(client, topic, message): |
| 80 | + # Method callled when a client's subscribed feed has a new value. |
| 81 | + print("New message on topic {0}: {1}".format(topic, message)) |
| 82 | + |
| 83 | + |
| 84 | +# Connect to WiFi |
| 85 | +print("Connecting to WiFi...") |
| 86 | +wifi.connect() |
| 87 | +print("Connected!") |
| 88 | + |
| 89 | +MQTT.set_socket(socket, esp) |
| 90 | + |
| 91 | +# Set up a MiniMQTT Client |
| 92 | +client = MQTT.MQTT(broker=secrets["broker"], port=secrets["broker_port"]) |
| 93 | + |
| 94 | +# Setup the callback methods above |
| 95 | +client.on_connect = connected |
| 96 | +client.on_disconnect = disconnected |
| 97 | +client.on_subscribe = subscribe |
| 98 | +client.on_unsubscribe = unsubscribe |
| 99 | +client.on_message = on_message |
| 100 | +client.add_topic_callback("device/batteryLevel", on_battery_msg) |
| 101 | + |
| 102 | +# Connect the client to the MQTT broker. |
| 103 | +print("Connecting to MQTT broker...") |
| 104 | +client.connect() |
| 105 | + |
| 106 | +# Subscribe to all notifications on the device/ topic |
| 107 | +client.subscribe("device/#", 1) |
| 108 | + |
| 109 | +# Start a blocking message loop... |
| 110 | +# NOTE: NO code below this loop will execute |
| 111 | +while True: |
| 112 | + try: |
| 113 | + client.loop() |
| 114 | + except (ValueError, RuntimeError) as e: |
| 115 | + print("Failed to get data, retrying\n", e) |
| 116 | + wifi.reset() |
| 117 | + client.reconnect() |
| 118 | + continue |
| 119 | + time.sleep(1) |
0 commit comments