The object-oriented programming language Java has existed since 1995 and has always been one of the most popular and widely used. Java has evolved over the years and is now used in programs and applications in a wide variety of industries. These include, for example, banks and credit institutions, but also insurance companies. Java is also frequently used in the area of web development, both for front-end and back-end. Java can of course also be used for industrial applications such as the sysWORXX CTR-700.
Java is particularly characterized by its platform independence. Once created, Java programs can be executed on almost any hardware. It does not matter whether it is a smartphone, tablet, Windows or Linux PC. This independence requires only two things:
1. Resources used must be available, such as communication interfaces or (digital or analog) inputs and outputs.
2. A Java Runtime Environment (JRE) must be available on the executing system. This is always the case for most operating systems
A further and not to be underestimated advantage of Java results from the very wide distribution: every prospective software developer, computer scientist or even hobby programmer will come into contact with Java already in the initial phase of his (professional) career. At many universities, for example, it is already covered in the first semesters. In the vastness of the Internet, Java is often recommended as a "programming language for beginners".
Due to the platform independence of Java to the greatest possible extent, the execution of a corresponding JRE is also possible on modern (automation) systems. With the Edge Controller sysWORXX CTR-700 the free Java implementation OpenJDK is offered as standard. With this implementation, Java programs can be executed and used for industrial purposes without any problems.
For the sysWORXX CTR-700 a corresponding class library is available with which the different hardware interfaces and I/Os can be integrated into a Java application. Java can not only be used for generic data processing, but also for direct access to the hardware periphery of the device.
Furthermore, a demo application illustrates how a Java application can be used in combination with the sysWORXX CTR-700.
The following example program is a Java application which is used as MQTT client on the sysWORXX CTR-700. This client connects to an MQTT broker, subscribes to predefined topics and switches the digital outputs of the device on or off according to the incoming messages.
The entire project can be downloaded at the end of this article and independently traced. The settings made in it are the same as those described in the following text.
For this example, a Maven project must be created in Eclipse (4.11) and the corresponding dependencies imported. In addition to the necessary Maven dependencies (maven-dependency-plugin, maven-compiler-plugin, maven-jar-plugin), the MQTT client requires the integration of the "Eclipse Paho" project and the "Java Native Access" library.
Additionally the driver class for the sysWORXX CTR-700 is required. This can be obtained from the virtual machine for the sysWORXX CTR-700 or from this demo project. The following figure shows how the project tree 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 required. As MQTT-Broker for example a mosquitto-Broker on the sysWORXX CTR-700 can be used, but also any other broker to which a connection is possible can be used.
The second MQTT client must be able to send messages to the given topics that the sysWORXX CTR-700 subscribes to. For this purpose, another Java application can be used, for example. In this demonstration Node-RED is used. The following figure shows the schematic process:
The structure and functionality of the main program can be found in the "Main.java" file. Before a connection can be established, all required variables are created and initializations are carried out.
It is necessary to create callback functions for the MQTT client for incoming messages and disconnections. Incoming messages are not only accepted, but also displayed on the console. Connection aborts are also 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.
Then the main loop of the program is executed. The client creates a connection with the specified IP address of an existing MQTT broker and subscribes to a total of four topics after a successful connection. The IP address used in the example is 192.168.64.100 and the default port for MQTT 1883.
The following topics are used in the example:
After a successful connection has been established, the program is in a continuous loop. This is only terminated when the connection to the MQTT broker is interrupted, after which it tries to establish a connection again, or the program is terminated (STRG + C on the CTR-700). This loop only executes an evaluation when new MQTT messages arrive. Furthermore, it describes the digital inputs corresponding to the digiOut array in each cycle. This array holds the information about the switching states of the digital outputs.
If the flag for a new incoming message has been set (see above), the following evaluation of the message is performed:
- An assignment is made to which subscribed topic the message matches.
- The content of the message is then evaluated for the selected topic:
- ON: The corresponding digital output is set to high (LED goes on). → digiOut[i] = true;
- OFF: The corresponding digital output is set to low (LED goes off). → digiOut[i] = false;
- If none of this applies, the message is ignored.
- Conclusion of the evaluation → Leaving the program section.
The following table visualizes the reactions of the program to various topics and payloads:
Digital output i is set to high.
Digital output i is set to low
The placeholder i stands for the four used channels DO0, DO1, DO2, DO3 (see list of topics above).
For the program flow described above to work and for the hardware of the sysWORXX CTR-700 to be used, a corresponding instance of the CTR-700 I/O driver must be created and initialized. This is done at the beginning of the program, but can be anywhere before using the interface. This is done as follows:
Ctr700Drv ctr700 = new Ctr700Drv();
After initialization, the class for the sysWORXX CTR-700 can be used like any other class. For example, the following method is used for the later output of the digital outputs:
The placeholder i is also used here for the channels used.
A documentation of each property and method of 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 program is permanently located after all required variables and classes have been initialized and a connection to the MQTT-Broker has been established.
In order for the program 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.
There the conversion to "Runnable JAR file" must be selected. In the next step a "Launch configuration" has to be selected, a path has to be defined and the tick for "Library handling" has to be selected at "Package required libraries into generated JAR". The following illustrations show the respective steps.
After the conversion, the generated Jar file must be transferred to the device. The easiest way to do this is with an SFTP tool such as WinSCP.
The Linux console of the CTR-700 must be accessed so that the application can now be started. This can be done either via SSH or the serial service interface (µUSB). After logging in, it is necessary to change to the directory in which the Jar file is located. Before it can be started, it is absolutely necessary for the program to have rights for execution. This can be done with the following command:
chmod +x demo.jar
The application can then be started with the following command:
java -jar demo.jar
The following figure shows what this process might look like. A Linux distribution was used for the access. However, the access under Windows is done in the same way.
Once the application has been executed, the CTR-700 is ready to process data via MQTT. For example, it is very easy to implement a controller via Node-RED, which can also be used in parallel on the sysWORXX CTR-700.
Injector nodes can be connected to an MQTT node. The injectors then send a certain message including topics via the MQTT node to the broker, who forwards it to all subscribers. The following illustrations show how the injector messages are configured and how the node RED flow can be set up:
The example shows how easy the sysWORXX CTR-700 can be used with Java. Every Java developer can develop applications on this basis, which can interact directly with the interfaces of the device. Instead of this relatively simple application, sensors and actuators could be connected and controlled to the digital inputs and outputs.
For the introduction and general use of the sysWORXX CTR-700 we offer a Linux VM for download on our product website. This includes a preconfigured Xubuntu installation. It also contains the class library, which is required for software development with Java. There you will also find further information material, such as the device manual, SD card image (operating system, etc.), but also further documentation on the sysWORXX CTR-700.