The world of PLCs is new for me. As someone who's been working in the embedded world for the last decade, I've been familiar with this part of the industry, but never really dug into the market, mostly because my concept of PLCs was that they were a) expensive and b) the thing that modern-day "IoT" product companies were trying to replace as the machinery and solutions connected to these PLCs are brought online.
This changed a little over a year ago when I got my hands on the Arduino Opta, a microPLC that Arduino expects will open up a new class of industrial applications for Automation Engineers and even wanna-be engineers like me. As you'd expect from Arduino, it's a device that's quite a bit cheaper than mainstream PLCs on the market, while delivering all of the quality, robustness and security that the Arduino Pro line of products has been consistently delivering for the last several years.
The Opta is a tidy little package that has an STM32 H7 MCU and secure element inside, a handful of IO and 4 ten-amp relays, and is ready to snap onto DIN rail. It comes in three flavors with different connectivity options:
- The Lite, which includes Ethernet;
- The RS485, which, as the name indicates, includes an RS485 interface;
- And the WiFi, which includes Bluetooth and Wi-Fi connectivity.
That's a great set of connectivity options, for sure, but for a number of automation applications, using facility Ethernet or WiFi isn't an option, so last fall, Arduino and Blues started working on an expansion to the Opta that includes the Notecard and brings Cellular and LoRa connectivity to the Opta ecosystem.
That product, the Blues Wireless for Arduino Opta expansion, is now available from the Blues store, and will be available in Arduino's distribution channels this fall. Like the Opta itself, it's in a DIN compatible case, and it plugs into the Opta via a 10-pin solderless AUX connector that Arduino has defined for their expansion ecosystem. That connector enables an I2C connection between the Opta's built-in MCU and the Notecard, meaning that talking to the Opta expansion is no different than any other MCU-to-Notecard application.
On the firmware side, developers have two choices for programming the Opta's connection to the Blues expansion:
- You can use our
note-arduino
library and route data from Notehub to your cloud application of choice, like Datacake. - You can use the
ArduinoIoTCloud
SDK which now includes built-in Notecard support and integrates directly with the Arduino IoT Cloud.
These are both great options. If you want full control and the flexibility to route data anywhere, we have you covered with the note-Arduino
library and Notehub. On the other hand, if you want a drop-in cellular addition to your existing Arduino IoT Cloud solutions, we support that too and it's as easy as a few firmware changes to make it happen.
As a part of developing our expansion module over the last year, I've been working with the Opta on an almost daily basis, starting with the first hacked-together POC pictured below.
Continuing with a franken-Opta solution I cobbled together to overcome a slight mishap in the layout of our AUX PCB inside the expansion. Hardware is hard, y'all.
And finally, with my own "real-world" on DIN rail Opta-based PLC application that I shamelessly lifted from one of the Arduino Opta examples and modified to fit my needs. Here it is, in all of it's portable because-I-will-be-hauling-it-with-me-to-Amsterdam-for-The-Things-Conference glory.
In this post, I'll show how I wired all this up, got things connected to the Arduino IoT cloud and built a simple dashboard for some remote monitoring and control.
Assembling the HardwareThe solution pictured here is a rethinking of the Power Quality Monitoring app we built at Blues last year, trading out the Dr. Wattson for a Finder 7M.24 Energy monitor, which has an RS485 interface.
The wiring for this project was almost as fun as writing the firmware. Again, because this is meant to be a portable demo, I started by splicing a grounded extension cord, stripping the wires and feeding them through a 10 amp breaker (plus heat-shrink tubing, safety first!) and into a 24V DIN-compatible power supply.
I connected the Neutral and Load wires to a Finder 7M.24 energy monitor so that I can measure energy usage on the system. Those wires were also connected to a DIN-compatible grounded plug box as well as one of the Output relays on the Opta, which allows me to use the Opta to control power to the plug box and enable or disable it remotely. More on that in a moment.
The 24 V power supply is then fed into the Opta and the Blues expansion, and as I mentioned above, the Opta and expansion are connected via a solderless 10-pin AUX connector that ships with the module.
Finally, I used jumper wires to make an RS485 connection between the A and B terminals on the Finder device and my Opta's A(-) and B(+) terminals.
Once everything was connected, I crossed my fingers, and plugged it in. And as there were no sparks or magic smoke released, I was ready to move onto the firmware.
The basis for my solution, for which the complete firmware sample is posted below, was the Energy Management for Opta Application Note in the Arduino docs. That was a great help in getting me set up using the Finder 7M.24 and reading Modbus messages from the meter. I won't cover that here so if you're interested, check out that guide. What I will cover here is how I used that example to get the Notecard-powered expansion module connected to the Arduino Cloud.
First things first, I installed two libraries, ArduinoIoTCloud
and Arduino_ConnectionHandler
, both of which have been extended to add Native Notecard support. If you've worked with the Notecard before, this means that, rather than manually sending JSON commands to the Notecard to configure it or add sensor data, the Notecard has been integrated into these libraies using the ArduinoCloud SDKs built-in transport mechanisms so you can use the SDK as you would for any other radio access technology and the library handles communication with the Notecard. One of my colleagues, TJ VanToll, has a great write-up about how this works and the steps to take to get the Notecard and Notehub set-up to use the Arduino Cloud. I'm not going to repeat that here, so be sure to check that out as you follow along as you'll need to create a Notehub project and set a few environment variables to complete the handshake between our cloud service and Arduino's.
Important Note: At the time of publication, the Notecard tweaks to the Arduino libraries are not quite released, so you'll need to follow TJs instructions to update those on your local filesystem, but once done, you're ready to use the Notecard.
Once those libraries are installed, the magic happens in a thingProperties.h
file, where I set up the NotecardConnectionHandler
as the underlying transport and configure the properties that I want to expose to the Arduino Cloud.
#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>
#include "arduino_secrets.h"
#define NOTECARD_PRODUCT_UID "your_product_uid"
void onRelayChange();
bool relay_closed;
float actual_voltage;
float actual_amps;
float actual_watts;
float actual_Va;
NotecardConnectionHandler ArduinoIoTPreferredConnection(NOTECARD_PRODUCT_UID);
void initProperties(){
ArduinoCloud.addProperty(relay_closed, Permission::ReadWrite).onUpdate(onRelayChange);
ArduinoCloud.addProperty(actual_voltage, Permission::Read).publishEvery(60);
ArduinoCloud.addProperty(actual_amps, Permission::Read).publishEvery(60);
ArduinoCloud.addProperty(actual_watts, Permission::Read).publishEvery(60);
ArduinoCloud.addProperty(actual_Va, Permission::Read).publishEvery(60);
if (::strncmp(SECRET_WIFI_SSID, "MY_WI_FI_SSID", sizeof(SECRET_WIFI_SSID))) {
ArduinoIoTPreferredConnection.setWiFiCredentials(SECRET_WIFI_SSID, SECRET_WIFI_PASS);
}
}
The Finder module captures a TON of great data and you can see everything in the full firmware application, but I opted just to send four fields to the Arduino Cloud with the current voltage, amps, watts and volt-amps (referred to as Apparent Power). These are all read only values that the library will publish to the Arduino Cloud every 60 seconds and which I'll display in a live dashboard.
In my main application, each time I poll the Finder module, I call a function to update these properties.
void updateCloudVariables() {
actual_voltage = V_actual;
actual_amps = A_actual;
actual_watts = W_actual;
actual_Va = Va_actual;
}
In addition, because I have one of the Opta's relays wired to my plug box, I added a read/write property that allows me to toggle the relay from the cloud. In order to enable that, I need to specify a function that the library will call when the property is changed in the cloud. The onRelayChange
function simply opens or closes the relay and turns on or off its corresponding LED based on the state of the cloud property.
void onRelayChange() {
if (relay_closed) {
digitalWrite(D3, HIGH);
digitalWrite(LED_D3, HIGH);
} else {
digitalWrite(D3, LOW);
digitalWrite(LED_D3, LOW);
}
}
Once I've configured my properties and callbacks, I added a IoT Cloud setup function that I call from my main application setup()
:
void iot_cloud_setup(){
// Defined in thingProperties.h
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
ArduinoCloud.setNotecardPollingInterval(3000); // default: 1000ms, min: 250ms
setDebugMessageLevel(DBG_VERBOSE);
ArduinoCloud.printDebugInfo();
}
This function calls the initProperties
method, configures the library to use my Expansion module's Notecard and finally sets up logging so I can view output during development.
The last thing I need to do is add a single line to my loop()
that instructs the ArduinoCloud library to update state and execute its own tasks.
ArduinoCloud.update();
And that's it! As I said above, there's a LOT more to the solution as it relates to reading from the Finder monitor and parsing modbus data, and you can check that out in the linked source code.
With my hardware setup, cloud configuration and firmware flashed, I should see good news in the Console letting me know that the Notecard and Notehub are talking to the Arduino Cloud.
13:40:11.862 -> Start
13:40:11.862 -> Energy Management - Modbus RTU Client
13:40:14.728 -> Notecard has been initialized.
13:40:14.728 -> Starting network connection...
13:40:14.728 -> ***** Arduino IoT Cloud Notecard - configuration info *****
13:40:14.728 -> Notecard UID: dev:860322068056388
13:40:14.728 -> Arduino Device ID: f9c7dadd-3c43-4709-975b-c38b19734a0f
13:40:14.728 -> Arduino Thing ID: awaiting connection...
13:40:22.370 -> Connected to Notehub!
13:40:22.370 -> NotecardConnectionHandler::initiateNotehubSync initiating Notehub sync...
13:40:22.533 -> NotecardConnectionHandler::initiateNotehubSync successfully initiated Notehub sync.
13:40:22.632 -> TimeServiceClass::sync Drift: -1725648008 RTC value: 1725648019
13:40:22.829 -> ArduinoIoTCloudNotecard::sendCommandMsgToCloud encoded 12 bytes for Command Message
13:40:23.126 -> Message sent correctly!
And with that, I'm ready to set up my dashboard.
Building a DashboardIn the Arduino Cloud, my Opta+Blues Expansion combo are associated with a Device, while the properties I configured in firmware map to Cloud Variables on a Thing, as depicted below. Once I've made the connection, I should see the values of my variables update as they change on the device.
From here, creating a dashboard using the Arduino Cloud is simple. I can add graphs and gauges to correspond to readings from the module.
And a switch to toggle the relay open or closed.
The final dashboard looks like this, a real time view of the readings coming from my solution, as well as the ability to remotely toggle the plug box and its connected light bulb on and off!
It's been an absolute blast working with the Arduino team on this product, from the hardware to the Arduino Cloud firmware and service integration over the last several months. From the start, Blues has always been about democratizing wireless connectivity for every developer and we're excited to see this product bring a new class of PLC applications online!
And if you're ready to get started with one of your own, head over to the Blues shop to grab a bundle and start automating!
Comments