Mainflux Edge (part 1.)
This is the first article in series about running Mainflux on the edge, and in this article we will cover basic edge architecture and system provisioning — configuration of the system in order to make it ready for device management and IoT messaging.
We at Mainflux Labs are building one of the world’s most sophisticated IoT system — Unified Cloud-Native IoT Platform. Open-sourced under Apache-2.0 license and published on our GitHub.
Mainflux on the Edge, IoT Gateway
Today’s IoT systems are composed of IoT Edge and Cloud platforms, with numbers of devices coming into play comes greater requirement for moving computation towards edge and decentralization. Moving computation towards edge requires hardware with versatile interfaces that can sense and actuate data from large variety of devices and software that can collect, store and analyze data, organize and authenticate devices preferably running on that very hardware. Such hardware with adequate software is IoT gateway.
Raspberry Pi being highly available and cheap yet with enough computing power and having possibility to connect with wide variety of available sensors has been gaining more and more traction from enthusiasts to professionals especially within IoT niche and makes a perfect candidate for an IoT gateway.
Mainflux IoT platform provides digital twin technology, data aggregation/collection and management, data storage and connectivity management, event management, device management and application enablement and spans over an edge-fog-cloud continuum.
Mainflux IoT platform can be run in the cloud and on the edge which makes it the Unified IoT platform. The same Mainflux code can be deployed on the simple edge IoT gateway and connect and synchronize to the central Mainflux instance running in the cloud.
In this article we will be going through the steps of setting up the minimal Mainflux deployment on Raspberry PI making it a first step towards building Mainflux gateway. We are going to use the minimal deployment of Mainflux services to make it lightweight as much as possible. Deploying Mainflux on gateway will make it able to connect with devices with authorization and authentication and storing data.
Install Raspberry PI OS and Prepare Environment
Download Raspberry Pi OS image from raspberrypi.org, I used 2020–05–27-raspios-buster-lite-armhf.img.
Install OS image to SD card with Raspberry Pi image writing tool rpi-imager.
Configure Raspberry Pi OS that you flashed to SD card to enable WiFi connection and enable ssh access to Raspberry Pi.
Create ssh
and wpa_supplicant.conf
in boot partition on SD card, ssh
is just empty file while wpa_supplicant.conf
contains wireless connection parameters.
Place the SD card into Raspberry Pi (I used Raspberry Pi 3 Model B+). When you power up Raspberry Pi it will connect to the WiFi you provided in wpa_supplicant.conf
and if you are connected with your workstation to the same WiFi network as Raspberry Pi you should be able to ping it:
ping raspberrypi.local
Now, let’s prepare services that we are going to deploy on Raspberry Pi.
Build Docker Images for Raspberry PI
To deploy Mainflux composition on Raspberry Pi we will need to build docker images for Raspberry Pi architecture and transfer images to Raspberry Pi. Currently only amd64
Mainflux images exist on docker hub and we will need images for armhf
architecture to deploy on Raspberry Pi OS.
For deploying Mainflux on Raspberry PI we will use a minimal set of Mainflux services, to make deployment lightweight as many as possible but keep as much features of Mainflux IoT possible like device management (authorization and authentication), data collecting and monitoring with support for essential MQTT protocol.
We will deploy:
mainflux-things
mainflux-mqtt
influxdb-writer
Instead of Verne-MQ MQTT broker which is used when deploying Mainflux in the cloud, we will use Mosquitto broker to avoid trouble of building Verne-MQ for armhf
architecture.
It should be mentioned that Mainflux can be deployed with native executables thus making deployment even more lightweight by removing the overhead of running in docker, however, idea here is to present how easy is to build edge gateway. So we will use docker-compose.
First lets build services. We could build services on Raspberry Pi, but to speed up we will do cross-build, so on your workstation get the code:
git clone https://github.com/mainflux/mainflux
cd mainflux
And then build service images specifying Raspberry Pi specific architecture:
Next, we will need to transfer images to Raspberry Pi so we will save images into the file and then transfer it via SSH.
Setup environment on the Raspberry Pi
Now sway away to Raspberry Pi, ssh into the device and configure for running Mainflux. We will need the following tools:
- Install git
- Install docker and docker-compose
SSH into the device using default credentials for Raspberry Pi OS (pi, raspberry).
ssh pi@raspberrypi.local
Install git
sudo apt install git
Install docker and docker-compose:
Now lets load images that we have previously transferred to Raspberry Pi after we’ve successfully built them, images ended on Raspberry Pi in $HOME
directory:
Now, let’s retrieve files for deploying Mainflux. I have prepared a branch for deployment on Raspberry Pi with docker-compose.yml
modified to use arm
images and stripped to minimum number of services.
Since we are deploying Mainflux on a gateway it is intended to use it to manage device access and data acquisition, there is no need for users management so we stripped users
service from deployment and we need to configure just one user in things
service:
In .env
configure the following variables with credentials as you wish:
MF_THINGS_SINGLE_USER_EMAIL=edge@email.com
MF_THINGS_SINGLE_USER_TOKEN=12345678
Value of MF_THINGS_SINGLE_USER_TOKEN
will be used as user access token for making Mainflux API request.
Now, let’s deploy Mainflux services, give it a few minutes, it needs to pull additional images that we are using.
When the composition is up and running you can create things
that represent your devices that you wish to manage with Raspberry Pi Mainflux powered gateway. Here you can read more on provision.
For making API request you will use MF_THINGS_SINGLE_USER_TOKEN
as Authorization
header value.
Create a thing:
Create a channel:
Connect channel
and thing
, take a note on channel and thing ids, we will need them to make a connection. Connecting thing
and channel
is means of authorizing thing to publish messages to that channel, there is no limit in number of connections each thing
and channel
can have
Now you can start sending messages over MQTT to Raspberry Pi. On your workstation execute the following
Publish with mosquitto
client
On Raspberry Pi check docker logs, you should see the following:
On Raspberry Pi you can observe the messages in InfluxDB
with:
Next would be to deploy Agent
and Export
services to a gateway which will serve as a glue to Mainflux in a cloud and thus enable centralized view and control over a gateway.
We will deal with this in the next article IoT Platform on Raspberry Pi, building an edge gateway (part 2.)
Until next time,
Mirko Teodorović
To learn more about Mainflux and how we’re building the world’s most sophisticated open-source Industrial IoT cloud, visit our website and follow us on Twitter!