Over a million developers have joined DZone.

Controlling Appliances Using Java and IoT

DZone 's Guide to

Controlling Appliances Using Java and IoT

· IoT Zone ·
Free Resource

The proliferation of internet-connected devices, from cars to heart monitors that interact without human intervention, is creating new possibilities in data gathering, predictive analytics and IT automation. Smart Home, Smart Refrigerator, Connected cars, etc., are some of the IoT gadgets that have come up and created hype in the market.

This blog will give the detailed procedure for controlling appliances. Definitely this blog will be an add-on for the folks who are interested in doing some home automation stuff.

Hardware required:

  • Raspberry Pi Kit
  • Arduino Uno
  • Relay Board
  • Bunch of wires

Software required:

  • Download and install the Eclipse development environment on the machine on which application will be developed
  • Install the following softwares on Raspberry Pi with Raspbian OS


workingHere Raspberry Pi acting as a gateway module receives HTTP request and sends the signal to Arduino UNO for controlling appliances attached to it via relay. The web module of the application (Here Smart Lab) will be running on the Jetty server installed on Raspberry Pi. Also wiring pi library which is wrapped by pi4j library installed on Raspberry pi enables the communication between end devices and gateway. Once the gateway receives HTTP request, the smart lab web module talks to pi4j library and sends the request to Arduino using I2C protocol. Then Arduino with Wire library, receives gateway request, processes it and send the request to end nodes. Here end node is a simple on-off switch and that is controlled by relay. Webcam (using Motion service) connected to Raspberry pi will enable the remote monitoring of the appliances.

Circuit Connections:

Steps to write the Java Servlet which listens to web browser requests and communicates with Arduino UNO using pi4j Library and I2C communication:

  • Create a Dynamic Web Project
  • Add the Pi4J library to your Project
    • Download Pi4J fromhere
    • Copy the ‘pi4j-core.jar’file into project’s ‘WEB INF/lib’ directory
  • Go to project -> Java Resources -> src and create a Java class which extends HttpServlet and copy the following code in doGet()
try {
      bus = I2CFactory.getInstance(I2CBus.BUS_1);
      arduino = bus.getDevice(0x04);
      PrintWriter out = response.getWriter();
      String act = request.getParameter("val");
           if (act.equals("on")) {
             byte on = 1;
                    try {
                    } catch (Exception e) {
           } else if (act.equals("off")) {
                byte off = 0;
                        try {
                        } catch (Exception e) {
} catch (Throwable e) {
  • Create index.html file in ‘WebContent’ folder and use the following code snippets to create web page
<label for="flip-1" > Device</label>
    <select name="flip-1" id="flip-1" data-role="slider" data-track-theme="e">
<option value="off" selected="selected">Off</option>
<option value="on">On</option>
$('#flip-1').bind("change", function(event, ui){
        if(event.target.value == 'on'){
        var value = "on";   
        $.getJSON('/projctname/MyServlet?val='+value,function(result) {
                    console.log("ON click success");    
   } else if(event.target.value == 'off'){
       var value = "off";
       $.getJSON('/projctname/MyServlet?val='+value,function(result) {
                console.log("OFF click success");
  • Export project from Eclipse (File -> Export) into a WAR file and Copy WAR file to the webapps folder of Jetty Server (/usr/share/jetty/webapps) onto Raspberry Pi
  • Dump the following code into Arduino UNO for receiving data from Raspberry Pi and control the appliances via relay board
#include <Wire.h>
void setup() {
    pinMode(6, LOW);
void loop() {
void receiveEvent(int howMany) {
    while(Wire.available()>0)   {
   int a = Wire.read(); 
             if(a == 1){
            }else if(a == 0){
  • View the output from web browser using the following URL



Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}