|
| 1 | +# adafruit_circuitpython_adafruitio usage with an esp32spi_socket |
| 2 | +from random import randint |
| 3 | +import board |
| 4 | +import busio |
| 5 | +from digitalio import DigitalInOut |
| 6 | +import adafruit_esp32spi.adafruit_esp32spi_socket as socket |
| 7 | +from adafruit_esp32spi import adafruit_esp32spi |
| 8 | +import adafruit_requests as requests |
| 9 | +from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError |
| 10 | + |
| 11 | +# Add a secrets.py to your filesystem that has a dictionary called secrets with "ssid" and |
| 12 | +# "password" keys with your WiFi credentials. DO NOT share that file or commit it into Git or other |
| 13 | +# source control. |
| 14 | +# pylint: disable=no-name-in-module,wrong-import-order |
| 15 | +try: |
| 16 | + from secrets import secrets |
| 17 | +except ImportError: |
| 18 | + print("WiFi secrets are kept in secrets.py, please add them there!") |
| 19 | + raise |
| 20 | + |
| 21 | +# If you are using a board with pre-defined ESP32 Pins: |
| 22 | +esp32_cs = DigitalInOut(board.ESP_CS) |
| 23 | +esp32_ready = DigitalInOut(board.ESP_BUSY) |
| 24 | +esp32_reset = DigitalInOut(board.ESP_RESET) |
| 25 | + |
| 26 | +# If you have an externally connected ESP32: |
| 27 | +# esp32_cs = DigitalInOut(board.D9) |
| 28 | +# esp32_ready = DigitalInOut(board.D10) |
| 29 | +# esp32_reset = DigitalInOut(board.D5) |
| 30 | + |
| 31 | +spi = busio.SPI(board.SCK, board.MOSI, board.MISO) |
| 32 | +esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) |
| 33 | + |
| 34 | +print("Connecting to AP...") |
| 35 | +while not esp.is_connected: |
| 36 | + try: |
| 37 | + esp.connect_AP(secrets["ssid"], secrets["password"]) |
| 38 | + except RuntimeError as e: |
| 39 | + print("could not connect to AP, retrying: ", e) |
| 40 | + continue |
| 41 | +print("Connected to", str(esp.ssid, "utf-8"), "\tRSSI:", esp.rssi) |
| 42 | + |
| 43 | +socket.set_interface(esp) |
| 44 | +requests.set_socket(socket, esp) |
| 45 | + |
| 46 | +# Set your Adafruit IO Username and Key in secrets.py |
| 47 | +# (visit io.adafruit.com if you need to create an account, |
| 48 | +# or if you need your Adafruit IO key.) |
| 49 | +aio_username = secrets["aio_username"] |
| 50 | +aio_key = secrets["aio_key"] |
| 51 | + |
| 52 | +# Initialize an Adafruit IO HTTP API object |
| 53 | +io = IO_HTTP(aio_username, aio_key, requests) |
| 54 | + |
| 55 | +try: |
| 56 | + # Get the 'temperature' feed from Adafruit IO |
| 57 | + temperature_feed = io.get_feed("temperature") |
| 58 | +except AdafruitIO_RequestError: |
| 59 | + # If no 'temperature' feed exists, create one |
| 60 | + temperature_feed = io.create_new_feed("temperature") |
| 61 | + |
| 62 | +# Send random integer values to the feed |
| 63 | +random_value = randint(0, 50) |
| 64 | +print("Sending {0} to temperature feed...".format(random_value)) |
| 65 | +io.send_data(temperature_feed["key"], random_value) |
| 66 | +print("Data sent!") |
| 67 | + |
| 68 | +# Retrieve data value from the feed |
| 69 | +print("Retrieving data from temperature feed...") |
| 70 | +received_data = io.receive_data(temperature_feed["key"]) |
| 71 | +print("Data from temperature feed: ", received_data["value"]) |
0 commit comments