Home Automation System Based On IOT V2




//PROGRAM ARDUINO 
#include <Wire.h>
#include <SoftwareSerial.h>
SoftwareSerial esp(2,3);

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16,2);

//-------------------------remort iot
#define RELAY1 8
#define RELAY2 9


char toggleState_1 = 'a'; //Define integer to remember the toggle state for switch 1
char toggleState_2 = 'b'; //Define integer to remember the toggle state for switch 2


String pinStatus = "ab";

void sendStatus(){
 Serial.println(pinStatus);
  //true;
}

void relayOnOff(int relay){

    switch(relay){
      case 1: 
             if(toggleState_1 == 'a'){
              digitalWrite(RELAY2, HIGH); // turn on relay 1
              toggleState_1 = 'A';
              }
             else{
              digitalWrite(RELAY2, LOW); // turn off relay 1
              toggleState_1 = 'a';
              }
      break;
      //----------------------------------------------------
      case 2: 
             if(toggleState_2 == 'b'){
             digitalWrite(RELAY2, HIGH); // turn on relay 1
              toggleState_2 = 'B';
              }
             else{
               digitalWrite(RELAY2, LOW); // turn off relay 1
              toggleState_2 = 'b';
              }
      break;
//----------------------------------------------------
      case 3: 
             if(toggleState_3 == 'c'){
             digitalWrite(RELAY2, HIGH); // turn on relay 1
              toggleState_3 = 'C';
              }
             else{
               digitalWrite(RELAY2, LOW); // turn off relay 1
              toggleState_3 = 'c';
              }
      break;
      //----------------------------------------------------
      case 4: 
             if(toggleState_4 == 'd'){
             digitalWrite(RELAY4, HIGH); // turn on relay 1
              toggleState_4 = 'D';
              }
             else{
               digitalWrite(RELAY4, LOW); // turn off relay 1
              toggleState_4 = 'd';
              }
      break;


      //----------------------------------------------------
      case 5: 
             if(toggleState_5 == 'e'){
             digitalWrite(RELAY5, HIGH); // turn on relay 1
              toggleState_5 = 'E';
              }
             else{
               digitalWrite(RELAY5, LOW); // turn off relay 1
              toggleState_5 = 'e';
              }
      break;
//----------------------------------------------------
      case 6: 
             if(toggleState_6 == 'f'){
             digitalWrite(RELAY6, HIGH); // turn on relay 1
              toggleState_6= 'F';
              }
             else{
               digitalWrite(RELAY6, LOW); // turn off relay 1
              toggleState_6 = 'f';
              }
      break;

      default : break;      
      } 
      delay(100);
}

#include <arduino-timer.h>
auto timer = timer_create_default(); // create a timer with default settings
long int data; 


//--------------------------------------------------------------------------------
const int wifiled = 13;


char buff[10];
String senddata; // complete message
//------------------------------------------CT



void setup()
{
  Serial.begin(9600);
  esp.begin(9600);
 
 pinMode(RELAY1, OUTPUT);
 pinMode(RELAY2, OUTPUT);

 
digitalWrite(RELAY1, HIGH);
digitalWrite(RELAY2, HIGH);

 lcd.begin();
// Turn on the blacklight and print a message.
 lcd.backlight();
 lcd.clear();
 lcd.setCursor(0,0);            // First line
 lcd.print("  Home Automation    ");
 lcd.setCursor(0,1);            // move to the begining of the second line 
 lcd.print("        v2     ");
 
 delay(3000);
 lcd.clear();
 
 pinMode(wifiled,     OUTPUT);
 digitalWrite(wifiled, HIGH);
 


//WiFi Control
  if(esp.available()>0)
   {         
      char c_val= esp.read(); // reading the data received from the bluetooth module
      switch(c_val)
       {
        case 'A': digitalWrite(RELAY1,  LOW); 
        toggleState_1 = 'A'; break; // when A is pressed on the app Turn on Pin 12
       
        case 'a': digitalWrite(RELAY1, HIGH); 
        toggleState_1 = 'a'; break; // when a is pressed on the app Turn off Pin 12
        //---------------------------------------------------------------------------------- 
        
        case 'B': digitalWrite(RELAY2,  LOW); 
        toggleState_2 = 'B'; break; // when B is pressed on the app Turn on Pin 11
         
        case 'b': digitalWrite(RELAY2, HIGH);   
        toggleState_2 = 'b'; break; // when b is pressed on the app Turn off Pin 11*/
       //---------------------------------------------------------------------------------- 
     
      
      }
      
    delay(200);
    pinStatus = String(toggleState_1) + String(toggleState_2)   ;
  }
 
timer.tick(); // tick the timer
}

//ESP PROGRAM
//https://blynk.cloud/dashboard/register

//6/6/2023

//############## Template ID from Blynk Dash Board ##########################

#define BLYNK_TEMPLATE_ID " xxxx"
#define BLYNK_TEMPLATE_NAME "xxxx"
#define BLYNK_AUTH_TOKEN "xxxxxx"
//----------------------------------------------------------------------------
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SoftwareSerial.h>
char auth[] = BLYNK_AUTH_TOKEN;

int pinState = 0; 

#define VPIN_BUTTON_1    V2 
#define VPIN_BUTTON_2    V3

//---------------------------------------------------------

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "xxxx"; //  set you wifi hostport
char pass[] = "xxxx";   //  set you password hostport
//---------------------------------------------------------

bool connectedFlag = false;
String pinStatus ;

// When App button is pushed - switch the state
BLYNK_WRITE(VPIN_BUTTON_1) {  
  pinState = param.asInt();
  if(pinState == 1){
    Serial.write('A');
  }
  else if(pinState == 0){
    Serial.write('a');
  }   
}

BLYNK_WRITE(VPIN_BUTTON_2) {  
  pinState = param.asInt();
  if(pinState == 1){
    Serial.write('B');
  }
  else if(pinState == 0){
    Serial.write('b');
  }  
}

void setup()
{
  // Debug console
  Serial.begin(9600);
 Blynk.begin(auth, ssid, pass);
}

void loop()
{
  while(Serial.available()) 
  {
    pinStatus = Serial.readString();
    updateState(pinStatus);
    connectedFlag = true;
  }
  if (connectedFlag){  
    Blynk.run();
  }
}

Tiada ulasan:

Catat Ulasan