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