adafruit_io_groups.py
1 # Subscribing to an Adafruit IO Group 2 # and Publishing to the feeds in the group 3 import time 4 from random import randint 5 6 import board 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 import neopixel 13 from adafruit_io.adafruit_io import IO_MQTT 14 import adafruit_minimqtt.adafruit_minimqtt as 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 53 # Define callback functions which will be called when certain events happen. 54 # pylint: disable=unused-argument 55 def connected(client): 56 # Connected function will be called when the client is connected to Adafruit IO. 57 # This is a good place to subscribe to feed changes. The client parameter 58 # passed to this function is the Adafruit IO MQTT client so you can make 59 # calls against it easily. 60 print("Connected to Adafruit IO!") 61 62 # Subscribe to Group 63 io.subscribe(group_key=group_name) 64 65 66 # pylint: disable=unused-argument 67 def disconnected(client): 68 # Disconnected function will be called when the client disconnects. 69 print("Disconnected from Adafruit IO!") 70 71 72 # pylint: disable=unused-argument 73 def message(client, feed_id, payload): 74 # Message function will be called when a subscribed feed has a new value. 75 # The feed_id parameter identifies the feed, and the payload parameter has 76 # the new value. 77 print("Feed {0} received new value: {1}".format(feed_id, payload)) 78 79 80 # Connect to WiFi 81 print("Connecting to WiFi...") 82 wifi.connect() 83 print("Connected!") 84 85 # Initialize MQTT interface with the esp interface 86 MQTT.set_socket(socket, esp) 87 88 # Initialize a new MQTT Client object 89 mqtt_client = MQTT.MQTT( 90 broker="io.adafruit.com", username=secrets["aio_user"], password=secrets["aio_key"], 91 ) 92 93 # Initialize an Adafruit IO MQTT Client 94 io = IO_MQTT(mqtt_client) 95 96 # Connect the callback methods defined above to Adafruit IO 97 io.on_connect = connected 98 io.on_disconnect = disconnected 99 io.on_message = message 100 101 # Group name 102 group_name = "weatherstation" 103 104 # Feeds within the group 105 temp_feed = "weatherstation.temperature" 106 humid_feed = "weatherstation.humidity" 107 108 # Connect to Adafruit IO 109 print("Connecting to Adafruit IO...") 110 io.connect() 111 112 print("Publishing new messages to group feeds every 5 seconds...") 113 114 while True: 115 io.loop() 116 temp_reading = randint(0, 100) 117 print("Publishing value {0} to feed: {1}".format(temp_reading, temp_feed)) 118 io.publish(temp_feed, temp_reading) 119 120 humid_reading = randint(0, 100) 121 print("Publishing value {0} to feed: {1}".format(humid_reading, humid_feed)) 122 io.publish(humid_feed, humid_reading) 123 time.sleep(5)