The temp_humidity.py code uses Raspberry Pi GPIO Pin 26 by default. If you'd like to use a different pin, change the DHT_DATA_PIN variable.
DHT_DATA_PIN = 26
Before running, we'll need to set our Adafruit IO Key and Adafruit IO Username. You can find both of these on your Adafruit IO profile page
# 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'
The next chunk of code creates an instance of the Adafruit IO REST client, sets up the temperature and humidity feeds, and sets up the DHT22 sensor.
# 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') # Set up DHT22 Sensor. dht22_sensor = Adafruit_DHT.DHT22
in the while True
loop, we first try to grab the humidity and sensor readings using Adafruit_DHT.read_retry
which retries (up to 15 times) to get a sensor reading.
humidity, temperature = Adafruit_DHT.read_retry(sensor, DHT_DATA_PIN)
If the DHT sensor receives a reading, it'll print out both of the values and send them to the Adafruit IO temperature and humidity feeds.
print('Temp={0:0.1f}*C Humidity={1:0.1f}%'.format(temperature, humidity)) aio.send(temperature.key, temperature) aio.send(humidity.key, humidity)
Sometimes you won't get a sensor reading and the results will be null (because Linux can't guarantee the timing of calls to read to the sensor). If that occurs, we'll print to the terminal. Then, we sleep for DHT_READ_TIMEOUT until the next read.
In your terminal, enter the following command to run the code:
python3 temp_humidity.py
You should now see the temperature and humidity values being sent to Adafruit IO.
Temp=25.5*C Humidity=59.1% Temp=25.5*C Humidity=59.1% Temp=25.5*C Humidity=59.1% Temp=25.4*C Humidity=59.0%
Check your dashboard on Adafruit IO, and you should see the line chart update with the changes in temperature and humidity.
""" '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)
Text editor powered by tinymce.