|
| 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 | +esp32_cs = DigitalInOut(board.D13) |
| 21 | +esp32_ready = DigitalInOut(board.D11) |
| 22 | +esp32_reset = DigitalInOut(board.D12) |
| 23 | + |
| 24 | +spi = busio.SPI(board.SCK, board.MOSI, board.MISO) |
| 25 | +esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) |
| 26 | +"""Use below for Most Boards""" |
| 27 | +status_light = neopixel.NeoPixel( |
| 28 | + board.NEOPIXEL, 1, brightness=0.2 |
| 29 | +) # Uncomment for Most Boards |
| 30 | +"""Uncomment below for ItsyBitsy M4""" |
| 31 | +# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2) |
| 32 | +# Uncomment below for an externally defined RGB LED |
| 33 | +# import adafruit_rgbled |
| 34 | +# from adafruit_esp32spi import PWMOut |
| 35 | +# RED_LED = PWMOut.PWMOut(esp, 26) |
| 36 | +# GREEN_LED = PWMOut.PWMOut(esp, 27) |
| 37 | +# BLUE_LED = PWMOut.PWMOut(esp, 25) |
| 38 | +# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED) |
| 39 | +wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light) |
| 40 | + |
| 41 | +### Topic Setup ### |
| 42 | + |
| 43 | +# MQTT Topic |
| 44 | +# Use this topic if you'd like to connect to a standard MQTT broker |
| 45 | +mqtt_topic = "test/topic" |
| 46 | + |
| 47 | +# Adafruit IO-style Topic |
| 48 | +# Use this topic if you'd like to connect to io.adafruit.com |
| 49 | +# mqtt_topic = 'aio_user/feeds/temperature' |
| 50 | + |
| 51 | +### Code ### |
| 52 | + |
| 53 | +# Define callback methods which are called when events occur |
| 54 | +# pylint: disable=unused-argument, redefined-outer-name |
| 55 | +def connect(client, userdata, flags, rc): |
| 56 | + # This function will be called when the client is connected |
| 57 | + # successfully to the broker. |
| 58 | + print("Connected to MQTT Broker!") |
| 59 | + print("Flags: {0}\n RC: {1}".format(flags, rc)) |
| 60 | + |
| 61 | + |
| 62 | +def disconnect(client, userdata, rc): |
| 63 | + # This method is called when the client disconnects |
| 64 | + # from the broker. |
| 65 | + print("Disconnected from MQTT Broker!") |
| 66 | + |
| 67 | + |
| 68 | +def subscribe(client, userdata, topic, granted_qos): |
| 69 | + # This method is called when the client subscribes to a new feed. |
| 70 | + print("Subscribed to {0} with QOS level {1}".format(topic, granted_qos)) |
| 71 | + |
| 72 | + |
| 73 | +def unsubscribe(client, userdata, topic, pid): |
| 74 | + # This method is called when the client unsubscribes from a feed. |
| 75 | + print("Unsubscribed from {0} with PID {1}".format(topic, pid)) |
| 76 | + |
| 77 | + |
| 78 | +def publish(client, userdata, topic, pid): |
| 79 | + # This method is called when the client publishes data to a feed. |
| 80 | + print("Published to {0} with PID {1}".format(topic, pid)) |
| 81 | + |
| 82 | +def on_battery_msg(client, topic, message): |
| 83 | + print("Battery Level: {}v".format(message)) |
| 84 | + |
| 85 | +def on_message(client, topic, message): |
| 86 | + """Method callled when a client's subscribed feed has a new |
| 87 | + value. |
| 88 | + :param str topic: The topic of the feed with a new value. |
| 89 | + :param str message: The new value |
| 90 | + """ |
| 91 | + print("New message on topic {0}: {1}".format(topic, message)) |
| 92 | + |
| 93 | + |
| 94 | +# Connect to WiFi |
| 95 | +print("Connecting to WiFi...") |
| 96 | +wifi.connect() |
| 97 | +print("Connected!") |
| 98 | + |
| 99 | +# Initialize MQTT interface with the esp interface |
| 100 | +MQTT.set_socket(socket, esp) |
| 101 | + |
| 102 | +# Set up a MiniMQTT Client |
| 103 | +client = MQTT.MQTT(broker=secrets["broker"], port=1883) |
| 104 | + |
| 105 | +# Connect callback handlers to client |
| 106 | +client.on_connect = connect |
| 107 | +client.on_disconnect = disconnect |
| 108 | +client.on_subscribe = subscribe |
| 109 | +client.on_unsubscribe = unsubscribe |
| 110 | +client.on_publish = publish |
| 111 | + |
| 112 | +client.on_message = on_message |
| 113 | +client.add_topic_callback("sensors/batteryLevel", on_battery_msg) |
| 114 | + |
| 115 | +# Connect the client to the MQTT broker. |
| 116 | +print("Connecting to MQTT broker...") |
| 117 | +client.connect() |
| 118 | + |
| 119 | +# Subscribe to all notifications on the sensors/ |
| 120 | +client.subscribe("sensors/#", 1) |
| 121 | + |
| 122 | +# Start a blocking message loop... |
| 123 | +# NOTE: NO code below this loop will execute |
| 124 | +# NOTE: Network reconnection is handled within this loop |
| 125 | +while True: |
| 126 | + try: |
| 127 | + client.loop() |
| 128 | + except (ValueError, RuntimeError) as e: |
| 129 | + print("Failed to get data, retrying\n", e) |
| 130 | + wifi.reset() |
| 131 | + client.reconnect() |
| 132 | + continue |
| 133 | + time.sleep(1) |
| 134 | + |
| 135 | + |
0 commit comments