/Coding Diary Day 02 | Build Eclipse paho MQTT client libs for PLCnext Control | PLCnext Store

Coding Diary Day 02 | Build Eclipse paho MQTT client libs for PLCnext Control | PLCnext Store

Video: Coding Diary Day 02 | Build Eclipse paho MQTT client libs for PLCnext Control | PLCnext Store


Hello and welcome back. This is the second video in a series… …where we're building a complete PLCnext application using C++. You remember from last time that we've decided to build an ACF component… …and we're going to use some open source libraries to help us build that component… …and then expose those as an RSC service. So the first step is to build the open source libraries. So let's get straight into the code. We've decided to use the paho MQTT open source library as the basis of our MQTT client on PLCnext.

So first we'll download that open source library… …and we'll build it for PLCnext just to prove that it's working. So first we'll just set up our host machine. The host in this case is a Linux Debian machine… …and the target is an Axioline AXC F 2152 PLCnext Control. Just a few setup details on the host: We need to install this PLCnext CLI. You can see that's installed here. We also need to have installed a Software Development Kit for the target. You can see we have three Software Development Kits installed. And we can see which targets have been installed by the CLI. There you can see the three targets corresponding to the three SDKs. And in this case we're using Visual Studio Code as the IDE. You can see that's already installed on the machine here. And the target is attached to this machine. So first we'll make a new directory for our solution. And we'll open Visual Studio Code.

In our empty project we'll just create a directory called "tools". We're going to be pulling the source code for the MQTT client open source libraries from GitHub… …using a script, so I'll just paste that script in here now. We could just run this build script from a command line and pass in the parameters that we need. But what we'll do in this case is use a feature of Visual Studio Code: We'll set up a task that will call this script and pass in the relevant parameters. To do that, we create a new directory ".vscode"… …and create a new file called "tasks.json". So in the tasks.json file we create a new task, give it a label,… …create three environment variables, one pointing to the SDK root,… …we name the ARP device and the ARP device version… …

READ  The BEST Small Phone You Can Buy!

and then we issue the command to run the build script with the variables. So we save that file, then we can go ahead and execute that task. That pulls the source code down from GitHub and then builds the code. So the build's finished and you can see it's created some new directories here. The "external" directory is where the source code for the paho MQTT C library was downloaded. The "build" directory is where the build process actually took place. And then the "deploy" directory includes the outputs of the build. And these are shared object libraries for the paho MQTT C library… …that have been built specifically for the platform that we're targeting. And you can see: using this method we can actually build for any platform depending on the SDK and the device we're targeting. We just change these parameters create a different task for each platform or each target… .

..and we can build a complete set of binaries for that target. So that's the paho MQTT C library. We need to do the same with the paho C++ library which uses the C library… …and is the one that we're going to use for our component. So I have another build script, this time for the C++ library. It looks very similar to the one for the C library. Again we pull down the source code from GitHub,… …check out specific version,… …and then we call CMake to build the binaries. And again we have a set of paho specific CMake flags that we can set here. So we can add a new task to build this. And here's our new task. So we'll go ahead and build the MQTT C++ library. Again it's pulling down the source code from GitHub and building locally. Okay so the builder's finished and we can see now that the… .

READ  Editing Tips to Making a Memorable Family Video with PowerDirector Mobile App

.."external" directory now contains the MQTT C++ source code… …and our "deploy" directory in the "lib" folder… …contains the MQTT C++ Shared Object library. Now we want to check that these libraries are actually working on our PLC. How can we do that? Well, the paho library comes with some sample applications. One of these samples is called "sync_publish" and if you look at the source code… …we can see that it connects to a server – in this case "localhost" – with a client ID… …and publishes some messages to a topic. So in our case we want to use a web-based broker. I know there's a broker at "test.mosquitto.org". We want to give a unique client ID on this public broker… …

and we want to publish to a unique topic. So we'll save that file. But before we build again we need to just update our build script… …so that we tell paho to build the samples as part of the build. Save that script and we'll execute it again. So that build's finished. And you can see that in the build directory we have our samples built as executables. Now let's download everything to the PLC. We'll download directly from our terminal window in Visual Studio Code. First we'll download all the paho MQTT Shared Object libraries. We'll download these to the "/usr/local/lib" directory on the PLC. We're going to copy the "sync_publish" binary to the PLC… …just into a directory that we've created in the "projects" directory just called "paho". Now we'll switch to a terminal.

READ  How to Configure Nox App Player Without Crash Free Apps

We'll log on to the PLC… …and let's take a look at the "/usr/local/lib" directory where we copied the Shared Object libraries. There's all our paho Shared Object libraries. We need to make them executable. Some of these are symbolic links, and the links were broken when we copied them. So we'll restore those symbolic links now. I've just created a script to make that happen. So I'll just execute that script. And now we can see that those symbolic links have been restored… …and our Shared Object libraries are ready to go. So now let's take a look at the executable that we downloaded. There it is the "sync_publish" program. So let's try running that.

Okay, so that's done something. But what has it done? Well, what we can do to check that is to connect to the same MQTT broker from another terminal… …and just see what messages are being published. I'm using a command line tool from Mosquitto… …to subscribe to the broker that our PLC is publishing to. And we need to subscribe to the topic on that broker. Now we're listening. We don't see anything coming through, we're just waiting for messages to be published. So we'll run our program again on the PLC. And then we can see we received three messages on our subscriber. And they're the three messages that were published by the paho client. So we can see that that all seems will be working: The paho client is working fine on the PLC. The next step is to wrap that paho client in our own component for PLCnext..