-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathtemp_humidity.py
83 lines (66 loc) · 2.56 KB
/
temp_humidity.py
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
"""
'temp_humidity.py'
==================================
Example of sending temperature and humidity data to Adafruit IO
Author(s): Brent Rubell
Tutorial Link: Tutorial Link: https://learn.adafruit.com/adafruit-io-basics-temperature-and-humidity
Dependencies:
- Adafruit IO Python Client
(https://github.com/adafruit/io-client-python)
- Adafruit_CircuitPython_AHTx0
(https://github.com/adafruit/Adafruit_CircuitPython_AHTx0)
"""
# import standard python modules.
import time
# import adafruit-blinka modules
import board
# import Adafruit IO REST client.
from Adafruit_IO import Client, Feed, RequestError
# Import AHTx0 library
import adafruit_ahtx0
# Set true to send tempertaure data in degrees fahrenheit ('f')?
USE_DEGREES_F = False
# Time between sensor reads, in seconds
READ_TIMEOUT = 60
# Set to your Adafruit IO key.
# Remember, your key is a secret,
# so make sure not to publish it when you publish this code!
ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'
# Set to your Adafruit IO username.
# (go to https://accounts.adafruit.com to find your username).
ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
# Create an instance of the REST client.
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
# Assign a temperature feed, if one exists already
try:
temperature_feed = aio.feeds('temperature')
except RequestError: # Doesn't exist, create a new feed
feed_temp = Feed(name="temperature")
temperature_feed = aio.create_feed(feed_temp)
# Assign a humidity feed, if one exists already
try:
humidity_feed = aio.feeds('humidity')
except RequestError: # Doesn't exist, create a new feed
feed_humid = Feed(name="humidity")
humidity_feed = aio.create_feed(feed_humid)
# Initialize the board's default I2C bus
i2c = board.I2C() # uses board.SCL and board.SDA
# Initialize AHT20 using the default address (0x38) and the board's default i2c bus
sensor = adafruit_ahtx0.AHTx0(i2c)
while True:
temperature = sensor.temperature
humidity = sensor.relative_humidity
if USE_DEGREES_F:
temperature = temperature * 9.0 / 5.0 + 32.0
print('Temp={0:0.1f}*F'.format(temperature))
else:
print('Temp={0:0.1f}*C'.format(temperature))
print('Humidity={1:0.1f}%'.format(humidity))
# Format sensor data as string for sending to Adafruit IO
temperature = '%.2f'%(temperature)
humidity = '%.2f'%(humidity)
# Send humidity and temperature data to Adafruit IO
aio.send(temperature_feed.key, str(temperature))
aio.send(humidity_feed.key, str(humidity))
# Timeout to avoid flooding Adafruit IO
time.sleep(READ_TIMEOUT)