Skip to content

Commit bb56514

Browse files
authored
Merge pull request #149 from brentru/update-temp-humid-example
Update example, `temp_humidity.py`
2 parents e418f20 + 15e1f44 commit bb56514

File tree

3 files changed

+56
-41
lines changed

3 files changed

+56
-41
lines changed

.github/workflows/build.yml

+9-13
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,23 @@
11
name: Build-CI
22

3-
on: [pull_request, push]
3+
on:
4+
push:
5+
branches:
6+
- master
47

58
jobs:
69

7-
approve: # First step
8-
runs-on: ubuntu-latest
9-
10-
steps:
11-
- name: Approve
12-
run: echo For security reasons, all pull requests to this repository need to be approved first before running any automated CI.
13-
1410
build:
1511
runs-on: ubuntu-latest
16-
17-
needs: [approve] # Require the first step to finish
1812
environment:
1913
name: IO
2014
steps:
2115
- uses: actions/checkout@v2
2216

23-
- name: Set up Python 3.6
24-
uses: actions/setup-python@v1
17+
- name: Set up Python
18+
uses: actions/setup-python@v4
2519
with:
26-
python-version: 3.6
20+
python-version: '3.10'
2721

2822
- name: Install dependencies
2923
run: |
@@ -41,6 +35,8 @@ jobs:
4135
SECRET_IO_KEY: ${{ secrets.CI_IO_KEY }}
4236
SECRET_IO_USER: ${{ secrets.CI_IO_USERNAME }}
4337
run: |
38+
echo "Secret key length: ${#SECRET_IO_KEY}"
39+
echo "Secret username length: ${#SECRET_IO_USER}"
4440
cd tests/
4541
ADAFRUIT_IO_KEY=$SECRET_IO_KEY ADAFRUIT_IO_USERNAME=$SECRET_IO_USER python -m unittest discover
4642
cd ..

docs/conf.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
# General information about the project.
3131
project = u'adafruit-io-python'
32-
copyright = u'2019 Adafruit Industries'
32+
copyright = u'2023 Adafruit Industries'
3333
author = u'Adafruit Industries'
3434

3535
# The version info for the project you're documenting, acts as replacement for
@@ -46,7 +46,7 @@
4646
#
4747
# This is also used if you do content translation via gettext catalogs.
4848
# Usually you set "language" from the command line for these cases.
49-
language = None
49+
language = "en"
5050

5151
# List of patterns, relative to source directory, that match files and
5252
# directories to ignore when looking for source files.

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)