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.


No way! SSH is disabled by default

Security is OK, I like it, but this makes things a bit harder. I’ll use UART-USB converter 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.

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 Devicebutton 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.

Connecting the sensor

We will need:

  • DHT22 or DHT11 humidity sensor
  • Wires

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 below.


And test it:

cd ~
python -c "import Adafruit_DHT as d; print d.read_retry(d.DHT22,...
Read more »