MODBUS application


Has anyone have an example of a MODBUS application collecting PLC data points and funneling through LOSANT MQTT. Appreciate sharing


Hi @Sreyams_Jain

Yes, I have an example for this. Here is the example code:

Because you asked, I can write a blog post on this topic. But, for now, I can illuminate a bunch of things.

I wanted to create a dashboard that allowed me to control the coils ( or outputs ) of the PLC. Like so:

First I created a device with a bunch of Boolean attributes that represents all the coils ( I called mine outputs ). Like so:

Then, using a gateway and the example code on Github, I use a Losant Device Command to send a message to the PLC. The flow looked like this:

This blog post talks about the process:

The example code is also reading discrete inputs every second and sending that data to Losant.

I can also elaborate more if you need me to. If you have any things specific you want to see, please let me know. I’d be happy to help. Like I said, I’ll create a tutorial like blog post about this as well.



You scenario is 90% of what we do with regards to acquisition on Losant.
We have a python agent, that polls COMAP equipment.

We decoupled the components all running on a single device.

python agents (possibly multiple) (on losant gateway) polls modbus slaves (TCP), and publishes to a redis queue.
a seperate agent listens on the redis queue and push data either directly to losant via mqtt or via a moquitto broker acting as a bridge (using QOS1 means we get replay if the link is down).

Originally had single agent doing everything (twisted async server) however dealing with all the disconnect/reconnect networking issues for different parts made the code too complex.
This model also allows us to inject additional specific data for channels (possibly not from modbus) about the device into the same mqtt stream, by simply publishing data from another source to the same redis channel.

So in summary
device <- python agent -> redis queue <- redis subscribe -> mqtt

This is all running on a pi.

We have nearly 20 engines running on a single site Around 20 different sites running at the moment with varying numbers of modbus devices

Example of one pod of engines.

I can’t directly share code, however I can outline specifics.

I am using modbus_tk package.
We parse a config file we download from the COMAP controllers which provides a map of registers (non contiguous) with names, scales, bit decoding (meaning of status bits etc)
The a config file is built listing the names of the registers, the ip address, unit address, cycle time, and what channel to publish on.

This means we can get a modbus device online in a few minutes.

Will follow up with more details.



1 Like

Thanks Tim, it is interesting project you have attempted. I would like to see a tutorial like post as you have indicated. That will really help as I am new to PLC programming.

Also I would like to see how this is all setup on Pi to test it out myself. Appreciate if you can share the code. I can send my email address.


Hi Taron

What kind of hardware are you using for the gateway?


Hi @paul_wareham,

For this, I believe their gateway was a custom on-premises box that they had. However, this would work on an industrial Dell or HPE gateway, or as something as small as a Raspberry Pi.

We’ve been working with our new Edge Compute Component:

It makes the task of this article way easier to solve. I highly recommend checking it out. For this, we have some supported gateways you can take a look at:

Let me know if you have any more questions :slight_smile: