Over a million developers have joined DZone.

Controlling Appliances Using Java and IoT

· IoT Zone

Access the survey results 'State of Industrial Internet Application Development' to learn about latest challenges, trends and opportunities with Industrial IoT, brought to you in partnership with GE Digital.

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

Working:

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);
      response.setContentType("text/html");
      PrintWriter out = response.getWriter();
      String act = request.getParameter("val");
           if (act.equals("on")) {
             byte on = 1;
                    try {
                        arduino.write(on);
                    } catch (Exception e) {
                        e.printStackTrace();
                     }
           } else if (act.equals("off")) {
                byte off = 0;
                        try {
                            arduino.write(off);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
           }
} catch (Throwable e) {
                 System.out.println(e);
 }
  • Create index.html file in ‘WebContent’ folder and use the following code snippets to create web page
HTML 
<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>
</select>
Javascript
$('#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() {
Wire.begin(4);                
    Wire.onReceive(receiveEvent);
    Serial.begin(9600);         
    pinMode(6, LOW);
}
void loop() {
}
void receiveEvent(int howMany) {
    while(Wire.available()>0)   {
   int a = Wire.read(); 
   Serial.println(a);
             if(a == 1){
Serial.println("HIGH"); 
                    pinMode(6,HIGH);
            }else if(a == 0){
                 Serial.println("Low");
                    pinMode(6,LOW);
            }
    } 
}
  • View the output from web browser using the following URL

http://IP:jettyPort/Nameofproject

The IoT Zone is brought to you in partnership with GE Digital.  Discover how IoT developers are using Predix to disrupt traditional industrial development models.

Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}