Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Anurag S. Vasanwala
Published © CC BY-NC

Home Automation Using Raspberry Pi 2 And Windows 10 IoT

Automated Home using Raspberry Pi 2 (Windows 10 IoT Core) and Arduino.

IntermediateFull instructions provided381,927
Home Automation Using Raspberry Pi 2 And Windows 10 IoT

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
For each room you want to automate, take separate Arduino UNO.
×1
Raspberry Pi 2 Model B
Raspberry Pi 2 Model B
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Breadboard (generic)
Breadboard (generic)
×1
LDR (Light Dependent Resistor)
LDR provides different voltage reading for different light intensity.
×1
PIR Motion Sensor (generic)
PIR Motion Sensor (generic)
Passive IR detects human presence.
×1
Relay (generic)
You can use any kind of relay module. No of relay will depend upon how many devices you want to control. [NoOfDevices = NoOfRelay]
×1
Siemens Contactor (3TF32 00-0AP0)
Contactor is going to be used to control water-pump of maximum rating of AC3: 16A 415VAC. Before purchasing contactor, please check ratings of your water-pump and your contry dependencies if there. "3TF32 00-AP0" is capable to drive up-to 16A and 415VAC water-pump. This contactor operates usinf in-built coil. Coil ratings are (specific to 3TF32 00-0AP0 only) 250VAC.
×1
Miniature Circuit Breaker (1P C16A)
MCB is attached before contactor to protect water-pump from electrical faults and overload. Here MCB also have specific ratings. Our contactor is of 16A and thus I have used 16A MCB. You can modify according to your convenience.
×1

Software apps and online services

Arduino IDE
Arduino IDE
To write Arduino firmware. Have explain in the article.
Visual Studio 2015
Microsoft Visual Studio 2015
Community edition is commanded and its free for up-to five users. But you can use higher than it if you have purchased licensed copy of Visual Studio 2015.
Windows 10 IoT Core
Microsoft Windows 10 IoT Core
As a primary OS, which controls the devices via Arduino and provides management console.

Story

Read more

Schematics

Schematic for one room

You can add multiple room by attaching individual Arduino with unique I2C slave address on the bus.

Fritzing Schematic File

Fritzing file containing complete schematic.

Code

Arduino Sketch

C/C++
/*
	Arduino Sketch v0.4

	This sketch is written for "Home Automation using Raspberry Pi 2 and Window 10 IoT"
	Refer this link: https://www.hackster.io/AnuragVasanwala/home-automation

	This sketch is tested on Atmega328p only.

	This sketch prepares an Arduino device as slave	device on I2C bus operated by
	Raspberry Pi 2 running Windows 10 IoT Core.

	Objectives:
		+ Periodically collect sensor data (Function: loop)
		+ OnRecevive, collect 3-bytes mode instruction
		  and performa operation based upon it. (Function: ReceiveData)
		+ OnRequest, send 14-bytes response array based
		  on selected mode by OnReceive. (Function: SendData)
	
	This sketch is provided as it is without any WARRANTY. You can use it for personal as well as
	commercial use.

	I am not liable for any loss of data or injuries caused by this sketch.
*/

#include <Wire.h>  
#define _DEBUG_

/* Arduino's I2C Slave Address */
#define SLAVE_ADDRESS 0x40

/* PIN DECLARATION */
int Pin_AmbientLight_LDR = A0;
int Pin_PassiveIR = 2;
int Pin_Temperature = A1;

/* Global Variable */
volatile short Value_AmbientLight_LDR, Value_Temperature;
volatile bool Value_PassiveIR;

/* Protocol Variable */
byte Mode, Pin, Value;
byte Response[14];


void setup()
{
	// Initialize pins
    pinMode(Pin_AmbientLight_LDR, INPUT);
    pinMode(Pin_PassiveIR, INPUT);
    pinMode(Pin_Temperature, INPUT);
    pinMode(0, OUTPUT);
    pinMode(1, OUTPUT);
    pinMode(3, OUTPUT);
    pinMode(4, OUTPUT);
    pinMode(5, OUTPUT);
    pinMode(6, OUTPUT);
    pinMode(7, OUTPUT);
    pinMode(8, OUTPUT);
    pinMode(9, OUTPUT);
    pinMode(10, OUTPUT);
    pinMode(11, OUTPUT);
    pinMode(12, OUTPUT);
    pinMode(13, OUTPUT);
    pinMode(A2, OUTPUT);
    pinMode(A3, OUTPUT);

#ifdef _DEBUG_
    Serial.begin(9600);
#endif
	
	// Initialize I2C Slave on address 'SLAVE_ADDRESS'
    Wire.begin(SLAVE_ADDRESS);
    Wire.onRequest(SendData);
    Wire.onReceive(ReceiveData);
}

void loop()
{
  // Read LDR
  //         Arduino supports 10-bit Analog Read.
  //             Thus we need to convert it into 8-bit.
  Value_AmbientLight_LDR = analogRead(Pin_AmbientLight_LDR);
  Value_AmbientLight_LDR = map(Value_AmbientLight_LDR, 0, 1023, 0, 255);
  
  // Read PassiveIR value
  Value_PassiveIR = (digitalRead(Pin_PassiveIR) == HIGH) ? true : false;

  // Read Temperature Sensor and Convert Voltage into Celsius
  Value_Temperature = (short)((float)(analogRead(Pin_Temperature) * 0.48828125));
  
  // Wait for 100 ms
  delay(100);
}

// Callback for I2C Received Data
void ReceiveData(int byteCount)
{
	// Read first byte which is Protocol Mode
  Mode = Wire.read();

	// Read second byte which is Pin. Only Valid for Mode 2
  Pin = Wire.read();

	// Read third byte which is Pin-Value. Only Valid for Mode 2
  Value = Wire.read();

	// Signal specified pin if Mode 2 is received
  if (Mode == 2)
  {
      digitalWrite(Pin, Value);
  }

#ifdef _DEBUG_
    Serial.print(Mode);
    Serial.print(" ");
    Serial.print(Pin);
    Serial.print(" ");
    Serial.println(Value);
#endif
}

void SendData()
{
    switch (Mode)
    {
        case 0: // Mode: Read Sensor
            Response[0] = (byte)Value_AmbientLight_LDR;

            // Value_PassiveIR is boolean so that we need to convert it into byte
            Response[1] = (byte)((Value_PassiveIR == true) ? 1 : 0);
            
            // Response[2] byte is Sign byte for Temperature
            //         0 - -ve Temperature
            //         1 - +ve Temperature
            Response[2] = (byte)((Value_Temperature < 0) ? 0 : 1);
      
            Serial.println(Value_Temperature);
            
            // -ve Temperature can't be sent in byte. Convert it into +ve equivalent
            Response[3] = (byte)((Value_Temperature < 0) ? (Value_Temperature*(-1)) : Value_Temperature);
            break;
        case 1: // Mode: Read Devices State
            Response[0] = (digitalRead(0) == HIGH) ? 1 : 0;
            Response[1] = (digitalRead(1) == HIGH) ? 1 : 0;
            Response[2] = (digitalRead(3) == HIGH) ? 1 : 0;
            Response[3] = (digitalRead(4) == HIGH) ? 1 : 0;
            Response[4] = (digitalRead(5) == HIGH) ? 1 : 0;
            Response[5] = (digitalRead(6) == HIGH) ? 1 : 0;
            Response[6] = (digitalRead(7) == HIGH) ? 1 : 0;
            Response[7] = (digitalRead(8) == HIGH) ? 1 : 0;
            Response[8] = (digitalRead(9) == HIGH) ? 1 : 0;
            Response[9] = (digitalRead(10) == HIGH) ? 1 : 0;
            Response[10] = (digitalRead(11) == HIGH) ? 1 : 0;
            Response[11] = (digitalRead(12) == HIGH) ? 1 : 0;
            Response[12] = (digitalRead(A2) == HIGH) ? 1 : 0;
            Response[13] = (digitalRead(A3) == HIGH) ? 1 : 0;
            break;
        case 2: // Mode: Set Device State
            Response[0] = (digitalRead(Pin) == HIGH) ? 1 : 0;
            break;
        default:
            break;
    }

	// Wire back response
  Wire.write(Response, 14);
}

Raspberry Pi 2 (Windows Headed App) Software

Credits

Anurag S. Vasanwala

Anurag S. Vasanwala

8 projects • 510 followers

Comments