Service configuration

With the Raspbian Linux distribution for Raspberry Pi SBCs, system services are usually managed with systemd. This uses a simple configuration file, with our club monitoring service using something like the following:

[Unit] 
Description=ClubStatus monitoring & control 
 
[Service] 
ExecStart=/home/user/clubstatus/clubstatus /home/user/clubstatus/config.ini 
User=user 
WorkingDirectory=/home/user/clubstatus 
Restart=always 
RestartSec=5 
 
[Install] 
WantedBy=multi-user.target 

This service configuration specifies the name of the service, with the service being started from the "user" user account's folder, and the configuration file for the service being found in the same folder. We set the working directory for the service, also enabling the automatic restarting of the service after five seconds if it were to fail for whatever reason.

Finally, the service will be started after the system has started to the point where a user can log in to the system. This way, we are sure that networking and other functionality has been started already. If one starts a system service too soon, it could fail due to missing functionality on account of things not having been initialized yet.

Next, here is the INI file configuration file:

[MQTT]
; URL and port of the MQTT server.
host = localhost
port = 1883

; Authentication
user = user
pass = password

; The topic status on which changes will be published.
clubStatusTopic = /my/topic

[HTTP]
port = 8080

[Relay]
; Whether an i2c relay board is connected. 0 (false) or 1 (true).
active = 0
; i2c address, in decimal or hexadecimal.
address = 0x20

The configuration file is divided into three sections, MQTT, HTTP, and Relay, with each section containing the relevant variables.

For MQTT, we have the expected options for connecting to the MQTT broker, including password-based authentication. We also specify the topic regarding which club status updates will be published here.

The HTTP section just contains the port we will be listening on, with the server listening on all interfaces by default. If necessary, one could make the network interface a used configurable as well by making this property configurable before starting the HTTP server.

Finally, the Relay section allows us to turn the relay board feature on or off, as well as configure the I2C device address if we are making use of this feature.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.147.124