Skip to main content

IoT : Remotely Control the LED in Raspberry Pi 2 using Java (pi4j) and MQTT Protocol

In this post I am going to demonstrate that how remotely control LED and how it can be connected to Raspberry Pi using MQTT protocol and control GPIO pins using pi4j library. This work is done  using Java code.

Recently I bought Raspberry Pi 2, so I just wanted to do some learning stuff, basically how to blink (on/off)  LED remotely.

I have performed extensive survey about Raspberry Pi on internet I found lots of stuff about connect LED with Raspberry Pi via GPIO pins. This created a lots of interest. So I m searched Java library to control gpio pins and I got pi4j library.   Actually pi4j library easy to learn and provide bunch of examples. Details of its are mentioned below:


pi4j Library

Its provides a friendly object-oriented I/O API and implementation libraries for Java programmers to access the full I/O capabilities of the Raspberry Pi platform. This project abstracts the low-level native integration and interrupt monitoring to enable Java programmers to focus on implementing their application business logic. For more details, refer this website : http://pi4j.com/index.html

GPIO pins layout are differ from each iteration of Raspberry pi. I have used Raspberry Pi2

GPIO Pins Layout for Raspberry Pi 2
Raspberry Pi 2 GPIO Layout
Raspberry Pi 2 GPIO Layout
I have wired gpio pin no 15 and 16 to 2 LED's. Diagrammatic represent is shown below:

Some important key steps:
To control LED via Java I used pi4j lib code.

Sample Code for turn on LED and turn off via GPIO 15 pin, is mentioned below:


After this steps, we can  control LED  remotely by  performing network protocols, For this performance I got Dzone Refcard about MQTT protocols.

MQTT Protocol
MQTT (formerly MQ Telemetry Transport) is a publish-subscribe based "light weight" messaging protocol for use on top of the TCP/IP protocol. It is designed for connections with remote locations where a "small code footprint" is required or the network bandwidth is limited. The publish-subscribe messaging pattern requires a message broker. The broker is responsible for distributing messages to interested clients based on the topic of a message   --Wikipedia

Basically it is client server model. We need server, its called "message broker". First we need to install server. So here we use "Mosquitto server". Mosquitto is an open source (BSD licensed) message broker that implements the MQ Telemetry Transport protocol versions 3.1. Download and install from http://mosquitto.org/download/

So we need client to connect message broker(server). There are many mqtt clients available. Generally, one of the popular client is Eclipse Paho. Secondle we need to add Paho client to our java project for communicate to message broker (mosquitto server).


I have used Amazon Ec2 instances (free tier). I installed Mosquitto server and WildFly JavaEE7 server. Then deploy my war file into wildfly server. Here Mosquitto is message broker and wildfly provide webpage for control LED. (I used JSF and Primefaces Lib for quick prototype)

In Raspberry Pi, our code is to going  connect the Mosquitto message broker with the help of paho library in port 1883. It is default port for MQTT and  this subscribe the topic  and waiting for any message to receive. If any message is received, then it process the message based on we are turn on LED using pi4j library. Check the code here.

In Wildfly server, based on JSF page any button triggered which directly connect to Mosquitto server using same paho library and publish the message in same topic which raspberry pi subscribed.  That's it.  Check the server code here.



My web page 


All codes in my github repo here

Screencast:
           

Popular posts from this blog

Docker : Tomcat Clustering with Load Balancer (Tomcat and Nginx)

In this post i will show Tomcat Clustering in Docker Container. In  my previous post i discussed how to achieve tomcat clustering with Nginx Front end . Its almost same scenario, but this time we will achieve via docker container. Docker Docker  is an  open-source  project that automates the deployment of  applications  inside  software containers , by providing an additional layer of abstraction and automation of  operating-system-level virtualization  on  Linux . [4]  Docker uses resource isolation features of the Linux kernel  such as  cgroups  and kernel  namespaces  to allow independent "containers" to run within a single Linux instance, avoiding the overhead of starting and maintaining  virtual machine   --Wikipedia

Understanding Virtual Host Concept in Tomcat

Hi in this post we will see how to setup virtual host in Apache Tomcat server. Virtual Host is in-built feature that allows to deploy multiple website(domains) in single instance of tomcat server. The main benefit in this way is its cost effective. Scenario: I am going to deploy 3 website with following domain names in single tomcat http://www.ramki.com http://www.krishnan.com http://www.blog.ramki.com The following diagram is my outline. Outline structure of Virtual Host Concept in Tomcat Here my tomcat IP address 192.168.1.15. or any IP address allocated my ISP. but it should be public IP address. How all domain names are pointing to my Tomcat?                   When we purchase the domain name we need to update the our tomcat IP address to it. like or we can simulate same DNS Setup through hosts file in both Linux and Windows. In Linux tha file is located at /etc/hosts Now How Setup...

Virtual Host + Apache httpd server + Tomcat + mod_jk connector

In my last post ( Virtual Host in Tomcat ) we discussed about how setup the virtual host in Tomcat. Its cost effective technique because only one public IP is enough to host multiple domain. If we have big organization and each department want to host their website in locally in different machine. then how to achieve the virtual host concept?. In this post we will see the how we do this. Update :   I posted  Virtual Host + Nginx + Tomcat  Its easy to configure, compare to Apache httpd server Problem Scenario:         In big organization they have multiple department, each department want to host their website in different machine. so these websites are accessed locally with different local IP address. When we mapping to public address then we face the problem. We have two choice either purchase as many public address or Put one server front  and delegate these request. ...