Configuring serial2mqtt
March 21, 2022 ยท View on GitHub
Most of the serial2mqtt options can be set from its configuration file. The configuration is in JSON format, and unless specified, located in the current directory named serial2mqtt.json. The whole file is a JSON object, built from the following groups:
serialfor serial port related parameters,mqttfor MQTT related parameters,logfor logging parameters, andprogrammer
The serial group
The serial group describes how to access the serial port, and how to communicate with the MCU over it. These are the supported options:
portsarray describing the serial ports, default value is/dev/ttyUSB0. See Serial port device format for details.baudrateserial baud rate, default value is115200.protocolspecifies how to communicate with the MCU, valid values arejsonObject(default) andjsonArray.reconnectIntervalis the time interval between loosing the serial connection and the reconnection starts. It is specified in milliseconds, the default value is5000. You can disable serial reconnection with0value, in this case serial2mqtt will exit upon serial disconnection.idleTimeoutis the maximal time interval without communication. Upon exceeding this limit, the serial connection will be closed and reopened. It is specified in milliseconds, the default value is5000. You can disable this functionality with0(can be useful during development and testing, but not recommented in production environment).silentIntervalspecifies the interval right after opening the serial device when all traffic will be discared. Its purpose is to let the bootloader run or avoid corrupted data appearing using USB ports. It is specified in milliseconds, the default value is0which disables this functionality.lowerDTROnOpencontrols how to handle the DTR line upon open.neverdisables lowering the DTR line on open,alwayswill lower it unconditionally on open. Default value isnever.lowerDTROnClosecontrols how to handle the DTR line upon close.neverdisables lowering the DTR line on close,alwayswill lower it unconditionally on close.ifIdlewill lower the DTR line on close if there's no received JSON message (log messages don't count) during the connection lifetime. Default value isnever.
Serial port device format
Serial ports can be local (traditional serial ports, USB dongles, bluetooth etc. accessible by a character device) or remote. Remote serial ports are serial ports on a remote machine, terminal server etc. shared over the network, e.g. this setup when the MCU is directly connected to an OpenWRT router.
If the serial port name begins with tcp://, it will be treated as a tcp address. The following address formats are supported:
tcp://127.0.0.1:1111(using IPv4 addresses)tcp://[::1]:1111(using IPv6 addresses)tcp://localhost:1111(using domain names)
Using the domain name format, the code tries every address record returned by the resolver.
Other names will be treated as a regular serial port specified by its character device.
The mqtt group
The mqtt group configures how to connect to the MQTT broker, and the extra functionality provided by serial2mqtt. These are the supported options:
connectionholds the MQTT broker address in URI format, default value istcp://test.mosquitto.org:1883.keepAliveIntervalis the MQTT keepalive interval parameter in seconds, default value is5.willTopicis the MQTT topic where the LWT message will be sent. Default value issrc/<hostname>.<serialPortShort>/serial2mqtt/alive.willMessageis the message body of the LWT message sent towillTopic. Default value isfalse.willQosis the QoS of the LWT message. Default value is0.willRetainedis the retained flag of the LWT message. Default value isfalse.clientIdis the MQTT client identifier, must be unique on the broker. Default value is<hostname>.<serialPortShort><process-id>.useris the user for MQTT authentication. Default value is an empty string, which means no authentication.passwordis the password for MQTT authentication. Default value is an empty string.reconnectIntervalis the time interval between loosing the MQTT broker connection and the reconnection start. It is specified in milliseconds, the default value is2000. You can disable MQTT broker reconnection with0value, in this case serial2mqtt will exit upon MQTT disconnection.publishIntervalis the time interval that serial2mqtt publishes its uptime and alive messages. You can disable this functionality with0(can be useful during development and testing).localPersistenceDiris the directory where local persistence database will be stored. Default value is an empty string, which disables this functionality. See Local persistence support for details.localPersistenceFiltersarray contains the topic filters, see Local persistence support for details.
Local persistence support
Local persistence is an useful, when:
- the MCU has no EEPROM to store its own configuration
- the configuration is too complex/big to store in the MCU
- the configuration changes are too frequent to wear out the EEPROM quickly
- the MCU developer is lazy to store the configuration locally
This feature implements something similar to the MQTT retained messages, but
- it is stored on the host running serial2mqtt so it is available during MQTT broker downtime
- no need to set the retain flag on the senders consistently. All of the messages will be saved matching one of the filters in the
localPersistenceFiltersarray
The local persistence functionality is disabled by default. The user has to set the localPersistenceDir parameter in the mqtt group to enable it, and has to give filters in the localPersistenceFilters array. If it's active, all of the stored messages will be played back right after every serial connection establishment and after every subscribe request.
Here's an example:
{
"mqtt": {
"connection": "tcp://test.mosquitto.org:1883",
"localPersistenceDir": "/var/serial2mqtt",
"localPersistenceFilters": ["hvac/controller/+/set"]
The log group
The log group configures how to handle logging:
protocolcontrols whether the serial protocol logging is enabled or disabled, default value isfalse.debugcontrols whether debug logging is enabled or disabled, default value istrue.mqttcontrols whether MQTT related logging is enabled or disabled, default value isfalse.programcontrols whether the MCU firmware programming logging is enabled or disabled, default value isfalse.useColorscontrols whether the logging messages are colored or not. Default value istrue.consolelog to stdoutfilefile prefix for rotating logs 5 of each 2MB