» » » Home Automation with Raspberry pi using Relay board

Home Automation with Raspberry pi using Relay board

posted in: Others, Sensors | 0

Problem Statement:

A major count of people want great comfort but at reasonable prices. We feel lazy to light up the houses every evening when the sun goes down and next morning, turning the lights off again Or to turn the Air conditioner/Fan/Heaters on/off as were the weather or room temperature.

 

Solution:

An inexpensive solution to avoid this extra work of turning off the appliances when required is here. It is to automate your houses in comparatively very less costs using simple plug and play products. It works as when the temperature goes up or down, it turns the Air conditioner or heater on, respectively. Also, when required, it will help to switch on or the lights of your home without manually switching them on. And many more appliances can be controlled.

Automate the world. Let us start your home.

 

Operation:

Here are the steps to implement the Home automation system using Raspberry pi with Relay board.

We will be using the Raspberry Pi 2 ,I2C header, I2C Relay controller MCP23008, MCP9808 Temperature sensor, TCS34903 luminance sensor, I2C connecting cable, Micro USB adapter, 12V power adapter for Relay board.

 

The requisite connections are as follows:

    • This will work over I2C . Take a I2C shield for Raspberry pi and gently connect it on to  the GPIO pins of Raspberry Pi.
      pi-adapter
    • Connect the one end of I2C cable to the in-port of TCS34903 and the other end to the I2C shield.
    • Connect the MCP9808 sensor’s in-pot to the TCS34903’s out using I2C cable.
      mcp9808-pi-conn
    • Connect the MCP23008 ’s in-pot to the MCP9808 sensor’s out using I2C cable.
      mcp9808-relay-conn
    • Also connect the Ethernet cable to Raspberry Pi .Wi-Fi router can also be used for the same.
    • Then, power the Raspberry Pi using a Micro USB adapter and MCP23008 Relay board using 12V adapter
      mcp9808-power-conn
    • Finally, connect the light with first relay and a fan or heater with second relay. You can expand the module or can connect more devices with the relays.
      mcp9808-final-conn

 

 

 

Communicating using I2C Protocol:

 

To make Raspberry Pi I2C enabled, proceed as mentioned below:

  • In terminal, type the following command to open the configuration settings:
    sudo raspi-config
  • Select “Advanced Options” in here.
  • Select “I2C” and Click “Yes”.
  • Reboot the system to set it up as per the changes made using the command reboot.

 

Programming the module:

 

The reward of using Raspberry Pi is, that is provides you the flexibility to opt a the programming language in which you want to program to interface the sensing device with Raspberry Pi. Harnessing this advantage of Raspberry Pi, we are demonstrating here its programming in the Java.

 

To set up the Java environment, Install the “pi4j libraby” from http://pi4j.com/install.html. Pi4j is a Java Input/Output Library for Raspberry Pi.
An easy and most preferred method to install the “pi4j library” is to execute the undermentioned command directly in your Raspberry Pi:

curl -s get.pi4j.com | sudo bash
OR
curl -s get.pi4j.com

 

Code:

 

import com.pi4j.io.i2c.I2CBus;

import com.pi4j.io.i2c.I2CDevice;

import com.pi4j.io.i2c.I2CFactory;

import java.io.IOException;

 

class MCP23008

{

public static void main(String args[]) throws Exception

{

int status, value, value1= 0x00;

// Create I2C bus

I2CBus bus = I2CFactory.getInstance(I2CBus.BUS_1);

// Get I2C device, MCP23008 I2C address is 0x20(32)

I2CDevice device = bus.getDevice(0x20);

// Get I2C device, MCP9808 I2C address is 0x18(24)

I2CDevice MCP9808 = bus.getDevice(0x18);

// Get I2C device, TCS34903 I2C address is 0x39(55)

I2CDevice TCS34903 = bus.getDevice(0x39);

 

// Set Wait Time register = 0xff (255) , wait time = 2.78 ms

TCS34903.write(0x83,(byte)0xFF);

// Enable Access to IR channel

    TCS34903.write(0xC0,(byte)0x80);

// Set Atime register to 0x00 (0) , maximum counts = 65535

TCS34903.write(0x81,(byte)0x00);

// Power ON , ADC enabled , Wait enabled

TCS34903.write(0x80,(byte)0x0B);

Thread.sleep(250);

 

// Read 8 Bytes of Data with clear/ir data LSB first

byte[] data1 = new byte[8];

        // Read Temperature Data

        byte[] data = new byte[2];

 

status = device.read(0x09);

// Configured all pins as OUTPUT

device.write(0x00, (byte)0x00);

Thread.sleep(500);

while(true){

MCP9808.read(0x05, data, 0, 2);

// Convert data

int temp = ((data[0] & 0x1F) * 256 + (data[1] & 0xFF));

if(temp > 4096)

{

temp -= 8192;

}

double cTemp = temp * 0.0625;

            System.out.printf(“Temperature in celsius is : %.2f C %n”, cTemp);

TCS34903.read(0x94,data1,0,8);

           

double ir    = ((data1[1] & 0xFF) * 256) + (data1[0] & 0xFF) * 1.00;

double red   = ((data1[3] & 0xFF) * 256) + (data1[2] & 0xFF) * 1.00;

double green = ((data1[5] & 0xFF) * 256) + (data1[4] & 0xFF) * 1.00;

double blue  = ((data1[7] & 0xFF) * 256) + (data1[6] & 0xFF) * 1.00;

// Calculate illuminance

double illuminance = (-0.32466) * (red) + (1.57837) * (green) + (-0.73191) * (blue);

System.out.printf(“Illuminance is :  %.2f  lux%n “, illuminance);

if (illuminance < 200)

{

value1 = status | (0x02);

}

else

{

value1 = status & (0x01);

}

device.write(0x09, (byte)value1);

if (cTemp > 30)

{

value = value1 | (0x01);

}

else

{

value = value1 & (0x02);

}

device.write(0x09, (byte)value);

Thread.sleep(300);

}

}

}

 

 

 

 

Leave a Reply