Skip to content

Commit 847df6d

Browse files
authored
Merge pull request #11 from brentru/add-example-analog
Add example: AnalogIn
2 parents 186305d + a90e973 commit 847df6d

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
"""
2+
Example of reading an analog light sensor
3+
and sending the value to Adafruit IO
4+
"""
5+
import time
6+
import board
7+
import busio
8+
from digitalio import DigitalInOut
9+
from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager
10+
from analogio import AnalogIn
11+
12+
# Import Adafruit IO REST Client
13+
from adafruit_io.adafruit_io import RESTClient, AdafruitIO_RequestError
14+
15+
# Delay between polling and sending light sensor data, in seconds
16+
SENSOR_DELAY = 30
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+
# PyPortal ESP32 Setup
26+
esp32_cs = DigitalInOut(board.ESP_CS)
27+
esp32_ready = DigitalInOut(board.ESP_BUSY)
28+
esp32_reset = DigitalInOut(board.ESP_RESET)
29+
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
30+
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
31+
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, board.NEOPIXEL)
32+
33+
"""
34+
# ESP32 Setup
35+
esp32_cs = DigitalInOut(board.D9)
36+
esp32_ready = DigitalInOut(board.D10)
37+
esp32_reset = DigitalInOut(board.D5)
38+
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
39+
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
40+
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, board.NEOPIXEL)
41+
"""
42+
43+
# Set your Adafruit IO Username and Key in secrets.py
44+
# (visit io.adafruit.com if you need to create an account,
45+
# or if you need your Adafruit IO key.)
46+
ADAFRUIT_IO_USER = secrets['adafruit_io_user']
47+
ADAFRUIT_IO_KEY = secrets['adafruit_io_key']
48+
49+
# Create an instance of the Adafruit IO REST client
50+
io = RESTClient(ADAFRUIT_IO_USER, ADAFRUIT_IO_KEY, wifi)
51+
52+
try:
53+
# Get the 'light' feed from Adafruit IO
54+
light_feed = io.get_feed('light')
55+
except AdafruitIO_RequestError:
56+
# If no 'light' feed exists, create one
57+
light_feed = io.create_new_feed('light')
58+
59+
# Set up an analog light sensor on the PyPortal
60+
adc = AnalogIn(board.LIGHT)
61+
62+
while True:
63+
light_value = adc.value
64+
print('Light Level: ', light_value)
65+
print('Sending to Adafruit IO...')
66+
io.send_data(light_feed['key'], light_value)
67+
print('Sent!')
68+
# delay sending to Adafruit IO
69+
time.sleep(SENSOR_DELAY)

0 commit comments

Comments
 (0)