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.
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-raspberrypi-python.git && cd cloud4rpi-raspberrypi-python git clone https://gist.github.com/f8327a1ef09ceb1ef142fa68701270de.git e && mv e/minimal.py minimal.py && rmdir -r e
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 '18.104.22.168' def host_name(): return 'hostname' def os_name(): 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.
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
- Pull-up resistor (5-10 KΩ)
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.