Skip to main content

Configuring the MQTT server

This script allows you to subscribe to an MQTT topic, receive messages and store them in a SQLite database. The steps to configure and execute it correctly are described below.

Configuration of the configuration file

Before running the script, a configuration file in .ini format must be created. This file contains the necessary parameters for the connection to the MQTT broker, the database and the log configuration. An example of a configuration file is as follows:

[DEFAULT]

;;topic
topic = testtopic/#

;; connection
host = test.mosquitto.org
port = 1883
;; protocol supports:  tcp, websockets, unix
; protocol = websockets 
; user = ro
; password = readonly
; ssl = 0
; trust_ssl = 1

;; Database
dblocation = /opt/pandora/pandora_iot_server/db
dbname = pandora_iot.db
; data_cleaning_interval = 300  
; data_cleaning_period = 86400  

;; Logs
log_name = pandora_iot.log   
log_location = /var/log/pandora/
; log_level = debug              
; max_log_bytes = 50_000_000         
; log_rotation_count = 3
Key parameters:
  • topic: Defines the MQTT topic to which the script will subscribe (e.g. testtopic/#).
  • host : Specify the address of the MQTT broker.
  • port: Specify the port of the MQTT broker.
  • protocol: The protocol to use for the MQTT connection (e.g. tcp or websockets).
  • ssl: Enables or disables the SSL connection (0 for disable, 1 for enable).
  • trust_ssl: Controls whether to trust the broker's SSL certificates.
  • dblocation: Directory where the SQLite database will be stored.
  • dbname: Name of the SQLite database.
  • data_cleaning_interval: Time interval (in seconds) between each execution of the data cleaning process.
  • data_cleaning_period: Time (in seconds) to remove old data from the database.
  • log_name: Name of the file where the logs will be stored.
  • log_location: Directory where the log files will be stored.
  • log_level: Log detail level (e.g. debug).
  • max_log_bytes: Maximum size (in bytes) that a log file can reach before being rotated.
  • log_rotation_count: Maximum number of log files that will be kept before deleting the oldest ones.
2. Start the service.

The service can be launched with the following command :

systemctl start pandora_iot_server

To stop receiving and storing messages, you can pause the service with :

systemctl stop pandora_iot_server

The service must be restarted with each configuration change for it to take effect:

systemctl restart pandora_iot_server