Easy IoT with Adafruit IO and a Raspberry Pi
Tags: beginners, tutorial, raspberrypi, iot
Published: 2020-12-05 20:59:09

---
title: Easy IoT with Adafruit IO and a Raspberry Pi
published: true
description: There are many complex IoT systems for the cloud. But what about the hobbyist? In this tutorial, we'll show you how to set up a full IoT system from start to finish that a beginner can complete.
tags: beginners,tutorial,raspberrypi,iot
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/2x4lynn7dzquxbma6lp4.jpg
canonical_url: https://www.jeremymorgan.com/tutorials/raspberry-pi/how-to-iot-adafruit-raspberrypi/
---

There are a ton of IoT solutions in the cloud. Azure, AWS, and Google Cloud all have great and complex IoT platforms out there. They're fantastic for doing enterprise IoT.

But what about folks who just want to dip their toes into IoT? What if you just want to do a small hobby project to learn IoT before jumping into the big stuff??

[Adafruit](https://io.adafruit.com/) has your solution. They have a fully functional, easy to use IoT platform for hobbyists who just like to mess around and learn things. This platform is so good, you could use it for enterprise stuff as well.

I wrote about the [Adafruit IO Platform](https://www.jeremymorgan.com/tutorials/raspberry-pi/how-to-adafruit-io/) years ago, but a lot has changed since then. It's become more powerful and even easier to use. I had some [water damage to a room](https://twitter.com/JeremyCMorgan/status/1332370296301445126), and the restoration company put dehumidifiers in it, which inspired me to build this. I found Adafruit.IO to be incredibly easy to use, so I decided to write this article.

## What You'll Need

This is a low cost project. To follow this tutorial, you will need:

- a [Raspberry Pi Zero](https://amzn.to/36rSHdS)
- or [Standard Raspberry Pi](https://amzn.to/3lschuY)
- the [AM2302 Temperature Humidity Sensor](https://amzn.to/39zKrdV)
- Some [Jumper Wires](https://amzn.to/39zEJZl)
- Optional [Header Pins](https://www.adafruit.com/product/400)
- A (FREE) [Adafruit IO account](https://accounts.adafruit.com/users/sign_up)

So you can build something like this for under $50 USD and learn some IoT. Let's dig in.

## Step 1: Device to gather input (Raspberry Pi Zero W)

For this project, I will use the [CanaKit Raspberry Pi Zero W kit](https://amzn.to/36rSHdS), which is only USD 32.99. A great entry price. However, you can use a standard [Raspberry Pi](https://amzn.to/3lschuY) for this project as well. The instructions will be identical, but the Zero is a lower cost option.

![Raspberry Pi Zero](https://dev-to-uploads.s3.amazonaws.com/i/2iosj8nw9kzx1ze3e861.jpg)

I picked this because you don't need a full, high powered Raspberry Pi to gather the data we need. The Raspberry Pi Zero performs this task well, at 1/3rd of the price. However, you will need to solder some things. So if you just want to plug the sensors in, get the full Raspberry Pi.

![Raspberry Pi Zero](https://dev-to-uploads.s3.amazonaws.com/i/1l5bpytn02v07x751p13.jpg)

The kit comes stocked with everything you need, including a few different options for the top of the case.

![Raspberry Pi Zero](https://dev-to-uploads.s3.amazonaws.com/i/heqgr2h6rjpzjowqk101.jpg)

The first step if you're using the Zero is to solder in some [header pins](https://www.adafruit.com/product/400) for the GPIO.

![Raspberry Pi Zero](https://dev-to-uploads.s3.amazonaws.com/i/mmwedmo2wbggzaf0w9r8.jpg)

If you don't want to solder in your header pins, I recently found this [solderless Raspberry Pi connector](https://www.adafruit.com/product/3662). I've never used them, but I ordered a few to try out.

![Raspberry Pi Zero](https://dev-to-uploads.s3.amazonaws.com/i/ky5wbwxnr21l3352xjf2.jpg)

I generally just solder in the first 12 pins, so I have:

- 1 3.3v pin
- 2 5v pins
- 2 Ground
- SDA / SCL
- TXD / RXD
- CLK PCM
- 6 GPIO Pins

If I need to expand from that, it's relatively easy, but we're only doing one sensor here.

That sensor is the [AM2302 Temperature and Humidity Sensor](https://amzn.to/39zKrdV) (USD 9.69).

![Raspberry Pi Zero](https://dev-to-uploads.s3.amazonaws.com/i/sm947q1zsat6obgi6tt2.jpg)

Here is how I've wired it to the GPIO:

![Raspberry Pi Zero](https://dev-to-uploads.s3.amazonaws.com/i/zo534bfe9f5a8oattrtd.jpg)

The power wire goes to pin 1.
Ground goes to pin 6.
Data and goes to Pin 11.

None of the colors of the wires matter as long is it's wired in this fashion.

![Raspberry Pi Zero](https://dev-to-uploads.s3.amazonaws.com/i/s15fu02840aabltipx6k.jpg)

Now that we set up the device, let's get an Operating system on it.

##  Step 2: Installing the Raspberry Pi OS

At this point, you'll want to insert your card and plug the Raspberry Pi into a monitor. By default, most Raspberry Pis and Pi Zeros have Noobs installed. If you don't have a preloaded card, you can [get Noobs here](https://www.raspberrypi.org/documentation/installation/noobs.md).

You'll be greeted by Noobs screen, with two options. You don't want either of them.

![Raspberry Pi Zero](https://dev-to-uploads.s3.amazonaws.com/i/rnmkf2vm9wj3x29sua2k.gif)

Instead, click the Wifi networks button and connect to your Wifi.

![Raspberry Pi Zero](https://dev-to-uploads.s3.amazonaws.com/i/zlilx2l4q6lk9tai2kfq.jpg)

This is a straightforward procedure, then you'll see some more options once connected:

![Raspberry Pi Zero](https://dev-to-uploads.s3.amazonaws.com/i/6f8ebz4rpttovzawqfig.jpg)

Choose the Raspberry Pi OS Lite version. You don't need a desktop for this, but just the core OS.

![ARaspberry Pi Zero](https://dev-to-uploads.s3.amazonaws.com/i/nd93r1zavxni6iqemqij.jpg)

It will then download, extract and install the operating system onto your Pi.

These instructions are the same for the Pi Zero, and conventional Raspberry Pi.

##  Step 3: Enable SSH

Once you have your OS installed, log in to the prompt.

- Username: **pi**
- Password: **raspberry**

Then type in

```
sudo raspi-config
```
and load up the Pi configuration.

![Raspberry Pi Zero](https://dev-to-uploads.s3.amazonaws.com/i/mnio3n9ngla2pzivqn38.jpg)

Now's a good time to change that default password.

Then scroll down to Interfacing options:

![Raspberry Pi Zero](https://dev-to-uploads.s3.amazonaws.com/i/ksngmsp7wflnl5bpik34.jpg)

And enable SSH connections:

![Raspberry Pi Zero](https://dev-to-uploads.s3.amazonaws.com/i/nq2g11rhdexm4tdy0nfv.jpg)

You'll see the following warning. Click yes:

![Raspberry Pi Zero](https://dev-to-uploads.s3.amazonaws.com/i/rj0b3uxynez8r7xlnyab.jpg)

And exit the program. Now you can disconnect this from the monitor and communicate with the Pi via SSH.

##  Step 4: Gather Data from AM2302

First off, you'll need Pip. Some Raspberry Pi OS distributions come with it, but this one does not so run the following commands:

```
sudo apt-get update
sudo apt install python3-pip
```
Next, we'll add the Adafruit libraries to our pi:

```
pip3 install adafruit-circuitpython-dht
sudo apt-get install libgpiod2
```
These libraries will help us extract the data from the sensor.

Now, let's write a quick script to test it.

```
mkdir ~/sensor && cd ~/sensor
```

Now create a script that looks like this:

```
import board
import adafruit_dht

dhtDevice = adafruit_dht.DHT22(board.D17)

temperature_c = dhtDevice.temperature
temperature_f = temperature_c * (9 / 5) + 32
humidity = dhtDevice.humidity

print(
      "Temp: {:.1f} F / {:.1f} C    Humidity: {}% ".format(
      temperature_f, temperature_c, humidity
      )
)
```

Let's walk through this.

This script will load in the libraries, board, and adafruit_dht, for the script.

```
import board
import adafruit_dht
```

Then we establish the device:

```
dhtDevice = adafruit_dht.DHT22(board.D17)
```
The "Board D17 indicates we're using GPIO17 on the board (pin 11)"

Next, we'll pull some readings:

```
temperature_c = dhtDevice.temperature
temperature_f = temperature_c * (9 / 5) + 32
humidity = dhtDevice.humidity
```

Here we are grabbing the temperature from the sensor, which defaults to Celcius. We then convert the Celcius to Fahrenheit for the next variable, then grab our humidity.

Finally we print it out:

```
print(
      "Temp: {:.1f} F / {:.1f} C    Humidity: {}% ".format(
      temperature_f, temperature_c, humidity
      )
)
```

Now, we're ready to save the file and run it.

```
python3 test.py
```

Your output should look like this:

![Adafruit Library Python](https://dev-to-uploads.s3.amazonaws.com/i/1030qq1tip7vu97a2s6z.gif)

Awesome! We have some data. Now we'll need to send it to Adafruit IO.

##  Step 5: Setting Up Adafruit.IO

The first thing you'll need to do is [sign up for an Adafruit IO account](https://accounts.adafruit.com/users/sign_up). It's free.

Then, you'll navigate to **feeds**, which should be:

>*io.adafruit.com/[Your username]/feeds*

![Connect to Adafruit IO](https://dev-to-uploads.s3.amazonaws.com/i/us9pkh43n783xhwsgft1.gif)

Click "New feed"

Create a new feed for humidity, and name it whatever you like. I'm choosing "RoomHumidity"

![Connect to Adafruit IO](https://dev-to-uploads.s3.amazonaws.com/i/4jqudtfy9zwkezgbeok4.jpg)

Do the same thing for temperature:

![Connect to Adafruit IO](https://dev-to-uploads.s3.amazonaws.com/i/d6dcg8esfxou2zbn7vqh.jpg)

So now you have two feeds established on AdafruitIO. Let's modify our script.

##  Step 6: Send Data to Adafruit.IO

Next, we're going to need to get our key for connecting to Adafruit IO:

![Connect to Adafruit IO](https://dev-to-uploads.s3.amazonaws.com/i/ocxryuljh5rcp31aukap.gif)

Copy your username and key:

![Connect to Adafruit IO](https://dev-to-uploads.s3.amazonaws.com/i/bt5f31vzl317jvwbw5uy.jpg)

Keep your username and key in a safe place. If you like, you can set them as environment variables on the pi.

Next, install the Adafruit IO Library on the pi:

```
pip3 install adafruit-io
```

Let's build a new script. Name it **sensor.py** or whatever you like.

We're going to build a script that samples the temperature and humidity every 30 seconds and sends it to Adafruit.io.

There are other ways of doing this, such as Cron jobs and the like, but I've found this way to be pretty straightforward.

First, we want to make sure we can run this script like an executable:

```
#!/usr/bin/env python3
```

Then we'll import our libraries:

```
import time
import adafruit_dht
import board
```

We'll establish our device again:

```
dht = adafruit_dht.DHT22(board.D17)
```
Next, we'll establish an instance of the REST client to communicate with Adafruit:

```
from Adafruit_IO import Client
aio = Client('[Your Username]', '[Your Key]')
```
Next, we'll create a while loop that runs continuously and samples the information, and sends it to adafruit:

```
while True:
    try:
        temperature = (dht.temperature * 9/5) + 32
        smalltemp = round(temperature, 2)
        humidity = dht.humidity

        humfeed = aio.feeds('roomhumidity')
        aio.send_data(humfeed.key, humidity)

        tempfeed = aio.feeds('roomtemperature')
        aio.send_data(tempfeed.key, round(temperature,2))

    except RuntimeError as e:
        print("Reading from DHT failure: ", e.args)
    time.sleep(30)
```

Now we can save the file. This is all you'll need to start pushing data.

Let's do a test run.

```
chmod +x sensor.py
python3 sensor.py
```
Start up the script and it should send data:

![Connect to Adafruit IO](https://dev-to-uploads.s3.amazonaws.com/i/l5vmp44vu6u7wcy95spe.gif)

![Connect to Adafruit IO](https://dev-to-uploads.s3.amazonaws.com/i/wmqbjgre54phssyqcs7k.gif)

The full sensor.py script:

```
#!/usr/bin/env python3

import time
import adafruit_dht
import board

dht = adafruit_dht.DHT22(board.D17)

from Adafruit_IO import Client
aio = Client('[ Your Username ]', '[ Your Key ]')

while True:
    try:
        temperature = (dht.temperature * 9/5) + 32
        smalltemp = round(temperature, 2)
        humidity = dht.humidity

        humfeed = aio.feeds('roomhumidity')
        aio.send_data(humfeed.key, humidity)

        tempfeed = aio.feeds('roomtemperature')
        aio.send_data(tempfeed.key, round(temperature,2))

    except RuntimeError as e:
        print("Reading from DHT failure: ", e.args)
    time.sleep(30)
```

Now that we know we're sending data let's set up a dashboard for it!

##  Step 7: Setting up an Adafruit.IO Dashboard

Alright, time for the fun stuff. The visuals. It's incredibly easy to set up a dashboard on Adafruit.IO.

Navigate to dashboards

![Create Adafruit IO Dashboard](https://dev-to-uploads.s3.amazonaws.com/i/cr8crrk9kppxheoby3m9.gif)

Click "new dashboard" and add a name and description:

![Create Adafruit IO Dashboard](https://dev-to-uploads.s3.amazonaws.com/i/t1c9i5hawt3hz3o1qe6d.jpg)

Now we have a new dashboard and a clean slate. Let's add a new block:

![Create Adafruit IO Dashboard](https://dev-to-uploads.s3.amazonaws.com/i/zlbgii7ezku4fc5b6t04.jpg)

I think I'll choose a gauge for this one:

![Create Adafruit IO Dashboard](https://dev-to-uploads.s3.amazonaws.com/i/tuout6kpg2jyf451x546.jpg)

Then select the feed:

![Create Adafruit IO Dashboard](https://dev-to-uploads.s3.amazonaws.com/i/dqis73g2m8x9rnc7p2os.jpg)

Now I can add some settings for the display of the gauge:

![Create Adafruit IO Dashboard](https://dev-to-uploads.s3.amazonaws.com/i/42ywb692z7jrwd1fq56w.jpg)

And save it. I've done the same thing for temperature, so now I have two nice gauges for a layout:

![Create Adafruit IO Dashboard](https://dev-to-uploads.s3.amazonaws.com/i/j4cl6ca0hwu1vbwmzp32.gif)

I added in a couple of line graphs, and we have a dashboard. I also set my privacy settings to be public because I don't care who knows the temperature of my garage:

![Create Adafruit IO Dashboard](https://dev-to-uploads.s3.amazonaws.com/i/9e2dut2gtzcacxo0sngz.gif)

And here's the public dashboard!

![Create Adafruit IO Dashboard](https://dev-to-uploads.s3.amazonaws.com/i/nzfblakvfh5b9l8bv0nz.gif)

## Summary

In this tutorial, we wired up a Raspberry Pi to a cheap sensor, installed some libraries to gather the data, sent it to Adafruit.IO, and then created a dashboard.

Though this tutorial looks long, it's something you can do in an hour or so. This is the perfect project for folks wanting to get into IoT and learn about it. Adafruit is the ultimate hobbyist platform.

I hope this has inspired you to do more cool IoT stuff! I will add more projects like this in the future. What do you think about this tutorial? Have questions? [Let me know](http://bit.ly/JeremyCMorgan)!!

Good luck with your project!

Want to learn more about the Raspberry Pi? Check out this [Raspberry Pi for Developers](https://bit.ly/RPIDevelopersCourse) course!