|
1 | 1 | """
|
2 | 2 | 'temp_humidity.py'
|
3 | 3 | ==================================
|
4 |
| -Example of sending analog sensor |
5 |
| -values to an Adafruit IO feed. |
| 4 | +Example of sending temperature and humidity data to Adafruit IO |
6 | 5 |
|
7 | 6 | Author(s): Brent Rubell
|
8 | 7 |
|
|
11 | 10 | Dependencies:
|
12 | 11 | - Adafruit IO Python Client
|
13 | 12 | (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) |
16 | 15 | """
|
17 | 16 |
|
18 | 17 | # import standard python modules.
|
19 | 18 | import time
|
20 | 19 |
|
21 |
| -# import adafruit dht library. |
22 |
| -import Adafruit_DHT |
| 20 | +# import adafruit-blinka modules |
| 21 | +import board |
23 | 22 |
|
24 | 23 | # import Adafruit IO REST client.
|
25 |
| -from Adafruit_IO import Client, Feed |
| 24 | +from Adafruit_IO import Client, Feed, RequestError |
26 | 25 |
|
27 |
| -# Delay in-between sensor readings, in seconds. |
28 |
| -DHT_READ_TIMEOUT = 5 |
| 26 | +# Import AHTx0 library |
| 27 | +import adafruit_ahtx0 |
29 | 28 |
|
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 |
32 | 34 |
|
33 | 35 | # Set to your Adafruit IO key.
|
34 | 36 | # Remember, your key is a secret,
|
|
42 | 44 | # Create an instance of the REST client.
|
43 | 45 | aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
|
44 | 46 |
|
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) |
48 | 60 |
|
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) |
51 | 65 |
|
52 | 66 | 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)) |
61 | 72 | 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 | + |
63 | 82 | # Timeout to avoid flooding Adafruit IO
|
64 |
| - time.sleep(DHT_READ_TIMEOUT) |
| 83 | + time.sleep(READ_TIMEOUT) |
0 commit comments