Electronic design professionals can leverage worldwide “maker” trend and use off-the-shelf hard- and software to kick off an IoT business project. Popular Raspberry Pi computer board is a good example. This article provides practical guidance and explains how Raspberry Pi can be used to build a compelling IoT node mockup for

  • manufacturer-independent evaluation purposes,
  • feasibility studies or
  • demonstration for management and/or customers

Raspberry Pi (short: “RasPi“) is a small versatile play-and-play computer board which works out-of-the-box and offers common interfaces for users and peripherals. It comes with an onboard OS and many generic software ingredients for all kind of embedded computing applications. Customers can add application-specific circuitry via I/O pins or use I²C bus which is offered by most IoT peripherals like sensors, controllers, secure elements, etc.

IoT (“Internet of Things”) stands for network applications with IoT devices (“nodes”) used at unattended locations. Typical IoT applications are monitoring and reporting remote conditions. By nature, IoT operators usually do not sit in front of IoT devices as these nodes are located at multiple places where the remote IoT work is done. Consequently, IoT devices must offer a couple of fundamental functions:

  • (wireless) network connectivity
  • “headless” design (i.e. no display, keyboard or mouse)
  • remote control for operators
  • forwarding and visualization of locally collected IoT data
  • (mutual) authentication and secure messaging
  • automatic startup and independent, autonomous operation
  • application-specific function block

Amazingly, Raspberry Pi meets all these requirements out-of-box and free-of-charge. In addition, it offers user controllable GPIOs for customization allowing users to add an application-specific subsystem quickly. Dedicated onboard OS functions plus easy-to-learn Python programming language are offering flexible hardware control. This is why RasPi works fine as a generic IoT node, although in most cases it will not be considered for a large-scale commercial IoT roll-out (e.g. because of price, CPU, board dimensions, security). But an IoT node mockup based on RasPi is an efficient approach for evaluation and demonstration and might be worthwhile to consider esp. for startups or IoT newbies. It can be used in pre-selection project stage before you buy evaluation boards or a design kit from a specific manufacturer.

In this article we are introducing our RasPi IoT node, a simple project based on a simple IoT application, but many fundamental IoT functions have been covered: headless WLAN design, remote control, webserver, stand-alone operation. Our RasPi IoT node project is a “toolbox“, elements can be used as a starting point for a custom mockup design according to your needs. As a sample IoT application for this project we used an environmental sensor (humidity, temperature).

Setup of Rasberry Pi as a generic IoT node

Fortunately, latest RasPi versions (Pi3 or Pi4) have onboard WLAN function, so you can easily prepare it for mentioned headless operation which is standard operation mode for IoT devices. As a start and in order to prepare for wireless operation we have to connect a USB keyboard, USB mouse and HDMI display to our RasPi board. A new RasPi should boot with a graphical user interface after power up. For headless operation three actions are required:

  1. Enable WLAN. Click on the wireless network icon in the top right-hand corner of the screen, select your network from the drop-down menu and enter associated security key. This will enable WLAN connection of your RasPi IoT node mockup you might want to verify right away using built-in Internet browser.
  1. Identify RasPi’s IP address. Click on berry icon in top left-hand corner and use pull down menu to navigate via “Accessories” tap to “Terminal”, click on it to open a terminal session. Command hostname - I returns IP address 192.168.178.31
  1. Change default UI to command line interface (“CLI”). Click on berry icon in top left-hand corner and use pull down menu to navigate via “Preferences“ to „Raspberry Pi Configuration“. Besides other stuff you should specify a friendly hostname, e.g. „iotnode“. As a start it makes sense to select “Auto Login” “as “Login as user shutdown pi”. Under “Interfaces” enable I2C and SSH. Finally, you change „Boot“ option to „To CLI“.

Then shutdown your RasPi, remove display, keyboard and mouse and reboot. Now you can use a PC to connect via WLAN to your RasPi IoT node mockup. Windows as well as MacOS come with onboard terminal programs offering command line interface for users. A built-in SSH client is being used to connect securely to a remote computer. Enter ssh pi@192.168.178.31 or ssh pi@iotnode followed by assigned password for user “pi”, then you enjoy full remote control via command line interface and access to complete RasPi OS (Debian) command set which is based on GNU/Linux.

Now we can start to customize our RasPi IoT node hard- and software according to our needs.

Hardware Setup – Humidity/Temperature sensor on I2C Bus

For our RasPi IoT node project we have chosen a popular IoT application: remote environmental sensing, so we have to add a few components. Related RasPi hardware extension is done via GPIO pins available on a 40-pin connector. It is a good idea to use a small standard breadboard and to connect it with a flat ribbon cable (see Figure 1).

IoT node mockup

Figure 1: RasPi IoT node Project Hardware (© chip.info.com)

Used sample sensor is HTU21D (manufacturer is TE Connectivity) resp. Si7021-A20 (manufacturer is Silicon Labs) which are functionally equivalent. Datasheet is telling us how to connect the sensor to an I2C bus.

IoT node mockup

Figure 2: Si7021 I2C connection (© Silicon Labs)

In order to simplify breadboard wiring you can buy ready-to-use modules from many local dealers, e.g. like SparkFun https://www.sparkfun.com (in the US) or https://cheaptronic24.de (in Germany). Connection with I2C bus can be done with 4 wires only.

IoT node mockup

Figure 3: I2C sensor module (© chip.info.com)

Note: For those of you who are interested in more humidity sensors available on the market today, see this article on www.chip-info.com: http://www.chip-info.com/articles/digital-humidity-sensors

HW initialization and functional test

Let us do this step-by-step. As a first step we use onboard i2c-tools package which allows you to check if your wiring has been done correctly and which I2C device address is being used:

sudo apt install -y i2c-tools # install package
sudo i2cdetect -y 1 # detect all devices on bus no. 1
IoT node mockup

Our sensor board has been found on I2C address 0x40, as expected.

Now we move to another package called pigpio which is a powerful OS extension for control of GPIOs incl. I2C bus. pigpio services are based on a library to be launched as a daemon before. See full documentation here: http://abyz.me.uk/rpi/pigpio/index.html. As a start, we use the pigs tool which allows us to trigger I2C transactions from command line level. Start pigpio library, then start it for use with pigs tool:

sudo apt-get install pigpio
sudo pigpiod # start pigpio library and daemon

For a first functional verification we extract a short list of relevant I2C sensor transactions from Si7021-A20 datasheet. These commands we convert into following pigs commands (see full command set here: http://abyz.me.uk/rpi/pigpio/pigs.html):

  1. get handle for device on bus no. 1, here returns “1”
  2. reset device (command: “0xFE”),
  3. trigger temperature measurement (command: “0xF3”),
  4. read sensor output, 3 bytes (MSB, LSB, CRC)

We enter these four commands one-by-one using pigs tool, results are returned on OS command level interface:

IoT node mockup

In this case, returned temperature code is 0x64 (MSB) and 0x3C (LSB) which is 25660 decimal. Conversion into temperature value using formula from datasheet: (175,72 * 25660 / 65536) – 46,85 = 21,95° Celsius. It seems that our sensor returns reasonable results.

As a next step we create a short Python program which is returning actual humidity and temperature values. For this purpose we just have to convert above experience with pigs tool into Python code, both using same pigpio library. In fact, Python is an easy-to-learn interpreted language, see full manual here: https://docs.python.org. Our demo program sense.py performs measurement of actual relative humidity and temperature at current location of our RasPi IoT node.

IoT node mockup

Invoking sense.py will display at Linux command line interface like this:

Visualization of IoT data (web server)

At this stage, RasPi IoT node data is visible only to use superuser pi@iotnode via SSH interface. In order to extend visibility (or to create a convincing IoT demo) publication of IoT data on a dedicated web page is beneficial. Amazingly, RasPi offers an onboard web server called NGNIX allowing quick implementation – even by non-experts. This is how to install it:

sudo apt update
sudo apt install nginx

Now we have to configure NGINX for first use. For our RasPi IoT node project we put a site-specific configuration file in directory /etc/nginx/sites-available. As a reference we use file default as a starting point, here we called the new configuration file iotnode similar to selected hostname of our RasPi IoT node. Use onboard editor nano:

sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/iotnode
sudo nano /etc/nginx/sites-available/iotnode

The original default file is specifying a lot of things we do not care about at this time. For the initial version of iotnode we just look for relevant parameters server_name, root and index and modify them according to our requirements:

server_name iotnode;
root /var/www/iotnode;
index index.html;

Next, we have enable configuration file iotnode . For this purpose we have to put a symbolic link (symlink) to another directory in place, this directory is called /etc/nginx/sites-enabled. At the same time we have to remove the existing link to original configuration default:

sudo unlink /etc/nginx/sites-enabled/default
sudo ln -s /etc/nginx/sites-available/iotnode /etc/nginx/sites-enabled/

The relevant directory subtree below /etc/nginx now should look like this:

Creation of initial “Hello World” web page

You might have noticed that our new NGINX configuration file /etc/nginx/sites-available/iotnode is referring to a file called /var/www/iotnode/index.html. This does not exist yet, so let us create it now. First we have to create the target directory, then we use nano to edit index.html:

sudo mkdir /var/www/iotnode
sudo nano /var/www/iotnode/index.html

As a start, this this sample index.html will display a simple “Hello World!” message: 

<!DOCTYPE html>
<html>Hello World!</html>

Now you can perform a final test and reload the NGNIX configuration:

sudo nginx -t
sudo nginx -s reload

Then, the initial “Hello World” version of our RasPi IoT node project website will display when you navigate to http://iotnode (or to http://192.168.178.31).

Integration of IoT data into web page

For Raspberry Pi there are several add-ons for creation of professional web pages and import of Python code available which you can use for your RasPi IoT node mockup. But for our project a simple approach based on standard Debian/Linux functions is sufficient. This idea is to redirect the screen output of sense.py to mentioned index.html file. For this purpose we have to incorporate a couple of print commands adding suitable HTML code to the output of sense.py. Result at OS command line interface will look like this:

IoT node mockup

Please note that we added a timestamp to sense.py output which is originated from onboard time module. In addition we used built-in Python function round to limit displayed temperature and humidity values to two decimal places. You can download the complete sense.py sample code here and use it as a starting point for your own mock-up project:

After execution of sense.py another two Linux commands are needed to 1) redirect sense.py out to file output.html and to 2) overwrite the initial “Hello World” homepage (index.html) of our RasPi IoT Node we configured before (see Visualization of IoT Data – Web Server ). At the same time we consolidate all these commands in a shell script, so we will end up with just one single command: sense.sh which will execute as an endless loop:

#!/bin/bash
while true
do
        sudo python /home/pi/sense.py > output.html #redirect sense.py output to file
        sudo cp /home/pi/output.html /var/www/iotnode/index.html #overwrite index.html homepage
        sleep 10 # wait 10 seconds for demo loop execution
done

In addition, we have to declare sense.sh as executable using chmod +x sense.sh. Then start the script using sudo ./sense.sh.

From now on and whenever you navigate with a browser to http://iotnode (or to http://192.168.178.31) up-to-date measurement data of your RasPi IoT Node will be displayed in a user-friendly way. It shows actual IoT data values and timestamp refreshed after every 10 seconds:

IoT node mockup

Use Control-C to stop script execution and return to command line prompt.

Setup for Autostart

In real life an IoT device will be fully operational after power-up, i.e. without any initial interaction via SSH. In order to prepare your mockup for automatic startup you will have to edit file /etc/rc.local and add required commands to be executed at boot time.

sudo pigpiod # start pigpio daemon
cd /home/pi
sudo ./sense.sh & # start script to execute sense.py endlessly

Don’t forget the “&”, otherwise the boot process will not finish because sense.sh runs infinitely. The ampersand allows sense.sh to run in a separate process and continue booting with the main process running.

A few words about IoT Security

For your mockup you do not have to address its vulnerability against any kind of hacking or misuse. But if you plan to leverage it for use in a deployable business application, you will have to secure communication channels for remote control and IoT data deployment. Good news is that used SSH protocol of Raspberry Pi OS offers user authentication based on public key cryptography which allows you protect remote control against password hacks.

Web communication can be secured using onboard SSL/TLS protocol of used NGINX web server. SSL/TLS manages authentication of communication partners using public key cryptography and associated key certificates, at least for the server. In our case, server authentication is sufficient because only IoT data is transmitted, i.e. we have a one-way communication anyway. Transmitted data will be encrypted with a unique session key. But for interactive IoT use cases mutual authentication might be required which is also supported by TLS implementation included in NGNIX. For strongest tamper-resistance of cryptographic keys use of a secure element might be necessary. A secure element is a piece of extra hardware which is controlled via I²C, too. So you can easily add a secure element to your RasPi IoT node mockup same way as described in this article. For further information about secure elements see this article here on www.chip-info.com: http://www.chip-info.com/articles/iot-secure-elements.