Introduction: Temperature and Humidity Monitoring Using Raspberry Pi

Picture of Temperature and Humidity Monitoring Using Raspberry Pi

Summer is coming, and those without an air conditioner should be prepared to control the atmosphere indoors manually. In this post, I’m describing the modern way to measure the most important parameters for human comfort: temperature and humidity. This collected data is sent to the cloud and processed there.

I am using a Raspberry Pi 1 board and the DHT22 sensor. You can do the same on any computer that has the Internet, GPIO, and Python. The cheaper DHT11 sensor also works fine.

Step 1: Preparing Hardware

Picture of Preparing Hardware

Let’s start from the very beginning, as I did not use my Raspberry Pi for quite a long time.

We will need:

  • Raspberry Pi board (or other IoT-oriented platform).
  • SD or microSD card (depending on the platform).
  • 5V/1A via micro-USB.
  • LAN cable, which provides the Internet connection.
  • HDMI display, RCA display, or UART port (to enable SSH).

The very first step is downloading Raspbian. I’ve chosen the Lite version, as I’m going to use SSH instead of display.
Things have changed since the last time I did it: now there is a great burning software called Etcher, which works perfectly, and has a stunning design.

After the image burning was completed, I inserted the SD card into my Pi, plugged the LAN and power cables in, and after a while, my router registered the new device.

Step 2: Enabling SSH

Picture of Enabling SSH

SSH is disabled by default. I can use either UART-USB converter or just connect a display to access the shell and enable SSH.

After rebooting, I’m finally in. First things first, let’s update:

sudo apt update && sudo apt upgrade -y

Now let’s connect this fresh device to the Cloud.

Step 3: Installing Cloud4RPi

Picture of Installing Cloud4RPi

I decided to try the cloud platform called Cloud4RPi, which is designed for IoT.

According to the docs, we need the following packages to get it running:

sudo apt install git python python-pip -y

The client library can be installed in a single command:

sudo pip install cloud4rpi

Now we need some sample code to ensure it works.

git clone https://github.com/cloud4rpi/cloud4rpi-common-python && cd cloud4rpi-common-python

I decided to run minimal.py, but I don’t like the fake data. Luckily, I noticed an easy way to make the diagnostic data real in this example.
Add one more import to the imports section:

from rpi import *

Then delete these functions that provide fake data (the rpi.py defines them now):

def cpu_temp():
return 70 def ip_address(): return '8.8.8.8' def hostname(): return 'hostname' def osname(): return 'osx'

Now we need a token, which allows Cloud4RPi to link the devices with the accounts. To get one, create an account on cloud4rpi.io and hit the New Device button on this page. Replace the __YOUR_DEVICE_TOKEN__ string in the minimal.py file with your device’s token and save the file. Now we are ready for the first launch.

python minimal.py

Open the device page and check that the data is there.

Now let’s move to real-world data.

Step 4: Connecting the Sensor

Picture of Connecting the Sensor

We will need:

The DHT22 sensor measures temperature and humidity simultaneously. The communication protocol is not standardized, so we don’t need to enable it in raspi-config — a simple GPIO pin is more than enough.

To acquire the data, I’ll use Adafruit’s great library for DHT sensors, but it may not work as is. I once found a strange constant delay in the code, which did not work for my hardware, and after two years my pull request is still pending. I have also changed the board detection constants because my Raspberry Pi 1 with BCM2835 was surprisingly detected as Raspberry Pi 3. I wish it were true… Thus, I recommend using my fork. If you experience any problems with it, please try the original repository, maybe it works for somebody, but I’m not one of them.

git clone https://github.com/Himura2la/Adafruit_Python_DHT.git
cd Adafruit_Python_DHT

As the library is written in C, it requires compilation, so you need the build-essential and python-dev packages.

sudo apt install build-essential python-dev -y
sudo python setup.py install

While the packages are installing, connect the DHT22 as shown in the picture.

And test it:

cd ~
python -c "import Adafruit_DHT as d; print d.read_retry(d.DHT22, 4)"

If you see something like (39.20000076293945, 22.600000381469727), you should know that this is the humidity in percents and temperature in Celsius.
Now, let’s assemble everything together!

Step 5: Sending Sensor Readings to Cloud

Picture of Sending Sensor Readings to Cloud

I’ll use the minimal.py as a base and add the DHT22 interaction into it.

cp cloud4rpi/examples/common/minimal.py ~/cloud_dht22.py
cp cloud4rpi/examples/common/rpi.py ~/rpi.py vi cloud_dht22.py

As DHT22 returns both temperature and humidity in a single call, I store them globally and update only once in a request, assuming the delay between them is more than 10 seconds. Consider the following code, which acquires the DHT22 data:

import Adafruit_DHT

temp, hum = None, None
last_update = time.time() - 20 
def update_data(): global last_update, hum, temp if time.time() - last_update > 10: hum, temp = Adafruit_DHT.read_retry(Adafruit_DHT.DHT22, 4) last_update = time.time()
def get_t(): update_data() return '%.2f' % temp
def get_h(): update_data() return '%.2f' % hum

Insert this code after the existing imports and edit the variables section so that it uses the new functions:

variables = {
'DHT22 Temp': { 'type': 'numeric', 'bind': get_t }, 'DHT22 Humidity': { 'type': 'numeric', 'bind': get_h }, 'CPU Temp': { 'type': 'numeric', 'bind': cpu_temp } }

Push the red button to start the data transfer:

python cloud_dht22.py

Then you can check the device page.

You can leave it as is, but I prefer having a service for everything. This ensures that the script is always running. Creating a service with the fully automated script:

wget -O - https://raw.githubusercontent.com/cloud4rpi/cloud4rpi/master/service_install.sh | sudo bash -s cloud_dht22.py

Starting the service:

sudo service cloud4rpi start

And checking it:

pi@raspberrypi:~ $ sudo service cloud4rpi status -l
● cloud4rpi.service - Cloud4RPi daemon Loaded: loaded (/lib/systemd/system/cloud4rpi.service; enabled) Active: active (running) since Wed 2017-05-17 20:22:48 UTC; 1min ago Main PID: 560 (python) CGroup: /system.slice/cloud4rpi.service └─560 /usr/bin/python /home/pi/cloud_dht22.py
May 17 20:22:51 raspberrypi python[560]: Publishing iot-hub/messages: {'type': 'config', 'ts': '2017-05-17T20...y'}]}
May 17 20:22:53 raspberrypi python[560]: Publishing iot-hub/messages: {'type': 'data', 'ts': '2017-05-17T20:2...40'}}
May 17 20:22:53 raspberrypi python[560]: Publishing iot-hub/messages: {'type': 'system', 'ts': '2017-05-17T20....4'}}

If everything works as expected, we can go on and use the Cloud4RPi platform’s capabilities to manipulate with the data.

Step 6: Charts and Alarms

Picture of Charts and Alarms

First of all, let’s plot the variables to see how do they change. This can be done by adding a new Control Panel and putting the required charts in it.

Another thing that we can do here is setting an Alert. This feature enables you to configure the safe range for a variable. As soon as the range is overstepped, it sends an email notification. On the Control Panel editing page, you can switch to Alerts and set one up.

Right after that, the humidity in my room started to decrease rapidly without any noticeable reason, and the alarm soon followed.

You can use Cloud4RPi for free with any hardware that is able to execute Python.
As for me, now I always know when to turn the air humidifier on, and I can even connect it to a relay for remote control via Cloud4RPi. I’m prepared for the heat! Welcome, Summer!

Comments

Swansong (author)2017-10-23

This would be a good setup to have in a bathroom or a greenhouse :)

Cloud4RPi (author)Swansong2017-10-25

Christa,

Thank you for your reply.

If you are interested in the use of our service in greenhouses, refer to the "DIY hydroponics system on RaspberryPi" article for more information. It demonstrates how our early adopter grows plants at home.

About This Instructable

1,096views

30favorites

License:

Bio: Cloud control panel for IoT projects
More by Cloud4RPi:How to Control Beer Fermentation Temperature and Gravity From Your SmartphoneHow to Flash MicroPython Firmware on a ESP8266 Based Sonoff Smart SwitchForward Gmail Alerts to Slack
Add instructable to: