Project tutorial

Plan your Holiday with Smart Power Planner © GPL3+

Heat up your house before getting home, Turn on holiday decors automatically when neighbors come and many more power plans for your home

  • 370 views
  • 0 comments
  • 6 respects

Components and supplies

Necessary tools and machines

Openbuilds m5 drill tap
OpenBuilds M5 Drill Tap
09507 01
Soldering iron (generic)

About this project

Introduction

Holiday times! It's the time we get a vacation to have fun and be relaxed. Everyone loves to make their holiday, an unforgettable one. But there will always be somethings that can bother you and spoil the holiday fun. As an example, while you are on your way to a party and suddenly remember that you forgot to turn off your heater at home. Or you may have wanted to turn on your heater for a moment before you getting home.

As always, we all want to make our holidays more interesting. It's Christmas and we want to make our Christmas tree, Door wreaths more attractive. By summing up all these things that we face during our holiday times, I decided to make a device that brings solutions to all the above-mentioned dilemmas.

Functionality and Applications

Mainly, this is a power controlling device that can be directly connected to the main current supply at your home. The electrical equipment which you wish to control is connected to this device.

This device includes 3 modes which can be controlled through WiFi.

  • Manual Mode - Power on/off manually by clicking the button
  • Human Detection Mode - Power on only when the presence of a human is detected
  • Timer Mode - Turn on the power depending on a countdown timer

Why these modes are important? Let's consider some practical scenarios.

  • You are on your way to a party and suddenly remember that you have forgotten to turn off your heater and it might cause a fire. By using this device, you can turn off the heater manually using your mobile phone's web browser without returning home. Further, you can turn on your house lights using this mode.
  • You made a Christmas wreath and some other decorations and you want to highlight them when someone arrives. You can use the human detection mode which will turn on the power when someone is around.
  • You need to turn off lights inside your house automatically when someone is not there. Here you can use the human detection mode which turns off your lights when you leave.
  • The weather is very cold and you need to heat up your house before you come. Here, timer mode can help you. You can setup a timer through the mobile phone for 15 minutes which will turn on the heater for 15 minutes. Further, this acts as a safety feature just in case you forget to turn off the heater.

Implementation

Basically this device is made using the components I have mentioned above.

You need to connect them together. Suppose you all have basic knowledge in these kind of stuff.

I have used 16X2 LCD shield with buttons to reduce the number of the wires.

I think you all can connect the modules together according the schematic I have uploaded. I didn't connect the Vcc and the Gnd pins to reduce complexity. Do not forget to connect them when you're actually making it.

Connecting modules is not a big issue. As we are going to work with the main power supply at home, safety should be our best priority.

Enclosure

To avoid any accidents, we must place all modules inside an enclosure. It's better if you can design an enclosure from acrylic sheets and cut them using a laser cutter. I found an old enclosure made by myself for another project.

To make space for the Power switch, LCD and PIR, I removed some parts from the original enclosure.

Placing Parts

Following images show the placements of the components.

Wiring

connect MKR1000 board to these components according to the schematic.

I have connected one of the 2 AC wires directly to the black connectors (See the enclosure). The remaining wire is connected to the middle connector of the relay. Remaining 2 connectors of the relay are connected to the red connectors of the enclosure as one for each.

A plug top has been connected to the other end of the AC wires.

How it Works

Before uploading the code to the MKR1000 board, set the ssid and the password according to your WiFi network.

Connect the electrical equipment to the red and black connectors. Turn on the device. After connecting to the WiFi network, the IP address will be displayed on the LCD screen.

Type the IP address in the mobile web browser. Now it will display the control page.

Select any of the modes as you desire and enjoy your vacation!

Schematics

Schematic
Only data pins are connected. You need to connect Vcc and ground pins.
diagram_LePOu13oqR.fzz

Code

CodeArduino
#include <LiquidCrystal.h>
#include <SPI.h>
#include <WiFi101.h>


#define pir1 7
#define relay 8

int but = 0;

LiquidCrystal lcd(11,12, 2,3,4,5);

const char* ssid = "######";
const char* password = "######";
WiFiServer server(80);

int mode = 0;
int hours=0;
int minutes = 0;
int seconds = 0;

void button(){
  but = analogRead(A0);
  
  if (but < 50) but = 4;
  else if (but <150) but = 3;
  else if (but <500) but = 2;
  else if (but <700) but = 1;
}

void manual(){
  lcd.print("Manual Mode");
  lcd.setCursor(0,1);
  lcd.print("Power OFF");

  while(1){
    button();
    if(but == 3 || manual == 1){
      lcd.setCursor(0,1);
      lcd.print("Power ON ");
      digitalWrite(relay,LOW);
      delay(500);
    }

    else if(but == 2){
      lcd.setCursor(0,1);
      lcd.print("Power OFF");
      digitalWrite(relay,HIGH);
      delay(500);
    }

    else if(but == 1 || pir==1 || timer==1){
      digitalWrite(relay,HIGH);
      mode++;
      but = 0;
      break;
    }
    
  }
}


void human_timer(){
  delay(500);
  while(1){
  button();
  int i;
  int on=0;
  int break_true = 0;

  if(but == 1 || manual == 1 || timer==1){
    break_true=1;
    mode++;
    but = 0;
    delay(500);
  }
  
  while(digitalRead(pir1)==HIGH){
     digitalWrite(relay,LOW);
     lcd.setCursor(0,1);
     lcd.print("   Detected  ");
     on=1;
      button();
     if(but == 1 || manual == 1 || timer==1){
      mode++;
      on=0;
      but = 0;
      break_true=1;
      break;
     }
     delay(5);
  }

if(on==1){
  lcd.setCursor(0,1);
  lcd.print("   On Timer");
  
  for (i=0;i<50;i++){
       delay(100);
       if(digitalRead(pir1)==HIGH){
          break;
        }
      else if(but == 1 || manual == 1 || timer==1){
      mode++;
      break_true=1;
      but = 0;
      digitalWrite(relay,HIGH);
      break; 
     }

     
  }
}
  if(break_true==1)
    break;
  if(i>48){
  digitalWrite(relay,HIGH);
  lcd.setCursor(0,1);
  lcd.print("Not Detected");
  }
}
}

void lcd_display(){
  lcd.setCursor(0,1);
    lcd.print(hours/10);
    lcd.setCursor(1,1);
    lcd.print(hours%10);
    lcd.setCursor(2,1);
    lcd.print(":");
    lcd.setCursor(3,1);
    lcd.print(minutes/10);
    lcd.setCursor(4,1);
    lcd.print(minutes%10);
    lcd.setCursor(5,1);
    lcd.print(":");
    lcd.setCursor(6,1);
    lcd.print(seconds/10);
    lcd.setCursor(7,1);
    lcd.print(seconds%10);
  
}

void timer_mode(){
  int time_on=0;
  button();
  while(but != 1){
    button();
    lcd_display();
    
    if(but == 3){
      while(but == 3){
        hours++;
        if(hours==100)
          hours=0;
        delay(250);
        button();  
    }
    }
    else if(but == 2){
      while(but == 2){
        
      hours--;
      if (hours<0)
          hours=99;
      delay(250);
      button();    
    }
    }
    lcd.setCursor(2,1);
    lcd.print(" ");
    delay(100);
  }

  delay(250);
  button();
  while(but != 1){
    
    lcd_display();
    button();
    if(but == 3){
      while(but == 3){
        button();
        minutes++;
        if(minutes==60)
          minutes=0;
        delay(250);
        button();  
    }
    }
    else if(but == 2){
      while(but == 2){
      button();  
      minutes--;
      if (minutes<0)
          minutes=59;
      delay(250);    
    }
    }
    lcd.setCursor(5,1);
    lcd.print(" ");
    delay(100);
  }

    delay(250);
  button();
  while(but != 1){
    button();
    lcd_display();
    
    if(but == 3){
      while(but == 3){
        
        seconds++;
        if(seconds==60)
          seconds=0;
        delay(250);
        button();  
    }
    }
    else if(but == 2){
      while(but == 2){
      button();  
      seconds--;
      if (seconds<0)
          seconds=59;
      delay(250);    
    }
    }
    lcd.setCursor(8,1);
    lcd.print(" ");
    delay(100);
  }
  delay(250);
  lcd_display();
  button();
  while(but!= 1){
    button();
    if(but == 4){                                 
      lcd.setCursor(10,1);
      lcd.print("ON ");
      digitalWrite(relay,LOW);
      time_on =1;
      delay(250);
      break;
    }
  }
  if(time_on==1){
      while(time_on==1){ 
                                  
        while(hours != 0 || minutes != 0 || seconds != 0){ 
          button();
          for(int i=0;i<10;i++){
            if(but == 4){
              button();
              digitalWrite(relay,HIGH);
              lcd.setCursor(10,1);
              lcd.print("OFF");
              time_on=0;
              break;
            }
            delay(100);
          }
          seconds--;
          if(seconds<0){
            minutes--;
            seconds=59;
          }
          if(minutes<0){
            hours--;
            minutes=59;
          }
          lcd.setCursor(0,1);
          lcd_display();
        }
        if(hours == 0 && minutes == 0 && seconds == 0){
            digitalWrite(relay,HIGH);
            lcd.setCursor(10,1);
            lcd.print("OFF");
            time_on=0;
        }
    }
  }
  button();
  while(1){
      button();
      if (but == 1){
        mode=0;
        delay(200);
        lcd.clear();
        break;
      }
      
  }
}


void setup() {
  
  lcd.begin(16, 2);
  pinMode(A0,INPUT);
  pinMode(pir,INPUT);
  pinMode(relay,OUTPUT);
  digitalWrite(relay,HIGH);

   WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    
  }

  server.begin();
 
  lcd.print(WiFi.localIP());
  delay(2000);
  
  
}
void loop() {

WiFiClient client = server.available();
  if (!client) {
    return;
  }
 
  // Wait until the client sends some data
  
  while(!client.available()){
    delay(1);
  }
 
  String request = client.readStringUntil('\r');
  
  client.flush();
 
 
  int manual = 0;
  int pir = 0;
  int timer = 0;
  
  if (request.indexOf("/POWER=ON") != -1)  {
    manual = 1;
    pir = 0;
    timer = 0;
  }
  if (request.indexOf("/POWER=OFF") != -1)  {
    manual = 0;
  }

  if (request.indexOf("/PIR=ON") != -1)  {
    manual = 0;
    pir = 1;
    timer = 0;
  }
   if (request.indexOf("/PIR=OFF") != -1)  {
    manual = 0;
    pir = 0;
    timer = 0;
  }

  /*Copy following function and change the request line and values of hours,     minutes, seconds variables according to your html button functions. */
  
   if (request.indexOf("/1min") != -1)  {
    minutes = 1;
    
    manual = 0;  //do not change these manual, pir, timer lines when coping
    pir = 0;
    timer = 1;
  }
  
  //-------------------------------------------------------------------------

  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println(""); //  do not forget this one
  client.println("<!DOCTYPE HTML>");
  client.println("<html>");
  client.print("<h2>Smart Power Controller</h2>");
  client.print("<h3>1) Manual Mode</h3>");
  client.print("Power Status : ");
 
  if(manual == 1) {
    client.print("On");
  } else if (manual == 0) {
    client.print("Off");
  }
  client.println("<br><br>");
  client.println("<a href=\"/POWER=ON\"\"><button>Turn On </button></a>");
  client.println("<a href=\"/POWER=OFF\"\"><button>Turn Off </button></a><br /><div></div>");  

  client.print("<h3>2) Human Detection Mode</h3>");
  client.print("Power Status : ");
  if(pir == 1) {
    client.print("On");
  } else if (pir == 0) {
    client.print("Off");
  }
  client.println("<br><br>");
  client.println("<a href=\"/PIR=ON\"\"><button>Turn On </button></a>");
  client.println("<a href=\"/PIR=OFF\"\"><button>Turn Off </button></a><br /><div></div>");  
  
  client.print("<h3>3) Timer Mode</h3>");
  client.print("Power Status : ");
  if(timer == 1) {
    client.print("On");
  } else if (timer == 0) {
    client.print("Off");
  }

/* Following code lines are for timmer buttons. You can change the code
   to setup time limits as you desire. Do not forget to change the above        timer function according to changes */
   
  client.println("<div><br><br>");
  client.println("<a href=\"/1min\"\"><button>1 Mint </button></a>");
  client.println("<a href=\"/5min\"\"><button>5 Mint </button></a>");
  client.println("<a href=\"/15min\"\"><button>15 Mint </button></a>");
  client.println("<a href=\"/30min\"\"><button>30 Mint </button></a>");
  client.println("<a href=\"/1hour\"\"><button>1 Hour </button></a></div>");

  
  client.println("<div><br><br>");
  client.println("<a href=\"/2hour\"\"><button>2 Hour </button></a>");
  client.println("<a href=\"/5hour\"\"><button>5 Hour </button></a>");
  client.println("<a href=\"/10hour\"\"><button>10 Hour </button></a>");
  client.println("<a href=\"/1day\"\"><button>1 Day </button></a>");
  client.println("<a href=\"/2day\"\"><button>2 Day </button></a></div>");
 
  client.println("</html>");
  
  if(mode == 0)
    manual();

  else if(mode==1){
    lcd.clear();
    lcd.print("Human Detection");
    human_timer();
    
  }

  else if(mode==2){
    lcd.clear();
    lcd.print("Timer Mode");
    lcd.setCursor(10,1);
    lcd.print("OFF");
    timer_mode();
  }

}

Comments

Add projectSign up / Login