Skip to content

Commit 0350cc8

Browse files
authored
Add environmental monitor example (#68)
1 parent 94ea0f0 commit 0350cc8

File tree

1 file changed

+134
-0
lines changed

1 file changed

+134
-0
lines changed

Diff for: examples/basics/environmental_monitor.py

+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
"""
2+
'environmental_monitor.py'
3+
===============================================================================
4+
Example of sending I2C sensor data
5+
from multiple sensors to Adafruit IO.
6+
7+
Tutorial Link: https://learn.adafruit.com/adafruit-io-air-quality-monitor
8+
9+
Adafruit invests time and resources providing this open source code.
10+
Please support Adafruit and open source hardware by purchasing
11+
products from Adafruit!
12+
13+
Author(s): Brent Rubell for Adafruit Industries
14+
Copyright (c) 2018 Adafruit Industries
15+
Licensed under the MIT license.
16+
All text above must be included in any redistribution.
17+
18+
Dependencies:
19+
- Adafruit_Blinka (CircuitPython, on Pi.)
20+
(https://github.com/adafruit/Adafruit_Blinka)
21+
- Adafruit_CircuitPython_SGP30.
22+
(https://github.com/adafruit/Adafruit_CircuitPython_SGP30)
23+
- Adafruit_CircuitPython_VEML6070.
24+
(https://github.com/adafruit/Adafruit_CircuitPython_VEML6070)
25+
- Adafruit_CircuitPython_BME280.
26+
(https://github.com/adafruit/Adafruit_CircuitPython_BME280)
27+
"""
28+
29+
# Import standard python modules
30+
import time
31+
32+
# import Adafruit Blinka
33+
import board
34+
import busio
35+
36+
# import sensor libraries
37+
import adafruit_sgp30
38+
import adafruit_veml6070
39+
import adafruit_bme280
40+
41+
# import Adafruit IO REST client
42+
from Adafruit_IO import Client, Feed, RequestError
43+
44+
# loop timeout, in seconds.
45+
LOOP_DELAY = 10
46+
47+
# Set to your Adafruit IO key.
48+
# Remember, your key is a secret,
49+
# so make sure not to publish it when you publish this code!
50+
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'
51+
52+
# Set to your Adafruit IO username.
53+
# (go to https://accounts.adafruit.com to find your username)
54+
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
55+
56+
# Create an instance of the REST client
57+
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
58+
59+
try: # if we already have the feeds, assign them.
60+
tvoc_feed = aio.feeds('tvoc')
61+
eCO2_feed = aio.feeds('eco2')
62+
uv_feed = aio.feeds('uv')
63+
temperature_feed = aio.feeds('temperature')
64+
humidity_feed = aio.feeds('humidity')
65+
pressure_feed = aio.feeds('pressure')
66+
altitude_feed = aio.feeds('altitude')
67+
except RequestError: # if we don't, create and assign them.
68+
tvoc_feed = aio.create_feed(Feed(name='tvoc'))
69+
eCO2_feed = aio.create_feed(Feed(name='eco2'))
70+
uv_feed = aio.create_feed(Feed(name='uv'))
71+
temperature_feed = aio.create_feed(Feed(name='temperature'))
72+
humidity_feed = aio.create_feed(Feed(name='humidity'))
73+
pressure_feed = aio.create_feed(Feed(name='pressure'))
74+
altitude_feed = aio.create_feed(Feed(name='altitude'))
75+
76+
# Create busio I2C
77+
i2c = busio.I2C(board.SCL, board.SDA, frequency=100000)
78+
# Create VEML6070 object.
79+
uv = adafruit_veml6070.VEML6070(i2c)
80+
# Create BME280 object.
81+
bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c)
82+
bme280.sea_level_pressure = 1013.25
83+
# Create SGP30 object using I2C.
84+
sgp30 = adafruit_sgp30.Adafruit_SGP30(i2c)
85+
sgp30.iaq_init()
86+
sgp30.set_iaq_baseline(0x8973, 0x8aae)
87+
88+
# Sample VEML6070
89+
def sample_VEML():
90+
for j in range(10):
91+
uv_raw = uv.read
92+
return uv_raw
93+
94+
95+
while True:
96+
print('Reading sensors...')
97+
# Read SGP30.
98+
eCO2_data = sgp30.co2eq
99+
tvoc_data = sgp30.tvoc
100+
101+
# Read VEML6070.
102+
uv_data = sample_VEML()
103+
104+
# Read BME280.
105+
temp_data = bme280.temperature
106+
# convert temperature (C->F)
107+
temp_data = int(temp_data) * 1.8 + 32
108+
humid_data = bme280.humidity
109+
pressure_data = bme280.pressure
110+
alt_data = bme280.altitude
111+
112+
print('sending data to adafruit io...')
113+
# Send SGP30 Data to Adafruit IO.
114+
print('eCO2:', eCO2_data)
115+
aio.send(eCO2_feed.key, eCO2_data)
116+
print('tvoc:', tvoc_data)
117+
aio.send(tvoc_feed.key, tvoc_data)
118+
time.sleep(2)
119+
# Send VEML6070 Data to Adafruit IO.
120+
print('UV Level: ', uv_data)
121+
aio.send(uv_feed.key, uv_data)
122+
time.sleep(2)
123+
# Send BME280 Data to Adafruit IO.
124+
print('Temperature: %0.1f C' % temp_data)
125+
aio.send(temperature_feed.key, temp_data)
126+
print("Humidity: %0.1f %%" % humid_data)
127+
aio.send(humidity_feed.key, int(humid_data))
128+
time.sleep(2)
129+
print("Pressure: %0.1f hPa" % pressure_data)
130+
aio.send(pressure_feed.key, int(pressure_data))
131+
print("Altitude = %0.2f meters" % alt_data)
132+
aio.send(altitude_feed.key, int(alt_data))
133+
# avoid timeout from adafruit io
134+
time.sleep(LOOP_DELAY * 60)

0 commit comments

Comments
 (0)