Internet, Bluetooth & Manual control homeautomation with realtime feedback using ESP32.

Internet, Bluetooth & Manual control homeautomation with realtime feedback using ESP32.

In this homeautomation system we can control our homeappliances via bluetooth , wifi and from manual switches,

 if the esp32 is connected with the internet the onboard leds will turn on,

then we can control our homeappliances with the blynk mobile application, and also from the manaul switches and we can also monitor the real time status in the blynk app.

Now I am going to turn off the WIFI and show you how it will work without the internet,.

Here you can see the leds were automatically turned off.,

Now we can able to control it with the bluetooth using an app and also from manaul switches.

Components Requried.

Designing the PCB

To design the circuit and PCB, we used EasyEDA which is a cloud based software to design PCBs.

Designing the circuit works like in any other circuit software tool, you place some components and you wire them together. Then, you assign each component to a footprint.

Having the parts assigned, place each component. When you’re happy with the layout, make all the connections and route your PCB.

Save your project and export the Gerber files.

Ordering the PCBs at PCBWay

This project is sponsored by PCBWay. PCBWay is a full feature Printed Circuit Board manufacturing service.

Turn your DIY breadboard circuits into professional PCBs – get 10 boards for approximately $5 + shipping (which will vary depending on your country).

Once you have your Gerber files, you can order the PCB. Follow the next steps.

1. Download the Gerber files – click here to download the .zip file.

2. Go to PCBWay website and open the PCB Instant Quote page. 

3. PCBWay can grab all the PCB details and automatically fills them for you. Use the “Quick-order PCB (Autofill parameters)”.

4. Press the “+ Add Gerber file” button to upload the provided Gerber files.

And that’s it. You can also use the Online Gerber Viewer to check if your PCB is looking as it should.

Now select the shipping method , the one you prefer and has cost efficient.

You can increase your PCB order quantity and change the solder mask color. I’ve ordered the Black color.

Once you’re ready, you can order the PCBs by clicking “Save to Cart” and complete your order.

After approximately one week using the DHL shipping method, I received the PCBs at my office.

As usual, everything comes well packed, and the PCBs are really high-quality. 

The letters on the silkscreen are really well-printed and easy to read. Additionally, the solder sticks easily to the pads.


Blynk Application.

Blynk mobile application is available for both Android as well as for IOS users.

v

Bluetooth Application.

CODE

#include <BlynkSimpleEsp32.h>
#include "BluetoothSerial.h" 
#include <AceButton.h>
using namespace ace_button;


#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run
`make menuconfig` to and enable it
#endif

BluetoothSerial SerialBT;

BlynkTimer timer;

// define the GPIO connected with Relays and switches
#define RelayPin1 15  //D23
#define RelayPin2 2  //D22
#define RelayPin3 4  //D21
#define RelayPin4 22  //D19


#define SwitchPin1 32  //D13
#define SwitchPin2 35  //D12
#define SwitchPin3 34  //D14
#define SwitchPin4 39  //D27


#define wifiLed1    25
#define wifiLed2    26
#define wifiLed3    27

#define VPIN_BUTTON_1    V1 
#define VPIN_BUTTON_2    V2
#define VPIN_BUTTON_3    V3 
#define VPIN_BUTTON_4    V4


int toggleState_1 = 1; //Define integer to remember the toggle state for relay 1
int toggleState_2 = 1; //Define integer to remember the toggle state for relay 2
int toggleState_3 = 1; //Define integer to remember the toggle state for relay 3
int toggleState_4 = 1; //Define integer to remember the toggle state for relay 4


int wifiFlag = 0;
char bt_data; // variable for storing bluetooth data 

ButtonConfig config1;
AceButton button1(&config1);
ButtonConfig config2;
AceButton button2(&config2);
ButtonConfig config3;
AceButton button3(&config3);
ButtonConfig config4;
AceButton button4(&config4);


void handleEvent1(AceButton*, uint8_t, uint8_t);
void handleEvent2(AceButton*, uint8_t, uint8_t);
void handleEvent3(AceButton*, uint8_t, uint8_t);
void handleEvent4(AceButton*, uint8_t, uint8_t);


  
#define WIFI_SSID "XXXXXXXXXXXXXXXXX"             //Enter Wifi Name
#define WIFI_PASS "XXXXXXXXXXXXX"           //Enter wifi Password

#define AUTH "XXXXXXXXXXXXXXXXXXXXXXX"                 // You should get Auth Token in the Blynk App.

BLYNK_WRITE(VPIN_BUTTON_1) {
  toggleState_1 = param.asInt();
  digitalWrite(RelayPin1, toggleState_1);
}

BLYNK_WRITE(VPIN_BUTTON_2) {
  toggleState_2 = param.asInt();
  digitalWrite(RelayPin2, toggleState_2);
}

BLYNK_WRITE(VPIN_BUTTON_3) {
  toggleState_3 = param.asInt();
  digitalWrite(RelayPin3, toggleState_3);
}

BLYNK_WRITE(VPIN_BUTTON_4) {
  toggleState_4 = param.asInt();
  digitalWrite(RelayPin4, toggleState_4);
}


BLYNK_CONNECTED() {
  // Request the latest state from the server
  Blynk.syncVirtual(VPIN_BUTTON_1);
  Blynk.syncVirtual(VPIN_BUTTON_2);
  Blynk.syncVirtual(VPIN_BUTTON_3);
  Blynk.syncVirtual(VPIN_BUTTON_4);
 
}


void all_Switch_ON(){
  digitalWrite(RelayPin1, LOW); toggleState_1 = 0; delay(100);
  digitalWrite(RelayPin2, LOW); toggleState_2 = 0; delay(100);
  digitalWrite(RelayPin3, LOW); toggleState_3 = 0; delay(100);
  digitalWrite(RelayPin4, LOW); toggleState_4 = 0; delay(100);

}

void all_Switch_OFF(){
  digitalWrite(RelayPin1, HIGH); toggleState_1 = 1; delay(100);
  digitalWrite(RelayPin2, HIGH); toggleState_2 = 1; delay(100);
  digitalWrite(RelayPin3, HIGH); toggleState_3 = 1; delay(100);
  digitalWrite(RelayPin4, HIGH); toggleState_4 = 1; delay(100);

}

void Bluetooth_handle()
{
  bt_data = SerialBT.read();
//  Serial.println(bt_data);
  delay(20);

  switch(bt_data)
      {
        case 'w': digitalWrite(RelayPin1, LOW);  toggleState_1 = 0; break; // if 'A' received Turn on Relay1
        case 'W': digitalWrite(RelayPin1, HIGH); toggleState_1 = 1; break; // if 'a' received Turn off Relay1
        case 'x': digitalWrite(RelayPin2, LOW);  toggleState_2 = 0; break; // if 'B' received Turn on Relay2
        case 'X': digitalWrite(RelayPin2, HIGH); toggleState_2 = 1; break; // if 'b' received Turn off Relay2
        case 'y': digitalWrite(RelayPin3, LOW);  toggleState_3 = 0; break; // if 'C' received Turn on Relay3
        case 'Y': digitalWrite(RelayPin3, HIGH); toggleState_3 = 1; break; // if 'c' received Turn off Relay3
        case 'z': digitalWrite(RelayPin4, LOW);  toggleState_4 = 0; break; // if 'D' received Turn on Relay4
        case 'Z': digitalWrite(RelayPin4, HIGH); toggleState_4 = 1; break; // if 'd' received Turn off Relay4
       
        case 'a': all_Switch_ON(); break;  // if 'Z' received Turn on all Relays
        case 'A': all_Switch_OFF(); break; // if 'z' received Turn off all Relays
        default : break;
      }
}

void checkBlynkStatus() { // called every 3 seconds by SimpleTimer

  bool isconnected = Blynk.connected();
  if (isconnected == false) {
    wifiFlag = 1;
    digitalWrite(wifiLed1, LOW);
    digitalWrite(wifiLed2, LOW);
    digitalWrite(wifiLed3, LOW);//Turn off WiFi LED
  }
  if (isconnected == true) {
    wifiFlag = 0;
    digitalWrite(wifiLed1, HIGH);
    digitalWrite(wifiLed2, HIGH);
    digitalWrite(wifiLed3, HIGH);//Turn on WiFi LED
  }
}

void setup()
{
  Serial.begin(9600);

  btStart();  //Serial.println("Bluetooth On");
  
  SerialBT.begin("HA_BT_ESP32"); //Bluetooth device name
  Serial.println("The device started, now you can pair it with bluetooth!");
  delay(5000);

  pinMode(RelayPin1, OUTPUT);
  pinMode(RelayPin2, OUTPUT);
  pinMode(RelayPin3, OUTPUT);
  pinMode(RelayPin4, OUTPUT);


  pinMode(wifiLed1, OUTPUT);
  pinMode(wifiLed2, OUTPUT);
  pinMode(wifiLed3, OUTPUT);

  pinMode(SwitchPin1, INPUT_PULLUP);
  pinMode(SwitchPin2, INPUT_PULLUP);
  pinMode(SwitchPin3, INPUT_PULLUP);
  pinMode(SwitchPin4, INPUT_PULLUP);


  //During Starting all Relays should TURN OFF
  digitalWrite(RelayPin1, toggleState_1);
  digitalWrite(RelayPin2, toggleState_2);
  digitalWrite(RelayPin3, toggleState_3);
  digitalWrite(RelayPin4, toggleState_4);


  config1.setEventHandler(button1Handler);
  config2.setEventHandler(button2Handler);
  config3.setEventHandler(button3Handler);
  config4.setEventHandler(button4Handler);


  button1.init(SwitchPin1);
  button2.init(SwitchPin2);
  button3.init(SwitchPin3);
  button4.init(SwitchPin4);


  WiFi.begin(WIFI_SSID, WIFI_PASS);
  timer.setInterval(3000L, checkBlynkStatus); // check if Blynk server is connected every 3 seconds
  Blynk.config(AUTH);
  delay(2000);
}

void loop()
{  
  if (WiFi.status() != WL_CONNECTED)
  {
   // Serial.println("WiFi Not Connected");
  }
  else
  {
    //Serial.println("WiFi Connected");
    Blynk.run();
  }

  timer.run(); // Initiates SimpleTimer
  if (wifiFlag == 0){
    }
  else{
    if (SerialBT.available()){
      Bluetooth_handle();
    }
  } 

  button1.check();
  button2.check();
  button3.check();
  button4.check();
 
}

void button1Handler(AceButton* button, uint8_t eventType, uint8_t buttonState) {
  Serial.println("EVENT1");
  switch (eventType) {
    case AceButton::kEventPressed:
      Serial.println("kEventPressed");
      toggleState_1 = 0;
      digitalWrite(RelayPin1, LOW);
      Blynk.virtualWrite(VPIN_BUTTON_1, toggleState_1);   // Update Button Widget  
      break;
    case AceButton::kEventReleased:
      Serial.println("kEventReleased");
      toggleState_1 = 1;
      digitalWrite(RelayPin1, HIGH);
      Blynk.virtualWrite(VPIN_BUTTON_1, toggleState_1);   // Update Button Widget  
      break;
  }
}

void button2Handler(AceButton* button, uint8_t eventType, uint8_t buttonState) {
  Serial.println("EVENT2");
  switch (eventType) {
    case AceButton::kEventPressed:
      Serial.println("kEventPressed");
      toggleState_2 = 0;
      digitalWrite(RelayPin2, LOW);
      Blynk.virtualWrite(VPIN_BUTTON_2, toggleState_2);   // Update Button Widget  
      break;
    case AceButton::kEventReleased:
      Serial.println("kEventReleased");
      toggleState_2 = 1;
      digitalWrite(RelayPin2, HIGH);
      Blynk.virtualWrite(VPIN_BUTTON_2, toggleState_2);   // Update Button Widget  
      break;
  }
}

void button3Handler(AceButton* button, uint8_t eventType, uint8_t buttonState) {
  Serial.println("EVENT3");
  switch (eventType) {
    case AceButton::kEventPressed:
      Serial.println("kEventPressed");
      toggleState_3 = 0;
      digitalWrite(RelayPin3, LOW);
      Blynk.virtualWrite(VPIN_BUTTON_3, toggleState_3);   // Update Button Widget  
      break;
    case AceButton::kEventReleased:
      Serial.println("kEventReleased");
      toggleState_3 = 1;
      digitalWrite(RelayPin3, HIGH);
      Blynk.virtualWrite(VPIN_BUTTON_3, toggleState_3);   // Update Button Widget  
      break;
  }
}

void button4Handler(AceButton* button, uint8_t eventType, uint8_t buttonState) {
  Serial.println("EVENT4");
  switch (eventType) {
    case AceButton::kEventPressed:
      Serial.println("kEventPressed");
      toggleState_4 = 0;
      digitalWrite(RelayPin4, LOW);
      Blynk.virtualWrite(VPIN_BUTTON_4, toggleState_4);   // Update Button Widget  
      break;
    case AceButton::kEventReleased:
      Serial.println("kEventReleased");
      toggleState_4 = 1;
      digitalWrite(RelayPin4, HIGH);
      Blynk.virtualWrite(VPIN_BUTTON_4, toggleState_4);   // Update Button Widget  
      break;
  }
}

Enter the SSID and  passward of your WIFI and also enter the authentication code of blynk that has to be sent in your registered email id.

You need to add all these libraries in your Arduino IDE. 
Download these libraries from given links below.

Video Tutorial.

Leave a Reply

Your email address will not be published. Required fields are marked *