This is my second IOT project i decided to use Pi4J instead of JAVA ME,why? Pi4J is very easy to install and setup and its JavaSE as opposed to JAVA ME check installation doc here.
For this project we will be using a Raspberry PI B+ as the micro controller to control a 5v 8 channel relay board to light two LEDS with an app desktop/android app.
5V 8 channel relay board
My connection is as

The layout for the Raspberry Pi model B+ GPIO pins using Pi4J numbering scheme is as shown in the diagram below

The connection on the raspberry pi and the relay board pins

relay board pins                  raspberry pi pins
VCC                                      5V  .
GND                                     Ground.
IN1                                        GPIO 15
IN2                                        GPIO 16

The yellow/white wire shown in the connection its the live wire from the 5V phone charger ,connected to the com in the relay board and the blue wire is the Neutral wire from the phone charger which connects the LEDS (Cathode)Negative .

the red wires are connected to the NO(Normally Open) contact  for IN1 (RED LED) and IN2 (BLUE LED) which runs to the breadboard and to limit the flow of current a 220 ohms connects the LEDS (Anode) Positive .NO Normally Open means that when the relay is still current wont flow through it.
NC Normally closed means when the relay is still current will flow through it.

The Code

Example code of switching on IN1 on the relay board connected to GPIO 15 on the raspberry pi.

import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.PinState;
import com.pi4j.io.gpio.RaspiPin;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.io.IOException;

public class Auto {

 public static void main(String[] args) throws InterruptedException, IOException {
     GpioController gpio_cont     = GpioFactory.getInstance();  
     GpioPinDigitalOutput relay_1 = gpio_cont.provisionDigitalOutputPin(RaspiPin.GPIO_15,"relay 1",PinState.HIGH);



If you have setup Pi4J correctly to compile the class in the Raspberry Pi execute the command in the terminal.

javac -cp .:classes:/opt/pi4j/lib/'*' -d . Auto.java

and to run

sudo java -cp .:classes:/opt/pi4j/lib/'*' Auto

To be able to send commands from the app will need to create a Server to listen for commands in the raspberry pi and a Client to send commands.
We will create a ServerSocket that will run on a given port number and an IP address since i have connected both the app and the raspberry pi to my LAN.

The Server class is hosted on GitHub.

The client code is as below its a JavaFX application

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.TilePane;
import javafx.stage.Stage;

 * @author Anon
public class ClientHomeAutoFx extends Application {
    Socket s;
    PrintWriter output;
    BufferedReader input;
    public void start(Stage primaryStage) {
        Button btn = new Button();
        btn.setText("Relay 1");
        btn.setOnAction((ActionEvent event) -> {
           Platform.runLater(() -> {
            try {
            } catch (IOException ex) {
                Logger.getLogger(ClientHomeAutoFx.class.getName()).log(Level.SEVERE, null, ex);
        Button btn_ = new Button();
        btn_.setText("Relay 2");
        btn_.setOnAction((ActionEvent event) -> {
           Platform.runLater(() -> {
            try {
            } catch (IOException ex) {
                Logger.getLogger(ClientHomeAutoFx.class.getName()).log(Level.SEVERE, null, ex);
        TilePane root = new TilePane();
        Scene scene = new Scene(root, 300, 250);
        primaryStage.setTitle("Home Auto Desktop");
    private void socketConfig(){
        try {
            s = new Socket("Raspberry IP", 9090);
            output=new PrintWriter(s.getOutputStream(),true);
            input =  new BufferedReader(new InputStreamReader(s.getInputStream()));  
        } catch (IOException ex) {
            Logger.getLogger(ClientHomeAutoFx.class.getName()).log(Level.SEVERE, null, ex);

Its the same for android just use the socketConfig method in your app and send a command button click

     button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

Am not good in electronics and thats why i choose to use a phone charger and LEDS,Since the relay board is rated 10A-250V, it works for many electronic appliances .I will just share a Video of lighting a bulb.




Popular Posts