RoboCircuits
Published © CC BY

How to make a Perfect Home Automation System

Perfect Smart Switches

AdvancedFull instructions provided5 hours2,077
How to make a Perfect Home Automation System

Things used in this project

Story

Read more

Schematics

Schematics

Code

Perfect Home Automation

Arduino
// Designed by RoboCircuits

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial

/* Fill-in your Template ID (only if using Blynk.Cloud) */
//#define BLYNK_TEMPLATE_ID   "YourTemplateID"


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#include<EEPROM.h>
#define DHTPIN 14
#define DHTTYPE    DHT11     // DHT 11

DHT_Unified dht(DHTPIN, DHTTYPE);

void ICACHE_RAM_ATTR button1();
void ICACHE_RAM_ATTR button2();

uint32_t delayMS;
int relay1 = 12;
int relay2 = 13;
int in1 = 4;
int in2 = 5;
boolean buttonState1 = false, buttonState2 = false, prevState1, prevState2;

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "robocircuits";
char pass[] = "robocircuits";

void button1()
{
  delayMicroseconds(150000);
  if(digitalRead(in1)==HIGH)                                     
  {
    buttonState1 =! buttonState1;
    digitalWrite(relay1,buttonState1);
    delayMicroseconds(150000);
    EEPROM.write(101,buttonState1);
    EEPROM.commit();
  }
}

void button2()
{
  delayMicroseconds(150000);
  if(digitalRead(in2)==HIGH)                                 
  {
    buttonState2 =! buttonState2;
    digitalWrite(relay2,buttonState2);
    delayMicroseconds(150000);
    EEPROM.write(102,buttonState2);
    EEPROM.commit();
  }
}

void setup()
{
  Serial.begin(9600);
  Serial.println("Code designed by RoboCircuits")
  EEPROM.begin(512);
  pinMode(relay1,OUTPUT);
  pinMode(relay2,OUTPUT);
  pinMode( in1, INPUT_PULLUP );
  pinMode( in2, INPUT_PULLUP );
  buttonState1 = EEPROM.read(101);
  buttonState2 = EEPROM.read(102);
  digitalWrite(relay1,buttonState1);
  digitalWrite(relay2,buttonState2);

  attachInterrupt(digitalPinToInterrupt(in1), button1, RISING);
  attachInterrupt(digitalPinToInterrupt(in2), button2, RISING);
  delay(100);
  
  Blynk.begin(auth, ssid, pass);
 
  dht.begin();
  
  sensor_t sensor;
  dht.temperature().getSensor(&sensor);
  dht.humidity().getSensor(&sensor);
  
  // Set delay between sensor readings based on sensor details.
  delayMS = sensor.min_delay / 1000;
}

void loop()
{
  Blynk.run();
  delay(delayMS);
  
  // Get temperature event and print its value.
  sensors_event_t event;
  dht.temperature().getEvent(&event);
  if (isnan(event.temperature)) {
    Serial.println(F("Error reading temperature!"));
  }
  else {
    
  Blynk.virtualWrite(V1,event.temperature);
    Serial.print(F("Temperature: "));
    Serial.print(event.temperature);
    Serial.println(F("C"));
  }
  
  // Get humidity event and print its value.
  dht.humidity().getEvent(&event);
  if (isnan(event.relative_humidity)) {
    Serial.println(F("Error reading humidity!"));
  }
  else {
    Blynk.virtualWrite(V0,event.relative_humidity);
    Serial.print(F("Humidity: "));
    Serial.print(event.relative_humidity);
    Serial.println(F("%"));
  }
  if(buttonState1 != prevState1)
  {
    Blynk.virtualWrite(V2,buttonState1);
    prevState1 = buttonState1;
  }
  delay(10);
  if(buttonState2 != prevState2)
  {
    Blynk.virtualWrite(V3,buttonState2);
    prevState2 = buttonState2;
  }
}
BLYNK_WRITE(V3)
{
  int buttonState1 = param.asInt();
  digitalWrite(relay1,buttonState1);
}
BLYNK_WRITE(V2)
{
  int buttonState2 = param.asInt();
  digitalWrite(relay2,buttonState2);
}

Credits

RoboCircuits

RoboCircuits

37 projects • 218 followers
YouTuber, Explorer, Creator, Programmer, Arduino Lover and Engineer

Comments