Adafruit Io Basics Temperature and Humidity
Adafruit Io Basics Temperature and Humidity
Adafruit Io Basics Temperature and Humidity
This guide is part of a series of guides that cover the basics of using Adafruit IO. It will show you how to send
temperature and humidity values wirelessly to Adafruit IO from a DHT22 sensor.
If you haven't worked your way through the Adafruit IO feed and dashboard basics guides, you should do that before
continuing with this guide so you have a basic understanding of Adafruit IO.
You should go through the setup guides associated with your selected set of hardware, and make sure you have
internet connectivity with the device before continuing. The following links will take you to the guides for your selected
platform.
If you have went through all of the prerequisites for your selected hardware, you are now ready to move on to the
Adafruit IO setup steps that are common between all of the hardware choices for this project. Let's get started!
A window will pop up with your Adafruit IO. Keep a copy of this in a safe place. We'll need it later.
If you need help getting started with creating feeds on Adafruit IO, check out the Adafruit IO Feed
Basics guide (https://adafru.it/ioA).
When you are finished editing the form, click Create Block to add the new block to the dashboard.
If you need help getting started with Dashboards on Adafruit IO, check out the Adafruit IO Dashboard Basics
guide (https://adafru.it/f5m).
We will need to connect the following pins from the Feather to the resistor and DHT22:
You will need to make sure you have at least version 2.4.1 of the Adafruit IO Arduino library installed before continuing.
You will also need to install the Adafruit Unified Sensor library.
For this example, you will need to open the adafruitio_15_temp_humidity example in the Adafruit IO Arduino library.
WiFi Config
WiFi is enabled by default in config.h so if you are using one of the supported WiFi boards, you will only need to
modify the WIFI_SSID and WIFI_PASS options in the config.h tab.
FONA Config
If you wish to use the FONA 32u4 Feather to connect to Adafruit IO, you will need to first comment out the WiFi
support in config.h
Ethernet Config
If you wish to use the Ethernet Wing to connect to Adafruit IO, you will need to first comment out the WiFi support in
config.h
Next, remove the comments from both of the Ethernet config lines in the Ethernet section of config.h to enable
Ethernet Wing support.
The next chunk of code creates an instance of the DHT class, and also sets up feed instances for the temperature and
humidity feeds.
The setup function initializes the DHT22 sensor, and also connects your feather to Adafruit IO. The code will wait until
you have a valid connection to Adafruit IO before continuing with the sketch. If you have any issues connecting, check
config.h for any typos in your username or key.
void setup() {
// initialize dht22
dht.begin();
// connect to io.adafruit.com
Serial.print("Connecting to Adafruit IO");
io.connect();
// we are connected
Serial.println();
Serial.println(io.statusText());
Next, we have the main loop() function. The first line of the loop function calls io.run(); this line will need to be
present at the top of your loop in every sketch. It helps keep your device connected to Adafruit IO, and processes any
incoming data.
The next chunk of code inside the loop() checks the current DHT22 temperature value, and saves the value in the
celsius and fahrenheit variables.
We then print both celsius and fahrenheit to the Arduino Serial Monitor, and save the fahrenheit value to the
temperature feed on Adafruit IO.
sensors_event_t event;
dht.temperature().getEvent(&event);
Serial.print("celsius: ");
Serial.print(celsius);
Serial.println("C");
Serial.print("fahrenheit: ");
Serial.print(fahrenheit);
Serial.println("F");
If you prefer to log celsius values, you can modify the call to the save() function.
temperature->save(celsius);
The final chunk of the loop() function requests a humidity reading from the DHT22, and prints the value to the
Arduino Serial Monitor. We also save the humidity value to the humidity feed on Adafruit IO.
dht.humidity().getEvent(&event);
Serial.print("humidity: ");
Serial.print(event.relative_humidity);
Serial.println("%");
Adafruit IO connected.
You should now see the temperature and humidity values being sent to Adafruit IO.
celsius: 18.30C
fahrenheit: 64.94F
humidity: 34.90%
celsius: 18.20C
fahrenheit: 64.76F
humidity: 35.40%
Check your dashboard on Adafruit IO, and you should see the line chart update with the changes in temperature and
humidity.
Now that you've got a graphing weather device using IO, you can add an OLED feather so you can see network status,
IP address, and the latest measurements!
Plug the OLED FeatherWing on top of your Feather, and check out our guide to get set up and test
it (https://adafru.it/nek)! Once you've verified that the OLED works, you can use this new code. Use the same config.h
file from the previous section, just replace the 'main' tab of code:
This is just the main code, and does not include the config.h - use the same config.h you had from the
previous working demo!
// oled display
Adafruit_SSD1306 oled = Adafruit_SSD1306(128, 32, &Wire);
void setup() {
oled.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x32)
oled.display();
// initialize dht22
dht.begin();
// connect to io.adafruit.com
Serial.print("Connecting to Adafruit IO");
io.connect();
// we are connected
Serial.println();
Serial.println(io.statusText());
void loop() {
sensors_event_t event;
dht.temperature().getEvent(&event);
Serial.print("celsius: ");
Serial.print(celsius);
Serial.println("C");
Serial.print("fahrenheit: ");
Serial.print(fahrenheit);
Serial.println("F");
dht.humidity().getEvent(&event);
Serial.print("humidity: ");
Serial.print(event.relative_humidity);
Serial.println("%");
Parts
1 x Raspberry Pi 3 - Model B+
The Raspberry Pi is a small linux board compatible with Adafruit IO projects.
ADD TO CART
If you're following along with a Raspberry Pi (https://adafru.it/ejq), we're going to use a T-Cobbler Plus for the IO Basics
Projects. This add-on prototyping board lets you easily connect a Raspberry Pi (Raspberry Pi Model Zero, A+, B+, Pi 2,
Pi 3) to a solderless breadboard:
$7.95
IN STOCK
ADD TO CART
We'll also need a temperature and humidity sensor. The DHT22 is a low-cost temperature and humidity sensor which
is easy to wire up. You'll also need a 10k ohm resistor.
$9.95
IN STOCK
ADD TO CART
Wiring
Instead, we are going to use the Adafruit Python DHT Sensor Library and the Adafruit IO Python Client Library.
The latest Raspbian (currently this is `Stretch`) is required for the installation of Adafruit IO + Blinka.
Go ahead and ssh into your Raspberry Pi via terminal or a ssh client:
and
We'll be using python3 and pip3 in our commands, use those versions of python and pip to make sure you're using
3 and not 2
Enter the following in your terminal to install the Adafruit Python DHT Library:
Run the following command to install the Adafruit IO Client for Python:
If the installation gives you 'insufficient permissions' errors, add 'sudo' before the call to pip3
Navigate to the root directory of the Pi by entering the following in the terminal:
cd ~
Download the latest version of the Adafruit IO Python Client's GitHub repository by running
cd io-client-python/examples/basics/
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 (https://adafru.it/BmD)
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.
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.
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.
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.
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.
Code
"""
'temp_humidity.py'
==================================
Example of sending analog sensor
values to an Adafruit IO feed.
Dependencies:
- Adafruit IO Python Client
(https://github.com/adafruit/io-client-python)
- Adafruit_Python_DHT
(https://github.com/adafruit/Adafruit_Python_DHT)
"""
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))
else:
print('Failed to get DHT22 Reading, trying again in ', DHT_READ_TIMEOUT, 'seconds')
# Timeout to avoid flooding Adafruit IO
time.sleep(DHT_READ_TIMEOUT)
Don't see your issue? Post up on the Adafruit IO Forum with your issue (https://adafru.it/plC).
My Serial Monitor prints "..." endlessly after the "Connecting to Adafruit IO" message
Your board is not connecting to Adafruit IO, but why? Let's find out:
Next, we're going to modify the while loop which waits for an IO connection in your sketch. Change the line in the
status check loop from Serial.println(.); to Serial.println(io.statusText());
Verify and re-upload the sketch. If you're receiving a Network disconnected error message, the board is not able to
talk to the internet. Re-check your hardware, connections, and router settings.
If it's still not showing Adafruit IO connected, check the IO status on the Adafruit Status page to make sure the
service is online.
There's a monitor page built-into Adafruit IO which provides a live view of incoming data and error messages. Keep
this page open while you send data to your Adafruit IO devices to monitor data and errors.