You are on page 1of 99

Home Automation Platform Based On Node-RED

A Degree Thesis
Submitted to the Faculty of the
Escola Tècnica d'Enginyeria de Telecomunicació de
Barcelona
Universitat Politècnica de Catalunya
by
Andreu Soler Mascarell

In partial fulfilment
of the requirements for the degree in
TELECOMMUNICATIONS TECHNOLOGIES AND
SERVICES ENGINEERING

Advisor: Anna Calveras Augé

Barcelona, June 2021


Abstract
In this final degree project, we are introduced to the wide world of IoT. The rise of this
concept and the technology related to it, makes everyone want to have a smart home. For
this purpose, different environmental monitoring devices, smart switches, smart lights,
smart locks, etc. are equipped.

But in order to control and monitor them, it is necessary to have control panels and a
platform that performs all the integration.

That is why this project will integrate Node-RED and its own control panel, saving the data
obtained in a database (InfluxDB) and being able to represent them in another type of
visualisation (Grafana). The concept of dockers and the installation involved were also
investigated.

After carrying out multiple proofs of concept, we obtained a very complete device
integration system, at a reduced cost and ideal for adding any smart device we want.

1
Resum
En aquest treball de final de grau ens introduïm en l’ampli món de l’IoT. L’auge d’aquest
concepte i la tecnologia relacionada amb ell fa que tothom vulgui tenir una casa intel·ligent.
Amb aquest propòsit, s’equipen diferents dispositius de control ambiental, interruptors
intel·ligents, llums intel·ligents, panys intel·ligents, etc.

Però per poder controlar-los i monitoritzar-los, és necessari disposar de panells de control


i una plataforma que realitzi tota la integració.

És per això que aquest projecte integrarà Node-RED i el seu propi tauler de control,
guardant les dades obtingudes en una base de dades (InfluxDB) i podent representar-les
en un altre tipus de visualització (Grafana). També es va investigar el concepte de dockers
i la instal·lació que implica.

Després de realitzar múltiples proves de concepte, obtenim un sistema d'integració de


dispositius molt complet, de cost reduït i ideal per afegir qualsevol dispositiu intel·ligent
que vulguem.

2
Resumen
En este trabajo de fin de grado, nos introducimos en el amplio mundo IoT. El auge que
está teniendo este concepto y la tecnología relacionada con ella, ocasiona que cualquier
persona quiera tener un hogar inteligente. Para ello, se equipan distintos dispositivos de
monitoreo ambiental, interruptores inteligentes, luces inteligentes, cerraduras inteligentes,
etc.

Pero para poder controlarlos y monitorearlos, es necesario contar con paneles de control
y una plataforma que realice toda la integración.

Es por ello que en este proyecto se realizará una integración con Node-RED y su propio
panel de control, guardando los datos obtenidos en una base de datos (InfluxDB) y
pudiéndolos representar en otro tipo de visualización (Grafana). También se ha indagado
en el concepto de los dockers y la instalación que ello conlleva.

Tras realizar múltiples pruebas de concepto, obtenemos un sistema de integración de


dispositivos muy completo, de coste reducido e ideal para añadir cualquier dispositivo
inteligente que queramos.

3
Acknowledgements
I would like to express my gratitude to several people. First of all, I want to thank my advisor
Anna Calveras for giving me the opportunity to carry out such an interesting project and
constantly helping me during the development of the project.

Of course, I would like to thank my friends who I have met at university for all the good
times that we have had during these years and that I hope to have many more.

Lastly, I would like to thank my family for trusting me and my decisions throughout all these
years.

To all of you, thank you.

4
Revision history and approval record

Revision Date Purpose

0 11/05/2021 Document creation

1 16/05/2021 Document implementation

2 29/05/2021 Document implementation

3 11/06/2021 Document implementation

4 18/06/2021 Document revision

DOCUMENT DISTRIBUTION LIST

Name e-mail

Andreu Soler Mascarell andreu.soler.mascarell@estudiantat.upc.edu

Anna Calveras Augé anna.calveras@upc.edu

Written by: Reviewed and approved by:

Date: 18/06/2021 Date 18/06/2021

Name: Andreu Soler Mascarell Name Anna Calveras Augé

Position: Project Author Position Project Supervisor

5
Table of contents
.

Abstract ............................................................................................................................................. 1
Resum ............................................................................................................................................... 2
Resumen ........................................................................................................................................... 3
Acknowledgements ........................................................................................................................... 4
Revision history and approval record ................................................................................................ 5
Table of contents ............................................................................................................................... 6
List of Figures .................................................................................................................................... 8
List of Tables: .................................................................................................................................. 10
1. Introduction .............................................................................................................................. 11
1.1. Statement of purpose ...................................................................................................... 11
1.2. Requirements and specifications .................................................................................... 11
1.3. Methods and procedures ................................................................................................. 12
1.4. Work plan with tasks, milestones and a Gantt diagram .................................................. 12
1.4.1. Workplan packages ................................................................................................. 12
1.4.2. Milestones................................................................................................................ 15
1.4.3. Gantt Diagram ......................................................................................................... 16
1.5. Deviations from the initial plan and incidences ............................................................... 16
1.6. Final Degree Thesis ........................................................................................................ 17
2. State of the art of the technology used or applied in this thesis: ............................................. 18
2.1. Internet of Things (IoT) .................................................................................................... 18
2.2. MQTT............................................................................................................................... 19
2.3. Node-RED ....................................................................................................................... 19
2.4. NPM ................................................................................................................................. 19
2.5. InfluxDB ........................................................................................................................... 20
2.6. Grafana ............................................................................................................................ 20
2.7. Docker ............................................................................................................................. 21
3. Methodology / project development: ....................................................................................... 22
3.1. Hardware Prototype......................................................................................................... 22
3.1.1. Raspberry Pi ............................................................................................................ 22
3.1.2. ESP32 with BMP280 ............................................................................................... 23
3.1.3. ESP32 with LED ...................................................................................................... 23
3.2. Home Automation Platform Comparison ......................................................................... 24
3.2.1. Introduction .............................................................................................................. 24
3.2.2. IOT Platform ............................................................................................................ 24

6
3.2.3. Platform Comparison ............................................................................................... 25
3.2.4. Final Implementation ............................................................................................... 28
3.3. Proof of Concept without dockers ................................................................................... 29
3.3.1. Install Raspberry Pi OS ........................................................................................... 29
3.3.2. Installing and running Node-RED on Raspberry Pi ................................................. 29
3.3.3. Installing MQTT Broker ............................................................................................ 33
3.3.4. Testing the Proof of Concept ................................................................................... 37
3.3.5. InfluxDB Installation ................................................................................................. 43
3.3.6. Grafana Installation ................................................................................................. 46
3.3.7. Add Influx as a Grafana data source ....................................................................... 47
3.3.8. Configuration of InfluxDB on NodeRED .................................................................. 49
3.3.9. Collecting some data with Grafana ......................................................................... 50
3.4. Dockers for IoT ................................................................................................................ 52
3.4.1. Introduction to Docker ............................................................................................. 52
3.4.2. Benefits of Using Docker ......................................................................................... 52
3.4.3. Why the IoT would use Docker (Benefits concretizing in the use of the IoT) .......... 54
3.5. Proof of Concept with dockers ........................................................................................ 55
3.5.1. Docker Installation ................................................................................................... 55
3.5.2. Docker MQTT Installation ........................................................................................ 57
3.5.3. Docker NodeRED Installation .................................................................................. 60
3.5.4. Docker InfluxDB Installation .................................................................................... 61
3.5.5. Docker Grafana Installation ..................................................................................... 62
3.6. Proof of Concept with other platforms (ThingsBoard CE) ............................................... 63
3.6.1. Install Raspberry Pi OS ........................................................................................... 63
3.6.2. Install Third-Party components (Java 11) ................................................................ 63
3.6.3. Install ThingsBoard Software................................................................................... 63
3.6.4. Install ThingsBoard database (PostgreSQL) ........................................................... 64
3.6.5. ThingsBoard Configuration service ......................................................................... 65
3.6.6. Run script and start ThingsBoard service ............................................................... 66
4. Results ..................................................................................................................................... 68
4.1. Screenshots final design & visualization ......................................................................... 68
4.2. MQTT Broker Tests ......................................................................................................... 71
4.3. Performance .................................................................................................................... 73
4.3.1. Raspberry Pi CPU & Memory Test .......................................................................... 73
4.3.2. Raspberry Pi memory storage ................................................................................. 75
4.3.3. MQTT connections test ........................................................................................... 75
5. Budget ..................................................................................................................................... 78

7
6. Conclusions and future development: ..................................................................................... 80
Bibliography: .................................................................................................................................... 81
Appendices: ..................................................................................................................................... 84
Appendix 1: Raspberry Pi Installation ............................................................................................. 84
Appendix 2: Node-RED Flow .......................................................................................................... 91
Appendix 3: ESP32 & BMP280 code for Node-RED ...................................................................... 92
Appendix 4: ESP32 & LED code for Node-RED ............................................................................. 95
Glossary .......................................................................................................................................... 98
.

List of Figures
Figure 1. Gantt diagram .................................................................................................................. 16
Figure 2. IoT application. Source [38] ............................................................................................. 18
Figure 3. MQTT structure. Source [3].............................................................................................. 19
Figure 4. Grafana example. Source [8] ........................................................................................... 20
Figure 5. Hardware design .............................................................................................................. 22
Figure 6. BMP280 connections ....................................................................................................... 23
Figure 7. LED connections .............................................................................................................. 23
Figure 8. OpenHAB example. Source [13] ...................................................................................... 25
Figure 9. Home Assistant example. Source [13] ............................................................................. 25
Figure 10. ThingsBoard example. Source [13] ................................................................................ 26
Figure 11. Node-RED example. Source [13] ................................................................................... 27
Figure 12. PoC without dockers design........................................................................................... 29
Figure 13. Command: bash node-red ............................................................................................. 30
Figure 14. Node-RED package installation ..................................................................................... 30
Figure 15. Node-RED manage palette (1) ...................................................................................... 31
Figure 16. Node dashboard modules installation (1) ...................................................................... 32
Figure 17. Node modules installation (2)......................................................................................... 32
Figure 18. Node modules installed on pallete ................................................................................. 32
Figure 19. Node-RED dashboard modules ..................................................................................... 33
Figure 20. Node-RED manage palette (2) ...................................................................................... 35
Figure 21. Node dashboard modules installation (2) ...................................................................... 35
Figure 22. MQTT Aedes node-red module ..................................................................................... 35
Figure 23. Configurating Aedes MQTT Broker ................................................................................ 36
Figure 24. MQTT Broker test with Node-RED ................................................................................. 37
Figure 25. Proof of Concept Node-RED flow .................................................................................. 37
Figure 26. Aedes Broker configuration ............................................................................................ 38

8
Figure 27. Inject node configuration ................................................................................................ 38
Figure 28. Function node configuration ........................................................................................... 39
Figure 29. MQTT publish node configuration .................................................................................. 39
Figure 30. MQTT Broker server configuration ................................................................................. 40
Figure 31. MQTT subcribe node configuration ................................................................................ 40
Figure 32. JSON node configuration. .............................................................................................. 41
Figure 33. Node-RED dashboard nodes configuration ................................................................... 41
Figure 34. Node-RED data reception .............................................................................................. 42
Figure 35. Node-RED Dashboard visualization .............................................................................. 42
Figure 36. InfluxDB vs SQLite. Source [22] .................................................................................... 43
Figure 37. Grafana relative time ranges .......................................................................................... 44
Figure 38. Grafana welcome menu ................................................................................................. 47
Figure 39. Influx as a Grfana data source ....................................................................................... 47
Figure 40. InfluxDB ip on Grafana ................................................................................................... 48
Figure 41. InfluxDB Access on Grafana .......................................................................................... 48
Figure 42. InfluxDB node modules .................................................................................................. 49
Figure 43. InfluxDB node configuration ........................................................................................... 49
Figure 44. Node-RED real data reception ....................................................................................... 50
Figure 45. InfluxDB database data .................................................................................................. 50
Figure 46. Grafana dashboard creation (1) ..................................................................................... 50
Figure 47. Grafana dashboard creation (2) ..................................................................................... 51
Figure 48. Grafana dashboard creation (3) ..................................................................................... 51
Figure 49. Command: curl docker ................................................................................................... 55
Figure 50. Verify docker version ...................................................................................................... 56
Figure 51. Upgrade docker package ............................................................................................... 57
Figure 52. Docker pull mosquitto ..................................................................................................... 57
Figure 53. Verify mqtt container is up.............................................................................................. 59
Figure 54. Check mosquitto is running correctly ............................................................................. 59
Figure 55. Configure mqtt user & password .................................................................................... 60
Figure 56. MQTT password encrypted ............................................................................................ 60
Figure 57. Docker pull Node-RED ................................................................................................... 60
Figure 58. Java version installed ..................................................................................................... 63
Figure 59. Verify PostgreSQL is running ......................................................................................... 64
Figure 60. PostgreSQL databases .................................................................................................. 65
Figure 61. ThingsBoard installation script ....................................................................................... 66
Figure 62. Verify ThingsBoard is running ........................................................................................ 66
Figure 63. ThingsBoard welcome menu.......................................................................................... 67

9
Figure 64. ThingsBoard main screen .............................................................................................. 67
Figure 65. Node-RED Dashboard (mobile visualisation) ................................................................ 68
Figure 66. Grafana Real-Time data dashboard (computer format) ................................................. 69
Figure 67. Grafana historical data dashboard (computer format) ................................................... 69
Figure 68. Grafana Real-Time data dashboard (mobile format) ..................................................... 70
Figure 69. Grafana historical data dashboard (mobile format) ....................................................... 70
Figure 70. Execute shell mosquitto container ................................................................................. 71
Figure 71. MQTTBox public broker subscription ............................................................................. 71
Figure 72. MQTTBox publish to public broker ................................................................................. 71
Figure 73. Mosquitto container public broker subscription .............................................................. 72
Figure 74. Mosquitto container local broker publishing ................................................................... 72
Figure 75. MQTTBox local broker subscription ............................................................................... 72
Figure 76. Figure 72. MQTTBox publish to local broker ................................................................. 72
Figure 77. Mosquitto container local broker subscription ................................................................ 73
Figure 78. htop command initalizing the system ............................................................................. 73
Figure 79. htop command with system stabilized (1) ...................................................................... 73
Figure 80. htop command with system stabilized (2) ...................................................................... 74
Figure 81. htop command with Grafana influxdb query .................................................................. 74
Figure 82. Raspberry Pi memory storage ....................................................................................... 75
Figure 83. Mqtt-stresser publishing results: Test condition: client=10, Messages=100 ................. 75
Figure 84. Mqtt-stresser receiving results: Test condition: client=10, Messages=100 ................... 76
Figure 85. Mqtt-bench receiving results: Test condition: client=10, Messages=100 ...................... 76
Figure 86. mqtt-bench command example ...................................................................................... 76
Figure 87. CPU effect of multiple mqtt connections ........................................................................ 77
Figure 88. Raspberry Pi Imager ...................................................................................................... 84
Figure 89. Raspberry Pi OS selection ............................................................................................. 85
Figure 90. Writing OS to SD card .................................................................................................... 85
Figure 91. Localising Raspberry Pi IP ............................................................................................. 87

List of Tables:
Table 1. Workplan packages ........................................................................................................... 15
Table 2. Milestones ......................................................................................................................... 16
Table 3. MQTT comparison. Source[18] ......................................................................................... 34
Table 4. Budget: variable costs ....................................................................................................... 78
Table 5. Budget: wages ................................................................................................................... 79
Table 6. Budget: fixed costs ............................................................................................................ 79

10
1. Introduction

1.1. Statement of purpose


The project is carried out in the GRETST degree of the ETSETB school of the UPC by the
department of telematics engineering.
The purpose of this project consists on developing a Home Automation platform in a
Raspberry Pi. The goal of the project is to develop a proof of concept by integrating several
sensors and actuators.
This low-cost home automation system will allow the integration of dashboards for data
visualization and interaction with the different sensors. One possibility is the use of the
mobile phone for this purpose, such as instantly controlling your home, such as receiving
alerts of unexpected events.
This project proposes the development of a home automation platform on a Raspberry Pi,
in which several sensors will be integrated. This platform will allow the integration of
dashboards that will allow the visualization of the data and the interaction with the different
sensors. One possibility is the use of mobile for this purpose.

The project main goals are:


- Self-installed sensors and actuators (BMP280, led strip, servo, pyroelectric motion
sensor, etc).
- Self-control of the home automation system (via ESP-32 and Raspberry Pi)
- Programmable logic to connect sensors with actions.
- A graphical interface to display sensor data.
- Server accessible from the local network, from any device via HTTP.
- Alarm functions (sending alerts by email, telegram, etc.).
- Visual interaction with a smartphone via http.

1.2. Requirements and specifications


Project requirements:
- Local IoT server for home use
- Visualization of sensor data in real time.
- Data storage for later interpretation.
- Notification of unexpected events instantly.

Project specifications:

11
- Operational open-source home automation software.
- Interconnection between sensors and actuators with the platform installed on the
raspberry using standard protocols such as MQTT or HTTP protocols.
- Operational database.
- Operational dashboard
- Alert notification.
- Access via smartphone

1.3. Methods and procedures


The project starts from scratch being a mix between an independent project and a
department development project, since the concepts and developments discussed will be
useful, both for the department itself and for teaching university subjects.
The idea of the project is based on an interest on the part of the student for the IoT concept
and all that it entails. From this point, together with the supervisor, the project to be carried
out was agreed.
This project is based on research, information filtering (which can be obtained online via
tutorials and information on the software tools that will be implemented) and development
of a low-cost domestic IoT network, for anyone with a minimal technological knowledge to
implement it.

1.4. Work plan with tasks, milestones and a Gantt diagram

1.4.1. Workplan packages

Project: Platform comparison WP ref: WP1

Major constituent: Research Sheet 1 of 6

Short description: Planned start date:11/02/2021


Compare open-source home automation platforms Planned end date: 01/03/2021
and select the ones that best fit the required
characteristics. Start event: 11/02/2021
End event: 08/03/2021

Internal task T1: Compare multiple platforms Deliverables: Dates:


Internal task T2: Take the decision of platforms to be Doc 10/03/2021
used Comparison

Project: Hardware prototype WP ref: WP2

12
Major constituent: hardware prototype Sheet 2 of 6

Short description: Planned start date: 08/03/2021


Install sensors and actuators (BME280, led strip) with Planned end date: 29/03/2021
the ESP32 and connect it to Raspberry Pi using
MQTT protocol. Start event: 19/04/2021
End event: 08/05/2021

Internal task T1: Connect BME280 and LEDs to Deliverables: Dates:


ESP32.
Summary 10/05/2021
Internal task T2: Write code ESP32. documentation
Internal task T3: Connect ESP32 to Raspberry Pi via
MQTT.
Internal task T4: Hardware validation.

Project: NodeRED Platform (Proof of Concept) WP ref: WP3

Major constituent: Software simulation Sheet 3 of 6

Short description: Planned start date: 22/02/2021


Install NodeRED and their dependencies to receive, Planned end date: 26/04/2021
process, interpret and visualize the data coming from
the sensor and warn of unexpected events. Start event: 22/02/2021
End event: 26/04/2021

Internal task T1: Install and configure Raspberry Pi Deliverables: Dates:


and NodeRED dependencies.
Demo T1&2 04/03/2021
Internal task T2: Install and configure node
Demo T3&4 05/04/2021
complements of NodeRED (MQTT, Dashboard, email
and Telegram alerts, etc.) Demo T5 12/04/2021

Internal task T3: Receive ESP32 data in real time. WP3 Summary 26/04/2021

Internal task T4: Treat and interpret the data for the
dashboard visualization.
Internal task T5: Alert's notification.
Internal task T6: WP validation.

13
Project: ThingsBoard CE Platform (Proof of WP ref: WP4
Concept)

Major constituent: Software simulation Sheet 4 of 6

Short description: Planned start date: 19/04/2021


Install ThingsBoard Community Edition and their Planned end date: 24/05/2021
dependencies to receive, process, interpret and
visualize the data coming from the sensor and warn Start event: 01/06/2021
of unexpected events. End event: 14/06/2021

Internal task T1: Install and configure Raspberry Pi Deliverables: Dates:


and ThingsBoard dependencies.
Demo T1&2 05/06/2021
Internal task T2: Install and configure complements of
Demo T3&4 10/06/2021
ThingsBoard.
Demo T5 14/06/2021
Internal task T3: Receive ESP32 data in real time.
WP4 Summary 14/06/2021
Internal task T4: Treat and interpret the data for the
dashboard visualization.
Internal task T5: Alert's notification.
Internal task T6: WP validation.

Project: Performance Improvement WP ref: WP5

Major constituent: Software Sheet 5 of 6

Short description: Planned start date: 17/05/2021


Make improvements to optimize performance and Planned end date: 14/06/2021
apply new functionalities to the system, which
although they are not essential, will give the option of Start event: 22/03/2021
greater use of it. End event: 31/05/2021

Internal task T1: Databases Deliverables: Dates:


Internal task T2: Docker (Proof of Concept 3 & 4) Database 31/05/2021
implementation
Internal task T3: Possible improvements
Docker
31/05/2021
implementation

14
Project: Documentation WP ref: WP6

Major constituent: Documentation Sheet 6 of 6

Short description: Planned start date: 01/03/2021


This work package consists of the TFG report writing Planned end date: 18/06/2021
tasks, where the documents to be delivered and the
summaries of each package will be documented. Start event: 01/03/2021

The entire implementation process and End event: 18/06/2021


documentation to be delivered will be documented
weekly.

Internal task T1: TFG Proposal and Workplan Deliverables: Dates:


Internal task T2: Project Critical Review Project Proposal 08/03/2021
Internal task T3: Document of final degree thesis Critical Review 14/04/2021
Internal task T4: Summary of work packages Final project Doc 18/06/2021
Internal task T5: Validation of WP tasks.
Table 1. Workplan packages

1.4.2. Milestones

WP# Task# Short title Milestone / deliverable Date (week)

WP1 T1,2 Platform comparison Platform Comparison 10/02/2021 (W5)

WP2 T1,2,3 Hardware prototype Hardware Summary 10/05/2021 (W14)

WP3 T1,2 NodeRED Platform Demo T1&2 04/03/2021 (W4)

WP3 T3,4 NodeRED Platform Demo T3&4 05/04/2021 (W5)

WP3 T5 NodeRED Platform Demo T5 12/04/2021 (W10)

WP3 T1,2,3,4,5 NodeRED Platform WP3 Summary 26/04/2021 (W12)

WP4 T1,2 ThingsBoard CE Platform Demo T1&2 05/06/2021 (W16)

WP4 T3,4 ThingsBoard CE Platform Demo T3&4 10/06/2021 (W17)

WP4 T5 ThingsBoard CE Platform Demo T5 14/06/2021 (W18)

WP4 T1,2,3,4,5 ThingsBoard CE Platform WP4 Summary 14/06/2021 (W18)

WP5 T1 Performance Improvement Database implementation 31/05/2021 (W16)

15
WP5 T2 Performance Improvement Docker implementation 31/05/2021 (W16)

WP6 T1 Documentation Project Proposal 08/03/2021

WP6 T2 Documentation Critical Review 14/04/2021

WP6 T3 Documentation Final project Doc 18/06/2021


Table 2. Milestones

1.4.3. Gantt Diagram

Figure 1. Gantt diagram

1.5. Deviations from the initial plan and incidences


The biggest incidence that we have found has been that very specific versions of the
software are required to be compatible with a very limited server such as the Raspberry Pi
with which the project has been carried out.

In addition, a shorter time had been estimated for the research and implementation of the
docker packaging system, which has taken much longer than expected.

The tasks of the work packages remain the same, although the delivery period has
changed in some of them.

16
What has changed is the priority of the tasks because it has been considered very important
to learn to work with dockers and their implementation in the home automation platform,
rather than carry out an extensive test with an alternative platform.

The unforeseen difficulty with the dockers led to the postponement of the hardware
deployment as a post-deployment test of the dockers, also leaving the ThingsBoard
software as the last point of investigation and installation.

1.6. Final Degree Thesis


The final report of this TFG is longer than the length indicated in the regulations, although
the variation in the size of the report is due to the need to include a large number of images
and commands to carry out the appropriate installations.

It is considered that the whole installation process should be included in the project
development section as it is an essential part of the project to implement the software
together.

17
2. State of the art of the technology used or applied in this
thesis:
A background, comprehensive review of the literature is required. This is known as the
Review of Literature and should include relevant, recent research that has been done on
the subject matter.

2.1. Internet of Things (IoT)


The Internet of Things (IoT) [1] describes the network of physical objects - “things” - that are
embedded with sensors, software, and other technologies for the purpose of connecting
and exchanging data with other devices and systems over the internet. These devices
range from ordinary household objects to sophisticated industrial tools.
The extensive set of applications for IoT devices is often divided into consumer, commercial,
industrial, and infrastructure spaces.[2]
Consumer applications: A growing portion of IoT devices are created for consumer use,
including connected vehicles, home automation, wearable technology, connected health,
and appliances with remote monitoring capabilities.
Organizational applications: Medical and healthcare (IoMT), transportation, in vehicular
communication systems (vehicle-to-everything communication, V2X) & building
automation.
Industrial applications: Manufacturing, agriculture & maritime.
Infrastructure applications: Metropolitan scale deployments, energy management,
environmental monitoring & Living Lab.

Figure 2. IoT application. Source [38]

18
2.2. MQTT
MQTT[3] stands for Message Queuing Telemetry Transport. It is a lightweight
messaging protocol for use in cases where clients need a small code footprint and
are connected to unreliable networks or networks with limited bandwidth resources.
It is primarily used for machine-to-machine (M2M) communication or Internet of
Things types of connections.
MQTT runs on top of TCP/IP using a PUSH/SUBSCRIBE topology. In MQTT
architecture, there are two types of systems: clients and brokers. A broker is the
server that the clients communicate with. The broker receives communications from
clients and sends those communications on to other clients. Clients do not
communicate directly with each other, but rather connect to the broker. Each client
may be either a publisher, a subscriber, or both.

Figure 3. MQTT structure. Source [3]

2.3. Node-RED
Node-RED[4][5] is a flow-based development tool for visual programming developed
originally by IBM for wiring together hardware devices, APIs and online services as part of
the Internet of Things.
Node-RED provides a web browser-based editor that makes it easy to wire together flows
using the wide range of nodes in the palette that can be deployed to its runtime in a single-
click. Also, you can create your own functions based on JavaScript. Elements of
applications can be saved or shared for re-use. The runtime is built on Node.js and the
flows created in Node-RED are stored using JSON.

2.4. NPM
NPM[6] (originally short for Node Package Manager) is the default package manager for the
JavaScript runtime environment Node.js. It consists of a command line client, also called
npm, and an online database of public and paid-for private packages, called the npm

19
registry. The registry is accessed via the client, and the available packages can be browsed
and searched via the npm website.

2.5. InfluxDB
InfluxDB[7] is an open-source time series database (TSDB) developed by InfluxData. It is
written in Go and optimized for fast, high-availability storage and retrieval of time series
data in fields such as operations monitoring, application metrics, Internet of Things sensor
data, and real-time analytics

2.6. Grafana
Grafana[8] is an open-source solution for running data analytics, pulling up metrics that
make sense of the massive amount of data and to monitor our apps with the help of cool
customizable dashboards.
Grafana connects with every possible data source, commonly referred to as databases
such as Graphite, Prometheus, Influx DB, ElasticSearch, MySQL, PostgreSQL etc.
The tool helps to study, analyse and monitor data over a period of time, technically called
time series analytics.
A big upside of the project is it can be deployed on-prem by organizations which do not
want their data to be streamed over to a vendor cloud for security & other reasons.
Here is a snapshot of a Grafana dashboard example, monitoring different things.

Figure 4. Grafana example. Source [8]

The dashboards contain a gamut of visualization options such as geo maps, heat maps,
histograms, all the variety of charts & graphs which a business typically requires to study
data.

20
2.7. Docker
Docker[9] is an open platform for developing, shipping, and running applications. Docker
enables you to separate your applications from your infrastructure so you can deliver
software quickly (this software packages are called containers). With Docker, you can
manage your infrastructure in the same ways you manage your applications. By taking
advantage of Docker’s methodologies for shipping, testing, and deploying code quickly,
you can significantly reduce the delay between writing code and running it in production.

Containers are isolated from one another and bundle their own software, libraries and
configuration files; they can communicate with each other through well-defined channels.
Because all of the containers share the services of a single operating system kernel, they
use fewer resources than virtual machines.

Note: An extended explanation of Dockers for IoT are added in the Methodology Section.

21
3. Methodology / project development:

3.1. Hardware Prototype


For all the proofs of concept that we will carry out in this project, we will carry out virtual
simulations but we will also implement real sensors. To do this, we will connect our
Raspberry Pi 3 B + with ESP32 devices through MQTT[10][11][12]. In one of the ESP32 you
will have connected a temperature, humidity, pressure and altitude device, such as the
BMP280. In the other, a led will be connected that will light up when a previously
programmed alert is activated.

Figure 5. Hardware design

3.1.1. Raspberry Pi
The specifications of the Raspberry Pi model that we have used (3 Model B+) are the
following:

• Broadcom BCM2837B0, Cortex-A53 (ARMv8) 64-bit SoC @ 1.4GHz


• 1GB LPDDR2 SDRAM
• 2.4GHz and 5GHz IEEE 802.11.b/g/n/ac wireless LAN, Bluetooth 4.2, BLE
• Gigabit Ethernet over USB 2.0 (maximum throughput 300 Mbps)
• Extended 40-pin GPIO header
• Full-size HDMI
• 4 USB 2.0 ports
• CSI camera port for connecting a Raspberry Pi camera
• DSI display port for connecting a Raspberry Pi touchscreen display
• 4-pole stereo output and composite video port
• Micro SD port for loading your operating system and storing data
• 5V/2.5A DC power input
• Power-over-Ethernet (PoE) support (requires separate PoE HAT)

22
3.1.2. ESP32 with BMP280
The BMP280 sensor connects with the ESP32 device by making the following junctions:
BMP280 -> ESP32
Pin Vin -> Pin 3.3V
Pin GND -> Pin GND
SCL Pin -> SCL Pin (GIOP 22)
Pin SDA -> Pin SDA (GIOP 21)
SCL and SDA pins are I2C (Inter-Integrated Circuit) pins:
• SCL stands for Serial CLock.
• SDA stands for Serial DAta.

Figure 6. BMP280 connections

3.1.3. ESP32 with LED


The LED connects with the ESP32 device by making the following junctions:
LED -> ESP32
Pin cathode -> Pin GND
Pin anode -> resistance
resistance -> Pin defined (in our case G4)

Figure 7. LED connections

23
3.2. Home Automation Platform Comparison

3.2.1. Introduction
In this project, a proof of concept integrating several sensors and actuators must
be developed, through which it is proposed to generate a low-cost home automation
system that will allow the integration of control panels for data visualization and
interaction with the different sensors. For this, a platform will be required to interpret
the data from the sensors and actuators, represent them graphically, store their
data and alert to unexpected events.

3.2.2. IOT Platform


An IOT platform[13] is a hardware and software system for managing IOT devices
and collecting, storing, visualising and analysing data from those devices.
There are many IOT platforms on the market, and the functionality of these
platforms varies enormously.
Although all IOT platform will have a dashboard to display data, some platforms are
essentially a dashboard and are only really capable of displaying data from devices.
Dashboards vs Platforms
You will often find the terms Dashboard and platform used interchangeably.
An IOT Dashboard can be considered as a basic IOT platform than can usually:
• Display data

• Control devices

However, an IOT platform can usually:


• Collect data from various sources

• Store data

• Control Devices

• Display Data

• Run Tests

• Deploy device updates

• Manage device Inventory

For our project, we need a complete IoT Platform.

24
3.2.3. Platform Comparison
To implement it, we have made a comparison between different platforms.
1. OpenHAB
OpenHAB is a home automation platform whose main aim is the integration and
interaction between different home automation technologies and devices.

Figure 8. OpenHAB example. Source [13]

Main features:
▪ Vendor and technology agnostic

▪ Developed in java

▪ Runs on any device capable of running JVM

▪ Integration of different home automation technologies into one

▪ Comes with different web-based UIs

▪ Provides APIs for being integrated in other systems

2. Home Assistant
This is a home automation platform running on Phyton 3. Home Assistant tracks
and controls all devices at home.

Figure 9. Home Assistant example. Source [13]

25
Main features:
▪ Quick and easy installation

▪ Mobile-friendly interface

▪ No need to store data in the cloud

▪ Set up of rules

▪ Integration with a wide variety of devices and services

3. ThingsBoard
ThingsBoard is an open-source IoT platform that enables rapid development,
management, and scaling of IoT projects. ThingsBoard allows you to manage
devices, collect the information generated, analyse and process data and visualize.
ThingsBoard is licensed under Apache 2.0.
Their goal is to provide the out-of-the-box IoT cloud or on-premises solution that
will enable server-side infrastructure for your IoT applications.

Figure 10. ThingsBoard example. Source [13]

Main features:
With ThingsBoard, you are able to:

• Provision devices, assets and customers, and define relations between them.

• Collect and visualize data from devices and assets.

• Analyse incoming telemetry and trigger alarms with complex event processing.

• Control your devices using remote procedure calls (RPC).

• Build work-flows based on a device life-cycle event, REST API event, RPC
request, etc.

26
• Design dynamic and responsive dashboards and present device or asset
telemetry and insights to your customers.

• Enable use-case specific features using customizable rule chains.

• Push device data to other systems.

4. Node-RED
Node-RED is a development team created by IBM for IoT devices and is the perfect
companion to a home automation platform such as Home Assistant. At its core, it
allows for easy automation of virtually any connected device or service in an easy-
to-understand visual flow interface composed of nodes and wires.
It provides a browser-based editor that makes it easy to wire together flows using
the wide range of nodes in the palette that can be deployed to its runtime in a single-
click.

Figure 11. Node-RED example. Source [13]

Features of Node Red:

• Node-RED provides a browser-based flow editor that makes it easy to wire


together flows using the wide range of nodes in the palette.
• Flows can be then deployed to the runtime in a single-click.
• JavaScript functions can be created within the editor using a rich text editor.
• A built-in library allows you to save useful functions, templates or flows for
re-use.

27
• The light-weight runtime is built on Node.js, taking full advantage of its event-
driven, non-blocking model.
• An online flow library allows you to share your best flows with the world.

5. Home Assistant Vs Openhab


Openhab: Requires some sort of knowledge to know how to insert command to
integrate devices, fairly complex and time consuming, but have far more options
and features than home Assistant.
Home Assistant: More User Friendly, easy to manage for the beginners.

6. Home Assistant vs Node Red


Home Assistant comes with it’s on YAML based automation capabilities. There’s
nothing wrong with this approach, it’s just not visual and can be quite tedious as
anyone who’s worked with YAML knows, one extra space can brick your setup and
force you to go line by line looking for the offense.
If you go the Node-RED route, it essentially just executes logic against Home
Assistant’s state machine, which just keeps track of all your sensors and devices
and their respective status (e.g., kitchen motion sensor is on, bedroom light is off).
For those not interested in coding, Node-RED is definitely more powerful given its
many nodes and powerful integrations with other platforms such as Telegram for
notifications. That said, it’s never a bad idea to learn the basics of a language like
Python or Java and you can create extremely sophisticated automation with a bit of
code.

3.2.4. Final Implementation


In this project, concept tests will be carried out with home automation platforms
such as Node-RED (a JavaScript-based tool, with a huge community and multiple
implementations that will reduce the amount of code programming). A proof of
concept will also be carried out with ThingsBoard Community Edition, a very
powerful tool in the cloud and the cloud will be tested in a local environment, without
online dependency.

28
3.3. Proof of Concept without dockers

In this initial proof of concept, the main part of the system and the multiple options
that have been raised with the same final purpose, to visualize temperature,
humidity, pressure and altitude data will be detailed. Although with certain
differences, since we have made several designs. Real-time data with Dashboard
Node-RED, data saving in the InfluxDB database and visualization of historical and
real-time data in Grafana[14][15][16].

Figure 12. PoC without dockers design

3.3.1. Install Raspberry Pi OS


A previous installation of the Raspberry Pi Lite is required (Explained at Appendix
1). Then you can proceed to install the required software.

3.3.2. Installing and running Node-RED on Raspberry Pi


First of all, we will perform some commands to update the apt-get repository:
sudo apt-get update
sudo apt-get upgrade

29
The Node-RED developers provide a script to install Node.js, npm and
Node-RED onto a Raspberry Pi. The script can also be used to upgrade an
existing install when a new release is available.
Running the following command will download and run the script.

bash < (curl -sL https://raw.githubusercontent.com/node-


red/linux-installers/master/deb/update-nodejs-and-
nodered)

Figure 13. Command: bash node-red

Figure 14. Node-RED package installation

The installed script for the Pi also sets it up to run as a service. This means
it can run in the background and be enabled to automatically start on boot.

If you want Node-RED to run when the Pi is turned on, or re-booted, you can
enable the service to autostart by running the command:
sudo systemctl enable nodered.service

30
To disable the service, run the command:
sudo systemctl disable nodered.service

Now, we execute systemctl start nodered, and Node-RED is running


so you can access the editor in a browser.
When browsing from another machine you should use the hostname or IP-
address of the Pi: http://<hostname>:1880. You can find the IP address by
running hostname -I on the Pi.

3.3.2.1. Installing Node-RED Dashboard


We can install the Node-RED Dashboard[17] running the following command:
npm install node-red-dashboard

Otherwise, we can install the dashboard nodes via Node-RED User


Interface:

• Select the icon at the top right


• Select the option "Manage palette":

Figure 15. Node-RED manage palette (1)

31
• Select the tab "Install", search node-red-dashboard and click to
"install":

Figure 16. Node dashboard modules installation (1)

• Select install to allow a Node-RED restart to install the node


modules

Figure 17. Node modules installation (2)

• All the node modules will be added:

Figure 18. Node modules installed on pallete

32
Figure 19. Node-RED dashboard modules

3.3.3. Installing MQTT Broker

3.3.3.1. Comparison between MQTT Brokers (Mosquitto vs Aedes [previously called


Mosca])

The following table lists the main characteristics of the two MQTT brokers[18]
that we are comparing:

Broker Description

• Lightweight open-source broker written in C. Probably


the most popular MQTT broker. Supports MQTT v3.1.1
and v5
• This is likely to be the default broker for edge networks.

Mosquitto • It supports TLS and there are plugins for authorization


using a database, but it has some downsides.
• Mosquitto does not support clustering, it makes scaling a
bit difficult. It is using only a single thread so can't take
advantage of multi core CPUs.

33
• Aedes replaces Mosca. Mosca is no longer being
developed.
• Aedes is a very simple broker and ideal for small home
network deployments and for learning MQTT.
• Aedes is Node.js based and so requires node.js to be
installed.
• It can also be installed as a node in node-red.
Mosca- • It is not very feature rich when compared to mosquitto but
Aedes
ideal for home automation projects.
• Currently supports v3.1.1
• Runs on Windows and Linux
• Aedes permits a high availability MQTT cluster for the
Internet of Things
• Mosca (Aedes) supports websockets and also SSL. See
Install Aedes Broker on Node-Red
Table 3. MQTT comparison. Source[18]

Summing up, both options are small enough to run on very simple hardware
like the raspberry PI. They are good and sufficient for the use we want to
give them. So, we will prove both of them, Mosquitto for their efficiency and
Aedes because is so easy to install on Node-RED.

3.3.3.2. Installing MQTT Broker as a Node-RED Node

We can install the MQTT Aedes[19] node running the following command:
npm install node-red-dashboard

Otherwise, we can install the MQTT Aedes node via Node-RED User
Interface:

• Select the icon at the top right and the option "Manage
palette":

34
Figure 20. Node-RED manage palette (2)

• Click on the install tab and search for aedes, then install it. Allow
to Node-RED for restarting after the installation.

Figure 21. Node dashboard modules installation (2)

• After installation, you should find a new node named "aedes


broker" in the network categories. Drag and drop it onto the main
window.

Figure 22. MQTT Aedes node-red module

35
3.3.3.3. Configurating Aedes MQTT Broker
Click twice on the node.
The node properties have two tabs: connections and security. and with very
few settings.
The main tab is shown below:

Figure 23. Configurating Aedes MQTT Broker

You need to set the ports for standard MQTT and MQTT over websockets if
used.
The standard port for MQTT is the 1883 port and the one for MQTT over
Websockets is the 1884 port.
The security tab contains options for username and password (if needed).

3.3.3.4. Installing Mosquitto MQTT Broker


To install the Mosquitto Broker enter these next commands:
sudo apt update
sudo apt install -y mosquitto mosquitto-clients
You’ll have to type Y and press Enter to confirm the installation. To make Mosquitto
auto start on boot up enter:
sudo systemctl enable mosquitto.service
For testing that installation was successful, Send the command:
mosquitto -v
This returns the Mosquitto version that is currently running in your Raspberry Pi

36
3.3.3.5. Testing MQTT Brokers
After both installations and adding an MQTT data receiver node in Node-RED
(explained in the next point), we have carried out connection tests from outside,
such as a windows computer with a broker, with the MQTT Broker of the Raspberry
(both Aedes and Mosquitto).
Note: since both use port 1883 by default, it will not be possible to have both active.
The solution would be to stop or uninstall one of them or run them on different ports.

Figure 24. MQTT Broker test with Node-RED

More detailed tests of MQTT Broker on Raspberry Pi would be found on Results


section.

3.3.4. Testing the Proof of Concept

For testing all the installation, we generated the next flow:

Figure 25. Proof of Concept Node-RED flow

37
Node Aedes MQTT Broker:
In this node we configure the MQTT Broker:

Figure 26. Aedes Broker configuration

Inject -> function -> mqtt publish: (Simulated data)


In this sequence we have programmed a periodic injection of JSON frames
with the following format:
Inject node:

Figure 27. Inject node configuration

38
Function node:

Figure 28. Function node configuration

msg.payload={
"temperature": parseFloat((Math.random()* (85 - (-40)) + (-40)).toFixed(2)),
"humidity": parseFloat((Math.random()*100).toFixed(2)),
"pressure": parseFloat((Math.random()* ((1110 - 300) + 300)).toFixed(2))
}
return msg;

Finally, we are publishing this JSON to the next MQTT broker and topic:

Figure 29. MQTT publish node configuration

39
Where the server is configured as follows (adding the broker's username and
password in security tab):

Figure 30. MQTT Broker server configuration

Mqtt subscriber -> json -> Dashboards: (Real or simulated data)


In this sequence we have subscribed to the MQTT Broker topic and we have
configured the visualization dashboards.

Mqtt subscriber:

Figure 31. MQTT subcribe node configuration

40
Json Node (Converts JSON string to Object):

Figure 32. JSON node configuration.

Finally, we have configured the dashboard for the three metrics:

Figure 33. Node-RED dashboard nodes configuration

41
Final Test:
Data reception:

Figure 34. Node-RED data reception

Data visualization:

Figure 35. Node-RED Dashboard visualization

42
3.3.5. InfluxDB Installation

3.3.5.1. InfluxDB decision


Why do we use InfluxDB[20][21][22] and no other alternatives as SQLite? What is the
difference between both? SQL databases like SQLite are very flexible and the
workhorse of many business applications. They are organized in rows of date like
time, temperature, humidity, etc. If we want to store our sensor data in SQLite, we
have to create a database, a table and then add all needed fields including their
data types. Then we can insert the sensor data line after line.
InfluxDB on the other hand is a time series database. It is a relatively new sort of
databases. They have a similar structure like SQLite, but the difference is that a
timestamp is automatically added every time we insert data and we do not need to
define the fields as we would do in SQLite. As we would like to see, they are added
as soon as we insert data into the database, do it is a little easier to work with a
time series database.

Figure 36. InfluxDB vs SQLite. Source [22]

But this is not the most important difference. Much more important is the reading of
the data stored in the database and how these new databases deal with historical
data.
What are typical questions we have for our sensor data?
We are interested in, for example, all data for the last hour, or the last 24 hours?
Or we want to have average temperatures for all days of the previous year?
What is common in all these questions? Time slots are always one part of the
question. Time series databases are optimized for such queries. Of course, you
also can create standard SQL queries to answer those questions, but this involves
a lot of work and you have to write all the different queries yourself. In contrast, for

43
Grafana, is so easy to implement InfluxDB and filtering with the time ranges you
choose, without one line of coding and with big results.

Figure 37. Grafana relative time ranges

The second difference is how databases deal with historical data:


Standard SQL database store and retain all data ever written to them and to get the
data for last year they have to read all sensor data and create averages when you
issue a query. This can create a heavy load and this slow especially on machines
like Raspberry Pi with limited memory. Time series databases offer retention
polices, where you can decide how long you want to keep the real-time data and
how the data should be averaged and compressed after that.
This is the main reason why I decided to use InfluxDB.

3.3.5.2. Install InfluxDB

First, we add Influx repositories to apt:


curl -sL https://repos.influxdata.com/influxdb.key | sudo apt-key
add –
echo "deb https://repos.influxdata.com/debian stretch stable" |
sudo tee /etc/apt/sources.list.d/influxdb.list

Update apt with the new repositories & install.


sudo apt update
sudo apt install influxdb

44
Then start the influxdb service and set it to run at boot:
sudo systemctl enable influxdb
sudo systemctl start influxdb
We should now be able to run the influx client with influx and create a user for later
(here I use a single admin user 'pi' for simplicity):
influx
> create database home
> use home
Using database home
> create user pi with password 'passwordhere' with all privileges
> grant all privileges on home to pi
> show users

> exit
You can now exit the Influx client by typing exit.

3.3.5.3. Influx Extra Configuration


Change configuration file:
sudo nano /etc/influxdb/influxdb.conf
In [http] uncomment:
enabled = true
and
bind-address =: 8086
And finally, restart influx to save the changes.
sudo service influxdb restart

influx -version
InfluxDB shell version: 1.8.5

45
3.3.6. Grafana Installation

The next step is to install Grafana[23], to view the data that will be entered into the
database.

Add the APT key used to authenticate packages:

wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key


add -

Add the Grafana APT repository:

echo "deb https://packages.grafana.com/oss/deb stable main" | sudo


tee -a /etc/apt/sources.list.d/grafana.list

Install Grafana:

sudo apt-get update


sudo apt-get install grafana

Grafana is now installed, but not yet running. To make sure Grafana starts up even
if the Raspberry Pi is restarted, we need to enable and start the Grafana Systemctl
service.

Enable the Grafana server:

sudo systemctl enable grafana-server

Start the Grafana server:

sudo systemctl start grafana-server

Grafana is now running on the machine and is accessible from any device on the
local network.

Open a browser and go to http://<ip address>:3000, where the IP address is the


address that you used to connect to the Raspberry Pi earlier. You’re greeted with
the Grafana login page.

46
Figure 38. Grafana welcome menu

Log in to Grafana with the default username admin, and the default password admin.

Change the password for the admin user when asked.

Grafana is now running on your Raspberry Pi. If the Raspberry Pi is ever restarted
or turned off, Grafana will start up whenever the machine regains power.

3.3.7. Add Influx as a Grafana data source


Now we have both Influx and Grafana running, we can stitch them together. Log in
to your Grafana instance and head to “Configuration -> Data Sources”. Select “Add
Data Source” and find InfluxDB under “Timeseries Databases”.

Figure 39. Influx as a Grfana data source

47
As we are running both services on the same Pi, set the URL to localhost and use
the default influx port of 8086:

Figure 40. InfluxDB ip on Grafana

We then need to add the database, user and password that we set earlier:

Figure 41. InfluxDB Access on Grafana

Now go ahead and hit “Save & Test” to connect everything together.

48
3.3.8. Configuration of InfluxDB on NodeRED
Install influxdb module[24]:
Go to Manage Palette -> Install and search 'node-red-contrib-influxdb'
Once installed, you can find the following nodes on the left.

Figure 42. InfluxDB node modules

Now, we will try to import our random data injected to MQTT topic 'bmp280' and
insert this data to the Influx Database.

Figure 43. InfluxDB node configuration

49
Figure 44. Node-RED real data reception

And we can see these values on the database:

Figure 45. InfluxDB database data

Note: if we want to delete some data from this database, we would use the next
command on the shell:
Delete all database:
DELETE FROM table_name WHERE condition
Delete a column:
ALTER TABLE table_name DROP COLUMN column_name
Delete specific data:
DELETE FROM table_name WHERE column_name=specific_data

3.3.9. Collecting some data with Grafana


Creating a new Dashboard and a new panel, we can observe the metrics menu
where we can configure the queries that will be made to the indicated database.

Figure 46. Grafana dashboard creation (1)

50
Also, on the right side we find the panel configuration, where we can modify the
axes, the display, the legend, the name, etc.

Figure 47. Grafana dashboard creation (2)

Finally, we have obtained a graph to visualize the temperature and humidity data
of our virtual sensor:

Figure 48. Grafana dashboard creation (3)

51
3.4. Dockers for IoT

3.4.1. Introduction to Docker


Docker is a container-based open platform for developing, deploying, and running
applications. Containerization enables you to create a container that includes your
application, any binaries or libraries that your application depends on, and any
configuration details. You can then run the software on multiple machines without
a lot of setups.
Dockers calls it containers because you can imagine of it like a shipping container.
Shipping containers are a standard size so that they can be moved around at ports,
and shipped via sea or land. They can also contain almost anything. Docker
containers can hold your software’s code and its dependencies, so that it can
easily run on many different machines. Developers often use them to create a web
application server that runs on their own machine for development, and is then
pushed to the cloud for the public to use.
The way which containerized applications operate is shown below.

As can be seen from this diagram, a Docker container includes an application plus
any binaries or libraries that the application requires in order for it to run. The
container runs under the control of Docker, which in turn runs on top of the operating
system.

3.4.2. Benefits of Using Docker


Running applications in containers brings a number of benefits:

• Portability

52
Once you have tested your containerized application you can deploy it to any
other system where Docker is running and you can be sure that your
application will perform exactly as it did when you tested it.

• Performance
Although virtual machines are an alternative to containers, the fact that
containers do not contain an operating system (whereas virtual machines
do) means that containers have much smaller footprints than virtual
machines, are faster to create, and quicker to start.

• Agility
The portability and performance benefits offered by containers can help you
make your development process more agile and responsive. Enhancing your
continuous integration and continuous delivery processes to take advantage
of containers and technology such as Enterprise Developer Build Tools for
Windows makes it easier for you to deliver the right software at the right time.
Enterprise Developer Build Tools for Windows is a component of Enterprise
Developer which provides all of the functionality of Enterprise Developer to
enable you to compile, build, and test COBOL code but without the overhead
of an IDE.

• Isolation
A Docker container that contains one of your applications also includes the
relevant versions of any supporting software that your application requires.
If other Docker containers contain applications that require different versions
of the same supporting software, that isn't a problem because the different
Docker containers are totally independent of one other.
This also means that as you move through the various stages of your
development lifecycle, you can have total confidence that an image you
create during development will perform exactly the same as it moves through
testing and potentially to your users.

• Scalability
You can quickly create new containers if demand for your applications
requires them. When using multiple containers, you can take advantage of
a range of container management options. See the Docker documentation
for more information on these options.

53
3.4.3. Why the IoT would use Docker (Benefits concretizing in the use of the IoT)
Containers are well-positioned to address some of the main challenges that
developers face when deploying software to IoT devices:
▪ Minimal hardware resources. Many IoT devices lack powerful computing
and memory resources. Their ability to process software updates is therefore
limited. Containers can help on this front because installing a new container
image does not require much computing power. An IoT device needs simply
to download an image, put it wherever it’s going to live and remove the old
image. Configuration processing is minimal.

▪ Geographic distribution. In some use cases, IoT devices are spread


across a large geographic area. Delivering software to them from a single
central repository may not work well. With Docker, it’s easy to spin up image
registries in multiple locations to serve the entire network well.

▪ Limited or sporadic network access. Despite the implications of the term


“internet of things,” not all devices on the internet of things are well-
connected to the internet. They may have limited network bandwidth, or be
online only occasionally. Docker can help to deliver software updates under
these circumstances because when container images are updated, Docker
downloads only the parts of the image that have changed.

▪ Widely varying device environments. The software that runs on an IoT


device could be almost anything. The diversity of software configurations on
IoT devices would normally make application installation difficult, because
applications would have to be configured for each type of environment if
installed via traditional methods. With containers, however, the operating
system version and other software variables are much less important. As
long as the device runs some kind of Linux distribution and has a container
runtime, you can install a containerized application on it without special
configuration.

54
3.5. Proof of Concept with dockers

This proof of concept only details the differences from installation without dockers.
The software configuration through the graphical interface is detailed in the
previous proof of concept.

3.5.1. Docker Installation

A previous installation of the Raspberry Pi Lite is required (Appendix 1). Then you
can proceed to install the dockers[25][26].
All dockers installation has been done with root permissions.

3.5.1.1. Download the Script and Install Docker on Raspberry Pi

Move on to downloading the installation script and execute the script using the
command:
curl -sSL https://get.docker.com | sh
This installs the required packages for your Raspbian Linux distribution.
The output will tell you which version of Docker is now running on your system.

Figure 49. Command: curl docker

55
3.5.1.2. Configure Docker

There are a couple of manual steps needed to get the best experience.
Set Docker to auto-start:
sudo systemctl enable docker
You can now reboot the Pi (using the command sudo shutdown -r now), or start
the Docker daemon with:
sudo systemctl start docker

Enable Docker client:


The Docker client can only be used by root or members of the docker group. Add
pi or your equivalent user to the docker group:
sudo usermod -aG docker pi
There is no specific output if the process is successful. For the changes to take
place, you need to log out the user session and then back in.
This is very important for having the right permissions.

3.5.1.3. Check Docker Version and Info

Check the version of Docker on your Raspberry Pi by typing:


docker version

Figure 50. Verify docker version

56
The output will display the Docker version along with some additional information.
For system-wide information (including the kernel version, number of containers
and images, and more extended description) run:
docker info
With the following command we would see the containers running on this docker
container (at the moment the container's list is empty):
docker ps

3.5.1.4. Upgrade Docker on Raspberry Pi

There is no need to re-run the convenience script to upgrade Docker. Furthermore,


running the convenience script again might cause issues if it attempts to
repositories that have been added already.
Upgrade Docker using the package manager with the command:
sudo apt-get update
sudo apt-get upgrade

Figure 51. Upgrade docker package

3.5.2. Docker MQTT Installation

3.5.2.1. Pull the latest image


[27][28]First,
pull the latest image from Docker Hub:
docker pull eclipse-mosquitto

Figure 52. Docker pull mosquitto

57
3.5.2.2. Create folders for MQTT

A directory is generated to add all the container data in a recognizable access.


Directories are created at the root, to avoid permission conflicts
sudo su
cd /
mkdir docker/mosquitto
mkdir docker/mosquitto/config
mkdir docker/mosquitto/data
mkdir docker/mosquitto/log

3.5.2.3. Creating initial config file

Next, create a test file called mosquitto.conf in the newly created subdirectory
mosquitto/config/ and edit the config file:
nano /docker/mosquitto/config/mosquitto.conf
And add the as a minimum set of conf directives.
persistence true
persistence_location /mosquitto/data/
log_dest file /mosquitto/log/mosquitto.log
#password_file /mosquitto/config/pwfile
allow_anonymous true
listener 1883 192.168.0.10

Note: the listener line IP is the Raspberry PI local IP


The full list of configuration items can be found here:
https://mosquitto.org/man/mosquitto-conf-5.html

3.5.2.4. Docker run command

The command to execute the mosquitto container is quite long because we indicate
the directory where to store the persistence files. So, if the running container is
stopped, the files will remain for the next run:

sudo docker run --name mosquitto -itd --restart=unless-stopped --


net=host -v /docker/mosquitto/config:/mosquitto/config -v
/docker/mosquitto/data:/mosquitto/data -v
/docker/mosquitto/log:/mosquitto/log -v
/docker/mosquitto/config/mosquitto.conf:/mosquitto/config/mosquitt
o.conf eclipse-mosquitto

58
Docker run command options:
--name: container identification
-i: We indicate that it will be interactive
-t: Open the TTY
-d: keep the container and current terminal session separate
--restart: the docker will not stop if machine is power off, only if the user stopped
manually
--net: connect a container to a network
-v: Bind mount a volume (host-source:container-dest)

Figure 53. Verify mqtt container is up

Note: If the error message "Error response from daemon: Conflict. The container
name" / mosquitto "is already in use by container" appears, you must uninstall the
detected docker, using the command sudo docker mosquitto rm -fv / mosquitto

Note: If when running docker ps you notice that the broker status is not up, check
if you already have an active mqtt broker (even if it is not as a docker) and disable
it or change the ports in any of them.

3.5.2.5. Verify everything is working

Open the file mosquitto.log from the directory mosquitto/log and check the installed
version of mosquitto:

Figure 54. Check mosquitto is running correctly

3.5.2.6. Configuring username and password for MQTT access

Execute the shell of the docker mosquitto container to modify the username and
password:

59
docker exec -it mosquitto sh

We execute the command mosquitto_passwd from the mosquitto bash to add a


user and its password (which will be saved in hash format):

cd mosquitto/config
mosquitto_passwd -c pwfile pi

Figure 55. Configure mqtt user & password

And now the file pwfile will contain (encrypted):

Figure 56. MQTT password encrypted

3.5.3. Docker NodeRED Installation

First, pull the latest image from Docker Hub[29]:


docker pull nodered/node-red

Figure 57. Docker pull Node-RED

60
We will carry out a process similar to that of MQTT, so that the data persists even
if the machine or the docker is restarted. To do this we will generate a directory
inside the 'docker' folder and we will need to ensure that any existing /data directory
has the correct ownership.
After that process, execute the command to run Node-RED Docker:
sudo su
cd /
mkdir /docker/mynodered
mkdir /docker/mynodered/data
We will need to ensure that any existing /data directory has the correct ownership.
This needs to be 1000:1000. This can be forced by the command:
sudo chown -R 1000:1000 /docker/mynodered/data
Execute the command to run the container with the right configuration and
directories:
sudo docker run --name mynodered -itd --restart=unless-stopped -p
1880:1880 -v /docker/mynodered/data:/data nodered/node-red

3.5.4. Docker InfluxDB Installation

Now to build this Raspberry Pi IoT project, once the Mosquitto is up and running,
we can install and configure InfluxDB[30]:
docker pull influxdb:1.8.5
mkdir /docker/influxdb
once the installation completes, it is possible to start InfluxDB:
docker run -itd --name influxdb --restart=unless-stopped -p
8086:8086 -v /docker/influxdb:/var/lib/influxdb influxdb:1.8.5
The next step is creating the database and the user that will access this database
First, start the InfluxDB CLI:
docker exec -it influxdb influx
Next, let us create the database and the user:
> create database home
> use home

61
Using database home
> create user pi with password 'passwordhere' with all privileges
> grant all privileges on home to pi
> show users

> exit
With these few lines, we have created a database named home and a user with
username pi and password passwordhere.

3.5.5. Docker Grafana Installation

Now we install Grafana analytics and interactive visualization web application, as


an alternative to the dashboard the Node-RED, where the interconnection with the
database is much easier[31].
docker pull grafana/grafana
cd /
sudo mkdir /docker/grafana-storage
docker run -itd --name grafana --user 0 --restart=unless-stopped -
v /docker/grafana-storage/data:/var/lib/grafana -p 3000:3000
grafana/grafana
Grafana is now running on the machine and is accessible from any device on the
local network.
Note: All the other configuration is the same as on the previous proof of concept.

62
3.6. Proof of Concept with other platforms (ThingsBoard CE)
The ThingsBoard CE (Community Edition) installation is the 100% open-source and on-
premise version of the ThingsBoard software, with maintenance carried out by the
community itself and without having dependencies on the cloud or maintenance payments.

3.6.1. Install Raspberry Pi OS

A previous installation of the Raspberry Pi Lite is required (Explained at Appendix


1). Then you can proceed to install the required software[32].

3.6.2. Install Third-Party components (Java 11)

ThingsBoard service is running on Java 11. Follow these instructions to install


OpenJDK 11:
sudo apt update
sudo apt upgrade
sudo apt install openjdk-11-jdk
Note: this installation usually takes a long time to complete.
It's important to configure your operating system to use OpenJDK 11 by default.
You can configure which version is the default using the following command:
sudo update-alternatives --config java
You can check the installation using the following command:
java -version

Figure 58. Java version installed

3.6.3. Install ThingsBoard Software

Download installation package.


wget
https://github.com/thingsboard/thingsboard/releases/download/v3.2.
2/thingsboard-3.2.2.deb
Install ThingsBoard as a service:
sudo dpkg -i thingsboard-3.2.2.deb

63
3.6.4. Install ThingsBoard database (PostgreSQL)

ThingsBoard team recommends to use PostgreSQL for development and


production environments with reasonable load (<5000 msg/sec)
Instructions below will help to install PostgreSQL.
Import the repository signing key:
wget --quiet -O -
https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key
add -
Add repository contents to your system:
RELEASE=$(lsb_release -cs)
echo "deb http://apt.postgresql.org/pub/repos/apt/ ${RELEASE}"-
pgdg main | sudo tee /etc/apt/sources.list.d/pgdg.list
Install and launch the PostgreSQL service:
sudo apt update
sudo apt upgrade
sudo apt -y install postgresql
sudo service postgresql start

Figure 59. Verify PostgreSQL is running

Once PostgreSQL is installed you may create a new user or set the password for
the main user. Execute the instructions below to set the password for main
postgresql user:
sudo su - postgres
psql
\password
\q

64
Then, press “Ctrl+D” to return to main user console and connect to the database to
create thingsboard DB:
psql -U postgres -d postgres -h 127.0.0.1 -W
CREATE DATABASE thingsboard;
\l #show databases
\q

Figure 60. PostgreSQL databases

3.6.5. ThingsBoard Configuration service

Edit ThingsBoard configuration file


sudo nano /etc/thingsboard/conf/thingsboard.conf
And add the following lines to the configuration file. Don’t forget to replace
“PUT_YOUR_POSTGRESQL_PASSWORD_HERE” with your real postgresql user
password:
# DB Configuration
export DATABASE_ENTITIES_TYPE=sql
export DATABASE_TS_TYPE=sql
export
SPRING_JPA_DATABASE_PLATFORM=org.hibernate.dialect.PostgreSQLDialect
export SPRING_DRIVER_CLASS_NAME=org.postgresql.Driver
export
SPRING_DATASOURCE_URL=jdbc:postgresql://localhost:5432/thingsboard
export SPRING_DATASOURCE_USERNAME=postgres
export SPRING_DATASOURCE_PASSWORD=PUT_YOUR_POSTGRESQL_PASSWORD_HERE
export SPRING_DATASOURCE_MAXIMUM_POOL_SIZE=5

65
# Specify partitioning size for timestamp key-value storage. Allowed
values: DAYS, MONTHS, YEARS, INDEFINITE.
export SQL_POSTGRES_TS_KV_PARTITIONING=MONTHS

Also, for slow machines as our system that has 1GB of RAM, add the following line:
# Update ThingsBoard memory usage and restrict it to 256MB in
/etc/thingsboard/conf/thingsboard.conf

export JAVA_OPTS="$JAVA_OPTS -Xms256M -Xmx256M"

3.6.6. Run script and start ThingsBoard service

Once ThingsBoard service is installed and DB configuration is updated, execute


the following script:
sudo /usr/share/thingsboard/bin/install/install.sh --loadDemo
Note: --loadDemo option will load demo data: users, devices, assets, rules, widgets.

Figure 61. ThingsBoard installation script

And finally, execute the following command to start ThingsBoard:


sudo service thingsboard start

Figure 62. Verify ThingsBoard is running

66
Once started, you will be able to open Web UI using the following link:
http://localhost:8080/
So now we have our Node-RED alternative platform running:

Figure 63. ThingsBoard welcome menu

The default users that can be changed before are:


System Administrator: sysadmin@thingsboard.org / sysadmin
Tenant Administrator: tenant@thingsboard.org / tenant
Customer User: customer@thingsboard.org / customer

Figure 64. ThingsBoard main screen

67
4. Results
This section includes all the results obtained like all dashboards visualization, tests
and performance analysis.

4.1. Screenshots final design & visualization

Firstly, the results of dashboards obtained through various tools and accessed from
different devices are visualised.

Figure 65. Node-RED Dashboard (mobile visualisation)

68
Figure 66. Grafana Real-Time data dashboard (computer format)

Figure 67. Grafana historical data dashboard (computer format)

69
Figure 68. Grafana Real-Time data dashboard (mobile format)

Figure 69. Grafana historical data dashboard (mobile format)

70
4.2. MQTT Broker Tests
Next, we will try to publish and subscribe with an external broker and with the local
broker.
First, execute the shell of the docker mosquitto container to run the mosquitto_pub
& mosquitto_sub commands:
docker exec -it mosquitto sh
mosquitto_pub -h test.mosquitto.org -t temperature -d -m
"Temperature = 22.5 ºC"

Figure 70. Execute shell mosquitto container

We can check from an app like MQTTBox (Chrome extension)that the written
message is being published:

Figure 71. MQTTBox public broker subscription

We can also carry out the opposite process (subscribe in the installed broker and
send from another machine):

Figure 72. MQTTBox publish to public broker

71
mosquitto_sub -h test.mosquitto.org -t temperature -d

Figure 73. Mosquitto container public broker subscription

Also, we can try with the local Mosquitto Broker as a host:


mosquitto_pub -h 192.168.0.10 -t test -d -m "Temperature = 23.2 ºC"

Figure 74. Mosquitto container local broker publishing

Figure 75. MQTTBox local broker subscription

And same by the other way:

Figure 76. Figure 72. MQTTBox publish to local broker

72
Figure 77. Mosquitto container local broker subscription

4.3. Performance

In this project, the Raspberry Pi Model 3 B+ is used as a server. The hardware


specifications are specified in section 3.1.

4.3.1. Raspberry Pi CPU & Memory Test


Through the htop command we observe the behaviour in certain situations.
Initializing the system (booting dockers):

Figure 78. htop command initalizing the system

It can be seen that while the system is booting and all its tasks, the CPU is at a
medium level without becoming saturated.

System stabilized:

Figure 79. htop command with system stabilized (1)

It can be seen how the CPU consumption is negligible.

73
Figure 80. htop command with system stabilized (2)

Regarding the percentage of available memory, it is observed that having only 1


GB of memory, with a very reduced integration, almost 50% of memory consumed
is obtained.

InfluxDB query:

Figure 81. htop command with Grafana influxdb query

At the moment that we go from a request to represent historical data for the whole
day, to represent the data of a full month, the system becomes saturated since a
single request is made to represent a lot of data (this saturation does not reach 1
minute).

74
4.3.2. Raspberry Pi memory storage
This project has been installed and tested on a 16 GB Micro SD memory.

Figure 82. Raspberry Pi memory storage

As can be seen, the amount of memory used with a data collection every 10
seconds for a month is very small, also considering the installed software, so it will
take at least a couple of years to fill the memory. (That is calculated without deleting
and averaging old data stored, which can be done with InfluxDB).

4.3.3. MQTT connections test


To test the data reception capacity of the installed docker MQTT, we have
performed a benchmark with different amounts of clients and messages sent to see
their speed[33][34][35].

Here the results of a scenario with multiple benchmarks (mqtt-stresser & mqtt-
bench )taking 10 clients sending multiple messages (100 msg/client) tested 5 times
and doing the median:

Publishing Throughput
70000

60000

50000
msg/sec

40000

30000

20000

10000

0
Fastest Slowest Median

Local Broker Public Broker

Figure 83. Mqtt-stresser publishing results: Test condition: client=10, Messages=100

75
Receiving Throughput
350

300

250
msg/sec

200

150

100

50

0
Fastest Slowest Median

Local Broker Public Broker

Figure 84. Mqtt-stresser receiving results: Test condition: client=10, Messages=100

MQTT-BENCH
2000

1500
msg/sec

1000

500

0
Test 1 Test 2 Test 3 Test 4 Test 5 Median

Local Broker Public Broker

Figure 85. Mqtt-bench receiving results: Test condition: client=10, Messages=100

Simulating with the benchmark mqtt-bench, with 10 concurrent MQTT clients, each
one sending 100 messages of 1 KByte each, we see how we could receive
approximately 1000 messages per second, the obtained results are:

Figure 86. mqtt-bench command example

76
On the other hand, if we increase the recurring customers to 25, we see how the
number of messages per second is radically reduced to less than half.

The effect of this large number of messages does not cause a large increase in
CPU consumption:

Figure 87. CPU effect of multiple mqtt connections

77
5. Budget
The objective of the thesis is a prototype of a home automation installation.
For the development of this project, the approximate costs of the components used,
both hardware and software, have been taken into account, as well as the salary of
the workers and the place where it was developed.
The project has been carried out in a term of 6 months

Variable costs:

Software:

All the software used in this project is open source, so there is no cost to include at
this point.

Hardware:

This section will detail the material used for the installation of the project, as well as
the necessary components for the design.

It is calculated using the annual amortization fee, which takes into account the
residual value (10%) and the approximate years of amortization of each product.

Amortization fee = (Acquisition value - Residual value) / Years of useful life

Hardware Price (€) Amortization (years) Annual cost (€)


Computer 830,00 6 124,50
Raspberry pi 3B+ 39,87 3 11,96
ESP32 9,50 3 2,85
BMP280 2,90 3 0,87
Wires & basic sensors 3,00 3 0,90
Micro SD 16 GB 3,89 3 1,17
Table 4. Budget: variable costs

Total annual cost: 142,25 €

Wages:

It is considered a junior engineer employee and a salary of 10 euros per hour,


together with the supervision of a senior engineer. In addition, the costs derived
from social security that the company must bear are taken into account.

78
Employee Salary/hour Total hours (h) Social Project cost (€)
(€/h) Security (%)
Junior
10 480 33,4 6403,2
Engineer
Senior
25 144 33,4 4802,4
Engineer
Table 5. Budget: wages

Fixed costs:

Finally, the use of a coworking space has been considered, which includes all kinds
of supplies, to be able to develop the prototype.

Fixed Cost Price (€/month) Project Cost (€)


Coworking 200 1200
Table 6. Budget: fixed costs

79
6. Conclusions and future development:

This project had one main objective: to create a domestic platform to control and
monitor different types of sensors.
To carry out this task, an investigation of different platforms has been developed to
install through a local server (such as a Raspberry Pi) to facilitate access from the
local network to the visualization and monitoring of data, which can be done from a
browser Web.
After this investigation, it was decided to design our platform from Node-RED since
it has been very interesting due to its block programming, multiple available plugins
and total customization.
Then, once the basic installation was done, improvements were added, such as a
database to store historical data, another type of more complete representation,
packaged by dockers, etc.
In summary, it has been possible to carry out a basic but complete installation of a
domestic platform to monitor household data and actions.
As a future development, there are many things that can be implemented, such as
multiple more sensors, integrations with platforms such as Telegram and Google
Assistant, a display of metrics in real time, among many others. That is the most
interesting thing about the IoT world, which is always in constant growth.

80
Bibliography:
[1] “What Is the Internet of Things (IoT)?” Oracle, www.oracle.com/internet-of-things/what-is-iot.

[2] Suwimon Vongsingthong, and Sucha Smanchat. “Internet Of Things: A Review Of Applications And

Technologies.” 4, vol. 21, Suaranaree University of Technology, 2014, p. Suranaree Journal of Science

and Technology, doi:10.14456/SJST.2014.38.

[3] “What Is MQTT? Definition and Details.” Paessler, www.paessler.com/it-explained/mqtt.

[4] Wikipedia contributors. “Node-RED.” Wikipedia, 30 Apr. 2021, en.wikipedia.org/wiki/Node-RED.

[5] “Node-RED.” Node-RED, nodered.org/about.

[6] Wikipedia contributors. “Npm (Software).” Wikipedia, 13 May 2021,

www.en.wikipedia.org/wiki/Npm_(software).

[7] InfluxData. “InfluxDB: Purpose-Built Open-Source Time Series Database.” InfluxData, 21 May 2021,

www.influxdata.com.

[8] Shivang. “What Is Grafana? Why Use It? Everything You Should Know About It.” 8bitmen.Com, 13 Aug.

2020, www.8bitmen.com/what-is-grafana-why-use-it-everything-you-should-know-about-it.

[9] Yegulalp, Serdar. “What Is Docker? The Spark for the Container Revolution.” InfoWorld, 19 Apr. 2019,

www.infoworld.com/article/3204171/what-is-docker-the-spark-for-the-container-revolution.html.

[10] Jain, Varul. “Altitude Monitoring with BMP280 Using ESP32 and Node-Red.” Medium, 27 Feb. 2019,

https://medium.com/@varuldcube100/altitude-monitoring-with-bmp280-using-esp32-and-node-red-

35bec7ff5ed2.

[11] Azzola, Francesco. “How to Connect ESP32 to the Smartphone Using Node-RED.” SwA, 22 Feb. 2021,

www.survivingwithandroid.com/connect-esp32-to-smartphone-node-red/.

[12] Santos, Sara. “ESP32 MQTT - Publish BME280 Sensor Readings (Arduino IDE).” Random Nerd Tutorials,

6 Apr. 2020, randomnerdtutorials.com/esp32-mqtt-publish-bme280-arduino.

[13] Santos, Sara. “9 Home Automation Open-Source Platforms for Your Projects.” Random Nerd Tutorials, 2

Apr. 2019, randomnerdtutorials.com/9-home-automation-open-source-platforms-for-your-projects.

[14] Soloelectronicos. “Instalación de Node-Red, InfuxDB y Grafana.” Soloelectronicos.com, 29 Nov. 2020,

soloelectronicos.com/2020/11/29/instalacion-de-node-red-infuxdb-y-grafana.

[15] Oneguyoneblog. “IoT Server: Mosquitto and Node Red on Raspberry Pi.” One Guy, One Blog, 5 Oct. 2017,

oneguyoneblog.com/2017/06/20/mosquitto-mqtt-node-red-raspberry-pi.

[16] “Running on Raspberry Pi: Node-RED.” Node-RED, nodered.org/docs/getting-started/raspberrypi.

[17] “Node-Red-Dashboard.” Node-RED Dashboard, flows.nodered.org/node/node-red-dashboard.

81
[18] Cope, Steve. “MQTT Brokers and Cloud Hosting Guide.” Steve’s Internet Guide, 10 Mar. 2021,

www.steves-internet-guide.com/mqtt-hosting-brokers-and-servers.

[19] “Node-Red-Contrib-Aedes.” Node-RED Aedes Broker, https://flows.nodered.org/node/node-red-

contrib-aedes.

[20] Centurio. “How to install InfluxDB and Grafana on a Raspberry Pi 3 – Centurios Blog.” Centurios Blog, 28

Oct. 2018, centurio.net/2018/10/28/howto-install-influxdb-and-grafana-on-a-raspberry-pi-3.

[21] “Installing InfluxDB & Grafana on Raspberry Pi.” Simon Hearne, simonhearne.com/2020/pi-influx-grafana.

[22] “#255 Node-Red, InfluxDB, and Grafana Tutorial on a Raspberry Pi.” YouTube, uploaded by Andreas

Spiess, 10 Feb. 2019, www.youtube.com/watch?v=JdV4x925au0&ab_channel=AndreasSpiess.

[23] “Install Grafana on Raspberry Pi.” Grafana Labs, grafana.com/tutorials/install-grafana-on-raspberry-pi.

[24] “Node-Red-Contrib-Influxdb.” Node-RED InfluxDB, flows.nodered.org/node/node-red-contrib-influxdb.

[25] Simic, Sofija. “How to Install Docker on Raspberry Pi.” Knowledge Base by PhoenixNAP, 11 May 2021,

phoenixnap.com/kb/docker-on-raspberry-pi.

[26] Ellis, Alex. “Get Started with Docker 1.12 on Raspberry Pi.” Alex Ellis’ Blog, 3 Aug. 2017,

blog.alexellis.io/getting-started-with-docker-on-raspberry-pi.

[27] Cooling, Niall. “Running the Eclipse-Mosquitto MQTT Broker in a Docker Container.” Sticky Bits - Powered

by Feabhas, 25 Mar. 2021, blog.feabhas.com/2020/02/running-the-eclipse-mosquitto-mqtt-broker-in-a-

docker-container.

[28] “Eclipse MQTT 2.x in Docker + user credentials on Synology.” YouTube, uploaded by BeardedTinker, 17

Feb. 2021, www.youtube.com/watch?v=ABb-63y0Em4&feature=youtu.be.

[29] “Running under Docker: Node-RED.” Node-RED, nodered.org/docs/getting-started/docker.

[30] “Docker Hub: InfluxDB.” Docker Hub, hub.docker.com/_/influxdb.

[31] “Configure Grafana Docker Image.” Grafana Labs,

grafana.com/docs/grafana/latest/administration/configure-docker/#run-grafana-container-using-bind-

mounts.

[32] “Installing ThingsBoard on Raspberry Pi 3 Model B.” ThingsBoard, thingsboard.io/docs/user-

guide/install/rpi.

[33] Mishra Biswajeeban. “Performance Evaluation of MQTT Broker Servers.” Computational Science and Its

Applications – ICCSA 2018, 2018, Lecture Notes in Computer Science, vol 10963. Springer, Cham.

https://doi-org.recursos.biblioteca.upc.edu/10.1007/978-3-319-95171-3_47

[34] Takanorig. “MQTT-Bench: MQTT Benchmark Tool.” GitHub, 6 Dec. 2015, github.com/takanorig/mqtt-

bench.

[35] Inovex. “MQTT Stresser.” GitHub, 7 July 2020, github.com/inovex/mqtt-stresser.

82
[36] The Raspberry Pi Foundation. “Raspberry Pi OS” Raspberry Pi, www.raspberrypi.org/software/.

[37] “Download the Free Nmap Security Scanner for Linux/Mac/Windows.” NMAP, nmap.org/download.html.

[38] Boulogeorgos, A.A., Diamantoulakis, P.D., & Karagiannidis, G. (2016). Low Power Wide Area Networks

(LPWANs) for Internet of Things (IoT) Applications: Research Challenges and Future Trends. ArXiv,

abs/1611.07449.

83
Appendices:

Appendix 1: Raspberry Pi Installation


1. Install Raspberry Pi OS Lite
Raspberry PI OS is the default choice with RPi boards. Supported by Raspberry Pi
Foundation, it is always compatible with their hardware. Opting for Raspberry PI
OS Lite vs Desktop version depends on your needs and your Linux Skills:
• If you want an environment easy to use for Linux non-experts and ready to
start, then you should use Desktop version.
• If you want the best from you Raspberry PI performances and use it to
expose services to other computers, the you should use Lite version.

Raspberry Pi have developed a graphical SD card writing tool that works on Mac
OS, Ubuntu 18.04 and Windows, and is the easiest option for most users as it will
download the image and install it automatically to the SD card.

• Download the latest version of Raspberry Pi Imager1 and install it.


• Connect an SD card reader with the SD card inside.
• Open Raspberry Pi Imager and choose the required OS from the list
presented.

Figure 88. Raspberry Pi Imager

84
Figure 89. Raspberry Pi OS selection

• Choose the SD card you wish to write your image to.


• Review your selections and click 'WRITE' to begin writing data to the SD
card.

Figure 90. Writing OS to SD card

You can now insert the SD card into the Raspberry Pi and power it up.
2. Accessing the Raspberry Pi
For the official Raspberry Pi OS, if you need to manually log in, the default user
name is pi, with password raspberry. Remember the default keyboard layout is set
to UK.
You should change the default password straight away to ensure your Raspberry
Pi is secure.

85
2.1. Configuring without HDMI display, Ethernet connection, keyboard and
mouse

Download to your PC the Image:


https://downloads.raspberrypi.org/raspios_lite_armhf/images/raspios_lite_arm
hf-2021-01-12/2021-01-11-raspios-buster-armhf-lite.zip
Download Rufus software and install it.
Link: https://www.raspberrypi.org/downloads/raspbian/

Open Rufus and select the SD card on which the Raspbian OS is to be installed.
Select the iso file of the Raspbian OS and click Start. SD card will be formatted
and Raspbian OS will be written on it.

To connect and configure your Raspberry Pi without any connection, you must
first create a file with name ‘ssh’ without any extension and put it in the SD card
folder. This is to enable SSH in Raspberry-Pi. There will be no code in this file.

All files for example in Windows notepad must be saved as ANSI type.
Create a file with name ‘wpa_supplicant.conf’ and put it in the SD card folder.
This file is to enable Raspberry-Pi Wifi adapter to connect to the Wifi with given
credentials. The code present in this file should be:
country=ES
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
ssid="yourwifiname"
scan_ssid=1
psk="yourwifipassword"
key_mgmt=WPA-PSK
}

Then you must look the ip that is assignes by using nmap [37] :
nmap -sP -PI -PT <ip address of host>/24

86
To know ip address of the host:
cmd -> ipconfig /all
Then, you can localise the Raspberry Pi IP with nmap and access using ssh
to:
ssh pi@ip

Figure 91. Localising Raspberry Pi IP

2.2. Connection using HDMI display, keyboard and mouse

You can change your keyboard layout (for example, to Spanish) with the
following steps:

− Enter sudo raspi-config in a terminal window


− Select Localization Options
− Navigate to and select Keyboard
− Configure with your Keyboard specifications.

Another command to configure your keyboard briefly and provisionally is using


the following command:
setxkbmap -layout es

3. Configure Network access


If you decide to connect the Raspberry via ethernet, the connection is very simple.
If you decide to connect it via WiFi, we must perform the following steps.
The quickest way to enable wireless networking is to use the command line raspi-
config tool.
Select the Localisation Options item from the menu, then the Change wireless
country option. On a fresh install, for regulatory purposes, you will need to specify
the country in which the device is being used. Then set the SSID of the network,
and the passphrase for the network. If you do not know the SSID of the network

87
you want to connect to, see the next section on how to list available networks prior
to running raspi-config.
sudo raspi-config

Optionally, network details can also be added to the Raspberry Pi via command
line.
Open the wpa-supplicant configuration file in nano:
sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
Go to the bottom of the file and add the following:
Note that with the latest Buster Raspberry Pi OS release, you must ensure that the
wpa_supplicant.conf file contains the following information at the top:

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=<Insert 2 letter ISO 3166-1 country code here>

The following parameters are obligatory for a basic configuration:


network={
ssid="name_wifi_network"
psk="password_wifi_network"
}

More parameters can be added depending on the configuration required.


• "key_mgmt" parameter can be add if the network you are connecting to
does not use a password (note that the "psk" parameter should be removed).
• "id_str" parameter: On recent versions of Raspberry Pi OS, it is possible to
set up multiple configurations for wireless networking. For example, you
could set up one for home and one for the office.
• If you are using a hidden network, an extra option in the wpa_supplicant file,
"scan_ssid", may help connection.
network={
ssid="yourHiddenSSID"
scan_ssid=1
psk="Your_wireless_network_password"
}

88
In our example, the configuration file looks like this:
country=ES
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
ssid="name_wifi_network_1"
psk="password_wifi_network_1"
id_str="name_1"
}
network={
ssid="name_wifi_network_2"
psk="password_wifi_network_2"
id_str="name_2"
}

Note: If we have some problems to scan for wireless networks, using the command:
sudo iwlist wlan0 scan

We will list the available interfaces using the command:


sudo ifconfig

If we cannot visualize the wlan0 interface, we apply the following command that will
display all interfaces which are currently available, even if down:
sudo ifconfig -a

So, if now we can visualize the wlan0, it means is down and we will apply the next
command:

If we continue without visualizing the wlan0 interface, we will apply the next
commands, because the system is blocking the interface:
rfkill list all
sudo rfkill unblock all
sudo ip link set wlan0 up

4. Enable SSH server:


It can be enabled from terminal using raspi-config:
Enter sudo raspi-config in a terminal window
Select Interfacing Options
Navigate to and select SSH

89
Choose Yes
Select Ok
Choose Finish

Alternatively, use systemctl to start the service:


sudo systemctl enable ssh
sudo systemctl start ssh

5. Configure static IP
To facilitate access to the command line a static IP will be configured, which gives
us the possibility of saving our access in the SSH client software.
To configure a static IP, we must edit the /etc/dhcpcd.conf file with the command:
sudo nano /etc/dhcpcd.conf

If we look at the content of the file, we will see some commented lines (they start
with '#') that have an example of static IP configuration:

# Example static IP configuration:


#interface eth0
#static ip_address=192.168.0.10/24
#static ip6_address=fd51:42f8:caae:d92e::ff/64
#static routers=192.168.0.1
#static domain_name_servers=192.168.0.1 8.8.8.8
fd51:42f8:caae:d92e::1

To create our own static IP address, for example for the WiFi interface (wlan0), we
copy the commented fragment and modify it so that it looks like the following.

interface wlan0
static ip_address=192.168.0.20/24
static routers=192.168.1.1
static domain_name_servers=192.168.1.1

interface = Name of the interface that we want to configure


static ip_address = Fixed address we want (leave / 24 at the end)
static routers = Gateway address (of the router)
static domain_name_servers = Address of the DND server (usually that of the
router).
If we want more than one DNS server you can add them separator by a space.

90
Now we restart the Raspberry Pi with the command:
sudo reboot

Finally, we check that, indeed, the assigned IP is the one we have configured by
executing with the command:
ifconfig wlan0

Appendix 2: Node-RED Flow


Flow clipboard:
[{"id":"f8e5facc.9be588","type":"tab","label":"Final
Flow","disabled":false,"info":""},{"id":"2ed4ab5c.22b614","type":"mqtt
in","z":"f8e5facc.9be588","name":"","topic":"bmp280","qos":"2","datatype":"auto","b
roker":"83fc0f7f.59a84","x":119,"y":220,"wires":[["b3932d8c.01ed7"]]},{"id":"c74cdc
d.8eaa02","type":"influxdb
out","z":"f8e5facc.9be588","influxdb":"48d8c90b.301e38","name":"","measurement
":"bmp280","precision":"s","retentionPolicy":"","database":"database","precisionV1
8FluxV20":"ms","retentionPolicyV18Flux":"","org":"organisation","bucket":"bucket",
"x":699,"y":220,"wires":[]},{"id":"425d34ff.f8d1fc","type":"debug","z":"f8e5facc.9be5
88","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"comple
te":"false","statusVal":"","statusType":"auto","x":629,"y":300,"wires":[]},{"id":"b3932
d8c.01ed7","type":"json","z":"f8e5facc.9be588","name":"","property":"payload","act
ion":"","pretty":false,"x":289,"y":220,"wires":[["425d34ff.f8d1fc","c74cdcd.8eaa02"]]
},{"id":"647d8be1.d71d24","type":"function","z":"f8e5facc.9be588","name":"","func"
:"msg.payload={\n \"temperature\": parseFloat((Math.random()* (85 - (-40)) + (-
40)).toFixed(2)),\n \"humidity\": parseFloat((Math.random()*100).toFixed(2)),\n
\"pressure\": parseFloat((Math.random()* ((1110 - 300) +
300)).toFixed(2))\n}\nreturn
msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":260,"y":60,"wires":[["399e
00ab.46301","3129be12.876422"]]},{"id":"5d886fec.29f6c","type":"inject","z":"f8e5f
acc.9be588","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":fals
e,"onceDelay":0.1,"topic":"","payload":"{}","payloadType":"json","x":110,"y":60,"wir
es":[["647d8be1.d71d24"]]},{"id":"3129be12.876422","type":"mqtt
out","z":"f8e5facc.9be588","name":"","topic":"bmp280","qos":"","retain":"","broker":"
83fc0f7f.59a84","x":440,"y":60,"wires":[]},{"id":"399e00ab.46301","type":"debug","z
":"f8e5facc.9be588","name":"","active":true,"tosidebar":true,"console":false,"tostat
us":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"aut
o","x":470,"y":120,"wires":[]},{"id":"83fc0f7f.59a84","type":"mqtt-
broker","name":"","broker":"localhost","port":"1883","clientid":"","usetls":false,"com
patmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","
birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","will
Qos":"0","willPayload":""},{"id":"48d8c90b.301e38","type":"influxdb","hostname":"lo
calhost","port":"8086","protocol":"http","database":"home","name":"","usetls":false,"

91
tls":"","influxdbVersion":"1.x","url":"http://localhost:8086","rejectUnauthorized":true}
]

Appendix 3: ESP32 & BMP280 code for Node-RED


#include <Wire.h>

#include <SPI.h>

#include <WiFi.h> // include WiFi library

#include <Adafruit_Sensor.h>

#include <Adafruit_BME280.h>

#define BME_SCK 13

#define BME_MISO 12

#define BME_MOSI 11

#define BME_CS 10

// configure Sea level pressure

//https://www.meteo.cat/observacions/xema/dades

#define SEALEVELPRESSURE_HPA (1017.9)

#include <PubSubClient.h>

Adafruit_BME280 bme; // I2C

//YOU MUST CHANGE ssid

const char* ssid = "your_wifi_network"; // The SSID (name) of the Wi-Fi network you want to connect
to

//YOU MUST CHANGE password

const char* password = "your_wifi_password"; // The password of the Wi-Fi network

//Platform server

const char* mqttServer = "192.168.43.79";

//Platform port

const int mqttPort = 1883;

//YOU MUST CHANGE your device TOKEN

const char* mqttUser = "";

const char* mqttPassword = "";

WiFiClient espClient;

PubSubClient client(espClient);

int status = WL_IDLE_STATUS;

unsigned long lastSend;

void setup() {

Serial.begin(115200); // Start the Serial communication to send messages to the computer

92
delay(10);

Serial.println('\n');

bool status;

// default settings

// (you can also pass in a Wire library object like &Wire2)

status = bme.begin(0x76);

if (!status) {

Serial.println("Could not find a valid BME280 sensor, check wiring!");

while (1);

/*Connecting ESP32 to WiFi */

WiFi.begin(ssid, password); // Connect to the network

Serial.print("Connecting to ");

Serial.print(ssid);

while (WiFi.status() != WL_CONNECTED) { // Wait for the Wi-Fi to connect

delay(500);

Serial.print('.');

Serial.println('\n');

Serial.println("Connection established!");

Serial.print("IP address:\t");

Serial.println(WiFi.localIP()); // Send the IP address of the ESP32 to the computer

Serial.println('\n');

//Connecting ESP32 to MQTT Broker, subscribing and receiving messages

client.setServer(mqttServer, mqttPort); //configure the PubSubClient with the address and port

while (!client.connected()) {

Serial.println("Connecting to MQTT...");

if (client.connect("ESP32Client", mqttUser, mqttPassword )) {

Serial.println("Client connected");

} else {

Serial.print("failed with state ");

Serial.print(client.state());

delay(2000);

lastSend=0;

93
}

void loop() {

if(millis()-lastSend>1000){

getAndSendData();

lastSend=millis();

client.loop();

void getAndSendData()

Serial.print("getAndSendData: ");

float h=bme.readHumidity();

float t=bme.readTemperature();

float p=bme.readPressure()/ 100.0F;

float a=bme.readAltitude(SEALEVELPRESSURE_HPA);

String temperature=String(t);

String humidity=String(h);

String pressure=String(p);

String altitude=String(a);

Serial.print("Temperature = ");

Serial.print(temperature);

Serial.println(" *C");

Serial.print("Pressure = ");

Serial.print(pressure);

Serial.println(" hPa");

Serial.print("Approx. Altitude = ");

Serial.print(altitude);

Serial.println(" m");

Serial.print("Humidity = ");

Serial.print(humidity);

Serial.println(" %");

Serial.println();

//prepare a JSON payload string

String payload = "{";

payload += "\"temperature\":"; payload += temperature; payload +=",";

//payload += "\"temperature\":"; payload += temperature;

94
payload += "\"pressure\":"; payload += pressure; payload +=",";

payload += "\"humidity\":"; payload += humidity; payload +=",";

payload += "\"altitude\":"; payload += altitude;

payload += "}";

char telemetry[1000];

payload.toCharArray( telemetry, 1000 );

client.publish( "bmp280", telemetry );

Serial.println( telemetry );

Serial.println('\n');

delay(10000);

Appendix 4: ESP32 & LED code for Node-RED


#include <WiFi.h> // include WiFi library

#include <PubSubClient.h> // Include library to create a client MQTT

const char* ssid = "your_wifi_network"; // The SSID (name) of the Wi-Fi network you want to connect
to

const char* password = "your_wifi_password"; // The password of the Wi-Fi network

const char* mqttServer = "192.168.43.79"; // The IP address of your MQTT Broker

const int mqttPort = 1883; // The TCP port of your MQTT Broker

const char* mqttUser = ""; // MQTT client user

const char* mqttPassword = ""; // MQTT client password

const int ledPin = 4;

WiFiClient espClient_1; // Define the client that allows us to transmit by WiFi. Must be different
for each MQQT client that you want to connect to a MQTT Broker

PubSubClient client(espClient_1); // Define the MQTT client that will access the MQTT Broker.

char payload[512]; // It will be the message to RECEIVE

/* Look for message received */

void callback(char* topic, byte* payload, unsigned int length) {

Serial.print("Message arrived in topic: ");

Serial.println(topic);

Serial.print("Message:");

95
for (int i = 0; i < length; i++) {

Serial.print((char)payload[i]);

digitalWrite (ledPin, HIGH);

delay(500);

digitalWrite (ledPin, LOW);

delay(500);

void setup() {

Serial.begin(115200); // Start the Serial communication to send messages to the computer

delay(10);

Serial.println('\n');

pinMode (ledPin, OUTPUT);

/*Connecting ESP32 to WiFi */

WiFi.begin(ssid, password); // Connect to the network

Serial.print("Connecting to ");

Serial.print(ssid);

while (WiFi.status() != WL_CONNECTED) { // Wait for the Wi-Fi to connect

delay(500);

Serial.print('.');

Serial.println('\n');

Serial.println("Connection established!");

Serial.print("IP address:\t");

Serial.println(WiFi.localIP()); // Send the IP address of the ESP32 to the computer

/*Connecting ESP32 to MQTT Broker, subscribing and receivin messages */

client.setServer(mqttServer, mqttPort); //configure the PubSubClient with the address and port

client.setCallback(callback); // for receiving messages

while (!client.connected()) { // While mqtt client it's not connected stay at loop

Serial.println("Trying to connect to MQTT");

96
if (client.connect("espClient_1", mqttUser, mqttPassword )) { // connect to broker (return true if connect, false
if not)

Serial.println("connected");

if(client.subscribe("alert", 1)){

Serial.println("Subscribed to alert");

} else {

Serial.print("failed with state ");

Serial.print(client.state());

delay(2000);

void loop() {

client.loop();

97
Glossary
This section contains alphabetically ordered list of all representative acronyms in
the project and the meaning they stand for.

DB: DataBase
IoT: Internet of Things
JS: JavaScript
JSON: JavaScript Object Notation
MQTT: Message Queuing Telemetry Transport
SSH: Secure SHell

98

You might also like