-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.py
More file actions
executable file
·204 lines (176 loc) · 7.2 KB
/
code.py
File metadata and controls
executable file
·204 lines (176 loc) · 7.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# Circuit Python 6.3.0
# -*- coding: utf-8 -*-
##################################################
# Temp/Humidity and Pollution Sensor
##################################################
# MIT License Copyright (c) 2022
##################################################
# Author: Daniel Cuthbert (@dcuthbert)
# Version: 1.0
##################################################
import board
import busio
import time
from adafruit_bme280 import basic as adafruit_bme280
from digitalio import DigitalInOut
from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager
from adafruit_io.adafruit_io import IO_HTTP
from simpleio import map_range
from adafruit_pm25.uart import PM25_UART
from analogio import AnalogIn
### Wifi & Configuration Stuffs ###
# We keep a number of important secrets and configurations in a secrets.py file
try:
from secrets import secrets
except ImportError:
print("Oh dears, the setec astronomy file isn't there.")
raise
# We don't need to hammer Adafruit IO, so can get away with every 10 minutes
PUBLISH_INTERVAL = 10
# This assumes you are using the AirLift FeatherWing. Change accordingly
# We will be using SPI and UART
esp32_cs = DigitalInOut(board.D13)
esp32_reset = DigitalInOut(board.D12)
esp32_ready = DigitalInOut(board.D11)
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets,)
# Let's check the battery level
# if on lipo, the batteries are as follows:
# 4.2V max
# 3.7V healthy
# 3.2V trouble ahead
vbat_voltage = AnalogIn(board.VOLTAGE_MONITOR)
def get_voltage(pin):
return (pin.value * 3.3) / 65536 * 2
battery_voltage = get_voltage(vbat_voltage)
print("Starting up the sensor and the battery voltage is: {:.2f}V".format(battery_voltage))
# We are using the PM2.5 sensor over UART, so lets get that ready to sample
reset_pin = None
uart = busio.UART(board.TX, board.RX, baudrate=9600)
pm25 = PM25_UART(uart, reset_pin)
# Create i2c object for the BME280 temp/humidity sensor
i2c = board.I2C()
bme_sensor = adafruit_bme280.Adafruit_BME280_I2C(i2c)
### Sensor Functions ###
# Returns a calculated air quality index (AQI) and category as a tuple (multiple items in a single variable)
# We ideally need a 24-hour concentration average as if you don't, you'll have a higher AQI than you expect
def calculate_aqi(pm_sensor_reading):
# Check sensor reading using EPA breakpoint (Clow-Chigh)
if 0.0 <= pm_sensor_reading <= 12.0:
# AQI calculation using EPA breakpoints (Ilow-IHigh)
aqi_val = map_range(int(pm_sensor_reading), 0, 12, 0, 50)
aqi_cat = "Good"
elif 12.1 <= pm_sensor_reading <= 35.4:
aqi_val = map_range(int(pm_sensor_reading), 12, 35, 51, 100)
aqi_cat = "Moderate"
elif 35.5 <= pm_sensor_reading <= 55.4:
aqi_val = map_range(int(pm_sensor_reading), 36, 55, 101, 150)
aqi_cat = "Unhealthy for some "
elif 55.5 <= pm_sensor_reading <= 150.4:
aqi_val = map_range(int(pm_sensor_reading), 56, 150, 151, 200)
aqi_cat = "Err, unhealthy"
elif 150.5 <= pm_sensor_reading <= 250.4:
aqi_val = map_range(int(pm_sensor_reading), 151, 250, 201, 300)
aqi_cat = "This isn't good"
elif 250.5 <= pm_sensor_reading <= 350.4:
aqi_val = map_range(int(pm_sensor_reading), 251, 350, 301, 400)
aqi_cat = "Run Daniel Run"
elif 350.5 <= pm_sensor_reading <= 500.4:
aqi_val = map_range(int(pm_sensor_reading), 351, 500, 401, 500)
aqi_cat = "Need a mask"
else:
print("Invalid PM2.5 concentration")
aqi_val = -1
aqi_cat = None
return aqi_val, aqi_cat
# Let's take a sample from the PM2.5 sensor over a 2.3 second sample rate
def sample_aq_sensor():
aq_reading = 0
aq_samples = []
time_start = time.monotonic()
while time.monotonic() - time_start <= 2.3:
try:
aqdata = pm25.read()
aq_samples.append(aqdata["pm25 env"])
except RuntimeError:
print("Unable to read from sensor, retrying...")
continue
# pm sensor output rate of 1s
time.sleep(1)
# average sample reading / # samples
for sample in range(len(aq_samples)):
aq_reading += aq_samples[sample]
aq_reading = aq_reading / len(aq_samples)
aq_samples.clear()
return aq_reading
# Take a reading from the BME280 temp/pressure/humidity sensor
def read_bme():
humid = bme_sensor.humidity
temp = bme_sensor.temperature
return temp, humid
# Now we have the data from the sensors, we need to do something with it.
# We are using the brilliant Adafruit.io site and client
io = IO_HTTP(secrets["aio_username"], secrets["aio_key"], wifi)
# Once you've created your feeds, you need to list the names here
# You can see all the feed addresses over at https://io.adafruit.com/USERNAME/feeds
feed_aqi = io.get_feed("air-quality-sensor.aqi")
feed_aqi_category = io.get_feed("air-quality-sensor.category")
feed_humidity = io.get_feed("air-quality-sensor.humidity")
feed_temperature = io.get_feed("air-quality-sensor.temperature")
feed_battery = io.get_feed("air-quality-sensor.battery")
# Set up location metadata from secrets.py file
location_metadata = {
"lat": secrets["latitude"],
"lon": secrets["longitude"],
"ele": secrets["elevation"],
}
elapsed_minutes = 0
prv_mins = 0
while True:
try:
print("Fetching time...")
cur_time = io.receive_time()
print("Time fetched OK!")
# Hourly reset
if cur_time.tm_min == 0:
prv_mins = 0
except (ValueError, RuntimeError) as e:
print("Failed to fetch time, retrying\n", e)
wifi.reset()
wifi.connect()
continue
if cur_time.tm_min >= prv_mins:
print("%d min elapsed.." % elapsed_minutes)
prv_mins = cur_time.tm_min
elapsed_minutes += 1
if elapsed_minutes >= PUBLISH_INTERVAL:
print("The current battery voltage is: {:.2f}".format(battery_voltage))
print("Reading the PM25 sensor ...")
aqi_reading = sample_aq_sensor()
aqi, aqi_category = calculate_aqi(aqi_reading)
print("AQI: %d" % aqi)
print("Category: %s" % aqi_category)
# temp and humidity
print("Reading the environmental sensor...")
temperature, humidity = read_bme()
print("Temperature: %0.1f C" % temperature)
print("Humidity: %0.1f %%" % humidity)
# Once we have the values, push them up
# we are adding the geolocaton to the aqi feed so add it there as a seperate box
print("Publishing to Adafruit IO...")
try:
io.send_data(feed_aqi["key"], str(aqi), location_metadata)
io.send_data(feed_aqi_category["key"], aqi_category)
io.send_data(feed_temperature["key"], str(temperature))
io.send_data(feed_humidity["key"], str(humidity))
io.send_data(feed_battery["key"], str(battery_voltage))
print("Much success, published!")
except (ValueError, RuntimeError) as e:
print("Failed to send data to IO, retrying\n", e)
wifi.reset()
wifi.connect()
continue
# Reset timer
elapsed_minutes = 0
time.sleep(30)