Skip to content

Commit 3da3957

Browse files
committed
add monitor code
1 parent a544133 commit 3da3957

1 file changed

Lines changed: 112 additions & 0 deletions

File tree

AIO_Environmental_Monitor/code.py

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# SPDX-FileCopyrightText: 2018 Brent Rubell for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
#
5+
# Adafruit IO Environmental Monitor for Feather or Raspberry Pi -
6+
# an internet-enabled environmental monitor
7+
8+
# Import standard python modules
9+
import time
10+
11+
# import Adafruit Blinka
12+
import board
13+
import busio
14+
15+
# import CircuitPython sensor libraries
16+
import adafruit_sgp30
17+
import adafruit_veml6070
18+
from adafruit_bme280 import basic as adafruit_bme280
19+
20+
# import Adafruit IO REST client
21+
from Adafruit_IO import Client, Feed, RequestError
22+
23+
# loop timeout, in seconds.
24+
LOOP_DELAY = 10
25+
26+
# Set to your Adafruit IO key.
27+
# Remember, your key is a secret,
28+
# so make sure not to publish it when you publish this code!
29+
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'
30+
31+
# Set to your Adafruit IO username.
32+
# (go to https://accounts.adafruit.com to find your username)
33+
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
34+
35+
# Create an instance of the REST client
36+
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
37+
38+
try: # if we already have the feeds, assign them.
39+
tvoc_feed = aio.feeds('tvoc')
40+
eCO2_feed = aio.feeds('eco2')
41+
uv_feed = aio.feeds('uv')
42+
temperature_feed = aio.feeds('temperature')
43+
humidity_feed = aio.feeds('humidity')
44+
pressure_feed = aio.feeds('pressure')
45+
altitude_feed = aio.feeds('altitude')
46+
except RequestError: # if we don't, create and assign them.
47+
tvoc_feed = aio.create_feed(Feed(name='tvoc'))
48+
eCO2_feed = aio.create_feed(Feed(name='eco2'))
49+
uv_feed = aio.create_feed(Feed(name='uv'))
50+
temperature_feed = aio.create_feed(Feed(name='temperature'))
51+
humidity_feed = aio.create_feed(Feed(name='humidity'))
52+
pressure_feed = aio.create_feed(Feed(name='pressure'))
53+
altitude_feed = aio.create_feed(Feed(name='altitude'))
54+
55+
# Create busio I2C
56+
i2c = busio.I2C(board.SCL, board.SDA, frequency=100000)
57+
# Create VEML6070 object.
58+
uv = adafruit_veml6070.VEML6070(i2c)
59+
# Create BME280 object.
60+
bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c)
61+
bme280.sea_level_pressure = 1013.25
62+
# Create SGP30 object using I2C.
63+
sgp30 = adafruit_sgp30.Adafruit_SGP30(i2c)
64+
sgp30.iaq_init()
65+
sgp30.set_iaq_baseline(0x8973, 0x8aae)
66+
67+
# Sample VEML6070
68+
def sample_VEML():
69+
for _ in range(10):
70+
uv_raw = uv.uv_raw
71+
return uv_raw
72+
73+
while True:
74+
print('Reading sensors...')
75+
# Read SGP30.
76+
eCO2_data = sgp30.eCO2
77+
tvoc_data = sgp30.TVOC
78+
79+
# Read VEML6070.
80+
uv_data = sample_VEML()
81+
82+
# Read BME280.
83+
temp_data = bme280.temperature
84+
# convert temperature (C->F)
85+
temp_data = int(temp_data) * 1.8 + 32
86+
humid_data = bme280.humidity
87+
pressure_data = bme280.pressure
88+
alt_data = bme280.altitude
89+
90+
print('sending data to adafruit io...')
91+
# Send SGP30 Data to Adafruit IO.
92+
print('eCO2:', eCO2_data)
93+
aio.send(eCO2_feed.key, eCO2_data)
94+
print('tvoc:', tvoc_data)
95+
aio.send(tvoc_feed.key, tvoc_data)
96+
time.sleep(2)
97+
# Send VEML6070 Data to Adafruit IO.
98+
print('UV Level: ', uv_data)
99+
aio.send(uv_feed.key, uv_data)
100+
time.sleep(2)
101+
# Send BME280 Data to Adafruit IO.
102+
print('Temperature: %0.1f C' % temp_data)
103+
aio.send(temperature_feed.key, temp_data)
104+
print("Humidity: %0.1f %%" % humid_data)
105+
aio.send(humidity_feed.key, int(humid_data))
106+
time.sleep(2)
107+
print("Pressure: %0.1f hPa" % pressure_data)
108+
aio.send(pressure_feed.key, int(pressure_data))
109+
print("Altitude = %0.2f meters" % alt_data)
110+
aio.send(altitude_feed.key, int(alt_data))
111+
# avoid timeout from adafruit io
112+
time.sleep(LOOP_DELAY * 60)

0 commit comments

Comments
 (0)