Published in


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.

Cloud4RPi Control Panel — Time Series Chart Widget

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.

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.

Etcher — Image Burning Process

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.

Router Stats

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

Connecting to a Raspberry Pi using PuTTY
Error Message When SSH is Disabled

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…

Connect a UART-USB Adapter to the Raspberry Pi

Using a display instead of UART makes it much easier.

Enabling SSH Server

After rebooting, I’m finally in.

Successful Connection to the Raspberry Pi using SSH

First things first, let’s update:

sudo apt update && sudo apt upgrade -y

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

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 && cd cloud4rpi-raspberrypi-python

The executable script is

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

sudo python3

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

Cloud4RPi Device Page — Diagnostics Section

Now let’s move to real-world data.

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

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

DHT22 Connection Scheme

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!

Sending Sensor Readings to Cloud

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

cp cloud4rpi-raspberrypi-python/ ./
cp cloud4rpi-raspberrypi-python/ ./

Remove the sample code:

Sample Code to be Removed — Code Difference in P4Merge Tool

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():
return round(temp, 2) if temp is not None else None
def get_h():
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.

Cloud4RPi Device Page — Variables Section

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-python directory:

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/

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.

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.

Monitoring in Cloud4RPi Control Panel

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.

Cloud4RPi — Setup a Humidity Range Alert

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

The Alert Email

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.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store