Introduction: Humidity in the Cloud

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.

Supplies

I am using a Raspberry Pi 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

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.

Great! Let’s go on and SSH into it.

Security is OK, I like it, but this makes things a bit harder. I’ll use UART-USB adapter to access the shell and enable SSH…

Using a display instead of UART makes it much easier.

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 2: 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 python3 python3-pip -y

The client library can be installed in a single command:

sudo pip3 install cloud4rpi

Now we need some sample code.

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

The executable script is control.py.

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 control.py file with your device’s token and save the file. Now we are ready for the first launch.

sudo python3 control.py

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

Now let’s move to real-world data.

Step 3: Connecting the Sensor

We will need:

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 4: Sending Sensor Readings to Cloud

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

cp cloud4rpi-raspberrypi-python/control.py ./cloud_dht22.py
cp cloud4rpi-raspberrypi-python/rpi.py ./rpi.py
vi cloud_dht22.py

Remove the sample code like in image above.

As DHT22 returns both temperature and humidity in a single call, I’m going to 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 round(temp, 2) if temp is not None else None def get_h(): update_data() return round(hum, 2) if hum is not None else None

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 } }

If you find that manipulations confusing, grab the final version of this file.
Push the red button to start the data transfer:

Then you can check the device page.

python3 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 that you already have in the cloud4rpi-raspberrypi-pythondirectory:

service_install.sh 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 5: 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!

With Cloud4RPi, you can control your Raspberry Pi and other IoT devices remotely in real time.
Visit our site and connect unlimited devices for free.