Java in industrial applications on the sysWORXX CTR-700 controller | Topics

Java Application Description
General Information
The object-oriented programming language Java has been in existence since 1995 and has always been one of the most popular and widely used languages. Java has continued to evolve over the years and is now used in programmes and applications across a wide range of sectors. These include, for example, banks and credit institutions, as well as insurance companies. Java is also frequently used in web development, for both front-end and back-end tasks. Furthermore, Java can, of course, be used for industrial applications, such as on the sysWORXX CTR-700.
Java is particularly characterised by its platform independence. Once created, Java programmes can run on virtually any hardware. It makes no difference whether this is a smartphone, tablet, Windows or Linux PC. This independence requires only two things:
- The required resources must be available, such as communication interfaces or (digital or analogue) inputs and outputs.
- A suitable Java Runtime Environment (JRE) must be present on the executing system. This is always the case for most operating systems
Another advantage of Java, which should not be underestimated, stems from its very widespread use: every aspiring software developer, computer scientist or even amateur programmer will come into contact with Java right from the early stages of their (professional) career. At many universities, for example, it is taught as early as the first few semesters. Across the vast expanse of the internet, Java is also frequently recommended asa ‘programming language for beginners’.
Integration on the sysWORXX CTR-700
Thanks to Java’s extensive platform independence, a compatible JRE can also be run on modern (automation) systems. The sysWORXX CTR-700 Edge Controller comes with the open-source Java implementation OpenJDK as standard. This allows Java programmes to be run easily and adapted for industrial use.
A corresponding class library is available for the sysWORXX CTR-700, which allows the various hardware interfaces and I/Os to be integrated into a Java application. This means that Java can be used not only for generic data processing, but also to access the device’s hardware peripherals directly.
Furthermore, a demo application illustrates how a Java application can be used in conjunction with the sysWORXX CTR-700.
MQTT demo including I/O connection
The following example programme is a Java application which is used as an MQTT client on the sysWORXX CTR-700. This client connects to an MQTT broker, subscribes to specified topics and switches the device’s digital outputs on or off in response to incoming messages.
The entire project can be downloaded at the end of this article and reproduced independently. The settings configured therein are the same as those discussed in the text that follows.
Prerequisites
For this example, a Maven project must be created in Eclipse (4.11) and the relevant dependencies imported. In addition to the necessary Maven dependencies (maven-dependency-plugin, maven-compiler-plugin, maven-jar-plugin), the MQTT client requires the ‘Eclipse Paho’ project and the ‘Java Native Access’ library to be included.
The driver class for the sysWORXX CTR-700 is also required. This can be obtained from the virtual machine for the sysWORXX CTR-700 or taken from this demo project. The following figure shows what the project structure should look like.
In addition to the sysWORXX CTR-700 and a connection to a computer, an MQTT broker and a second controllable client are also required. A mosquitto broker running on the sysWORXX CTR-700 can serve as the MQTT broker, for example, but any other broker that can be connected to may also be used.
The second MQTT client must be able to send messages to the specified topics to which the sysWORXX CTR-700 is subscribed. Another Java application, for example, can be used for this purpose. In this demonstration, Node-RED is used for this. The following figure shows the schematic workflow:
Programme Flow
The structure and functionality of the main programme can be found in the “Main.java” file . Before a connection can be established, all necessary variables are created and initialised.
It is necessary to create appropriate callback functions for the MQTT client to handle incoming messages and connection failures. Incoming messages are not only received but also displayed on the console. Connection failures are likewise displayed on the console.
When new messages arrive, the payload and the topic are each stored in an array so that they can be processed further outside the callback function. A flag is also set to signal a new message.
The programme’s main loop is then executed. The client establishes a connection to the specified IP address of an existing MQTT broker and, once the connection is successful, subscribes to a total of four topics. The IP address used in the example is 192.168.64.100 and the standard port for MQTT is 1883.
The following topics are used in the example:
- ctr700/do0
- ctr700/do1
- ctr700/do2
- ctr700/do3
Once a successful connection has been established, the programme enters an infinite loop. This loop only ends when the connection to the MQTT broker is lost, at which point it attempts to re-establish a connection, or when the programme is terminated (CTRL + C on the CTR-700). This loop only performs an evaluation when new MQTT messages arrive. Furthermore, in each cycle, it reads the digital inputs according to the digiOut array. This array holds information about the circuit states of the digital outputs
If the flag for a new incoming message has been set (see above), the message is evaluated as follows:
- The message is mapped to the subscribed topic to which it corresponds.
- The content of the message is then evaluated for the selected topic:
- ON: The corresponding digital output is set to high (LED turns on). → digiOut[i] = true;
- OFF: The corresponding digital output is set to low (LED switches off). → digiOut[i] = false;
- If neither of these applies, the message is ignored.
- End of evaluation → Exit the programme section.
The following table illustrates the programme’s responses to various topics and payloads.
Using the class library for the sysWORXX CTR-700
To ensure that the programme flow described above works and that the sysWORXX CTR-700 hardware can be used, a corresponding instance of the CTR-700 I/O driver must be created and initialised. This is done at the start of the programme, but can be carried out at any point before the interface is used. This is done as follows:
Ctr700Drv ctr700 = new Ctr700Drv();
ctr700.init();
Once initialised, the class for the sysWORXX CTR-700 can be used just like any other class. For example, the following method is used to set the digital outputs later on:
ctr700.setDigiOut(i, digiOut[i]);
Here too, the placeholder ‘i’ stands for the channels being used.
Documentation for every property and method in the class library can be found in the Java file “Ctr700Drv.java”.
Below is an excerpt from the demo. This shows the loop in which the programme remains continuously once all the required variables and classes have been initialised and a connection to the MQTT broker has been established.
Transferring and launching the application
In order for the programme to run on the CTR-700, it must first be compiled as an executable JAR file. In Eclipse, this can be done via the path “File → Export…” , as shown in the following figure.
Here, the option to convert to a “Runnable JAR file” must be selected . In the next step, a “Launch configuration” must be selected, a path specified, and the checkbox for “Library handling” must be ticked to select “Package required libraries into generated JAR”. The following figures illustrate the respective steps.
After conversion, the generated JAR file must be transferred to the device. The easiest way to do this is using an SFTP tool, such as WinSCP.
To launch the application, you must access the CTR-700’s Linux console. This can be done either via SSH or the serial service interface (µUSB). Once logged in, you must navigate to the directory containing the JAR file. Before it can be launched, it is essential that the programme is granted execution permissions. This can be done using the following command:
chmod +x demo.jar
The application can then be launched using the following command:
java -jar demo.jar
The following figure shows what this process might look like. A Linux distribution was used for this demonstration. However, the procedure is the same on Windows.
Control via the second MQTT client
Once the application has been run, the CTR-700 is ready to process data via MQTT. For example, control can be implemented very easily using Node-RED, which can also be used in parallel on the sysWORXX CTR-700.
To do this, injector nodes can be connected to an MQTT node. The injectors then send a specific message, including topics, via the MQTT node to the broker, which forwards it to all subscribers. The following figures show how the injector messages are configured and how the Node-RED flow can be set up:
There is one injector node for switching each digital output on and one for switching it off.
Final notes
The example discussed here illustrates how easy it is to use the sysWORXX CTR-700 with Java. Any Java developer can use this as a basis to develop applications that can interact directly with the device’s interfaces. Instead of this relatively simple application, sensors and actuators could, for example, be connected to the digital inputs and outputs and controlled.
To help you get started and for general use of the sysWORXX CTR-700, we offer a Linux VM for download on our product website. This includes a pre-configured Xubuntu installation. It also includes the class library required for software development in Java. Further information, such as the device manual, SD card image (operating system, etc.), and additional documentation on the sysWORXX CTR-700, can also be found there.