Integrating YAKINDU Statechart Tools with MQTT

As we mentioned earlier, we used YAKINDU Statechart Tools for the safety logic, that controls the model railways. In this post we are going to show, how we use MQTT in YAKINDU Statechart Tools, how we combine model based tools with IoT technologies.

We implemented data classes that are converted into the JSON format automatically by Gson. There is a general Command class which identifies what type of JSON message we are sending, and it determines the Payload of the message transfers.

E.g. if we transfer a direction status of a turnout, then the corresponding JSON looks like: {“command”:“SEND_TURNOUT_STATUS”, “content”:“{“id”:12, “status”: “STRAIGHT”}“} . It means that the turnout that has the ID 12 is set into the STRAIGHT direction.

Besides, we have a general PayloadHelper class whose sendCommandWithContent method can create a correct JSON message from a command and a jsonConvertible object. Note that jsonConvertible objects are those that are depicted on the figure above. First, it creates the corresponding JSON message from the command and the jsonConvertible, and finally it publishes the message to the broker through an MQTT Publisher object.

There is another method, called getPayloadFromMessage that extracts the payload from an MQTT Message and converts it to a Payload object whose content’s type always depends on the corresponding command’s value. In this way we implemented a general JSON to Java object conversion solution with the help of Gson.

Finally, let’s see how the Yakindu  model uses these functions. On the following figure a safety-logic specific command, called PASSAGE_REQUEST is sent to the corresponding direction: PASSAGE_REQUEST_TOP, PASSAGE_REQUEST_STRAIGHT, PASSAGE_REQUEST_DIVERGENT. It means the turnout asks the next turnout whether the train can enter its section. The content of the JSON message is generated and sent at the end of the method.

This is where statechart modelling in Yakindu meets IoT!

You can find all the sources codes, along with other modules (MQTT clients, YAKINDU Statechart Tools models, OpenCV codes, Complex Event Processing codes and models, etc) in our GitHub repository at https://github.com/FTSRG/BME-MODES3.

The integration of the Lego robot with the controller: using MQTT from Python

In the Lego robot subproject of the MoDeS3 it was important to integrate the sensor information from the Lego sensors, the control and also the logic responsible for the safety. For this purpose, we have implemented an advanced control protocol in Python. This script runs on an embedded Linux distribution, called EV3dev. With this operating system we can utilize the Lego devices connected to the EV3 brick, while have access to all generic Linux packages, like the mosquitto broker.

The simplified overview of depdendecies is depicted on the next picture:

Overview

Our logic is able to detect when the motors are overdriven, or the robot is getting to a twisted and dangerous position, and prevents it from further attacking its limits by stopping them. By this protocol other components can control the crane by MQTT messages, or stop it if any other sensor detects something dangerous.

The code snippet below runs in a cycle and if it notices chage in the state of the touch-sensor, sends a message through MQTT (Paho). If needed it also executes some safety routines.

using MQTT

Sensors of the hardware are depicted on the following figures: these sensors provide the information:

SensorSensor2

The solution is built modularly, each part is responsible for certain movements and sensor information. The control software enables the user to control any of the motors individually, and get back raw sensor data through MQTT. A safety modul is observes the behaviours and available information and intervenes if something goes wrong.

Getting started with MQTT (Mosquitto and Paho)

As part of the Eclipse IoT Challange 2016, we shall use as many open source implementations of IoT standards, and Eclipse based technologies as we can. For communication we chose MQTT and its open source broker (Mosquitto) and client (Paho) implementation.

MQTT

Here is a short description about MQTT from its homepage:

MQTT stands for MQ Telemetry Transport. It is a publish/subscribe, extremely simple and lightweight messaging protocol, designed for constrained devices and low-bandwidth, high-latency or unreliable networks. The design principles are to minimise network bandwidth and device resource requirements whilst also attempting to ensure reliability and some degree of assurance of delivery. These principles also turn out to make the protocol ideal of the emerging “machine-to-machine” (M2M) or “Internet of Things” world of connected devices, and for mobile applications where bandwidth and battery power are at a premium.

As you can find out, MQTT architecture consists of brokers and clients. Brokers interconnect clients through different topics. All clients receive the messages published to the topic they are subscribed for. A message can be anything that is convertible to a byte array. This way an N:N connection cardinality can be easily achieved as depicted on the next figure.

Different open source and proprietary implementations of MQTT brokers and clients exist in most programming languages (C, C++, Java, .NET, Python, JavaScript, etc) . We use Mosquitto as a broker, and Paho as a client implementation.

Mosquitto

The Mosquitto broker is the focus of the project and aims to be a lightweight and function MQTT broker that can run on relatively constrained systems, but still be powerful enough for a wide range of applications.

To get started with Mosquitto visit its website at http://www.eclipse.org/mosquitto/. Download and install it on your computer. We use the binary compiled for Ubuntu as it works seamless.

The default address of the running Mosquitto service is tcp://localhost:1883, on localhost over TCP protocol at port 1883.

However, if you would not like to install Mosquitto yourself, there are two Mosquitto brokers publicly available online:

  1. One that is operated by Mosquitto website itself at http://test.mosquitto.org/
  2. One that is operated by Eclipse at tcp://iot.eclipse.org:1883

Paho

The Paho project provides open-source client implementations of MQTT and MQTT-SN messaging protocols aimed at new, existing, and emerging applications for Machine‑to‑Machine (M2M) and Internet of Things (IoT). 

To get started with Paho visit its website at http://www.eclipse.org/paho/ and look for the client sample codes. We use Java Client through Maven.

 Demo time

Now we will provide some sample code snippets in Java which demostrate a “Hello World” message exchange between two clients. We use Maven for dependency management so the pom.xml is provided as well.

The code snippets are available at https://www.eclipse.org/paho/clients/java/.

Publisher (sender) client sample:

Subscriber (receiver) client sample:

Last but not least, the pom.xml that is necessary for Maven:

Conclusion

So far we have been satisfied with Paho and Mosquitto since they provide an open source implementation of the lightweight messaging protocol MQTT.

Happy coding!