Skip to content

Commit 4d3d566

Browse files
authored
Merge pull request adafruit#1 from adafruit/master
Merging from Adafruit
2 parents 35decf1 + 5cffd69 commit 4d3d566

File tree

94 files changed

+4270
-16
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+4270
-16
lines changed

.github/workflows/githubci.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ jobs:
1212
runs-on: ubuntu-latest
1313

1414
steps:
15-
- uses: actions/setup-python@v1
15+
- uses: actions/setup-python@v2
1616
with:
17-
python-version: '3.x'
17+
python-version: '3.8'
1818
- uses: actions/checkout@v2
1919
- uses: actions/checkout@v2
2020
with:

Adafruit_ESP32_Arduino_Demos/SerialESPPassthrough/SerialESPPassthrough.ino

+3-3
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,12 @@ unsigned long baud = 115200;
7575
#endif
7676

7777
#if defined(ADAFRUIT_PYPORTAL)
78-
#define NEOPIXEL_PIN 2
78+
#define PIN_NEOPIXEL 2
7979
#elif defined(ADAFRUIT_METRO_M4_AIRLIFT_LITE)
80-
#define NEOPIXEL_PIN 40
80+
#define PIN_NEOPIXEL 40
8181
#endif
8282

83-
Adafruit_NeoPixel pixel = Adafruit_NeoPixel(1, NEOPIXEL_PIN, NEO_GRB + NEO_KHZ800);
83+
Adafruit_NeoPixel pixel = Adafruit_NeoPixel(1, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800);
8484

8585
void setup() {
8686
Serial.begin(baud);

Adafruit_IO_Air_Quality/code.py

+190
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
import time
2+
import board
3+
import busio
4+
from digitalio import DigitalInOut
5+
import neopixel
6+
from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager
7+
from adafruit_io.adafruit_io import IO_HTTP
8+
from simpleio import map_range
9+
10+
import adafruit_pm25
11+
import adafruit_bme280
12+
13+
### Configure Sensor ###
14+
# Return BME280 environmental sensor readings in degrees Celsius
15+
USE_CELSIUS = False
16+
# Interval the sensor publishes to Adafruit IO, in minutes
17+
PUBLISH_INTERVAL = 10
18+
19+
### WiFi ###
20+
21+
# Get wifi details and more from a secrets.py file
22+
try:
23+
from secrets import secrets
24+
except ImportError:
25+
print("WiFi secrets are kept in secrets.py, please add them there!")
26+
raise
27+
28+
# AirLift FeatherWing
29+
esp32_cs = DigitalInOut(board.D13)
30+
esp32_reset = DigitalInOut(board.D12)
31+
esp32_ready = DigitalInOut(board.D11)
32+
33+
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
34+
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
35+
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)
36+
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
37+
38+
# Connect to a PM2.5 sensor over UART
39+
uart = busio.UART(board.TX, board.RX, baudrate=9600)
40+
pm25 = adafruit_pm25.PM25_UART(uart)
41+
42+
# Connect to a BME280 sensor over I2C
43+
i2c = busio.I2C(board.SCL, board.SDA)
44+
bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c)
45+
46+
### Sensor Functions ###
47+
def calculate_aqi(pm_sensor_reading):
48+
"""Returns a calculated air quality index (AQI)
49+
and category as a tuple.
50+
NOTE: The AQI returned by this function should ideally be measured
51+
using the 24-hour concentration average. Calculating a AQI without
52+
averaging will result in higher AQI values than expected.
53+
:param float pm_sensor_reading: Particulate matter sensor value.
54+
55+
"""
56+
# Check sensor reading using EPA breakpoint (Clow-Chigh)
57+
if 0.0 <= pm_sensor_reading <= 12.0:
58+
# AQI calculation using EPA breakpoints (Ilow-IHigh)
59+
aqi_val = map_range(int(pm_sensor_reading), 0, 12, 0, 50)
60+
aqi_cat = "Good"
61+
elif 12.1 <= pm_sensor_reading <= 35.4:
62+
aqi_val = map_range(int(pm_sensor_reading), 12, 35, 51, 100)
63+
aqi_cat = "Moderate"
64+
elif 35.5 <= pm_sensor_reading <= 55.4:
65+
aqi_val = map_range(int(pm_sensor_reading), 36, 55, 101, 150)
66+
aqi_cat = "Unhealthy for Sensitive Groups"
67+
elif 55.5 <= pm_sensor_reading <= 150.4:
68+
aqi_val = map_range(int(pm_sensor_reading), 56, 150, 151, 200)
69+
aqi_cat = "Unhealthy"
70+
elif 150.5 <= pm_sensor_reading <= 250.4:
71+
aqi_val = map_range(int(pm_sensor_reading), 151, 250, 201, 300)
72+
aqi_cat = "Very Unhealthy"
73+
elif 250.5 <= pm_sensor_reading <= 350.4:
74+
aqi_val = map_range(int(pm_sensor_reading), 251, 350, 301, 400)
75+
aqi_cat = "Hazardous"
76+
elif 350.5 <= pm_sensor_reading <= 500.4:
77+
aqi_val = map_range(int(pm_sensor_reading), 351, 500, 401, 500)
78+
aqi_cat = "Hazardous"
79+
else:
80+
print("Invalid PM2.5 concentration")
81+
aqi_val = -1
82+
aqi_cat = None
83+
return aqi_val, aqi_cat
84+
85+
86+
def sample_aq_sensor():
87+
"""Samples PM2.5 sensor
88+
over a 2.3 second sample rate.
89+
90+
"""
91+
aq_reading = 0
92+
aq_samples = []
93+
94+
# initial timestamp
95+
time_start = time.monotonic()
96+
# sample pm2.5 sensor over 2.3 sec sample rate
97+
while time.monotonic() - time_start <= 2.3:
98+
try:
99+
aqdata = pm25.read()
100+
aq_samples.append(aqdata["pm25 env"])
101+
except RuntimeError:
102+
print("Unable to read from sensor, retrying...")
103+
continue
104+
# pm sensor output rate of 1s
105+
time.sleep(1)
106+
# average sample reading / # samples
107+
for sample in range(len(aq_samples)):
108+
aq_reading += aq_samples[sample]
109+
aq_reading = aq_reading / len(aq_samples)
110+
aq_samples.clear()
111+
return aq_reading
112+
113+
114+
def read_bme280(is_celsius=False):
115+
"""Returns temperature and humidity
116+
from BME280 environmental sensor, as a tuple.
117+
118+
:param bool is_celsius: Returns temperature in degrees celsius
119+
if True, otherwise fahrenheit.
120+
"""
121+
humid = bme280.humidity
122+
temp = bme280.temperature
123+
if not is_celsius:
124+
temp = temp * 1.8 + 32
125+
return temperature, humid
126+
127+
128+
# Create an instance of the Adafruit IO HTTP client
129+
io = IO_HTTP(secrets["aio_user"], secrets["aio_key"], wifi)
130+
131+
# Describes feeds used to hold Adafruit IO data
132+
feed_aqi = io.get_feed("air-quality-sensor.aqi")
133+
feed_aqi_category = io.get_feed("air-quality-sensor.category")
134+
feed_humidity = io.get_feed("air-quality-sensor.humidity")
135+
feed_temperature = io.get_feed("air-quality-sensor.temperature")
136+
137+
# Set up location metadata from secrets.py file
138+
location_metadata = (secrets["latitude"], secrets["longitude"], secrets["elevation"])
139+
140+
elapsed_minutes = 0
141+
prv_mins = 0
142+
143+
while True:
144+
try:
145+
print("Fetching time...")
146+
cur_time = io.receive_time()
147+
print("Time fetched OK!")
148+
# Hourly reset
149+
if cur_time.tm_min == 0:
150+
prv_mins = 0
151+
except (ValueError, RuntimeError) as e:
152+
print("Failed to fetch time, retrying\n", e)
153+
wifi.reset()
154+
wifi.connect()
155+
continue
156+
157+
if cur_time.tm_min >= prv_mins:
158+
print("%d min elapsed.." % elapsed_minutes)
159+
prv_mins = cur_time.tm_min
160+
elapsed_minutes += 1
161+
162+
if elapsed_minutes >= PUBLISH_INTERVAL:
163+
print("Sampling AQI...")
164+
aqi_reading = sample_aq_sensor()
165+
aqi, aqi_category = calculate_aqi(aqi_reading)
166+
print("AQI: %d" % aqi)
167+
print("Category: %s" % aqi_category)
168+
169+
# temp and humidity
170+
print("Sampling environmental sensor...")
171+
temperature, humidity = read_bme280(USE_CELSIUS)
172+
print("Temperature: %0.1f F" % temperature)
173+
print("Humidity: %0.1f %%" % humidity)
174+
175+
# Publish all values to Adafruit IO
176+
print("Publishing to Adafruit IO...")
177+
try:
178+
io.send_data(feed_aqi["key"], str(aqi), location_metadata)
179+
io.send_data(feed_aqi_category["key"], aqi_category)
180+
io.send_data(feed_temperature["key"], str(temperature))
181+
io.send_data(feed_humidity["key"], str(humidity))
182+
print("Published!")
183+
except (ValueError, RuntimeError) as e:
184+
print("Failed to send data to IO, retrying\n", e)
185+
wifi.reset()
186+
wifi.connect()
187+
continue
188+
# Reset timer
189+
elapsed_minutes = 0
190+
time.sleep(30)

BLE_Candy_Claw/code.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import time
2+
import board
3+
import pulseio
4+
from adafruit_motor import servo
5+
from adafruit_bluefruit_connect.packet import Packet
6+
from adafruit_bluefruit_connect.button_packet import ButtonPacket
7+
from adafruit_ble import BLERadio
8+
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
9+
from adafruit_ble.services.nordic import UARTService
10+
11+
ble = BLERadio()
12+
uart = UARTService()
13+
advertisement = ProvideServicesAdvertisement(uart)
14+
15+
pwm = pulseio.PWMOut(board.A3, duty_cycle=2 ** 15, frequency=50)
16+
17+
my_servo = servo.Servo(pwm)
18+
19+
while True:
20+
print("WAITING...")
21+
# Advertise when not connected.
22+
ble.start_advertising(advertisement)
23+
while not ble.connected:
24+
pass
25+
26+
# Connected
27+
ble.stop_advertising()
28+
print("CONNECTED")
29+
30+
# Loop and read packets
31+
while ble.connected:
32+
if uart.in_waiting:
33+
packet = Packet.from_stream(uart)
34+
if isinstance(packet, ButtonPacket):
35+
# if buttons in the app are pressed
36+
if packet.pressed:
37+
if packet.button == ButtonPacket.DOWN:
38+
print("pressed down")
39+
for angle in range(90, 170, 90): # 90 - 170 degrees, 90 degrees at a time.
40+
my_servo.angle = angle
41+
time.sleep(0.05)
42+
if packet.button == ButtonPacket.UP:
43+
print("pressed up")
44+
for angle in range(170, 90, -90): # 170 - 90 degrees, 9 degrees at a time.
45+
my_servo.angle = angle
46+
time.sleep(0.05)
47+
# Disconnected
48+
print("DISCONNECTED")

BLE_Host_Computer/ble_eval_client.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Connect to an "eval()" service over BLE UART.
2+
3+
from adafruit_ble import BLERadio
4+
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
5+
from adafruit_ble.services.nordic import UARTService
6+
7+
ble = BLERadio()
8+
9+
uart_connection = None
10+
11+
while True:
12+
if not uart_connection:
13+
print("Trying to connect...")
14+
for adv in ble.start_scan(ProvideServicesAdvertisement):
15+
if UARTService in adv.services:
16+
uart_connection = ble.connect(adv)
17+
print("Connected")
18+
break
19+
ble.stop_scan()
20+
21+
if uart_connection and uart_connection.connected:
22+
uart_service = uart_connection[UARTService]
23+
while uart_connection.connected:
24+
s = input("Eval: ")
25+
uart_service.write(s.encode("utf-8"))
26+
uart_service.write(b'\n')
27+
print(uart_service.readline().decode("utf-8"))

BLE_Host_Computer/ble_eval_server.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Provide an "eval()" service over BLE UART.
2+
3+
from adafruit_ble import BLERadio
4+
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
5+
from adafruit_ble.services.nordic import UARTService
6+
7+
ble = BLERadio()
8+
uart = UARTService()
9+
advertisement = ProvideServicesAdvertisement(uart)
10+
11+
while True:
12+
ble.start_advertising(advertisement)
13+
print("Waiting to connect")
14+
while not ble.connected:
15+
pass
16+
print("Connected")
17+
while ble.connected:
18+
s = uart.readline()
19+
if s:
20+
try:
21+
result = str(eval(s))
22+
except Exception as e:
23+
result = repr(e)
24+
uart.write(result.encode("utf-8"))

BME680_BSEC_OLED_Demo/.trinket_m0.test.only

Whitespace-only changes.

0 commit comments

Comments
 (0)