MODBUS on the Pi Part 4, Creating a MODBUS Supervisor

Managing your MODBUS devices is done by a ‘MODBUS Supervisor’ herein known as the ‘Supervisor’. It is typically a server at a central location that has direct communication access to each of the MODBUS devices you want to monitor. The Raspberry PI has more than enough power to act as a supervisor for up to 100 MODBUS devices, depending on how often you want to poll them for status information. A very simple MODBUS device like a pressure monitor or flow meter has a tiny amount of data to pull, so you could probably monitor many hundreds of devices with ease even using a PI!

Thankfully, the Linux distribution for the PI (Rasbian) is based on Debian and pretty much anything we write can run on the PI or a full fledged Linux server. I have chosen to write the Supervisor code in the Qt framework as it is a native C++ environment with the added benefits of ‘signals and slots’ that makes it very easy to write code. I have used Qt for many projects, from command line Linux web services to OpenGL based Scientific rendering applications. I really like it and prefer it to all other development platforms due to it’s clear documentation, good IDE, high performance executables and multi-platform capabilities. I have also written Qt apps under Linux and ported them to Windows without any difficulties. Ok, enough about Qt, on to the Supervisor!

The basic functionality of a supervisor involves receiving commands from a user or service to pull data and send commands from the MODBUS devices. There are a million ways this can be done, but the method I have chosen is to use a MySQL database with a command queue to deliver these requests to the Supervisor. The reason for the command queue is for tracing and logging. If you were to use this program in a commercial environment it is very important to have a log of all commands that were sent to the devices and when they were sent, as well as which commands completed successfully and which did not. Also, if we are using a queue with some status information, we can gracefully handle any program crashes, or system failures with confidence in knowing what has been done, and what has failed. MySQL can handle 10000 times what we will be throwing at it for this purpose.

An added benefit of using MySQL for the command queue is that we can easily interface a web application to the database without much work. If we were to create a custom API of some kind to access the Supervisor, we would end up with a lot of extra work verses a simple ‘INSERT INTO…’ SQL statement. Tip: Never reinvent the wheel if you can help it because it is a waste of time and will make you appear unproductive compared to other developers.

I have completed writing a MODBUS Supervisor in Qt which I will be fully publishing here, and I have written a php/javascript based website that allows you to access all of your MODBUS devices using the Supervisor, but it will take a fair amount of time to publish all of this information as it is a very large amount of code. I will not be sharing a github repository of the php code as the purpose of this article is not to give the world an industrial quality MODBUS solution for free, but to be an on-line portfolio of my skills and knowledge.

Step 1, MySQL tables for the command queue and logging

Step 2, Simple Qt Application that connects to the SQL database and reads the Command queue.