Skip to content

Commit cbf40ab

Browse files
authored
Merge pull request #1931 from kattni/feather-esp32-s2
Add Feather ESP32-S2 examples.
2 parents c1a45d4 + 32d88ff commit cbf40ab

10 files changed

Lines changed: 314 additions & 1 deletion

File tree

.pylintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ confidence=
5252
# no Warning level messages displayed, use"--disable=all --enable=classes
5353
# --disable=W"
5454
# disable=import-error,print-statement,parameter-unpacking,unpacking-in-except,old-raise-syntax,backtick,long-suffix,old-ne-operator,old-octal-literal,import-star-module-level,raw-checker-failed,bad-inline-option,locally-disabled,locally-enabled,file-ignored,suppressed-message,useless-suppression,deprecated-pragma,apply-builtin,basestring-builtin,buffer-builtin,cmp-builtin,coerce-builtin,execfile-builtin,file-builtin,long-builtin,raw_input-builtin,reduce-builtin,standarderror-builtin,unicode-builtin,xrange-builtin,coerce-method,delslice-method,getslice-method,setslice-method,no-absolute-import,old-division,dict-iter-method,dict-view-method,next-method-called,metaclass-assignment,indexing-exception,raising-string,reload-builtin,oct-method,hex-method,nonzero-method,cmp-method,input-builtin,round-builtin,intern-builtin,unichr-builtin,map-builtin-not-iterating,zip-builtin-not-iterating,range-builtin-not-iterating,filter-builtin-not-iterating,using-cmp-argument,eq-without-hash,div-method,idiv-method,rdiv-method,exception-message-attribute,invalid-str-codec,sys-max-int,bad-python3-import,deprecated-string-function,deprecated-str-translate-call
55-
disable=too-many-instance-attributes,len-as-condition,too-few-public-methods,anomalous-backslash-in-string,no-else-return,simplifiable-if-statement,too-many-arguments,duplicate-code,no-name-in-module,no-member,print-statement,parameter-unpacking,unpacking-in-except,old-raise-syntax,backtick,long-suffix,old-ne-operator,old-octal-literal,import-star-module-level,raw-checker-failed,bad-inline-option,locally-disabled,locally-enabled,file-ignored,suppressed-message,useless-suppression,deprecated-pragma,apply-builtin,basestring-builtin,buffer-builtin,cmp-builtin,coerce-builtin,execfile-builtin,file-builtin,long-builtin,raw_input-builtin,reduce-builtin,standarderror-builtin,unicode-builtin,xrange-builtin,coerce-method,delslice-method,getslice-method,setslice-method,no-absolute-import,old-division,dict-iter-method,dict-view-method,next-method-called,metaclass-assignment,indexing-exception,raising-string,reload-builtin,oct-method,hex-method,nonzero-method,cmp-method,input-builtin,round-builtin,intern-builtin,unichr-builtin,map-builtin-not-iterating,zip-builtin-not-iterating,range-builtin-not-iterating,filter-builtin-not-iterating,using-cmp-argument,eq-without-hash,div-method,idiv-method,rdiv-method,exception-message-attribute,invalid-str-codec,sys-max-int,bad-python3-import,deprecated-string-function,deprecated-str-translate-call,import-error,missing-docstring,invalid-name,bad-whitespace,consider-using-enumerate,unexpected-keyword-arg,consider-using-f-string
55+
disable=too-many-instance-attributes,len-as-condition,too-few-public-methods,anomalous-backslash-in-string,no-else-return,simplifiable-if-statement,too-many-arguments,duplicate-code,no-name-in-module,no-member,print-statement,parameter-unpacking,unpacking-in-except,old-raise-syntax,backtick,long-suffix,old-ne-operator,old-octal-literal,import-star-module-level,raw-checker-failed,bad-inline-option,locally-disabled,locally-enabled,file-ignored,suppressed-message,useless-suppression,deprecated-pragma,apply-builtin,basestring-builtin,buffer-builtin,cmp-builtin,coerce-builtin,execfile-builtin,file-builtin,long-builtin,raw_input-builtin,reduce-builtin,standarderror-builtin,unicode-builtin,xrange-builtin,coerce-method,delslice-method,getslice-method,setslice-method,no-absolute-import,old-division,dict-iter-method,dict-view-method,next-method-called,metaclass-assignment,indexing-exception,raising-string,reload-builtin,oct-method,hex-method,nonzero-method,cmp-method,input-builtin,round-builtin,intern-builtin,unichr-builtin,map-builtin-not-iterating,zip-builtin-not-iterating,range-builtin-not-iterating,filter-builtin-not-iterating,using-cmp-argument,eq-without-hash,div-method,idiv-method,rdiv-method,exception-message-attribute,invalid-str-codec,sys-max-int,bad-python3-import,deprecated-string-function,deprecated-str-translate-call,import-error,missing-docstring,invalid-name,bad-whitespace,consider-using-enumerate,unexpected-keyword-arg,consider-using-f-string,unspecified-encoding
5656

5757
# Enable the message, report, category or checker with the given id(s). You can
5858
# either give multiple identifier separated by comma (,) or put this option
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
2+
# SPDX-License-Identifier: Unlicense
3+
"""
4+
CircuitPython analog voltage value example
5+
"""
6+
import time
7+
import board
8+
import analogio
9+
10+
analog_pin = analogio.AnalogIn(board.A0)
11+
12+
13+
def get_voltage(pin):
14+
return (pin.value * 2.6) / 51375
15+
16+
17+
while True:
18+
print(get_voltage(analog_pin))
19+
time.sleep(0.1)
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
2+
# SPDX-License-Identifier: Unlicense
3+
"""
4+
CircuitPython Adafruit IO Example for BME280 and LC709203 Sensors
5+
"""
6+
import time
7+
import ssl
8+
import alarm
9+
import board
10+
import digitalio
11+
import wifi
12+
import socketpool
13+
import adafruit_requests
14+
from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError
15+
from adafruit_lc709203f import LC709203F, PackSize
16+
from adafruit_bme280 import basic as adafruit_bme280
17+
try:
18+
from secrets import secrets
19+
except ImportError:
20+
print("WiFi and Adafruit IO credentials are kept in secrets.py, please add them there!")
21+
raise
22+
23+
# Duration of sleep in seconds. Default is 600 seconds (10 minutes).
24+
# Feather will sleep for this duration between sensor readings / sending data to AdafruitIO
25+
sleep_duration = 600
26+
27+
# Update to match the mAh of your battery for more accurate readings.
28+
# Can be MAH100, MAH200, MAH400, MAH500, MAH1000, MAH2000, MAH3000.
29+
# Choose the closest match. Include "PackSize." before it, as shown.
30+
battery_pack_size = PackSize.MAH400
31+
32+
# Setup the little red LED
33+
led = digitalio.DigitalInOut(board.LED)
34+
led.switch_to_output()
35+
36+
# Pull the I2C power pin low
37+
i2c_power = digitalio.DigitalInOut(board.I2C_POWER_INVERTED)
38+
i2c_power.switch_to_output()
39+
i2c_power.value = False
40+
41+
# Set up the BME280 and LC709203 sensors
42+
bme280 = adafruit_bme280.Adafruit_BME280_I2C(board.I2C())
43+
battery_monitor = LC709203F(board.I2C())
44+
battery_monitor.pack_size = battery_pack_size
45+
46+
# Collect the sensor data values and format the data
47+
temperature = "{:.2f}".format(bme280.temperature)
48+
temperature_f = "{:.2f}".format((bme280.temperature * (9 / 5) + 32)) # Convert C to F
49+
humidity = "{:.2f}".format(bme280.relative_humidity)
50+
pressure = "{:.2f}".format(bme280.pressure)
51+
battery_voltage = "{:.2f}".format(battery_monitor.cell_voltage)
52+
battery_percent = "{:.1f}".format(battery_monitor.cell_percent)
53+
54+
55+
def go_to_sleep(sleep_period):
56+
# Create a an alarm that will trigger sleep_period number of seconds from now.
57+
time_alarm = alarm.time.TimeAlarm(monotonic_time=time.monotonic() + sleep_period)
58+
# Exit and deep sleep until the alarm wakes us.
59+
alarm.exit_and_deep_sleep_until_alarms(time_alarm)
60+
61+
62+
# Fetch the feed of the provided name. If the feed does not exist, create it.
63+
def setup_feed(feed_name):
64+
try:
65+
# Get the feed of provided feed_name from Adafruit IO
66+
return io.get_feed(feed_name)
67+
except AdafruitIO_RequestError:
68+
# If no feed of that name exists, create it
69+
return io.create_new_feed(feed_name)
70+
71+
72+
# Send the data. Requires a feed name and a value to send.
73+
def send_io_data(feed, value):
74+
return io.send_data(feed["key"], value)
75+
76+
77+
# Wi-Fi connections can have issues! This ensures the code will continue to run.
78+
try:
79+
# Connect to Wi-Fi
80+
wifi.radio.connect(secrets["ssid"], secrets["password"])
81+
print("Connected to {}!".format(secrets["ssid"]))
82+
print("IP:", wifi.radio.ipv4_address)
83+
84+
pool = socketpool.SocketPool(wifi.radio)
85+
requests = adafruit_requests.Session(pool, ssl.create_default_context())
86+
87+
# Wi-Fi connectivity fails with error messages, not specific errors, so this except is broad.
88+
except Exception as e: # pylint: disable=broad-except
89+
print(e)
90+
go_to_sleep(60)
91+
92+
# Set your Adafruit IO Username and Key in secrets.py
93+
# (visit io.adafruit.com if you need to create an account,
94+
# or if you need your Adafruit IO key.)
95+
aio_username = secrets["aio_username"]
96+
aio_key = secrets["aio_key"]
97+
98+
# Initialize an Adafruit IO HTTP API object
99+
io = IO_HTTP(aio_username, aio_key, requests)
100+
101+
# Turn on the LED to indicate data is being sent.
102+
led.value = True
103+
# Print data values to the serial console. Not necessary for Adafruit IO.
104+
print("Current BME280 temperature: {0} C".format(temperature))
105+
print("Current BME280 temperature: {0} F".format(temperature_f))
106+
print("Current BME280 humidity: {0} %".format(humidity))
107+
print("Current BME280 pressure: {0} hPa".format(pressure))
108+
print("Current battery voltage: {0} V".format(battery_voltage))
109+
print("Current battery percent: {0} %".format(battery_percent))
110+
111+
# Adafruit IO sending can run into issues if the network fails!
112+
# This ensures the code will continue to run.
113+
try:
114+
print("Sending data to AdafruitIO...")
115+
# Send data to Adafruit IO
116+
send_io_data(setup_feed("bme280-temperature"), temperature)
117+
send_io_data(setup_feed("bme280-temperature-f"), temperature_f)
118+
send_io_data(setup_feed("bme280-humidity"), humidity)
119+
send_io_data(setup_feed("bme280-pressure"), pressure)
120+
send_io_data(setup_feed("battery-voltage"), battery_voltage)
121+
send_io_data(setup_feed("battery-percent"), battery_percent)
122+
print("Data sent!")
123+
# Turn off the LED to indicate data sending is complete.
124+
led.value = False
125+
126+
# Adafruit IO can fail with multiple errors depending on the situation, so this except is broad.
127+
except Exception as e: # pylint: disable=broad-except
128+
print(e)
129+
go_to_sleep(60)
130+
131+
go_to_sleep(sleep_duration)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
2+
# SPDX-License-Identifier: Unlicense
3+
"""
4+
CircuitPython Simple Example for BME280 and LC709203 Sensors
5+
"""
6+
import time
7+
import board
8+
import digitalio
9+
from adafruit_bme280 import basic as adafruit_bme280
10+
from adafruit_lc709203f import LC709203F, PackSize
11+
12+
# Pull the I2C power pin low
13+
i2c_power = digitalio.DigitalInOut(board.I2C_POWER_INVERTED)
14+
i2c_power.switch_to_output()
15+
i2c_power.value = False
16+
17+
# Create sensor objects, using the board's default I2C bus.
18+
i2c = board.I2C()
19+
bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c)
20+
battery_monitor = LC709203F(board.I2C())
21+
battery_monitor.pack_size = PackSize.MAH400
22+
23+
# change this to match your location's pressure (hPa) at sea level
24+
bme280.sea_level_pressure = 1013.25
25+
26+
while True:
27+
print("\nTemperature: {:.1f} C".format(bme280.temperature))
28+
print("Humidity: {:.1f} %".format(bme280.relative_humidity))
29+
print("Pressure: {:.1f} hPa".format(bme280.pressure))
30+
print("Altitude: {:.2f} meters".format(bme280.altitude))
31+
print("Battery Percent: {:.2f} %".format(battery_monitor.cell_percent))
32+
print("Battery Voltage: {:.2f} V".format(battery_monitor.cell_voltage))
33+
time.sleep(2)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
2+
# SPDX-License-Identifier: Unlicense
3+
"""
4+
CircuitPython Capacitive Touch Pin Example - Print to the serial console when one pin is touched.
5+
"""
6+
import time
7+
import board
8+
import touchio
9+
10+
touch = touchio.TouchIn(board.A4)
11+
12+
while True:
13+
if touch.value:
14+
print("Pin touched!")
15+
time.sleep(0.1)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
2+
# SPDX-License-Identifier: Unlicense
3+
"""
4+
CircuitPython Capacitive Two Touch Pin Example - Print to the serial console when a pin is touched.
5+
"""
6+
import time
7+
import board
8+
import touchio
9+
10+
touch_one = touchio.TouchIn(board.A4)
11+
touch_two = touchio.TouchIn(board.A5)
12+
13+
while True:
14+
if touch_one.value:
15+
print("Pin one touched!")
16+
if touch_two.value:
17+
print("Pin two touched!")
18+
time.sleep(0.1)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""
2+
CircuitPython Digital Input Example - Blinking an LED using a button switch.
3+
"""
4+
import board
5+
import digitalio
6+
7+
led = digitalio.DigitalInOut(board.LED)
8+
led.direction = digitalio.Direction.OUTPUT
9+
10+
button = digitalio.DigitalInOut(board.D5)
11+
button.switch_to_input(pull=digitalio.Pull.UP)
12+
13+
while True:
14+
if not button.value:
15+
led.value = True
16+
else:
17+
led.value = False
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
2+
# SPDX-License-Identifier: Unlicense
3+
"""
4+
CircuitPython Essentials Storage CP Filesystem boot.py file
5+
"""
6+
import board
7+
import digitalio
8+
import storage
9+
10+
pin = digitalio.DigitalInOut(board.A0)
11+
pin.switch_to_input(pull=digitalio.Pull.UP)
12+
13+
# If the pin is connected to ground, the filesystem is writable by CircuitPython
14+
storage.remount("/", readonly=pin.value)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
2+
# SPDX-License-Identifier: Unlicense
3+
"""
4+
CircuitPython Essentials Storage CP Filesystem code.py file
5+
"""
6+
import time
7+
import board
8+
import digitalio
9+
import microcontroller
10+
11+
led = digitalio.DigitalInOut(board.LED)
12+
led.switch_to_output()
13+
14+
try:
15+
with open("/temperature.txt", "a") as temp_log:
16+
while True:
17+
# The microcontroller temperature in Celsius. Include the
18+
# math to do the C to F conversion here, if desired.
19+
temperature = microcontroller.cpu.temperature
20+
21+
# Write the temperature to the temperature.txt file every 10 seconds.
22+
temp_log.write('{0:.2f}\n'.format(temperature))
23+
temp_log.flush()
24+
25+
# Blink the LED on every write...
26+
led.value = True
27+
time.sleep(1) # ...for one second.
28+
led.value = False # Then turn it off...
29+
time.sleep(9) # ...for the other 9 seconds.
30+
31+
except OSError as e: # When the filesystem is NOT writable by CircuitPython...
32+
delay = 0.5 # ...blink the LED every half second.
33+
if e.args[0] == 28: # If the file system is full...
34+
delay = 0.15 # ...blink the LED every 0.15 seconds!
35+
while True:
36+
led.value = not led.value
37+
time.sleep(delay)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
2+
# SPDX-License-Identifier: Unlicense
3+
"""
4+
CircuitPython analog voltage value example
5+
6+
REMOVE THIS TEXT AND ALL THE FOLLOWING, AND THE PYLINT DISABLE COMMENT, BEFORE SUBMITTING TO LEARN:
7+
Update VOLTAGE to the max voltage returned by the board you're using.
8+
Update VALUE to the max analog pin value returned by the board you're using.
9+
10+
For example:
11+
If you are using Feather ESP32-S2, update VOLTAGE to 2.6 and VALUE to 51375.
12+
13+
Remove the #pylint: disable=undefined-variable before submitting to Learn. It is unnecessary
14+
once the VOLTAGE and VALUE are updated to valid numbers.
15+
"""
16+
import time
17+
import board
18+
import analogio
19+
20+
analog_pin = analogio.AnalogIn(board.A0)
21+
22+
23+
def get_voltage(pin):
24+
return (pin.value * VOLTAGE) / VALUE # pylint: disable=undefined-variable
25+
26+
27+
while True:
28+
print(get_voltage(analog_pin))
29+
time.sleep(0.1)

0 commit comments

Comments
 (0)