Showing posts with label Eclipse Paho Java Client. Show all posts
Showing posts with label Eclipse Paho Java Client. Show all posts

Saturday, November 12, 2016

IOT: Java + Spring Boot + Pi4j + MQTT Eclipse Paho + MySQL+ Raspberry Pi + LED




    


Objective: The Objective of this exercise is to integrate Spring Boot, Pi4J, MySQL database on to Raspberry Pi by creating a simple Web application to toggle (ON or OFF) the LED on Raspberry Pi based on the REST URL hit on the browser and to persist the LED information (i.e blink status, blink datetimestamp, blink counter, JSON format) to MySQL database. Also we can view the LED information that is persisted in the MySQL database. Also the LED information is published to a topic on MQTT Broker, which can accessed by any MQTT client subscribed for that topic.

YouTube Video:
The YouTube Video for this tutorial is available at this link and also can be found at the end of the tutorial.

GitHub Source:
https://github.com/agilerules/IOT/tree/master/iotled-mqtt
Task details:

Once the application is successfully setup (as per the instructions below) and MySQL database and table are configured, here is what you see as the output.
1.  Every time once the URL http://<raspberryIp>:9999 is hit (or refreshed), it will Hello World!!! as shown in the below screen shot.
2.  Every time once the URL http://<raspberryIp>:9999/light is hit (or refreshed) then the LED on the Raspberry Pi will toggle (on or off) and you should see the LED Status (ON/OFF), time and the blink counter on the browser output. Notice that the blink counter will increase by 1 for every hit.
Now refresh he browser again, where you see that the status is OFF (and also the LED on the Raspberry Pi will be OFF) now and the counter is increased to 2.
Now refresh he browser again, where you see that the status is ON (and also the LED on the Raspberry Pi will be ON) now and the counter is increased to 3.
For every refresh that is made above, there will be an entry in the MySQL database in the table led_details for the transactions made in the above steps, which can be validated by invoking the MySQL PhpMyAdmin screen installed on Raspberry Pi (as instructed in the blog). 
http://<raspberryIp>/phpmyadmin/
3.  Every time once the URL http://<raspberryIp>:9999/display is hit (or refreshed) then it fetches the top 10 records persisted in the MySQL database table led_details.
4. MQTT Testing: Open a new terminal window of Raspberry Pi using SSH and subscribe for the MQTT topic using the following command:
mosquitto_sub -d -v -t iot/led
The concept here is the that we are trying to subscribe to the MQTT Topic using MQTT client (mosquitto_sub).

5. Now every time you hit the browser with the URL http://<raspberryIp>:9999/light, you will notice that the LED information is published on the screen (as highlighted in red). The reason here is that we have the logic in the code to publish the LED information to a particular topic (here iot/led) on MQTT Broker, which can be viewed from any MQTT Client, subscribed to the same topic.
Dependencies:
Software:
  • Spring Boot with Tomcat Embedded
  • Pi4j
  • Java
  • Maven (Please find my blog here on how to install Maven on Raspberry Pi)
  • MySQL & PhpMyAdmin (Please find my blog here on how to install MySQL and PhpMyAdmin on Raspberry Pi)
  • MQTT Broker (Please find my blog here on how to install MQTT Broker on Raspberry Pi).

Hardware:
  • Raspberry Pi (installed with Java, Maven)
  • 3 Jumper wires
  • LED
  • 220 Ohm Resistor
  • Breadboard
  • GPIO Ribbon Cable
Steps:

1.  MySQL database has to be installed first on the Raspberry Pi and have to create the table led_details to capture the LED information. Please follow the instructions provided in the blog on how to install MySQL database on Raspberry Pi, create a database and the table. 
CREATE DATABASE agilerulesdb;
USE agilerulesdb;
CREATE USER ‘admin’@’localhost’ IDENTIFIED BY ‘password’;
GRANT ALL PRIVILEGES ON agilerulesdb.* TO 'admin'@'localhost';
--Relogin with the new user "admin" that is created and execute the following scripts
mysql -u admin -p
use agilerulesdb;
CREATE TABLE led_details
(
ID int NOT NULL AUTO_INCREMENT,
BlinkStatus varchar(3),
DateTimeStamp DATETIME,
BlinkCounter INT,
JsonPayload varchar(255),
PRIMARY KEY (ID)
);
Note: You can change the database name, user id and password in the above list, as per you convenience.
2.  Install MQTT Broker on the Raspberry Pi and test the installation with the MQTT Client using the instructions provided in the blog.  We are going to the use the MQTT Broker URL to publish the LED information, which will be explained further down in the tutorial.
3.  Complete the following Wiring process of Raspberry Pi, LED, Resistor and with few jumper wires as shown in the below diagram. 
Wiring Diagram:

Breadboard Diagram:

Bread Board Wiring
Bread Board Wiring

          - A wire from Pin 6 to Ground ( - on Breadboard)
          - A wire from Pint 12 (GPIO_1) to + line on Breadboard
          - Fix the LED and the long end of LED Anode (+) to be connected to Resistor (Yellow side on top         of Resistor)
          - Other end of Resistor (i.e the red colour on top) to the + line of Breadboard
          - LED Cathode (-) to – line of Breadboard
    4.    Now connect to Raspberry Pi using Putty with your Raspberry Pi Credentials and by-default, you will in the path /home/pi.
    5.    For proper organization purpose, I created folders (using mkdir command) \projects. But this is not mandatory. You can even skip this step and move to next step.
          mkdir projects
    6.   From the \projects folder, execute the following command to Git clone the code to your local raspberry Pi.
          git clone https://github.com/agilerules/IOT.git

    7.     Once the git clone is complete, you will see IOT folder created in \projects folder. Now go to IOT folder (using the command cd IOT) and you will see the folder iotled-mqtt inside this folder IOT.
    8.    Move to this iotled-mqtt folder (using the command cd iotled-mqtt).
    9. Now execute the following command to start the maven build. (Incase if mvn command is not recognized then it means that maven is not installed on your Raspberry Pi.  Please follow my blog on how to install Maven on Raspberry Pi.
    mvn package
    10. Finally, you should see maven BUILD SUCCESS message like this.
    11. Now go to \target folder to find the jar file that was created by the Maven build (above step).
    12. Now come back to previous folder (i.e to iotled-mqtt) and run the following command and you should see the Spring in your screen as shown in the below screen shot, which indicates that Spring Boot is working fine.
    sudo java -jar target/iotled-mqtt-0.0.1-SNAPSHOT.jar

    13. You should finally see the message saying “Started Application...”. Also you can see that the REST end points / and /light are loaded and Tomcat Started on port 9999.
    14. Now go to browser and hit the below URL where replace <raspberryIp> with your Raspberry pi IP address.
    http://<raspberryIp>:9999/

    15. Similarly once you hit the below URL (multiple times), the LED connected to Raspberry Pi should toggle (On or Off) and you should see the LED Status (ON/OFF), time and the blink counter. Notice that the blink counter will increase by 1 for every hit.
    http://<raspberryIp>:9999/light


    Now refresh the browser again, where you see that the status is OFF now and the counter is increased to 2.



    Now refresh the browser again, where you see that the status is ON now and the counter is increased to 3.
    16. Once you hit the below URL http:// <raspberryIp>:9999/display, it will fetch the top 10 records persisted in the MySQL database table led_details.

    17. Here is the source code explanation below:
          The complete project structure is available here:
    a) Pom.xml
    The below are two library dependencies that we need for this application: spring-boot-starter-web - This is for Spring boot to enable the Web application with embedded Tomcat. This would need the following parent Spring Boot starter package to be defined in <parent> node. <artifactId>spring-boot-starter-parent</artifactId>
     pi4j-core – This is for Pi4J to communicate with Raspberry Pi GPIO pins.
    spring-boot-starter-data-jpa: This is required for Spring Data JPA communication.
    mysql-connector-java: This is driver class required to connect to MySQL database
    gson: This is required to convert Java Objects into their JSON representation and viceverza.
    Junit:  This required for JUnit testing

    b) Application.java
    This is simple Spring Boot code with @SpringBootApplication annotation and SpringApplication.run() method with the arguments (class, args) inside main() method.
    @SpringBootApplication:
    The @SpringBootApplication annotation is equivalent to using @Configuration, @EnableAutoConfiguration and @ComponentScan with their default attributes.
    @SpringBootApplication is a convenience annotation that adds all of the following:
    @Configuration tags the class as a source of bean definitions for the application context.
    @EnableAutoConfiguration tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings.
    Normally you would add @EnableWebMvc for a Spring MVC app, but Spring Boot adds it automatically when it sees spring-webmvc on the classpath. This flags the application as a web application and activates key behaviours such as setting up a DispatcherServlet.
    @ComponentScan tells Spring to look for other components, configurations, and services in the the hello package, allowing it to find the HelloController.
    Did you notice that there wasn’t a single line of XML? No web.xml file either. This web application is 100% pure Java and you didn’t have to deal with configuring any plumbing or infrastructure.
    The run() method returns an ApplicationContext and this application then retrieves all the beans that were created either by your app or were automatically added thanks to Spring Boot. It sorts them and prints them out.

    c) LEDModel.java
    This class is the JPA Model class to capture the LED information like blinkStatus (ON/OFF), dateTimeStamp (the blink time stamp), blinkCounter (the count of blinks), jsonPayload( all the other fields represented in json format). It holds the private variables with respective getters and setters. Here is the explanation for the JPA Annotations used in this class:
    @Entity:
    The @Entity annotation indicates that the JavaBean is a persistent entity. JPA would automatically pick up this class for Persistence.
    @Table(name = "led_details")
    @Table annotation explicitly configures which table the entity is mapped to. Here the (name = "led_details") denotes that this bean is mapped to the database table name led_details. Note that led_details is a MySQL table name, which holds the LED information.

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID")
    @Id annotation denotes that the this field below this definition is primary key field in the database
    @GeneratedValue(strategy = GenerationType.AUTO) - denotes that we are going to use Auto increment field for the primary key and that's is depicted with the strategy as GenerationType.AUTO
    @Column(name = "ID") - denotes the Field name defined in the database table. Similar to this we have other @Column fields defined.

    d) MqttPublishSubscribeUtility.java
    This class is the utility class which implements MQTT protocol to publish the LED information on a topic. To give some overview on MQTT, it is a machine-to-machine (M2M)/"Internet of Things" connectivity protocol, designed as an extremely lightweight publish/subscribe messaging transport. It is useful for connections with remote locations where a small code footprint is required and/or network bandwidth is at a premium.

    This utility class has a method named mqttConnect()which connect to MQTT broker that is installed on Raspberry Pi whose broker URL is defined in the properties file named mqtt.properties, which is available in the src/main/resources folder.
    Here are the entries for mqtt.properties file:
    src/main/resources/mqtt.properties
    mqttConnect() method:
    It makes use of Eclipse Paho API’s to connect to MQTT broker:

    Here are the steps at high level:
    1.  Initialize the MqttClient along with the Borker URL, Client Id and persistence object
    2.  Initialize the MqttConnectOptions and setCleanSession() to true.
    3.  Invoke the connect() method on MqttClient.
    MqttClient sampleClient = new MqttClient(props.getProperty("BROKER_URL"), props.getProperty("CLIENT_ID"),persistence);
         
    MqttConnectOptions connOpts = new MqttConnectOptions();
          connOpts.setCleanSession(true);
          sampleClient.connect(connOpts);
    mqttConnectNPublishNSubscribe():



    This method will inturn invoke the mqttConnect() method described above to connect to MQTT broker and will publish the MqttMessage payload to the topic named iot/led (defined in the mqtt.properties file). It accepts the JsonObject as payload input from which the LED parameters defined in the LEDModel.java are set.
    Here are the steps involved:
    1. Initialize the MqttClient by calling the method mqttConnect().
    2. Initialize the MqttMessage object with payload as input
    3. Set QOS (Quality Of Service) parameter as 2 (defined in mqtt.properties) for the message. The highest QoS is 2, it guarantees that each message is delivered exactly once. It is the safest and also the slowest quality of service level. The other two QOS values are 1 (which means the message is delivered atleast once) and 0 (which means the message is delivered atmost once)
    4. Invoke the setCallback() method to callback listener to use the events that happen asynchronously
    5. Finally publish the MQTT message on the topic named iot/led, defined in the mqtt.properties file.
    MqttClient sampleClient = mqttConnect();
    MqttMessage message = new MqttMessage(payload.toString().getBytes(Charset.forName("UTF-8")));
    if(props.getProperty("QOS")!=null){ message.setQos(Integer.parseInt(props.getProperty("QOS")));
    }
    sampleClient.setCallback(new SimpleCallback());
          sampleClient.publish(props.getProperty("TOPIC_NAME"), message);
    e) LedDAORepository.java:
    This is DAO repository that extends JPARespository interface provided by Spring. This class has one findBy method (called findTop10ByOrderByIdDesc ) which will fetch the top 10 records from the database table led_details order by the column ID descending. @Transactional annotation defines the scope of the database transaction.

    f) LedController.java:
    @RestController:
    Spring 4.0 introduced @RestController, a specialized version of the controller which is a convenience annotation that does nothing more than add the @Controller and @ResponseBody annotations. By annotating the controller class with @RestController annotation, you no longer need to add @ResponseBody to all the request mapping methods. The @ResponseBody annotation is active by default.
    @RequestMapping maps / to the index() method and returns “Hello World!!”
    @RequestMapping maps /light to the light() method which uses the Pi4j API’s to toggle LED on or off and returns the response with the Light Status as ON/OFF, Time, blink counter information.
    @RequestMapping maps /display to the display() method to fetch the top 10 records persisted in the MySQL database table led_details.

    Explanation of Request Mapping /:
    This method will return Hello World!!! as shown in the below screen shot.
    @RequestMapping("/")
          public String greeting(){
                return "Hello World!!";
          }

    The / is the default mapping that will be triggered and it prints Hello World!! as shown below.
    Explanation of Request Mapping /light:
    This method uses the Pi4j API’s to toggle LED on or off and returns the response with the Light Status as ON/OFF, Time, blink counter information.
    Source Code Explanation:



    Here are the steps at high level:
    1. Initialize the Pi4J API GPIOController using GPIOFactory’s getInstance() method
    2. Invoke the method provisionDigitialoutputPin() method to provision the pin GPIO_01 as the Ouput pin and default the Pin state to LOW (which means, it will be in off state by-defualt) and “My LED” is the user defined name for the LED. Set the BlinkStatus on ledModel instance to OFF (as it is in OFF state for the first time).
    3. Now toggle the pin (ON/OFF) by invoking the toggle() method.
    if(pin==null){
    System.out.println("Entering for the first time..");
    GpioController gpio = GpioFactory.getInstance();
    pin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_01,"My LED",PinState.LOW);
    ledModel.setBlinkStatus("OFF");
    }
    pin.toggle();
    4. Have a blink counter logic to increment the counter for every refresh and set the value to blinkConter() method of ledModel.
    blinkCounter++;
    ledModel.setBlinkCounter(blinkCounter);
    5.Set the current date time stamp using setDateTimeStamp() method.
    ledModel.setDateTimeStamp(new Date().toString());

    6. Using gson API’s, create a JsonObject and add the LED parameters using addProperty() method. Note that getId() method will fetch the MySQL’s Auto Increment number from the table led_details.
    JsonObject payload = new JsonObject();
    payload.addProperty("Id", ledModel.getId());
    payload.addProperty("blinkStatus", ledModel.getBlinkStatus());
    payload.addProperty("dateTimeStamp",  ledModel.getDateTimeStamp());
    payload.addProperty("blinkCounter",  ledModel.getBlinkCounter());ledModel.setJsonPayload(payload.toString());

    7. Invoke the mqttConnectNPublishNSubscribe() method to connect to MQTT broker and publish the message payload  to a topic.
    mqttPublishSubscribeUtility.mqttConnectNPublishNSubscribe(payload)
    8.Finally, persist the LED information to MySQL database using LedDAORepository instance, which is auto wired using Spring’s @AutoWired annotation.
    ledDAORepository.save(ledModel);



    Explanation of Request Mapping /display:
    This method will display the top 10 records persisted in the MySQL database table led_details.

    Source Code Explanation:
    Here are the steps at high level:
    1. Initialize the StringBuffer Object
    2. Invoke the method findTop10ByOrderByIdDesc() will fetch the top 10 records from MySQL database table led_details, order by primary key field Id and fetch them in descending order, which will basically return the List.

    3. Iterate the List to fetch the values and append them to String Buffer object that is created in the first step and finally return this StringBuffer instance.
    StringBuffer strBuffer = new StringBuffer();
    List<LedModel> ledModels = ledDAORepository.findTop10ByOrderByIdDesc();
            for (LedModel ledModel : ledModels){
                strBuffer.append("<html><body>");
    strBuffer.append("Id="+ledModel.getId()+" |BlinkStatus="+ledModel.getBlinkStatus()+" |DateTimeStamp="+ledModel.getDateTimeStamp()+"|Json Payload="+ledModel.getJsonPayload());
                        strBuffer.append(System.getProperty("line.separator"));
                        strBuffer.append("<br>");
                        strBuffer.append("</body></html>");
                  }
              return strBuffer;
         }
    application.properties: (in src\main\resources)

    server.port = 9999
    The above entry indicates the embedded Tomcat in Spring Boot to use the port 9999 (rather than the default 8080 port).

    That’s it. Hope this tutorial is useful for you.

    IOT: Install MQTT Mosquitto on Raspberry Pi and Test with various client utilities

    Objective: The Objective of this tutorial is to install Mosquitto Broker on Raspberry Pi (for the OS - Raspbian Wheezy and Jessie) and test the MQTT connection using the following client utilities:
    a) mosquitto-clients
    b) Eclipse Paho GUI
    c) Eclipse Paho Java Client


    Steps to install Mosquitto Broker on Raspberry Pi:

    The following are the steps to be followed to install Mosquitto Broker on Raspberry Pi:
    1.     SSH to Raspberry Pi with your credentials.
    2.     First things first, update your packages.
     sudo apt-get update
     sudo apt-get upgrade
     In my case, the current version of the Raspberry Pi is Jessie.
    3.   Create a directory where you want to install mosquitto. This step is not mandatory. But I am creating this, so that the folders and softwares installed on raspberry Pi are better organised. Here I am planning to create a directory softwares and in that sub-folder named mosquito.
    mkdir softwares
    cd softwares
    mkdir mosquitto
    cd mosquitto

    4.   Now, you need to add the Mosquitto repository. Enter the following commands.
    sudo wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key
    sudo apt-key add mosquitto-repo.gpg.key
    sudo rm mosquitto-repo.gpg.key

    5.   Next, make the repository available to apt-get. First we need to change to the apt sources list directory.
    cd /etc/apt/sources.list.d/

    6.   Now install the packages list for your version of Raspbian.
          For Wheezy (old version)
          sudo wget http://repo.mosquitto.org/debian/mosquitto-wheezy.list
    For Jessie (latest)
    sudo wget http://repo.mosquitto.org/debian/mosquitto-jessie.list
    In my case, it is Jessie.

    7.     Now update apt information and install Mosquitto.
     sudo apt-get update
     sudo apt-get install mosquitto mosquitto-clients
    Note: The above command will install both mosquito and mosquito-clients. If you want to just install mosquito (and not the clients, then you can just use the command sudo apt-get install mosquito). Here I am installing both mosquitto and mosquitto-clients. mosquitto-clients are good to have because you can test the connectivity of your mosquito broker.

    8.     Start the mosquito service with the following command:
    service mosquitto status

    9.      Check if the mosquito process is running with the following command
     ps -ef | grep mosquitto-jessie.list

    10.  The default port that MQTT listens is 1883. So let’s check that with the following command:
    netstat –tln

    Now with this step, the Mosquitto is successfully installed on Raspberry Pi.
    11.  To start the broker to auto run at raspberry pi start up, enter the following command:
    sudo update-rc.d mosquitto defaults

    Testing the MQTT Connection with client

    We are now going to see how to test the MQTT connection using the following client utilities:
    a) mosquitto-clients
    b) Eclipse Paho GUI
    c) Eclipse Paho Java Client.

    a) Testing the connection with client mosquitto-clients:
    Now let’s test the Broker connection using one of the MQTT client named mosquitto_pub and mosquitto_sub. These utilities come along with the MQTT Broker installation, as we did in Step 7.
    mosquitto_pub is a simple MQTT version 3.1 client that will publish a single message on a topic and exit. More details can be found here.

    mosquitto_sub is a simple MQTT version 3.1 client that will subscribe to a topic and print the messages that it receives. More details can be found here:
    1.  In Step 7, we installed mosquitto-clients which will come with the following two utilities:
    mosquitto_sub  — A command line utility for subscribing to Topics
    mosquitto_pub  — A command line utility for publishing simple messages one at a time
    2.  Running mosquitto_sub:
    Open a terminal window (let's call A) and subscrible for the topic "hello/world"
    mosquitto_sub -d -v -t hello/world
    -v is for making the output verbose. This will print the Topic name as well along with the actual message payload.
    Terminal A:
    3.  Running mosquitto_pub:
    Now open another terminal window (let's call this B) and run the mosquitto_pub to send some test messages out.
    mosquitto_pub -d -t hello/world -m "Hello World"
    Terminal B:
    4.  Now you will see the published message "Hello World" on Terminal A.
    Terminal A:

    That's the end of testing the MQTT connection using mosquitto-clients.


    b) Testing the connection with client Eclipse Paho GUI (RCP):
    1.  There are lot of MQTT client GUI Tools available in the market and one of it is Eclipse Paho GUI.
    2.  The following 3 GUI tools are provided by Eclipse Paho:
    ·         RCP application. Can run standalone or in the Eclipse IDE.
    ·         Eclipse plugin. Not all the features of the Java API are included.
    ·         Java Swing application (IA92 replacement).
    Here I am going to install the first one, i.e RCP application for this tutorial. In my case, I want to install this on Windows laptop, so going to download Windows 64-bit link provided or directly click the below link and download the zip file (by selecting the closest mirror).
    http://repo.eclipse.org/content/repositories/paho-releases/org/eclipse/paho/org.eclipse.paho.ui.app/1.0.0/org.eclipse.paho.ui.app-1.0.0-win32.win32.x86_64.zip
    I am going to run this standalone. So just unzip the zip file to any folder and click "paho" Application file, which will open the Paho GUI client.

    3.  Now click the + button on the top right corner to add a new connection to enter you
    Replace the localhost with your Raspberry Pi IP address (OR the IP address of the server wherever you installed your Mosquitto broker) and click “Connect” button. Note that the port is by-default 1883 and ClientId can be retained whatever the Paho UI has generated.

    4.  Incase if you are not sure of your raspberry pi Ip address, you can use ifconfig command on the SSH terminal window and it will give you the IP Address on the screen.

    5.  If your server details are correct, you will be connected successfully, as shown below. 

    6. Now subscribe to a new topic by clicking the + icon and enter the Topic name hello/world and click “Subscribe” button, which indicates that you are subscribing for the topic hello/world

    7. Now publish some message (let’s say “Message from Eclipse Paho”) to the topic hello/world by clicking the “Publish” button.

    In the History tab, you will see the message that is both Published and Received.

    If you still have the Terminal A window (from step 13 above) still open, you will see the below message even there.


        c) Testing the connection with client Eclipse Paho Java Client:

    The Paho Java Client is an MQTT client library written in Java for developing applications that run on the JVM or other Java compatible platforms such as Android.

    The Paho Java Client provides two APIs: MqttAsyncClient provides a fully asychronous API where completion of activities is notified via registered callbacks. MqttClient is a synchronous wrapper around MqttAsyncClient where functions appear synchronous to the application.
    1.  Now we are going to see how to write a Java MQTT Client code using Eclipse Paho, which will connect to MQTT Broker and publish a message to the topic hello/world.
    2.     Now connect to Raspberry Pi using SSH and with your login credentials.
    3.     Now git clone your code. In my case, the command is:
    git clone https://github.com/agilerules/IOT.git

    Note: The explanation of the code can be found in the below section.
    4.   Once the git clone is complete, you will see IOT folder created. Now go to IOT folder (using the command cd IOT) and you will see the folder Utilities inside this folder IOT. You can ignore the other sub-folders for this demo.
    5.   Move to this Utilities folder (using the command cd Utilities)
    6.   Now before running this Java class, let’s open a new Terminal Window by connecting to Raspberry via SSH and let’s subscribe to topic “hello/world” using mosquitto_sub utility (another MQTT Client, which we installed before) with the following steps:
    Open a terminal window and subscribe for the topic "hello/world"
    mosquitto_sub -d -v -t hello/world
    -v is for making the output verbose. This will print the Topic name as well along with the actual message payload.
    7.   Now execute the following command to start the maven build. (Incase if mvn command is not recognized then it means that maven is not installed on your Raspberry Pi.  Please follow my tutorial on how to install Maven on Raspberry Pi.)
    mvn exec:java -Dexec.mainClass="com.agilerules.demo.MqttPublishSample"

    8.   Finally, you should see the System.out.println() messages from our MqttPublishSample.java, as shown below, which indicates that the Java classes has run and connected to our MQTT broker and successfully published the message “Message from MqttPublishSample” to the topic hello/world.
    9.   If you still have the terminal window [in step 4 above in the section a) Testing the connection with client mosquitto-clients] open, you will see the below message “Message from MqttPublishSample” even there.

    This concludes that the Eclipse Paho Java client testing is working fine.

    Code Explanation:
    Pom.xml
    10.  Since the Paho MQTT code isn't in Maven Central, we need to include its repository in the pom.xml using the following way:
    <repositories>
          <repository>
                <id>Eclipse Paho Repo</id>
                <name>Paho MQTT Client</name>
                <url>https://repo.eclipse.org/content/repositories/paho-releases/</url>
          </repository>
    </repositories>
    Note: You can also use the Nightly snapshots URL, if required and here is the URL:
    https://repo.eclipse.org/content/repositories/paho-snapshots/
    11. Here is the dependency for Eclipse Paho.
      <dependencies>
          <dependency>
                <groupId>org.eclipse.paho</groupId>
                <artifactId>org.eclipse.paho.client.mqttv3</artifactId>
                <version>1.0.1</version>
          </dependency>
       </dependencies>
    Note: I am using the version 1.0.1. You can modify the version whatever you want.

    12.   Here is the complete structure of the pom.xml
    MqttPublishSample.java
    13.  This Java sample is a client code which will connect to MQTT Broker and publish a message to the topic hello/world.

    14. The following are the inputs that are required for the client to connect to broker and publish the message. The inputs are - like what should be topic (hello/world) I am in interested in, what message to be published, what should be the Quality of Service (qos) – 2 during the connection, what is the broker URL and Client Id.
    15.    The following are the steps for Eclipse Paho client to connect to Broker and publish the message:
    -MqttClient instance to be initialized with the inputs like broker, ClientId and Persistence.
    -MqttConnectOptions to be initialized to set Clean Session by passing boolean true
    -Connect to the client using mqttClient.connect() method
    -Publish a message to the client by providing the message content, setting the Quality of Service (QOS) and publish the message to the topic (hello/world)
    -Finally disconnect from the client using mqttClient.disconnect() method