Erik DumasMichael Rodriguez
Published

Smart Home Small Scale

Combining multiple sensors, such as PIR motion sensors and temperature sensors, to create a model of a smart home.

IntermediateShowcase (no instructions)6 hours285
Smart Home Small Scale

Things used in this project

Hardware components

PIR Motion Sensor (generic)
PIR Motion Sensor (generic)
×2
BMP180
×1
Photon
Particle Photon
×2
Breadboard (generic)
Breadboard (generic)
×1
Male/Female Jumper Wires
Male/Female Jumper Wires
×1
LED (generic)
LED (generic)
×9
DC Fan 12V .09A (Generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Resistor 221 ohm
Resistor 221 ohm
×3

Software apps and online services

ThingSpeak API
ThingSpeak API
Particle.io

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Hot glue gun (generic)
Hot glue gun (generic)

Story

Read more

Schematics

Circuit Diagram

Graph of Temperature Vs. Time

img_4992_Y4bGUtq6fS.JPG

img_4993_kzh3GIPu35.JPG

img_4994_7s0ScqKNfi.JPG

img_4995_dRluCyjYkX.JPG

Circuit Diagram

Code

Temperature Taking and Publishing Code as well as first floor LED actiavation.

C/C++
// This #include statement was automatically added by the Particle IDE.
#include <ThingSpeak.h>
// This #include statement was automatically added by the Particle IDE.
#include <Adafruit_BMP085.h>
// This #include statement was automatically added by the Particle IDE.
#include "Adafruit_BMP085/Adafruit_BMP085.h"



/*
	Wiring
	------
	BMP085 Vcc to 3.3V
	BMP085 GND to GND
	BMP085 SCL to D1
	BMP085 SDA to D0
*/


TCPClient client;
double Temp;
bool temp_broken = false;
Adafruit_BMP085 sensor;  // Sensor instance
int count = 0;
unsigned int myChannelNumber = 256395;
const char * myWriteAPIKey = "CF2COILCQ63FPO27"; //thingspeak 

void setup() {
    Serial.begin(9600);
    ThingSpeak.begin(client);
    
    pinMode(D5, OUTPUT);
	  pinMode(D7, OUTPUT);
	
    digitalWrite(D5,HIGH);
    // Initialize Sensor BMP180
	if (!sensor.begin()) {
	  Serial.println("No sensor found, please check wiring!");
	  while (1) {}
	};
    
}

//loop includes ThingSpeak code to send data to ThingSpeak for graphing purposes
//loop includes code to decide whether the temperature readings are above threshold and to turn on fan(AC)
void loop() {
    ThingSpeak.setField(1,sensor.readTemperature());
    ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
    delay(5500);
    
    
    // Publish events. Wait for 5 second between publishes
    Temp = sensor.readTemperature();
    delay(500);
    PublishSensorInfo();
    blinkLed();   
    delay(5000);
  if (sensor.readTemperature() > 26.00){
      if (temp_broken==true){
      Particle.publish("May51992","TurnOn",60,PUBLIC);
      temp_broken=false;
      }
  }
      else{
          if (temp_broken==false){
              Particle.publish("May51992","TurnOff",60,PUBLIC);
              temp_broken=true;
          }
      }
      
  }



// Publish Temperature, Pressure sensor readings
void PublishSensorInfo(){
    Serial.print("Temperature = ");
    Serial.print(Temp);
    Serial.println(" *C");

    Serial.print("Pressure    = ");
    Serial.print(sensor.readPressure()/100.0);
    Serial.println(" hPa");

    char sensorInfo[64];
    sprintf(sensorInfo, "Temperature=%.2f °C, Pressure=%.2f hPa", sensor.readTemperature(), sensor.readPressure()/100.0);
    Particle.publish("PhotonTemp", sensorInfo);
}


// Blink onboard LED every time readings are taken and published
void blinkLed(){
    digitalWrite(D7, HIGH);   
    delay(500);
    digitalWrite(D7, LOW);   
    delay(500);
}

Motion Sensor Application and fan control

C/C++
// Sketch for Particle Photon - PIR Sensor / Motion Detection
// By Anton

int inputPin = D0; //2nd floor motion sensor input
int inputpin2 = D2;//3rd floor motion sensor input
int ledPin = D1;//2nd floor LEDs
int ledPin2 = D3;//3rd floor LEDs
int pirState = LOW;//initialized PIR state 2nd floor
int pirState1 = LOW;//initialized PIR state 3rd floor
int val = 0;// variable for reading the pin status 2nd floor
int val1 = 0;//variable for reading the pin status 3rd floor
int fan = D5;//pin for fan control (output)
int calibrateTime = 5000;      // wait for the thingy to calibrate

void setup() {
    Particle.subscribe("May51992",fanControl);
    pinMode(fan, OUTPUT);
    pinMode(ledPin2, OUTPUT);
    pinMode(ledPin, OUTPUT);
    pinMode(inputPin, INPUT);     // declare sensor as input
    pinMode(inputpin2, INPUT);
    digitalWrite(fan, LOW);
    pinMode(D7, OUTPUT);
}

//loop includes calibration, reading PIR sensors and reporting the data from PIR sensors
void loop() {
 // if the sensor is calibrated
  if (calibrated()) {
  
    // get the data from the 2nd floor sensor
    readTheSensor();
    //get the data from the 3rd floor sensor
    readTheSensor1();

    // report it out, if the state has changed (2nd floor)
    reportTheData();
    // report it out, if the state has changed (3rd floor)
    reportTheData1();
    }

}
//Read 2nd floor sensor
void readTheSensor() {
    val = digitalRead(inputPin);
}
//calibrate 2nd floor sensor
bool calibrated() {
    return millis() - calibrateTime > 0;
}

//setup for turning LED lights on and off
void setLED(int state) {
    digitalWrite(ledPin, state);
    
}

//reads data from 2nd floor sensor and determines if there was 
//motion by cutting on LED lights when motion occurs
void reportTheData() {
    if (val == HIGH) {
        // the current state is no motion
        // i.e. it's just changed
        // announce this change by publishing an event
        if (pirState == LOW) {
          // we have just turned on
          Particle.publish("PhotonMotion", "Third Floor Motion Detected", PRIVATE);
          // Update the current state
          pirState = HIGH;
          setLED(pirState);
          delay(5000);
        }
    } else {
        if (pirState == HIGH) {
          // we have just turned of
          // Update the current state
          Particle.publish("PhotonMotion", "Off", PRIVATE);
          pirState = LOW;
          setLED(pirState);
        }
   
    }
    }

//read 3rd floor sensor
void readTheSensor1() {
   val1 = digitalRead(inputpin2);
}
//calibrate 3rd floor sensor
bool calibrated1() {
    return millis() - calibrateTime > 0;
}
//setup for turning LED lights on and off
void setLED1(int state1) {
    digitalWrite(ledPin2, state1);
}

//reads data from 3rd floor sensor and determines if there was 
//motion by cutting on LED lights when motion occurs
void reportTheData1() {
    if (val1 == HIGH) {
        // the current state is no motion
        // i.e. it's just changed
        // announce this change by publishing an event
        if (pirState1 == LOW) {
          // we have just turned on
          Particle.publish("PhotonMotion", "Second Floor Motion Detected", PRIVATE);
          // Update the current state
          pirState1 = HIGH;
          setLED1(pirState1);
          delay(5000);
        }
    } else {
        if (pirState1 == HIGH) {
          // we have just turned of
          // Update the current state
          Particle.publish("PhotonMotion", "Off", PRIVATE);
          pirState1 = LOW;
          setLED1(pirState1);
        }
    }
}

//controls whether the fan turns on relative to temperature threshold
void fanControl(const char *event,const char *data)
{
    
    if (strcmp(data,"TurnOn")==0){
        digitalWrite(fan,HIGH);
        //digitalWrite(D7,1);
    }
    else if (strcmp(data,"TurnOff")==0){
        digitalWrite(fan,LOW);
    }
}

Credits

Erik Dumas

Erik Dumas

2 projects • 0 followers
Physics and Mechanical Engineering Student UNC Charlotte
Michael Rodriguez

Michael Rodriguez

1 project • 0 followers
UNCC engineering
Thanks to Sven Huijbrechts and Anton.

Comments