Aaron Kow
Published © CERN-OHL

IoT Home Security Model

A model of home security system that utilized sensors and internet to monitor the security status in real-time.

IntermediateShowcase (no instructions)107,344
IoT Home Security Model

Things used in this project

Hardware components

Dollhouse Furniture Set
×1
Craft Plywood Sheets
×1
Plywood Sheet 64.5cm (L) x 61.5cm (W) x 0.8cm (H)
×1
Clear Cast Acrylic Sheet
×1
Arduino Yun
Arduino Yun
×1
Arduino Mega 2560
Arduino Mega 2560
×1
NFC Module for Arduino
×1
Ultrasonic Sensor
×1
Vibration Sensor
×1
DHT22 Temperature Sensor
DHT22 Temperature Sensor
×1
Water Sensor
×1
DFRobot Gas Sensor MQ-5
×1
DFRobot Current Sensor
×1
Positive Acting Presensitized PCB (15x30cm)
×1
Relay
×1
Buzzer
Buzzer
×1
Tactile Push Button
×1
5mm LED: White
×5
5 mm LED: Green
5 mm LED: Green
×1
5 mm LED: Red
5 mm LED: Red
×1

Software apps and online services

Arduino IDE
Arduino IDE
AWS IoT
Amazon Web Services AWS IoT
DipTrace
Apple Terminal
Sublime 2

Hand tools and fabrication machines

Multimeter
UV Light Exposure Machine UV Photosensitive Plate PCB Exposure Box
Soldering Gun
Wire Stripper
Drill Driver
Solder Sucker Desoldering Pump
Hot Air Heat Gun Blower
Epoxy Adhesive Liquid
Utility Knife
Mechanical Drafting Pencil
Measure Tape
Ruler

Story

Read more

Schematics

c. PCB Picture

Printed Circuit Board for IoT Home Security Model

Schematics Diagram

Schematics diagram designed for IoT Home Security Model.

Floor Plan

Floor plan designed for IoT Home Security Model.

Floor Plan

This is the floor plan designed for IoT Home Security Model.

PCB Picture

Printed Circuit Board for IoT Home Security Model.

Code

IoTHome-Yun

Arduino
Arduino Yun Source Code for IoT Home Security Model.
/*
  THIS SOURCE CODE ORIGINALLY INTENDED FOR USE IN UCSI UNIVERSITY ENGINEERING FINAL YEAR PURPOSES ONLY,
  NOW IS FULLY OPEN-SOURCE UNDER MIT LICENSE

  The MIT License (MIT)

  Copyright (c) 2015 AaronKow
  
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:
  
  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.
  
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.
*/

/*External library for temp + humid sensor (DHT22)*/
#include <DHT.h>

/*External library for Cloud Data*/
#include <Process.h>

/*External library enables communication between Arduino and OpenWrt-Yun*/
#include <Bridge.h>

/*External library for initiate the YunServer*/
#include <YunServer.h>

/*External library for managing the connection*/
#include <YunClient.h>

YunServer server; //enabling the the Yun to listen for connected clients

/*Current Sensor Configuration*/
const int numReadings = 5;
float readings[numReadings];  
int index = 0;                 
float total = 0;             
float average = 0;
float currentValue = 0;
float initVal = 0;

/*Ultrasonic Sensor Configuration*/
const int trigPin = 2;
const int echoPin = 3;
long duration, cm;

/*Gas Sensor Configuration*/
int gasValue;

/*TempHumid Sensor Configuration*/
const int tempPin = 5;
float t,h;                         //variables for temperature sensor
DHT dht(tempPin);                  //define temperature sensor configuration

/*Cloud Data Configuration*/
String value0, value1, value2, value3, value4, value5, value6, value7;  // For sensors values
String led1, led2, led3, led4;  // For LED values
String ipAddress = "192.168.0.105:3000";    // set your ip address here
String userid = "your-user-id-here";        // set your user id here

void setup() {
  /*Http Client Setup*/
  pinMode(8, OUTPUT);              // Living Room Lights
  pinMode(9, OUTPUT);              // Bedroom Light
  pinMode(10, OUTPUT);             // Bathroom Light
  pinMode(11, OUTPUT);             // Kitchen Light
  
  /*Current Sensor Setup*/
  pinMode(0, INPUT);  
  pinMode(13,OUTPUT);  // for transmit data to Mega
  for (int thisReading = 0; thisReading < numReadings; thisReading++){
    readings[thisReading] = 0;       
  }
  
  /*Ultrasonic Sensor Setup*/
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  
  /*Vibration Sensor Setup*/
  pinMode(A2, INPUT);
  pinMode(A3, OUTPUT);
  
  /*Water Sensor Setup*/
  pinMode(6, INPUT);
  
  /*Buzzer Pin Setup*/
  pinMode(7, OUTPUT);
  
  /*Initiate Setup*/
  Serial.begin(115200); //Set serial baud rate to 115200 bps
  Bridge.begin();  // Initialize the Bridge communication
  server.begin();  // enabling Yun to listen for connected clients
  server.noListenOnLocalhost();    // tells the server to begin listening for incoming connections  
}

void loop() {
  /*http action*/
  YunClient client = server.accept();
  if (client.connected()) {
    Serial.println("CLIENT CONNECTED!");
    process(client); // Process request
    client.stop(); // Close connection and free resources
  }
  
  clearCloudData();
  systemStatus();
  currentSensor();
  ultrasonicSensor();
  gasSensor();
  vibrationSensor();
  TempHumidSensor();
  waterSensor();
  cloudData();
  //delay(500);
}

void process(YunClient client) {
  String command = client.readStringUntil('/'); // read the command
  if (command == "digital") { // verify if command for digital
    digitalCommand(client);
  }
}

void digitalCommand(YunClient client) {
  int pin, value;
  pin = client.parseInt(); // Read pin number

  // If the next character is a '/' it means an URL preceived
  if (client.read() == '/') {
    value = client.parseInt(); // taking value from client
    digitalWrite(pin, value);  // proceed to changes on the selected pin
  } 
  else {
    value = digitalRead(pin); // read value if no changes made
  }

  // Send feedback to client
  client.print(F("Pin D"));
  client.print(pin);
  client.print(F(" set to "));
  client.println(value);
  Serial.println(value);

  // Update datastore key with the current pin value
  String key = "D";
  key += pin;
  Bridge.put(key, String(value));
}

void clearCloudData(void){
  value0 = value1 = value2 = value3 = value4 = value5 = value6 = value7="";
}

void systemStatus(void){
  if(digitalRead(12) == 1){
    value0 += 1;
  }
  else{
    value0 += 0;
  }
}

void currentSensor(){
  total= total - readings[index];
      if(digitalRead(12) == 1){
        readings[index] = analogRead(0);
        readings[index] = (readings[index]-512)*5/1024/0.04+2.85;   // calibrate your own current sensor here
        total= total + readings[index];       
        index = index + 1;                    
        if (index >= numReadings)              
          index = 0;                           
        average = total/numReadings;
        currentValue = average;
        if (currentValue<0){
          currentValue = 0.51;    // this to ensure current stay at 0.51A if current drop below 0A
        }
        value1 += currentValue;
      }
      else{
        currentValue = 0;
        value1 += 0;
      }
      
      Serial.println(currentValue);
      
      if (currentValue >1){ //TO BE ADJUST
        digitalWrite(13, HIGH);
        buzzer(true);
        cloudData();
      }
      else{
        digitalWrite(13, LOW);
        buzzer(false);
      }
      delay(20);
}

void ultrasonicSensor(){  //require to adjust
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  cm = (duration / 29 / 2)+3;  // to centimetres

  if (cm < 20){ //eliminate data fluctuation
    buzzer(true);
    delay(500);
  }
  else{
    buzzer(false);
  }
  
  Serial.print(cm);
  Serial.println(" cm");
  value2 += cm;
  delay(20);
}

void gasSensor(){
  gasValue=(analogRead(5) * 0.01); //Read Gas value from analog 0
  Serial.println(gasValue, DEC);  //Print the value to serial port
  value3 += (gasValue);
  if(gasValue<6){
    if(gasValue == 0){
      buzzer(false);
    }
    else{
      buzzer(true);
    }
  }
  else{
    buzzer(false);
  }
  delay(20);
}

void vibrationSensor(){
  if(digitalRead(12) == 1){
    digitalWrite(A3, !digitalRead(A2));
    if (digitalRead(A2) != digitalRead(A3)){
      buzzer(true);
      delay(500);
      Serial.println("Vibrated!"); //1
      value4 += 1;
    }
    else{
      buzzer(false);
      Serial.println("No Vibration..."); //0
      value4 += 0;
    }
  }
  else{
    //Serial.println("No Signal!");
    buzzer(false);
    value4 += 0;
  }
}

void TempHumidSensor(){
  // Wait a few seconds between measurements.
  delay(20); //2000 = 2 seconds

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  h = dht.readHumidity();
  // Read temperature as Celsius
  t = dht.readTemperature();
  Serial.print("Humidity: "); 
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: "); 
  Serial.print(t);
  Serial.print(" *C ");
  Serial.print("\n");
  value5 += t;
  value6 += h;
}

void waterSensor(){
  if (digitalRead(12) == HIGH){
    if (digitalRead(6) == LOW){
      buzzer(true);
      Serial.println("Water Status Warning!"); //1
      value7 += 0;
    }
    else{
      buzzer(false);
      Serial.println("Water Status OK"); //0
      value7 += 1;
    }
    delay(20);
  }
  else{
    //Serial.println("No Signal");
    value7 += 1;
  }
}

void buzzer(boolean sound){
  if(digitalRead(12) == 1){ //1 means security system is online
    if(sound){
      digitalWrite(7, HIGH);
      delay(100);
    }
    else{
      digitalWrite(7, LOW);
    }
  }
  else{  //disable any buzzer if system offline
    digitalWrite(7, LOW);
  }
}

void cloudData(void){
  if(value0 == "0")
  {
    value0 = "0";
    value1 = value2 = value3 = value4 = value5 = value6 = value7="";
  }

  led1 =  digitalRead(8);
  led2 =  digitalRead(9);
  led3 =  digitalRead(10);
  led4 =  digitalRead(11);
  
  Process p;
  p.runShellCommand("curl \"http://" + ipAddress + "/ledstatus?userid=" + userid + "&led1state=" + led1 + "&led2state=" + led2 + "&led3state=" + led3 + "&led4state=" + led4 + "\" -k");
  p.runShellCommand("curl \"http://" + ipAddress + "/sensordata?userid=" + userid + "&ultrasonic=" + value2 + "&current=" + value1 + "&vibration=" + value4 + "&water=" + value7 + "&gas=" + value3 + "&temp=" + value5 + "&humid=" + value6 + "&nfc=" + value0 +"\" -k");
  while(p.running()); 
  //delay(100);
}

IoTHome-Mega

Arduino
Arduino Mega 2560 Source Code for IoT Home Security Model.
/*
  THIS SOURCE CODE ORIGINALLY INTENDED FOR USE IN UCSI UNIVERSITY ENGINEERING FINAL YEAR PURPOSES ONLY,
  NOW IS FULLY OPEN-SOURCE UNDER MIT LICENSE

  The MIT License (MIT)

  Copyright (c) 2015 AaronKow
  
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:
  
  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.
  
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.
*/


//pin 3 to control relay power for Ultrasonic and gas sensor
//pin 4 to power vibration sensor
//pin 5 to power Temp + Humid sensor
//pin 6 to power Water sensor
//pin 7 to show NFC-System Status Red Light
//pin 8 to show NFC-System Status Green Light
//pin 12 to notify Yun about System Status
//pin 13 to receive current status from Yun

/* NFC Configuration source code from dfRobot Wiki, link: http://goo.gl/qfvi4e */
const unsigned char wake[16]={
  0x55, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};//wake up NFC module
const unsigned char firmware[9]={
  0x00, 0x00, 0xFF, 0x02, 0xFE, 0xD4, 0x02, 0x2A, 0x00};
const unsigned char tag[11]={
  0x00, 0x00, 0xFF, 0x04, 0xFC, 0xD4, 0x4A, 0x01, 0x00, 0xE1, 0x00};//detecting tag command
const unsigned char std_ACK[25] = {
  0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0x00, 0x00, 0xFF, 0x0C,
  0xF4, 0xD5, 0x4B, 0x01, 0x01, 0x00, 0x04, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00, 0x4b, 0x00};
unsigned char old_id[5];
const unsigned char samConfig[10]={
  0x00, 0x00, 0xFF, 0x03, 0xFD, 0xD4, 0x14, 0x01, 0x17, 0x00};
  
const unsigned char myCard[25]={
  /* Set your NFC here */
  // Example of my phone NFC below:
  // My phone NFC:   0 0 FF 0 FF 0 0 0 FF 11 EF D5 4B 1 1 0 4 25 91 5 4 3 2 1 79 
  0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0x00, 0x00, 0xFF, 0x11,
  0xEF, 0xD5, 0x4B, 0x01, 0x01, 0x00, 0x04, 0x25, 0x91, 0x05,
  0x04, 0x03, 0x02, 0x01, 0x79};
 
unsigned char receive_ACK[25];//Command receiving buffer
//int inByte = 0;               //incoming serial byte buffer
 
/*External include for NFC*/
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#define print1Byte(args) Serial3.write(args)
#define print1lnByte(args)  Serial3.write(args),Serial3.println()
#else
#include "WProgram.h"
#define print1Byte(args) Serial3.print(args,BYTE)
#define print1lnByte(args)  Serial3.println(args,BYTE)
#endif

void setup() {
  /*NFC Setup*/
  Serial3.begin(115200);    //open Serial3 with device
  //while (!Serial);
  wake_card();
  SAMConfig();
  delay(100);
  read_ACK(15);
  delay(100);
  display(15);
  Serial.println("Read Firmware");
  firmware_version();
  delay(100);
  read_ACK(19);
  delay(100);
  display(19);
  
  /*Data Transmission to Yun Setup*/
  pinMode(12, OUTPUT);  //Tell Yun where system Logged in or out
  digitalWrite(12, HIGH);
  
  /*System Status Setup*/
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  digitalWrite(7, LOW);
  digitalWrite(8, HIGH);
  
  /*General Setup*/
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(13, INPUT);
  digitalWrite(3, LOW);  //to control relay
  digitalWrite(4, HIGH);
  digitalWrite(5, HIGH);
  digitalWrite(6, HIGH);
  Serial.begin(115200);   // open serial with PC
}

void loop() {
  //Serial.println(digitalRead(13));
  checkStatus();
  nfc();
}

void nfc(){
  send_tag();
  delay(100); 
  read_ACK(25);
  delay(100);
  Serial.println("Display Tag:");
  display (25);
  checkID();
  delay(200);
  //delay(2500);
  //copy_id ();
}

void checkID(void){
  int j;
  for (j=0; j<25; j++){
    if (receive_ACK[j] == myCard[j]){
      //proceed for validation
    }
    
    else{
      Serial.println("Scanning for Card...");
      break;
    }
  }
  if (j==25){
    Serial.println("Card Recognized!");
    digitalWrite(12, !digitalRead(12));
    digitalWrite(7, !digitalRead(7));
    digitalWrite(8, !digitalRead(8));
    if(digitalRead(12) == HIGH){
      Serial.println("System Online");  //Security system offline
    }
    else{
      Serial.println("System Offline");  
    }
  }
}
 
/*void copy_id (void) 
{//save old id
  int ai, oi;
  for (oi=0, ai=19; oi<5; oi++,ai++) {
    old_id[oi] = receive_ACK[ai];
  }
}*/
 
  
char cmp_id (void) 
{//return true if find id is old
  int ai, oi;
  for (oi=0,ai=19; oi<5; oi++,ai++) {
    if (old_id[oi] != receive_ACK[ai])
      return 0;
  }
  return 1;
}
 
 
int test_ACK (void) 
{// return true if receive_ACK accord with std_ACK
  int i;
  for (i=0; i<19; i++) {
    if (receive_ACK[i] != std_ACK[i])
      return 0;
  }
  return 1;
}
 
 
void send_id (void) 
{//send id to PC
  int i;
  Serial.print ("ID: ");
  for (i=19; i<= 23; i++) {
    Serial.print (receive_ACK[i], HEX);
    Serial.print (" ");
  }
  Serial.println ();
}
 
 
void UART1_Send_Byte(unsigned char command_data)
{//send byte to device
  print1Byte(command_data);
#if defined(ARDUINO) && ARDUINO >= 100
  Serial3.flush();// complete the transmission of outgoing serial data 
#endif
} 
 
 
void UART_Send_Byte(unsigned char command_data)
{//send byte to PC
  Serial.print(command_data,HEX);
  Serial.print(" ");
} 
 
 
void read_ACK(unsigned char temp)
{//read ACK into reveive_ACK[]
  unsigned char i;
  for(i=0;i<temp;i++) {
    receive_ACK[i]= Serial3.read();
  }
}
 
 
void wake_card(void)
{//send wake[] to device
  unsigned char i;
  for(i=0;i<16;i++){ //send command
    UART1_Send_Byte(wake[i]);
    //Serial.print(wake[i], HEX);
  }
}
 
 
void firmware_version(void)
{//send fireware[] to device
  unsigned char i;
  for(i=0;i<9;i++) //send command
    UART1_Send_Byte(firmware[i]);
}
 
 
void send_tag(void)
{//send tag[] to device
  unsigned char i;
  for(i=0;i<11;i++) //send command
    UART1_Send_Byte(tag[i]);
}
 
 
void display(unsigned char tem)
{//send receive_ACK[] to PC
  unsigned char i;
  for(i=0;i<tem;i++) //send command
    UART_Send_Byte(receive_ACK[i]);
  Serial.println();
}

void SAMConfig(void)
{
  unsigned char i;
  for(i=0;i<10;i++){ //send command
    UART1_Send_Byte(samConfig[i]);
  }
}

void checkStatus(void){
  if(digitalRead(12) == 0){ //system offline
    digitalWrite(3, HIGH);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
  }
  else{  //system online
    if(digitalRead(13) == 1){ //check current status
      digitalWrite(3, HIGH);
      digitalWrite(4, LOW);
      digitalWrite(5, LOW);
      digitalWrite(6, LOW);
    }
    
    else{ //system back online
      digitalWrite(3, LOW);
      digitalWrite(4, HIGH);
      digitalWrite(5, HIGH);
      digitalWrite(6, HIGH);
    }
  }
}

Modified DHT.cpp

C/C++
Modified DHT.cpp for IoT Home Security Model
/* 
  DHT library

  MIT license
  written by Adafruit Industries

  Modified by Aaron Kow for IoT Home Security Model
*/

#include "DHT.h"

DHT::DHT(uint8_t pin, uint8_t count) {
  _pin = pin;
  _count = count;
  firstreading = true;
}

void DHT::begin(void) {
  // set up the pins!
  pinMode(_pin, INPUT);
  digitalWrite(_pin, HIGH);
  _lastreadtime = 0;
}

//boolean S == Scale.  True == Farenheit; False == Celcius
float DHT::readTemperature(void) {
  float f;

  if (read()) {
      f = data[2] & 0x7F;
      f *= 256;
      f += data[3];		//>>>>>>>> improvement required<<<<<<<<
      f /= 10;
      if (data[2] & 0x80){ //negative-checker, if the and equate to 1 is true
	     f *= -1;
      }
      return f;
  }

  else{
  	return NAN;
  }
}

float DHT::readHumidity(void) {
  float f;
  if (read()) {
      f = data[0];
      f *= 256;
      f += data[1];	//>>>>>>>> improvement required<<<<<<<<
      f /= 10;
      return f;
  }

  else{
    return NAN;
  }
}

boolean DHT::read(void) {
  uint8_t laststate = HIGH;
  uint8_t counter = 0;
  uint8_t j = 0, i;
  unsigned long currenttime;

  // Check if sensor was read less than two seconds ago and return early
  // to use last reading.
  currenttime = millis();
  if (currenttime < _lastreadtime) {	//reset the last read time
    _lastreadtime = 0;
  }

  if (!firstreading && ((currenttime - _lastreadtime) < 2000)) {
    return true; // return last correct measurement
    //delay(2000 - (currenttime - _lastreadtime));
  }

  firstreading = false;
  /*
    Serial.print("Currtime: "); Serial.print(currenttime);
    Serial.print(" Lasttime: "); Serial.print(_lastreadtime);
  */
  _lastreadtime = millis();

  data[0] = data[1] = data[2] = data[3] = data[4] = 0;	//clear data
  
  // pull the pin high and wait 250 milliseconds
  digitalWrite(_pin, HIGH);
  //original delay(250);

  // now pull it low for ~20 milliseconds
  pinMode(_pin, OUTPUT);
  digitalWrite(_pin, LOW);
  delay(20);
  noInterrupts();			//disable interrupt for time-sensitive code
  digitalWrite(_pin, HIGH);
  delayMicroseconds(40);
  pinMode(_pin, INPUT);

  // read in timings
  for ( i=0; i< MAXTIMINGS; i++) {	//MAXTIMINGS = 85
    counter = 0;
    
  /*calibrated:response to the sensor,
    break once pin turns high.*/	
    while (digitalRead(_pin) == laststate) {	//intially laststate = high
      counter++;		
      delayMicroseconds(1);
      if (counter == 255) {	//255 is the maximum timeout
        break;
      }
    }
    laststate = digitalRead(_pin);

    if (counter == 255) break;	//if timeout, break directly

    /* 
	Condition 1: ignore first 3 transitions
	Condition 2: accept only figure divide by 2
    */
    if ((i >= 4) && (i%2 == 0)) {
      // shove each bit into the storage bytes
      data[j/8] <<= 1;	//divide by eight to ensure whole number, 0-4
      if (counter > _count) //_count here represent 60us
        data[j/8] |= 1;
      j++;
    }
  }

  interrupts();

  // check we read 40 bits and that the checksum matches
  if ((j >= 40) && 
      (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) ) {
    return true;
  }
  else{
    return false;    
  }
}

Modified DHT.h

C/C++
Modified DHT.h for IoT Home Security Model
/* 
  DHT library

  MIT license
  written by Adafruit Industries

  Modified by Aaron Kow for IoT Home Security Model
*/

#ifndef DHT_H
#define DHT_H
#if ARDUINO >= 100

#include "Arduino.h"
#else
 #include "WProgram.h"
#endif

#define MAXTIMINGS 85

class DHT {
 private:
  uint8_t data[6];
  uint8_t _pin, _count;
  unsigned long _lastreadtime;
  boolean firstreading;

 public:
  DHT(uint8_t pin, uint8_t count=6);
  void begin(void);
  float readTemperature(void);
  float readHumidity(void);
  boolean read(void);
};
#endif

IoT-Home-Security-Model-HW

Source code repository for IoT Home Security Model Harewares

AWS-IoT-Home-Security-Model-HW

Source code repository for AWS IoT Home Security Model Hardwares

AWS-IoT-Home-Security-Model-SW [Not Longer Maintain]

Source codes for IoT Home Security Model Dashboard using AWS IoT

Credits

Aaron Kow

Aaron Kow

3 projects • 117 followers
No Hardware, No Life @_aaronkow

Comments