Install mosquitto server
// Install the required packages sudo apt-get update sudo apt-get install build-essential libwrap0-dev libssl-dev libc-ares-dev uuid-dev xsltproc sudo apt-get install mosquitto mosquitto-clients // Add user mosquitto (password: mosquitto) sudo adduser mosquitto // Create the user maestro (password: wireless) that will be able to connect to the MQTT server sudo mosquitto_passwd -c /etc/mosquitto/pwfile maestro // mosquitto user will be owner of folder /var/lib/mosquitto sudo chown mosquitto:mosquitto /var/lib/mosquitto/ -R // Edit the configuration file sudo vim /etc/mosquitto/mosquitto.con
The configuration file should look like this:
// Replace "192.168.1.125" with the IP address of your virtual machine listener 8883 192.168.1.125 persistence true persistence_location /var/lib/mosquitto/ persistence_file mosquitto.db log_dest syslog log_dest stdout log_dest topic log_type error log_type warning log_type notice log_type information connection_messages true log_timestamp true allow_anonymous false password_file /etc/mosquitto/pwfile
To check that it works, let’s open three terminals on the virtual machine:
// Start mosquitto server mosquitto -c /etc/mosquitto/mosquitto.conf
// Subscribe to topic mosquitto_sub -h 192.168.1.125 -p 8883 -v -t ‘test/hackathon’ -u maestro -P wireless
// Publish message to the server mosquitto_pub -h 192.168.1.125 -p 8883 -t test/hackathon -m “Hi there” -u maestro -P wireless
If everything went ok, you should see on the subscriber terminal the text “Hi there” that has been submitted by the publisher.
Install Modbus master and MQTT client
Go to System > Software > Configuration and replace the URL of the last line with this one: “http://repo.maestro-wireless.eu/packages/“. Click Submit at the bottom of the page.
Click on Actions to switch the tab and then press Update lists. Now, on the filter text box enter “Modbusmaster_V3.2” and click Find package. Click on Available packages and Install Modbusmaster_V3.2. This operation will take ~5 minutes as it needs to download all dependencies. It is very likely that you will get a Bad Gateway screen as the operation will timeout. Do not worry. Wait for 5 minutes and then go back to the web interface of the router.
After 4 minutes, go back to System > Software and make sure that Modbusmaster_V3.2 is listed at the top in the Installed packages column. Go to System > Reboot and restart your router. The reboot process will take a bit longer than usual as all dependencies need to be configured accordingly.
Go to Services > Modbus master and check that the application has been installed successfully. You should see “Modbus Status: Mysql Installation Done”.
Go back to System > Software and Update lists again. Enter “mqtt” in the filter and check the available packages. Install Modbus_MQTT 2.0
Reboot the router again after the installation is done.
Modbus configuration file
The configuration of the Modbus Master application is done via a CSV file that includes all the settings needed by the program. A sample of a configuration file is provided for download at the end of this section. A fully working configuration file (the one used in this example) is provided as well and it will be the basis to explain the different parameters that we can configure.
- Rows 1, 2 and 4 are just markup so the Modbus master package can retrieve the settings from the file
- Row 3 holds the configuration of the MQTT server. The meaning of the fields are:
It is possible to define up to 10 servers, one per line.
192.168.1.125: IP address of the MQTT server
8883: port where the server is listening
maestro: username used to publish data
wireless: password used to publish data
hackathon/bcn: topic where the data will be published
- Row 6 holds extra settings for the Modbus logger feature:
E224: logger ID (it can be the IMEI, MAC address or any other value that identifies the unit)
3000: timeout, in ms, for Modbus RTU
1000: timeout, in ms, for Modbus TCP
1: enable write/read functionality for a particular tag from TCP client
1234: TCP port number to receive read/write requests
- Row 8 defines the start of a new Polling Group
It is possible to define up to 6 different Polling Groups, each one of them with its own settings. Each Polling Group can retrieve data from up to a maximum of 500 registers. The total number of registers that can be read is 1500.
- Row 10 defines the settings for Polling Group 1
Group1: is the name of the first group
5: is the polling interval, in seconds
1: defines the logging functionality (0: log only if data changes its value, 1: log all)
0: GPS data enable (0-disable / 1-enable)
0: LAN data enable (0-disable / 1-enable)
1: WAN data enable (0-disable / 1-enable)
- mwsCode]1[/mwsCode]: Cellular data enable (0-disable / 1-enable)
1: General Info data enable (0-disable / 1-enable)
- Row 12 (and similarly Row 16) contains a Modbus query:
HMETER: name that identifies the Modbus slave
127: slave ID
4: function code (3-read holding registers / 4-read input registers)
15: start address
1: length (number of registers to read)
NULL: IP address (for Modbus TCP)
NULL: port number (for Modbus TCP)
- Row 13 (and similarly Row 17) defines a piece of data included retrieved with the Modbus request in the previous row:
voltage: is the tag name used to identify this parameter retrieved from the Modbus slave
0: it is the offset, in this case it is the first register read from Row 12
2: is the length, in bytes
3: tag datatype (3 is unsigned integer)
0.01: scaling coefficient A (Ax+B). It means the value will be multiplied by 0.01 once read
0: scaling coefficient B (Ax+B). It allows to define an offset to add to the value read
4: alert flag
for analog values: 1-threshold on both / 2-threshold on min value / 3-threshold on max value / 4-Disable alert
for digital values: 1-alert on 0 / 2-alert on 1 / 3-alert on change / 4-Disable alert
NULL: minimum threshold (for alert)
NULL: maximum threshold (for alert)
NULL: mobile number to send alert SMS
0: critical tag (1-critical / 0-normal)
When an alarm situation is detected, all critical tags are polled out of turn depending on the polling group they are defined in.
0: alarm tag (1-Yes / 0-No)
0: store as float (1-Yes / 0-No)
- The configuration file allows to define up to 5 different Upload Groups. This allows to poll data every X seconds and report it to the server every Y seconds. The main settings are defined in Row 23:
Group1: name of the Upload Group
20: upload interval, in seconds
1: Generic agent data for Polling Group 1 (1-Enable / 0-Disable)
0: Generic agent data for Polling Group 2 (1-Enable / 0-Disable)
0: Generic agent data for Polling Group 3 (1-Enable / 0-Disable)
0: Generic agent data for Polling Group 4 (1-Enable / 0-Disable)
0: Generic agent data for Polling Group 5 (1-Enable / 0-Disable)
0: Generic agent data for Polling Group 6 (1-Enable / 0-Disable)
- Row 24 (and similarly Row 25) defines a tag to be uploaded:
voltage:tag name (as defined in Row 13)
1:Polling Group number
HMETER: equipment ID (as defined in Row 12)
Test the application
Once the configuration is done and save in the CSV file we can start testing it.
- Tick the Enable box, Upload the configuration file and click on Save & Apply:
- Configure the Serial settings for the Modbus RTU mode from Services > Serial tab. Click Save & Apply:
- From the Data Window, by clicking on Enable Data Window and then clicking on Show Data it is possible to check in real time the values obtained by the Modbus master application:
- Go back to your server and execute the following commands:
// Start the Mosquitto server mosquitto -c /etc/mosquitto/mosquitto.conf // In another terminal, subscribe to the topic used in the configuration file mosquitto_sub -h 192.168.1.125 -p 8883 -v -t 'hackathon/bcn' -u maestro -P wireless