Chathuranga Liyanage
Published © GPL3+

Plan your Holiday with Smart Power Planner

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

IntermediateFull instructions provided5 hours538

Things used in this project

Hardware components

Arduino MKR1000
Arduino MKR1000
×1
PIR Motion Sensor (generic)
PIR Motion Sensor (generic)
×1
RobotGeek Relay
RobotGeek Relay
×1
9V 1A Switching Wall Power Supply
9V 1A Switching Wall Power Supply
×1
SparkFun LCD Keypad Shield
×1
Slide Switch
Slide Switch
×1

Hand tools and fabrication machines

Drill Taps
OpenBuilds Drill Taps
Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

Schematic

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

Code

Code

Arduino
#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();
  }

}

Credits

Chathuranga Liyanage

Chathuranga Liyanage

10 projects • 56 followers
Entrepreneur | Founder - SRQ Robotics | Roboticist

Comments