Skip to main content

Using Modbus with the Brainboxes BB-400 Neuron Edge Controller

Reading a single-phase Modbus energy meter with the Pi-powered PLC

Reading a single-phase Modbus energy meter with the Pi-powered PLC and much more.

In this post, we take a look at how you can quickly interface Modbus devices, such as energy meters, temperature sensors and I/O modules, with the Brainboxes BB-400 (181-7467) .

Modbus 101

The Modbus protocol has been around for quite some time and in fact, its roots can be traced back to the 1970s. Thanks to its relatively simple and efficient nature, together with the fact that it is royalty-free, it has enjoyed significant success over the years and can be found used in many applications spanning industrial control, building automation and similar. With widespread support via PLCs, application-specific controllers, sensors, actuators and HMIs etc.

There are a number of different protocol versions for serial ports and IP networks, with perhaps the most common ones being Modbus RTU, a binary protocol that is used over serial communication links, and Modbus TCP, which is typically used over Ethernet connections.

The RTU variant remains popular for many applications and particularly those that are cost-sensitive, due to the fact that RS-485 links can be used with a bus architecture comprised of a single pair of wires, serving many devices. In such cases, where you might be reading a bank of energy meters every few minutes at most, a 100M Ethernet link for each would clearly be overkill.

Each Modbus station can address up to 247 devices per data link. Each device has a unique address and may have many registers. There are then standard Function Codes that are recognised by Modbus devices. For example, Function Code 4 is used to read an input register.

Hardware setup

Hardware setup - BB-400 serial port connected to an SDM120M

In our simple setup, we have the BB-400 serial port connected to an SDM120M (871-8302) energy meter, which by default is configured with a unit ID of 1 and 2,400 baud communications. Both of which can be changed by writing to the appropriate Modbus holding registers. Which we might need to do if we had multiple devices on the same bus or perhaps wanted to increase the baud rate.

Accessing the BB-400

Accessing the BB-400 through Raspbian

The BB-400 runs a Raspbian based operating system and is configured out-of-the-box to use mDNS, a.k.a. “Zeroconf”, which means that you don’t need to log on to your router to find out what IP address it has been allocated; instead you can simply use the hostname bb400-xxxx.local, where xxxx is the last four digits of the MAC address, which is printed on the side of the enclosure.

In our case to access the web interface we used:

https://bb400-0250.local:9090/

Note how port number 9090 needs to be appended.

Before the page loads, we have to acknowledge a security warning that results from a self-signed certificate being used. This shouldn’t be an issue since we’re not expecting random third parties to access the web interface and if we select to save this exception in our browser, we won’t be prompted again unless the certificate changes and if this happens, we may want to look into why.

The default username is “bb” and the password is the same four last digits of the MAC address.

Note that we can also log in via SSH, e.g. from the Linux prompt using:

$ ssh bb@bb400-0250.local

Port configuration

Web interfaces showing Port configuration

The web interface allows us to view the BB-400 status and to configure things such as the integrated digital I/O, networking and wireless, and to run software update. There is also a menu option for configuring the serial port, which can be seen above. Here we can set the port to be one of RS-232, RS422/RS-485 full-duplex, or RS-485 half-duplex.

Since our Modbus device is half-duplex and we are just using two data lines, this is what we configured the serial port for. We can also apply default port settings for the baud rate and number of data bits etc. here. Note how the web interface also conveniently shows us which pins we should be connecting to and the jumper settings for our configured port type.

PCB jumpers on the BB-400

The PCB jumpers can be seen above and for further details see the Brainboxes website.

Node-RED

Node-RED - prototyping applications on the BB-400

The fastest route to getting up and running prototyping applications on the BB-400 is to use Node-RED, which comes pre-configured and its web interface can be accessed by using the same URL and changing the port suffix to 1880. The same username and password are used to log in.

Functionality in Node-RED is expanded upon via third party “nodes”.

Node-RED - nodes pre-installed that add support for Modbus,

Handily there are nodes pre-installed that add support for Modbus, plus a pre-configured flow — a tab/page in the editor — which shows these in use.

Node-RED - simple flow for a Modbus Read type

We’ll start with a simpler flow and above you can see a Modbus Read type node being configured. For this, we need to specify the device unit ID, function code type, register address and a number of input registers that we want to read. Here we are configuring to read the voltage. We also set a poll rate that determines the frequency of readings.

Node-RED - this server is configured to use a serial connection

At the bottom of the form, we have a Server field and we can have multiple servers configured within and across flows. Each server can then be used by one or more nodes that perform read or write operations. Above we can see that this server is configured to use a serial connection, but TCP is also possible. We’ve specified the serial port, which in the case of the BB-400 is /dev/ttySC0. We also set the baud rate and a default unit ID to use if one is not specified by the Modbus node.

Modbus Read node has been connected to a debug node

Above we can see that the Modbus Read node has been connected to a debug node, with the output visible on the right. The values read are in 32 bit IEEE 754 floating-point format, held in two adjacent registers. Hence will need converting to something more useful.

Node-RED allows you to use resources that others have shared

One of the nice things about Node-RED is that it’s incredibly popular and a lot of the time you can avoid reinventing the wheel by using resources that others have shared. In fact, it’s trivial to share an entire flow and a quick search turned up this page. You can see the result of this being import into a new flow, pictured above.

Node-RED - simply reuse the formatting function

Rather than use the entire flow, it was decided instead to simply reuse the formatting function, which can be seen above, along with the much more useful output this provides.

This is just a simple example and the palette of Modbus nodes provided includes a similar one but for writing to registers, along with a variety of other nodes with more flexible features.

Other options

Although Node-RED may be seen more as a rapid prototyping tool, some do use this in production environments and we had a Raspberry Pi 2 here in the office that ran for a number of years controlling the heating, without any issues whatsoever. However, there is also a lot to be said for simple, dedicated programs that do one thing and do it well, with minimal lines of code.

Like Node-RED the Python programming language is also incredibly popular and with a gentle on-ramp that has made it highly popular with novices and hobbyists, alongside more advanced users. A quick search turned up the sdm-modbus Python module for use with this particular energy meter, which actually builds on top of the pymodbus module.

Meanwhile, those who prefer the C programming language may use libmodbus and there is no shortage of other options, given the maturity and popularity of Modbus.

Andrew Back

Open source (hardware and software!) advocate, Treasurer and Director of the Free and Open Source Silicon Foundation, organiser of Wuthering Bytes technology festival and founder of the Open Source Hardware User Group.
DesignSpark Electrical Logolinkedin