|
| 1 | +# Example of tagging data with location values |
| 2 | +# and sending it to an Adafruit IO feed. |
| 3 | + |
| 4 | +from random import randint |
| 5 | +import board |
| 6 | +import neopixel |
| 7 | +import busio |
| 8 | +from digitalio import DigitalInOut |
| 9 | +from adafruit_esp32spi import adafruit_esp32spi |
| 10 | +from adafruit_esp32spi import adafruit_esp32spi_wifimanager |
| 11 | +import adafruit_esp32spi.adafruit_esp32spi_socket as socket |
| 12 | + |
| 13 | +from adafruit_minimqtt import MQTT |
| 14 | +from adafruit_io.adafruit_io import IO_MQTT |
| 15 | + |
| 16 | +### WiFi ### |
| 17 | + |
| 18 | +# Get wifi details and more from a secrets.py file |
| 19 | +try: |
| 20 | + from secrets import secrets |
| 21 | +except ImportError: |
| 22 | + print("WiFi secrets are kept in secrets.py, please add them there!") |
| 23 | + raise |
| 24 | + |
| 25 | +# If you are using a board with pre-defined ESP32 Pins: |
| 26 | +esp32_cs = DigitalInOut(board.ESP_CS) |
| 27 | +esp32_ready = DigitalInOut(board.ESP_BUSY) |
| 28 | +esp32_reset = DigitalInOut(board.ESP_RESET) |
| 29 | + |
| 30 | +# If you have an externally connected ESP32: |
| 31 | +# esp32_cs = DigitalInOut(board.D9) |
| 32 | +# esp32_ready = DigitalInOut(board.D10) |
| 33 | +# esp32_reset = DigitalInOut(board.D5) |
| 34 | + |
| 35 | +spi = busio.SPI(board.SCK, board.MOSI, board.MISO) |
| 36 | +esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) |
| 37 | +"""Use below for Most Boards""" |
| 38 | +status_light = neopixel.NeoPixel( |
| 39 | + board.NEOPIXEL, 1, brightness=0.2 |
| 40 | +) # Uncomment for Most Boards |
| 41 | +"""Uncomment below for ItsyBitsy M4""" |
| 42 | +# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2) |
| 43 | +# Uncomment below for an externally defined RGB LED |
| 44 | +# import adafruit_rgbled |
| 45 | +# from adafruit_esp32spi import PWMOut |
| 46 | +# RED_LED = PWMOut.PWMOut(esp, 26) |
| 47 | +# GREEN_LED = PWMOut.PWMOut(esp, 27) |
| 48 | +# BLUE_LED = PWMOut.PWMOut(esp, 25) |
| 49 | +# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED) |
| 50 | +wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light) |
| 51 | + |
| 52 | +# Define callback functions which will be called when certain events happen. |
| 53 | +def connected(client): |
| 54 | + # Connected function will be called when the client is connected to Adafruit IO. |
| 55 | + # This is a good place to subscribe to feed changes. The client parameter |
| 56 | + # passed to this function is the Adafruit IO MQTT client so you can make |
| 57 | + # calls against it easily. |
| 58 | + print("Connected to Adafruit IO!") |
| 59 | + |
| 60 | + # Subscribe to a location feed! |
| 61 | + io.subscribe("location") |
| 62 | + |
| 63 | + |
| 64 | +def disconnected(client): |
| 65 | + # Disconnected function will be called when the client disconnects. |
| 66 | + print("Disconnected from Adafruit IO!") |
| 67 | + |
| 68 | + |
| 69 | +def message(client, feed_id, payload): |
| 70 | + # Message function will be called when a subscribed feed has a new value. |
| 71 | + # The feed_id parameter identifies the feed, and the payload parameter has |
| 72 | + # the new value. |
| 73 | + print("Feed {0} received new value: {1}".format(feed_id, payload)) |
| 74 | + |
| 75 | + |
| 76 | +# Connect to WiFi |
| 77 | +wifi.connect() |
| 78 | + |
| 79 | +# Initialize a new MQTT Client object |
| 80 | +client = MQTT( |
| 81 | + socket=socket, |
| 82 | + broker="io.adafruit.com", |
| 83 | + username=secrets["aio_user"], |
| 84 | + password=secrets["aio_key"], |
| 85 | + network_manager=wifi, |
| 86 | + log=True, |
| 87 | +) |
| 88 | + |
| 89 | +# Initialize an Adafruit IO MQTT Client |
| 90 | +io = IO_MQTT(client) |
| 91 | + |
| 92 | +# Connect the callback methods defined above to Adafruit IO |
| 93 | +io.on_connect = connected |
| 94 | +io.on_disconnect = disconnected |
| 95 | +io.on_message = message |
| 96 | + |
| 97 | +# Connect to Adafruit IO |
| 98 | +io.connect() |
| 99 | + |
| 100 | +# Set data |
| 101 | +data_value = 42 |
| 102 | + |
| 103 | +# Set up metadata associated with data_value |
| 104 | +# lat, lon, ele |
| 105 | +metadata = "40.726190, -74.005334, -6" |
| 106 | + |
| 107 | +# Send data and location metadata to the 'location' feed |
| 108 | +print("Sending data and location metadata to IO...") |
| 109 | +io.publish("location", data_value, metadata) |
| 110 | +print("Data sent!") |
| 111 | + |
| 112 | + |
| 113 | +# Listen forever... |
| 114 | +io.loop_blocking() |
0 commit comments