Skip to content

Commit b76b900

Browse files
author
brentru
committed
add example for using the pyportal's light sensor, and analog data readings
1 parent 186305d commit b76b900

File tree

1 file changed

+71
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)