This site contains the documentation that is relevant to older WSO2 product versions and offerings.
For the latest WSO2 documentation, visit https://wso2.com/documentation/.

MQTT IoT

This sample demonstrates how WSO2 MB can use the MQTT transport to publish data from running vehicles to a central server and then use that data for analysis.

About the sample

It creates mock vehicles and simulates the functionality of publishing data (such as the current speed, current acceleration and the engine temperature) from the vehicle to a topic hierarchy in MB every second.

Data is published to the topic hierarchy in the following format:

VehicleType/VehicleModel/VehicleId/Sensor

For example, a vehicle of type abc, model xyz and vehicle ID 123 would publish data as shown below to the relevant hierarchies.

  • abc/xyz/123/engintemperature

  • abc/xyz/123/speed

  • abc/xyz/123/acceleration

To publish messages to a hierarchy, use "/" as the hierarchy separator as shown above. 3 MQTT clients will retrieve the real time data published from each vehicle through the broker as follows:

  • Subscribe for +/+/+/enginetemperature to retrieve engine temperature values of all the vehicles. Then calculate the average value each second.

  • Subscribe for car/# to retrieve all sensor data published by cars. Then find and output the car which has the maximum acceleration value for the given seconds.

  • Subscribe for bike/bikeModel/# to retrieve all sensor data for the given bike model. Then output the latest speed readings of the bike.

For subscribing to each use case hierarchy, two wild cards have been used in this sample.

Wild CardDescription
+

This suggest to subscribe to a single level

    • eg :- xyz/+/abc will subscribe to both xyz/1/abc and xyz/2/abc but not xyz/3/def

#

This suggest to subscribe to all subtrees

    • eg :- xyz/# will subscribe to all xyz/1/abc, xyz/2/abc/ and xyz/3/def

The <MB_HOME>/Samples/MqttIoT/src/main/java/org/wso2/sample/mqtt directory has the following classes:

The model directory in the mqtt directory has the following classes:

Prerequisites

Before you build the sample, the prerequisites for MB samples should be in place.

Building the sample

If you are building an MQTT sample for the first time, you need to build the sample using Maven. This will download the Maven dependencies needed for your MQTT samples. Once the dependencies are downloaded, you can build any of the MQTT samples using either Maven or Ant.

Using Maven:
  1. Navigate to the MqttIot sample folder in the <MB_HOME>/samples directory.
  2. Execute the mvn clean install command to build and run the sample.
  3. If the build is successful, the output will be printed in the terminal as shown below.
Using Ant:

Be sure that the Maven dependencies required for MQTT samples are already downloaded as explained here.

  1. Navigate to the MqttIot sample folder in the <MB_HOME>/samples directory.

  2. Execute the ant command to build and run the sample.

  3. If the build is successful, the output will be printed in the terminal as shown below.

Analyzing the output

Following is the output is printed in the terminal if the sample is successfully executed:

     [java] INFO  [org.wso2.sample.mqtt.model.Vehicle] - Speed of Vehicle12 updated to 74.0
     [java] INFO  [org.wso2.sample.mqtt.model.Vehicle] - Engine temperature of Vehicle12 updated to 254.0
     [java] INFO  [org.wso2.sample.mqtt.model.Vehicle] - Sensor readings of Vehicle0 sent to server.
     [java] INFO  [org.wso2.sample.mqtt.model.Vehicle] - Sensor readings of Vehicle1 sent to server.
     [java] INFO  [org.wso2.sample.mqtt.model.Vehicle] - Sensor readings of Vehicle2 sent to server.
     [java] INFO  [org.wso2.sample.mqtt.model.Vehicle] - Sensor readings of Vehicle3 sent to server.
     [java] INFO  [org.wso2.sample.mqtt.model.Vehicle] - Sensor readings of Vehicle4 sent to server.
     [java] INFO  [org.wso2.sample.mqtt.model.Vehicle] - Sensor readings of Vehicle5 sent to server.
     [java] INFO  [org.wso2.sample.mqtt.model.Vehicle] - Sensor readings of Vehicle6 sent to server.
     [java] INFO  [org.wso2.sample.mqtt.model.Vehicle] - Sensor readings of Vehicle7 sent to server.
     [java] INFO  [org.wso2.sample.mqtt.model.Vehicle] - Sensor readings of Vehicle8 sent to server.
     [java] INFO  [org.wso2.sample.mqtt.model.Vehicle] - Sensor readings of Vehicle9 sent to server.
     [java] INFO  [org.wso2.sample.mqtt.model.Vehicle] - Sensor readings of Vehicle10 sent to server.
     [java] INFO  [org.wso2.sample.mqtt.model.Vehicle] - Sensor readings of Vehicle11 sent to server.
     [java] INFO  [org.wso2.sample.mqtt.model.Vehicle] - Acceleration of Vehicle0 updated to 34.0
     [java] INFO  [org.wso2.sample.mqtt.model.Vehicle] - Speed of Vehicle0 updated to 0.0
     [java] INFO  [org.wso2.sample.mqtt.model.Vehicle] - Engine temperature of Vehicle0 updated to 181.0
     [java] INFO  [org.wso2.sample.mqtt.model.Vehicle] - Acceleration of Vehicle1 updated to 229.0
     [java] INFO  [org.wso2.sample.mqtt.model.Vehicle] - Speed of Vehicle1 updated to 78.0
     [java] INFO  [org.wso2.sample.mqtt.model.Vehicle] - Engine temperature of Vehicle1 updated to 176.0
     [java] INFO  [org.wso2.sample.mqtt.model.Vehicle] - Acceleration of Vehicle2 updated to 95.0
     [java] INFO  [org.wso2.sample.mqtt.model.Vehicle] - Speed of Vehicle2 updated to 37.0
     [java] INFO  [org.wso2.sample.mqtt.model.Vehicle] - Engine temperature of Vehicle2 updated to 56.0
     [java] INFO  [org.wso2.sample.mqtt.model.Vehicle] - Acceleration of Vehicle3 updated to 59.0
     [java] INFO  [org.wso2.sample.mqtt.model.Vehicle] - Speed of Vehicle3 updated to 44.0
     [java] INFO  [org.wso2.sample.mqtt.model.Vehicle] - Engine temperature of Vehicle3 updated to 168.0
     [java] INFO  [org.wso2.sample.mqtt.model.Vehicle] - Acceleration of Vehicle4 updated to 97.0
     [java] INFO  [org.wso2.sample.mqtt.model.Vehicle] - Speed of Vehicle4 updated to 182.0
     [java] INFO  [org.wso2.sample.mqtt.model.Vehicle] - Engine temperature of Vehicle4 updated to 225.0
     [java] INFO  [org.wso2.sample.mqtt.model.Vehicle] - Acceleration of Vehicle5 updated to 200.0
     [java] INFO  [org.wso2.sample.mqtt.model.Vehicle] - Speed of Vehicle5 updated to 51.0
     [java] INFO  [org.wso2.sample.mqtt.model.Vehicle] - Engine temperature of Vehicle5 updated to 256.0
     [java] INFO  [org.wso2.sample.mqtt.model.Vehicle] - Acceleration of Vehicle6 updated to 5.0
     [java] INFO  [org.wso2.sample.mqtt.model.Vehicle] - Sensor readings of Vehicle12 sent to server.
     [java] INFO  [org.wso2.sample.mqtt.model.Vehicle] - Speed of Vehicle6 updated to 145.0
     [java] INFO  [org.wso2.sample.mqtt.model.Vehicle] - Engine temperature of Vehicle6 updated to 284.0
     [java] INFO  [org.wso2.sample.mqtt.model.Vehicle] - Acceleration of Vehicle7 updated to 28.0
     [java] INFO  [org.wso2.sample.mqtt.model.Vehicle] - Speed of Vehicle7 updated to 142.0
     [java] INFO  [org.wso2.sample.mqtt.model.Vehicle] - Engine temperature of Vehicle7 updated to 296.0
     [java] INFO  [org.wso2.sample.mqtt.model.Vehicle] - Acceleration of Vehicle8 updated to 22.0
     [java] INFO  [org.wso2.sample.mqtt.model.Vehicle] - Speed of Vehicle8 updated to 13.0
     [java] INFO  [org.wso2.sample.mqtt.model.Vehicle] - Engine temperature of Vehicle8 updated to 157.0
     [java] INFO  [org.wso2.sample.mqtt.model.Vehicle] - Acceleration of Vehicle9 updated to 185.0
     [java] INFO  [org.wso2.sample.mqtt.model.Vehicle] - Speed of Vehicle9 updated to 24.0
     [java] INFO  [org.wso2.sample.mqtt.model.Vehicle] - Engine temperature of Vehicle9 updated to 249.0
     [java] INFO  [org.wso2.sample.mqtt.model.Vehicle] - Acceleration of Vehicle10 updated to 357.0
     [java] INFO  [org.wso2.sample.mqtt.model.Vehicle] - Speed of Vehicle10 updated to 180.0
     [java] INFO  [org.wso2.sample.mqtt.model.Vehicle] - Engine temperature of Vehicle10 updated to 201.0
     [java] INFO  [org.wso2.sample.mqtt.model.Vehicle] - Acceleration of Vehicle11 updated to 207.0
     [java] INFO  [org.wso2.sample.mqtt.model.Vehicle] - Speed of Vehicle11 updated to 8.0
     [java] INFO  [org.wso2.sample.mqtt.model.Vehicle] - Engine temperature of Vehicle11 updated to 306.0
     [java] INFO  [org.wso2.sample.mqtt.model.Vehicle] - Acceleration of Vehicle12 updated to 328.0
     [java] INFO  [org.wso2.sample.mqtt.model.Vehicle] - Speed of Vehicle12 updated to 85.0
     [java] INFO  [org.wso2.sample.mqtt.model.Vehicle] - Engine temperature of Vehicle12 updated to 0.0