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: