NIkolina Duranec
DATA ENGINEER
As technology advances, we increasingly want to manage our devices online. As all these devices use electrical energy, it would be perfect to turn on that device when the electricity is the cheapest. We wanted to use a messaging system that allows us to receive messages, but also send commands to the devices when needed. The original idea was to use the Azure IoT Hub for device messaging, but we decided to use Eclipse Mosquitto. We agreed on Eclipse Mosquitto because, due to the expectation of a large number of devices, Eclipse Mosquitto running on AKS is more affordable than Azure IoT Hub.
They are both messaging systems used for communication between IoT devices and applications.
Eclipse Mosquitto vs Azure IoTHub
Eclipse Mosquitto is an open-source message broker that implements the MQTT (Message Queuing Telemetry Transport) protocol, which is widely used in IoT applications. It provides a lightweight, efficient, and reliable way for devices to communicate with each other and exchange data. It is ideal for low-power, resource-constrained environments that need to communicate with each other in real time. Eclipse Mosquitto supports multiple authentication methods: password files, authentication plugins, and unauthorized/anonymous access.
On the other hand, IoT Hub is a managed cloud service provided by Microsoft Azure that supports multiple messaging protocols, including MQTT, AMQP, and HTTP. Azure IoT Hub provides a scalable and secure way for IoT devices to connect to the cloud, send telemetry data, and receive commands and notifications. It offers features such as device management, message routing, and device-to-cloud and cloud-to-device communication. Azure IoT Hub charges depending on the number of messages and depending on how many messages we plan to have, we choose a tier.
MQTT (Message Queuing Telemetry Transport)
MQTT (Message Queuing Telemetry Transport) is a lightweight messaging protocol designed for use in constrained networks with low bandwidth and high latency.
MQTT is based on the publish-subscribe model, in which clients connect to a broker and subscribe to specific topics. Publishers send messages to the broker, which then delivers them to all subscribed clients. MQTT supports a variety of quality of service (QoS) levels, which determine how reliably messages are delivered. QoS 0 (at most once) provides fire-and-forget delivery, QoS 1 (at least once) provides guaranteed delivery with possible duplicates, and QoS 2 (exactly once) provides guaranteed delivery without duplicates.
MQTT is widely used in the Internet of Things (IoT) domain, where it provides a lightweight and efficient way to connect sensors and devices to the cloud.
Deploying Eclipse Mosquitto
First, we need to define the configuration for our broker:
apiVersion: v1
kind: ConfigMap
metadata:
name: mosquitto-config
data:
mosquitto.conf: |
allow_anonymous false
password_file mosquitto/config/password.txt
persistence true
persistence_location /mosquitto/data
log_dest file /mosquitto/log/mosquitto.log
listener 1883
listener 9001
protocol websockets
Here is an example of a Secret containing the password file content that is generated using the method that can be found on this page: mosquitto_passwd man page.
apiVersion: v1
kind: ConfigMap
metadata:
name: mosquitto-password
labels:
app: mosquitto
data:
password.txt: |
We can then deploy the broker in the desired configuration, together with the storage and a designated Service:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mosquitto-data
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: mosquitto
spec:
replicas: 1
selector:
matchLabels:
app: mosquitto
template:
metadata:
labels:
app: mosquitto
spec:
containers:
- name: mosquitto
image: eclipse-mosquitto
ports:
- containerPort: 1883
- containerPort: 9001
volumeMounts:
- mountPath: /mosquitto/config/mosquitto.conf
subPath: mosquitto.conf
name: config
- mountPath: /mosquitto/data/
name: data
- mountPath: /mosquitto/config/password.txt
subPath: password.txt
name: mosquitto-password
volumes:
- name: config
configMap:
name: mosquitto-config
- name: data
persistentVolumeClaim:
claimName: mosquitto-data
- name: mosquitto-password
configMap:
name: mosquitto-password
---
apiVersion: v1
kind: Service
metadata:
name: mosquitto
spec:
selector:
app: mosquitto
ports:
- name: mqtt
port: 1883
targetPort: 1883
- name: mqtt-ws
port: 9001
targetPort: 9001
type: LoadBalancer
An example how we can publish message and subscribe on some topic.
Mosquitto Broker for Energy Communities project
For now, for the Energy Communities project, we send messages to the dongle via the Mosquitto broker. For example, we want to ping or we want to update the firmware. This is why we need cloud-to-device communication.
We send messages in JSON format. On the other hand, we can get a response from the device in real time.
An example of what the command and response for ping looks like.