Skip to content

Commit

Permalink
Merge pull request #149 from brentru/update-temp-humid-example
Browse files Browse the repository at this point in the history
Update example, `temp_humidity.py`
  • Loading branch information
brentru authored Nov 8, 2023
2 parents e418f20 + 15e1f44 commit bb56514
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 41 deletions.
22 changes: 9 additions & 13 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
name: Build-CI

on: [pull_request, push]
on:
push:
branches:
- master

jobs:

approve: # First step
runs-on: ubuntu-latest

steps:
- name: Approve
run: echo For security reasons, all pull requests to this repository need to be approved first before running any automated CI.

build:
runs-on: ubuntu-latest

needs: [approve] # Require the first step to finish
environment:
name: IO
steps:
- uses: actions/checkout@v2

- name: Set up Python 3.6
uses: actions/setup-python@v1
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: 3.6
python-version: '3.10'

- name: Install dependencies
run: |
Expand All @@ -41,6 +35,8 @@ jobs:
SECRET_IO_KEY: ${{ secrets.CI_IO_KEY }}
SECRET_IO_USER: ${{ secrets.CI_IO_USERNAME }}
run: |
echo "Secret key length: ${#SECRET_IO_KEY}"
echo "Secret username length: ${#SECRET_IO_USER}"
cd tests/
ADAFRUIT_IO_KEY=$SECRET_IO_KEY ADAFRUIT_IO_USERNAME=$SECRET_IO_USER python -m unittest discover
cd ..
Expand Down
4 changes: 2 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

# General information about the project.
project = u'adafruit-io-python'
copyright = u'2019 Adafruit Industries'
copyright = u'2023 Adafruit Industries'
author = u'Adafruit Industries'

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

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
Expand Down
71 changes: 45 additions & 26 deletions examples/basics/temp_humidity.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
"""
'temp_humidity.py'
==================================
Example of sending analog sensor
values to an Adafruit IO feed.
Example of sending temperature and humidity data to Adafruit IO
Author(s): Brent Rubell
Expand All @@ -11,24 +10,27 @@
Dependencies:
- Adafruit IO Python Client
(https://github.com/adafruit/io-client-python)
- Adafruit_Python_DHT
(https://github.com/adafruit/Adafruit_Python_DHT)
- Adafruit_CircuitPython_AHTx0
(https://github.com/adafruit/Adafruit_CircuitPython_AHTx0)
"""

# import standard python modules.
import time

# import adafruit dht library.
import Adafruit_DHT
# import adafruit-blinka modules
import board

# import Adafruit IO REST client.
from Adafruit_IO import Client, Feed
from Adafruit_IO import Client, Feed, RequestError

# Delay in-between sensor readings, in seconds.
DHT_READ_TIMEOUT = 5
# Import AHTx0 library
import adafruit_ahtx0

# Pin connected to DHT22 data pin
DHT_DATA_PIN = 26
# 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,
Expand All @@ -42,23 +44,40 @@
# Create an instance of the REST client.
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)

# Set up Adafruit IO Feeds.
temperature_feed = aio.feeds('temperature')
humidity_feed = aio.feeds('humidity')
# 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)

# Set up DHT22 Sensor.
dht22_sensor = Adafruit_DHT.DHT22
# 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:
humidity, temperature = Adafruit_DHT.read_retry(dht22_sensor, DHT_DATA_PIN)
if humidity is not None and temperature is not None:
print('Temp={0:0.1f}*C Humidity={1:0.1f}%'.format(temperature, humidity))
# Send humidity and temperature feeds to Adafruit IO
temperature = '%.2f'%(temperature)
humidity = '%.2f'%(humidity)
aio.send(temperature_feed.key, str(temperature))
aio.send(humidity_feed.key, str(humidity))
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('Failed to get DHT22 Reading, trying again in ', DHT_READ_TIMEOUT, 'seconds')
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(DHT_READ_TIMEOUT)
time.sleep(READ_TIMEOUT)

0 comments on commit bb56514

Please sign in to comment.