Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
Adithya TG
Published © GPL3+

Alexa Based Smart Home Monitoring

You're at your Office and want to know about your home? Control your household devices and get alerts about home no matter wherever you are!

AdvancedFull instructions provided2 days75,557
Alexa Based Smart Home Monitoring

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
ESP8266 ESP-01
Espressif ESP8266 ESP-01
×1
DHT11 Temperature & Humidity Sensor (4 pins)
DHT11 Temperature & Humidity Sensor (4 pins)
×1
4-CHANNEL RELAY CONTROLLER FOR I2C
ControlEverything.com 4-CHANNEL RELAY CONTROLLER FOR I2C
×1
IR obstacle sensor
×1
camera
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE
Alexa Skills Kit
Amazon Alexa Alexa Skills Kit
Amazon Alexa echosim.io
ThingSpeak API
ThingSpeak API
Backendless
Pushingbox

Story

Read more

Schematics

Circuit for uploading the code to ESP8266

Circuit Setup

Code

Smart_bell_ESP8266.ino

Arduino
Code for ESP8266 to get alert about intruder and guest
/*
 *  This sketch is implementation of smart bell and home security
 *
 */

#include <ESP8266WiFi.h>
#include<String.h>

//------------------------------------------
//       variables
//------------------------------------------
int intruder=0,guest=2;                     // pin 0 as intruder alert and pin2 as guest alert
const char* ssid     = "SSID";            //your SSID of WIFI
const char* password = "password";       // password of Wifi
//--------------------------------------------

const char* host = "api.thingspeak.com";

void setup() {
  Serial.begin(115200);
  delay(10);

  // We start by connecting to a WiFi network

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  
  /* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default,
     would try to act as both a client and an access-point and could cause
     network-issues with your other WiFi-devices on your WiFi-network. */
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");  
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

int value = 0;
int x,y=0,z,k=0;
void loop() {
  delay(1000);
  ++value;

  Serial.print("connecting to ");
  Serial.println(host);
  
  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }

  Serial.print("Requesting URL: ");
 //============================================================================================================
 //                                        intruder alert
 //============================================================================================================
 x=digitalRead(intruder);
//Serial.println(x);
  if(x==0)
  {
    y=0;
  }
  if(x==1&&y==0)
  {
    Serial.println("Sending Alert Pls Wait.....");
    delay(1000);
  String host1="api.pushingbox.com";
    client.print(String("GET ")  + "/pushingbox?devid=<deviceID_intruderAlert> HTTP/1.1\r\n" +
               "Host: " + host1 + "\r\n" + 
               "Connection: close\r\n\r\n");                       // executing pushing box api
    client.print(String("GET ")  + "/update?api_key=<write_key_intruderAlert>&field1=1 HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" + 
               "Connection: close\r\n\r\n");                        //updating intruder notification
               
   
    delay(1000);
    y=1;
  } 

//============================================================================================================
//                                         End of intruder alert
//============================================================================================================
 //============================================================================================================
 //                                        guest alert
 //============================================================================================================
long t1;
 z=digitalRead(guest);
  if(z==0 && (millis()>=t1+300000)) // wait for 5 min to avoid multiple alert sending due to multiple door bell press
  {
    k=0;
  }
  if(x==1&&k==0)
  {
    t1=millis();         // storing curent time stamp in t1
    Serial.println("Sending Alert Pls Wait.....");
    delay(1000);                      //delay for sending alert and switch debouncing
  String host1="api.pushingbox.com";
    client.print(String("GET ")  + "/pushingbox?devid=<deviceID_guestAlert> HTTP/1.1\r\n" +
               "Host: " + host1 + "\r\n" + 
               "Connection: close\r\n\r\n");                       // executing pushing box api
    client.print(String("GET ")  + "/update?api_key=<write_key_guestAlert>&field1=1 HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" + 
               "Connection: close\r\n\r\n");                        //updating guest notification
               
    delay(1000);
    k=1;
  } 

//============================================================================================================
//                                         End of guest alert
//============================================================================================================

  unsigned long timeout = millis();
  while (client.available() == 0) {
    if (millis() - timeout > 5000) {
      Serial.println(">>> Client Timeout !");
      client.stop();
      return;
    }
  }
 //Serial.println("closing connection");
}

Home_monitor_arduino.ino

Arduino
Code for Arduino to control appliances
//This sketch in the implementation of home automation 
#include <SoftwareSerial.h>// import the serial library
#include<String.h>
//-----------------------------------------
//    pin definations
//-----------------------------------------
#define Light 2              // pin 2 to control light
#define fan 3                 // pin 3 to control fan
//------------------------------------------
//       variables
//------------------------------------------
char e;
int p=0,q=0;        //these variables prevent switching ON/OFF of already turned ON/OFF devices

SoftwareSerial home_monitor(10, 11); // RX, TX

char control_data; // the data transmitted over Bluetooth

void setup() {
  // put your setup code here, to run once:
  home_monitor.begin(115200);
  Serial.begin(9600);
  pinMode(13,OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  if (home_monitor.available()){
     control_data=home_monitor.read();   // getting control information from ESP8266 serially
     e=control_data;
     Serial.print(control_data);
 //============================================================================================================
 //                                        Device Control
 //============================================================================================================

 // Performing required operations based on the value of channel
    //-------------------------------------------------------------
    //                        LIGHT
    //-------------------------------------------------------------
    
    if(e=='0'&&p==0)         //Light ON            
    {
      digitalWrite(Light,LOW);
      p=1;
    }
     if(e=='1'&&p==1)        //Light off
    {
      digitalWrite(Light,HIGH);
      p=0;
    }
    
    //-------------------------------------------------------------
    //                        Heater
    //-------------------------------------------------------------
   
    if(e=='2'&&q==0)
    {
      digitalWrite(fan,HIGH);
      q=1;
    }
    if(e=='3'&&q==1)
    {
      digitalWrite(fan,LOW);
      q=0;
    }
  }
}

Home_Monitor_ESP8266.ino

Arduino
Code For ESP8266 to update humidity and temperature in thingspeak and also retrieve control information from thingspeak
/*
 * This sketch updates wheather information in thingspeak.com and also retrieve control information from thingspeak.com
 * It send the control information to arduino serially
 */

#include <ESP8266WiFi.h>
#include<String.h>
#include "DHT.h"        // including the library of DHT11 temperature and humidity sensor
#define DHTTYPE DHT11   // DHT 11

#define dht_dpin 2      // define GPIO2 as data input 
DHT dht(dht_dpin, DHTTYPE); 
//------------------------------------------
//       variables
//------------------------------------------
//dht DHT;
char c,e;                       // stores the bytes coming from the server

float temp,h;                   //stores float value of temp and humididty
String humidity,temperature,line;    //stores string value of temp and humididty
int co=0;                       //control the sending of Alert

const char* ssid     = "SSID";            //your SSID of WIFI
const char* password = "password";       // password of Wifi
//--------------------------------------------

const char* host = "api.thingspeak.com";

void setup() {
  dht.begin();
  Serial.begin(115200);
  delay(10);

  // We start by connecting to a WiFi network

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  
  /* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default,
     would try to act as both a client and an access-point and could cause
     network-issues with your other WiFi-devices on your WiFi-network. */
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");  
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

int value = 0;

void loop() {
  delay(1000);
  ++value;

  Serial.print("connecting to ");
  Serial.println(host);
  
  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }

  Serial.print("Requesting URL: ");
 //============================================================================================================
 //                                        Mini Weather Station
 //============================================================================================================
  h = dht.readHumidity();                    //reads humidity and temperature
  temp = dht.readTemperature();
  if(temp<50.0)
  {
    co=0;
  }
  if(temp>=50.0&&co==0)      // send alert if temperature is above 50 degrees and co is used to avoid multiple alert sending
  {
    Serial.println("Sending Alert please wait.......");
    delay(1000);
    String host1="api.pushingbox.com";
    client.print(String("GET ")  + "/pushingbox?devid=<deviceID_fireAlert> HTTP/1.1\r\n" +
               "Host: " + host1 + "\r\n" + 
               "Connection: close\r\n\r\n");           //pushingbox api to send alert 
    delay(1000);
     client.print(String("GET ")  + "/update?key=<write_key_fireAlert>&field1=1 HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" + 
               "Connection: close\r\n\r\n");           //updating thingspeak fire alert channel
    co=1;
  }
  humidity=String(h);
  temperature=String(temp); 
  Serial.println(h);
  Serial.println(temp);   
  client.print(String("GET ")  + "/update?key=<write_key_humidity>&field1="+humidity+" HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" + 
               "Connection: close\r\n\r\n");                                                         //update humidity in thingspeak channel
  client.print(String("GET ")  + "/update?key=<write_key_temperature>&field1="+temperature+" HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" + 
               "Connection: close\r\n\r\n");                                                         ////update temperature in thingspeak channel      

//============================================================================================================
//                                         End of Weather Station
//============================================================================================================
//============================================================================================================
//                                Sending Control info to arduino Serially
//============================================================================================================
  // This will send the request to the server
  client.print(String("GET ")  + "/channels/<channelID_control>/field/field1/last.html HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" + 
               "Connection: close\r\n\r\n");         // get information about device control
  unsigned long timeout = millis();
  while (client.available() == 0) {
    if (millis() - timeout > 5000) {
      Serial.println(">>> Client Timeout !");
      client.stop();
      return;
    }
  }
  
  // Read all the lines of the reply from server and print them to Serial
  while(client.available()){
    String line = client.readStringUntil('\r');
  }
  Serial.print(line);                         // received by arduino serially
 //Serial.println("closing connection");
}

Credits

Adithya TG

Adithya TG

3 projects • 65 followers
Passionate Electronics and communication engineering, IOT, MicroControllers.

Comments