Skip to content

Commit 3fa2cf4

Browse files
committed
update example
1 parent e418f20 commit 3fa2cf4

File tree

1 file changed

+45
-26
lines changed

1 file changed

+45
-26
lines changed

examples/basics/temp_humidity.py

+45-26
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
"""
22
'temp_humidity.py'
33
==================================
4-
Example of sending analog sensor
5-
values to an Adafruit IO feed.
4+
Example of sending temperature and humidity data to Adafruit IO
65
76
Author(s): Brent Rubell
87
@@ -11,24 +10,27 @@
1110
Dependencies:
1211
- Adafruit IO Python Client
1312
(https://github.com/adafruit/io-client-python)
14-
- Adafruit_Python_DHT
15-
(https://github.com/adafruit/Adafruit_Python_DHT)
13+
- Adafruit_CircuitPython_AHTx0
14+
(https://github.com/adafruit/Adafruit_CircuitPython_AHTx0)
1615
"""
1716

1817
# import standard python modules.
1918
import time
2019

21-
# import adafruit dht library.
22-
import Adafruit_DHT
20+
# import adafruit-blinka modules
21+
import board
2322

2423
# import Adafruit IO REST client.
25-
from Adafruit_IO import Client, Feed
24+
from Adafruit_IO import Client, Feed, RequestError
2625

27-
# Delay in-between sensor readings, in seconds.
28-
DHT_READ_TIMEOUT = 5
26+
# Import AHTx0 library
27+
import adafruit_ahtx0
2928

30-
# Pin connected to DHT22 data pin
31-
DHT_DATA_PIN = 26
29+
# Set true to send tempertaure data in degrees fahrenheit ('f')?
30+
USE_DEGREES_F = False
31+
32+
# Time between sensor reads, in seconds
33+
READ_TIMEOUT = 60
3234

3335
# Set to your Adafruit IO key.
3436
# Remember, your key is a secret,
@@ -42,23 +44,40 @@
4244
# Create an instance of the REST client.
4345
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
4446

45-
# Set up Adafruit IO Feeds.
46-
temperature_feed = aio.feeds('temperature')
47-
humidity_feed = aio.feeds('humidity')
47+
# Assign a temperature feed, if one exists already
48+
try:
49+
temperature_feed = aio.feeds('temperature')
50+
except RequestError: # Doesn't exist, create a new feed
51+
feed_temp = Feed(name="temperature")
52+
temperature_feed = aio.create_feed(feed_temp)
53+
54+
# Assign a humidity feed, if one exists already
55+
try:
56+
humidity_feed = aio.feeds('humidity')
57+
except RequestError: # Doesn't exist, create a new feed
58+
feed_humid = Feed(name="humidity")
59+
humidity_feed = aio.create_feed(feed_humid)
4860

49-
# Set up DHT22 Sensor.
50-
dht22_sensor = Adafruit_DHT.DHT22
61+
# Initialize the board's default I2C bus
62+
i2c = board.I2C() # uses board.SCL and board.SDA
63+
# Initialize AHT20 using the default address (0x38) and the board's default i2c bus
64+
sensor = adafruit_ahtx0.AHTx0(i2c)
5165

5266
while True:
53-
humidity, temperature = Adafruit_DHT.read_retry(dht22_sensor, DHT_DATA_PIN)
54-
if humidity is not None and temperature is not None:
55-
print('Temp={0:0.1f}*C Humidity={1:0.1f}%'.format(temperature, humidity))
56-
# Send humidity and temperature feeds to Adafruit IO
57-
temperature = '%.2f'%(temperature)
58-
humidity = '%.2f'%(humidity)
59-
aio.send(temperature_feed.key, str(temperature))
60-
aio.send(humidity_feed.key, str(humidity))
67+
temperature = sensor.temperature
68+
humidity = sensor.relative_humidity
69+
if USE_DEGREES_F:
70+
temperature = temperature * 9.0 / 5.0 + 32.0
71+
print('Temp={0:0.1f}*F'.format(temperature))
6172
else:
62-
print('Failed to get DHT22 Reading, trying again in ', DHT_READ_TIMEOUT, 'seconds')
73+
print('Temp={0:0.1f}*C'.format(temperature))
74+
print('Humidity={1:0.1f}%'.format(humidity))
75+
# Format sensor data as string for sending to Adafruit IO
76+
temperature = '%.2f'%(temperature)
77+
humidity = '%.2f'%(humidity)
78+
# Send humidity and temperature data to Adafruit IO
79+
aio.send(temperature_feed.key, str(temperature))
80+
aio.send(humidity_feed.key, str(humidity))
81+
6382
# Timeout to avoid flooding Adafruit IO
64-
time.sleep(DHT_READ_TIMEOUT)
83+
time.sleep(READ_TIMEOUT)

0 commit comments

Comments
 (0)