Translate

Saturday, March 16, 2019

Set up a Raspberry IoT server



For some projects can be useful to have a place and mean where to collect and store data retrieved form some process.
IoT 101.


There are many IoT services out there, many free, but for some applications is better to have everything close.
Let set up a small and efficient IoT local server based on MQTT protocol running over a Raspberry Pi.

MQTT (Message Queuing Telemetry Transport) is a machine-to-machine (M2M) protocol used to communicate between devices and a server, in order to collect data but also to allow the server to issue commands back to the devices.
There are MQTT libraries and stack for many platforms and of course repositories for a MQTT server.
So an IoT server typically implement an MQTT stack but is not the only part needed to have a working server for IoT.
The second part is something capable to interact with the collected data and do some work on it.
This is done using a code like Node-Red, i.e. a code capable to work on data flow and other generated events.
This software allows to do operations on the received data via MQTT.
Or, look in this way, MQTT is a "pipe" that transport data from the device to the server.
The data received via the pipe need to be used to do something, like creating (and display) charts, do operations on them, store them on DB or files, etc.
The software that do that is a flow-based organizer. Node-Red is an open source visual flow-based code.
Visual because allows to create blocks that performs different operations graphically, via web.

The environment

The goal is to have a local server, running on Linux, where to store data coming from devices.
A Raspberry Pi running Raspbian is a good starting point.
Also a more generic Linux machine can be used, but let start with the Raspberry.

Basically the MQTT stack (called broker, name Mosquitto) will be installed on the Raspberry together with the control/analysis/flow-control server (Node-Red)

Installation of MQTT 

Update the Raspbian then install the MQTT server :

sudo apt install -y mosquitto mosquitto-clients

Then enable it with :

sudo systemctl enable mosquitto.service

To test the installation locally (better after a restart of the Raspberry) :

mosquitto -v

It should return something like that :

$ mosquitto -v
1552330559: mosquitto version 1.3.4 (build date 2018-09-28 22:21:32+0000) starting
1552330559: Using default config.
1552330559: Opening ipv4 listen socket on port 1883.
1552330559: Error: Address already in use

$ 

Installation of Node-Red

The installation is quite straightforward but it involve more deeply modifying the system.
Be sure to have at least a Jessie version of Raspbian and consider that the installation will update npm.
If you have applications using npm it can be disrupted.

To start the installation just log in to the Raspberry, and assuming the installation is done after installing MQTT digit :


bash <(curl -sL https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/update-nodejs-and-nodered)

Just follow the questions.
After the installation is possible to start the Node-Red server with the command :

node-red-start

If no errors are popping out will be possible to access to the Node-Red design page opening a browser and pointing to the Raspberry machine at the port 1880.
For example, assuming the Raspberry has the IP 192.168.1.10, to access the design page will be :

http://192.168.1.10:1880

Note ! By default the design page is NOT password protected ! Is open.
Normally is not a big deal since the idea is to stay anyway behind a firewall.

Basics on Node-Red

Connect MQTT to Node-Red


The first thing to understand is how to link a MQTT "pipe" to a Node-Red flow.
Before to continue I suggest to read some generic info about Node-Red, anyway Node-Red has different blocks, some are input blocks (i.e. capable to receive data from outside), process block (capable to work on the received data) and output blocks, capable to send data to the outside world.
It has also blocks related to the User Interface, i.e. capable to show data and messages on the Website.

Node-Red has an input block for MQTT.
The block has 4 parameters :

  • Server
  • Topic
  • QoS
  • Name
The server field need to be the MQTT server address. In our case the server is the same, so we can use localhost. However MQTT uses the port 1883, so the server address will be : localhost:1883
The Topic field is the name of the "pipe" that transport data.
i.e. the client will name a pipe and the data sent with that name will be sent out.
Node-Red will redirect all the received data with the specific pipe-name to the block with the Topic contains that name.
QoS is Quality of Service, it allows to prioritize the flow in case of multiple pipes on the same flow.
And the name is a field that allows to assign a unique name to the block to better show the relationships on the graphic.



References

There is a lot of documentation online about MQTT and Node-Red.

No comments:

Post a Comment