|
1 |
| -""" |
2 |
| -Example of reading an analog light sensor |
3 |
| -and sending the value to Adafruit IO |
4 |
| -""" |
| 1 | +# Example of publishing the value of an ADC to Adafruit IO |
| 2 | +# adafruit_circuitpython_adafruitio with an esp32spi_socket |
5 | 3 | import time
|
6 | 4 | import board
|
7 | 5 | import busio
|
8 |
| -from digitalio import DigitalInOut |
9 | 6 | from analogio import AnalogIn
|
10 |
| -from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager |
11 |
| - |
12 |
| -# Import NeoPixel Library |
13 |
| -import neopixel |
14 |
| - |
15 |
| -# Import Adafruit IO HTTP Client |
| 7 | +from digitalio import DigitalInOut |
| 8 | +import adafruit_esp32spi.adafruit_esp32spi_socket as socket |
| 9 | +from adafruit_esp32spi import adafruit_esp32spi |
| 10 | +import adafruit_requests as requests |
16 | 11 | from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError
|
17 | 12 |
|
18 |
| -# Delay between polling and sending light sensor data, in seconds |
19 |
| -SENSOR_DELAY = 30 |
20 |
| - |
21 |
| -# Get wifi details and more from a secrets.py file |
| 13 | +# Add a secrets.py to your filesystem that has a dictionary called secrets with "ssid" and |
| 14 | +# "password" keys with your WiFi credentials. DO NOT share that file or commit it into Git or other |
| 15 | +# source control. |
| 16 | +# pylint: disable=no-name-in-module,wrong-import-order |
22 | 17 | try:
|
23 | 18 | from secrets import secrets
|
24 | 19 | except ImportError:
|
25 | 20 | print("WiFi secrets are kept in secrets.py, please add them there!")
|
26 | 21 | raise
|
27 | 22 |
|
28 |
| -# ESP32 Setup |
29 |
| -try: |
30 |
| - esp32_cs = DigitalInOut(board.ESP_CS) |
31 |
| - esp32_ready = DigitalInOut(board.ESP_BUSY) |
32 |
| - esp32_reset = DigitalInOut(board.ESP_RESET) |
33 |
| -except AttributeError: |
34 |
| - esp32_cs = DigitalInOut(board.D9) |
35 |
| - esp32_ready = DigitalInOut(board.D10) |
36 |
| - esp32_reset = DigitalInOut(board.D5) |
| 23 | +# If you are using a board with pre-defined ESP32 Pins: |
| 24 | +esp32_cs = DigitalInOut(board.ESP_CS) |
| 25 | +esp32_ready = DigitalInOut(board.ESP_BUSY) |
| 26 | +esp32_reset = DigitalInOut(board.ESP_RESET) |
| 27 | + |
| 28 | +# If you have an externally connected ESP32: |
| 29 | +# esp32_cs = DigitalInOut(board.D9) |
| 30 | +# esp32_ready = DigitalInOut(board.D10) |
| 31 | +# esp32_reset = DigitalInOut(board.D5) |
37 | 32 |
|
38 | 33 | spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
|
39 | 34 | esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
|
40 |
| -status_light = neopixel.NeoPixel( |
41 |
| - board.NEOPIXEL, 1, brightness=0.2 |
42 |
| -) # Uncomment for Most Boards |
43 |
| -"""Uncomment below for ItsyBitsy M4""" |
44 |
| -# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2) |
45 |
| -wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light) |
| 35 | + |
| 36 | +print("Connecting to AP...") |
| 37 | +while not esp.is_connected: |
| 38 | + try: |
| 39 | + esp.connect_AP(secrets["ssid"], secrets["password"]) |
| 40 | + except RuntimeError as e: |
| 41 | + print("could not connect to AP, retrying: ", e) |
| 42 | + continue |
| 43 | +print("Connected to", str(esp.ssid, "utf-8"), "\tRSSI:", esp.rssi) |
| 44 | + |
| 45 | +socket.set_interface(esp) |
| 46 | +requests.set_socket(socket, esp) |
46 | 47 |
|
47 | 48 | # Set your Adafruit IO Username and Key in secrets.py
|
48 | 49 | # (visit io.adafruit.com if you need to create an account,
|
49 | 50 | # or if you need your Adafruit IO key.)
|
50 | 51 | aio_username = secrets["aio_username"]
|
51 | 52 | aio_key = secrets["aio_key"]
|
52 | 53 |
|
53 |
| -# Create an instance of the Adafruit IO HTTP client |
54 |
| -io = IO_HTTP(aio_username, aio_key, wifi) |
| 54 | +# Initialize an Adafruit IO HTTP API object |
| 55 | +io = IO_HTTP(aio_username, aio_key, requests) |
55 | 56 |
|
56 | 57 | try:
|
57 | 58 | # Get the 'light' feed from Adafruit IO
|
|
60 | 61 | # If no 'light' feed exists, create one
|
61 | 62 | light_feed = io.create_new_feed("light")
|
62 | 63 |
|
63 |
| -# Set up an analog light sensor on the PyPortal |
64 |
| -adc = AnalogIn(board.LIGHT) |
| 64 | +# Set up an ADC |
| 65 | +adc = AnalogIn(board.A0) |
65 | 66 |
|
| 67 | +SENSOR_DELAY = 30 |
66 | 68 | while True:
|
67 | 69 | light_value = adc.value
|
68 |
| - print("Light Level: ", light_value) |
| 70 | + print("ADC Value: ", light_value) |
69 | 71 | print("Sending to Adafruit IO...")
|
70 | 72 | io.send_data(light_feed["key"], light_value)
|
71 | 73 | print("Sent!")
|
|
0 commit comments