Close

IoT in your existing Switchboard

kapil-kumarKapil Kumar wrote 05/28/2020 at 12:58 • 8 min read • Like

GOOGLE ASSISTANT AND ESP8266 BASED SMART APPLIANCE CONTROL

Voice control AI
GOOGLE Assistant

To 
Control your Lights & Fan

Home automation has become very common now days and people are using IoT to automate everything in their home. Today, I am going to explain you how we can create a cheap google assistant based smart home appliance control system which you can also sell as product.
After going through this tutorial you will be able to control any number of appliances in your home from your android phone anywhere, anytime and also you can check the status of your lights, Fan, TV, etc. Yes, your existing switchboard can get smarter with just a small change and NOT much cost.

Hardware Components:

  1. Arduino UNO
  2. ESP8266 WiFi module (ESP-01) /NodeMCU
  3. Relay Module 5 volt
  4. Wires for connection
  5. AC to DC 5 volt power supply
  6. LM1117 or AMS1117 3.3V regulator
  7. AC appliances
  8. WiFi router

Tools

  1. Soldering iron
  2. Wire cutter
  3. Screw driver 
  4. Multimeter
  5. Electrical tape

Connect ESP8266 with Arduino for Programming

FTDI
Programming ESP-01

Pin Description
ESP8266

Connect the ESP8266 module with Arduino UNO as shown in the figure above, make sure that Rx is connected to Rx and Tx is connected with Tx as ESP-01 module is not 5 Volts tolerable use voltage divider for Rx. When you want to program the board check first if GPIO0 is connected to ground otherwise you will not be able to upload any code to your ESP-01
Enable pin should also be supplied with VCC and optional for RST.

Setup Arduino IDE
Install Blynk library for arduino IDE following this link here
After installing suitable library open new sketch and copy the code given below:
Edit SSID, PASSWORD, Authentication Key with yours before uploading to your ESP8266 module
Don't just copy paste, try to improve the code and the code is self explanatory if something not understood do let me know

Code:

#define BLYNK_PRINT Serial
#define RELAY_PIN_1      2  
#define PUSH_BUTTON_1     1   
#define VPIN_BUTTON_1    V2
#define RELAY_PIN_2      0  
#define PUSH_BUTTON_2    3   
#define VPIN_BUTTON_2    V0     

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = "bccggllfyufjvbbm";
char ssid[] = "Free";
char pass[] = "12345678";

BlynkTimer timer;

void checkPhysicalButton();///

int relay1State = LOW;
int pushButton1State = HIGH;

int relay2State = LOW;
int pushButton2State = HIGH;

// Every time we connect to the cloud...

BLYNK_CONNECTED() {

  // Request the latest state from the server

  Blynk.syncVirtual(VPIN_BUTTON_1);
  Blynk.syncVirtual(VPIN_BUTTON_2);

}

BLYNK_WRITE(VPIN_BUTTON_1) {
  relay1State = param.asInt();
  digitalWrite(RELAY_PIN_1, relay1State);
}
BLYNK_WRITE(VPIN_BUTTON_2) {
  relay2State = param.asInt();
  digitalWrite(RELAY_PIN_2, relay2State);
}

void checkPhysicalButton()
{
  if (digitalRead(PUSH_BUTTON_1) == LOW) {
    // pushButton1State is used to avoid sequential toggles
    if (pushButton1State != LOW) {

      // Toggle Relay state
      relay1State = !relay1State;
      digitalWrite(RELAY_PIN_1, relay1State);

      // Update Button Widget
      Blynk.virtualWrite(VPIN_BUTTON_1, relay1State);
    }
    pushButton1State = LOW;
  }
  else if (digitalRead(PUSH_BUTTON_1) == HIGH)
  {
    // pushButton1State is used to avoid sequential toggles
    if (pushButton1State != HIGH) {

      // Toggle Relay state
      relay1State = !relay1State;
      digitalWrite(RELAY_PIN_1, relay1State);

      // Update Button Widget
      Blynk.virtualWrite(VPIN_BUTTON_1, relay1State);
    }
    pushButton1State = HIGH;
  }
  if (digitalRead(PUSH_BUTTON_2) == LOW) {
    // pushButton2State is used to avoid sequential toggles
    if (pushButton2State != LOW) {

      // Toggle Relay state
      relay2State = !relay2State;
      digitalWrite(RELAY_PIN_2, relay2State);

      // Update Button Widget
      Blynk.virtualWrite(VPIN_BUTTON_2, relay2State);
    }
    pushButton2State = LOW;
  } else if (digitalRead(PUSH_BUTTON_2) == HIGH)
  {
    // pushButton1State is used to avoid sequential toggles
    if (pushButton2State != HIGH) {

      // Toggle Relay state
      relay2State = !relay2State;
      digitalWrite(RELAY_PIN_2, relay2State);

      // Update Button Widget
      Blynk.virtualWrite(VPIN_BUTTON_2, relay2State);
    }
    pushButton2State = HIGH;
  }
}

void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);
  
  pinMode(RELAY_PIN_1, OUTPUT);
  pinMode(PUSH_BUTTON_1, INPUT_PULLUP);
  digitalWrite(RELAY_PIN_1, relay2State);
  pinMode(RELAY_PIN_2, OUTPUT);
  pinMode(PUSH_BUTTON_2, INPUT_PULLUP);
  digitalWrite(RELAY_PIN_2, relay2State);

  timer.setInterval(500L, checkPhysicalButton);
}

void loop()
{
       Blynk.run();
       timer.run();

}
///

Interfacing with IoT platform

Here, we are using BLYNK as our IoT platform to enable our device to communicate with cloud server. Install Blynk application in your smart phone and login or sign up.

In Blynk terminal Click on, Create New Project and then choose  "Generic Board" for Esp-01 in hardware devices
now swipe left on screen and drag buttons from the widget section.

Now, click on button and select pin option and choose virtual pins V2 and V0.
Change your button to switch using slider

Steps to setup blynk
Setup Blynk App

On the top of your screen in app, select settings to get your "authentication key" for this project and copy this key to apply in code.Press play button to make your device receive signals from cloud.

Connecting Appliances with the Device

Here, An extra care needs to be taken as you are playing with AC now. If device installation demands help please ask some professional to help you before doing anything with AC supply.

connect components like ESP32
Connections

Connect everything as shown in the image above and make sure connections are not loose.
you can create your own PCB also as it will look more neat and clean.
for using your existing switchboard as earlier with manual button operations too then there are some additional connections to do.

ESP8266 google assistant project # iot# home automation
Final Diagram

According to above Diagram we are using GPIO1 and GPIO3 as input pins connected to switches. Changing the values of these pin can change the status of relays respectively and at the same time ESP-01 is updating the Blynk server with status of that appliance. So, that user won't get confused about current status of that appliance.

Now, we can control appliances only through Blynk app and not using google assistant, to do that follow few more steps

Integrate with IFTTT

Go to IFTTT’s website and sign up to it using your Google or Facebook account

app signup

After Signing in click on profile from the header and select create .

Click on "this".

this
Search for google Assistant and select it. And then Click on Connect

google voice

At this point IFTTT will ask you permission to use your google account to add voice commands to it. Which you simply allow by clicking on "Allow".

Select the card that says “Say a simple phrase”.

Next, for the first text box type the phrase that you want to say to Google

Assistant. It can be anything such as “Turn on the Light”, “Turn on the fan” or anything you like.

For the next two text boxes, you write some other ways to say the first command. For example, if in the first text box you wrote “Turn on the

Lights”, then in the second and third text boxes you can write something like “Light up the room” or “Switch on the light”.

In the fourth text box type the reply that Google Assistant should respond with. For example, “Okay, Turning on the Light”.

Finally, click on ‘Create Trigger’.

Now click on "that"

techelektronify

and type webhooks select it, and click connect. Webhooks will allow us to send commands to the Blynk Server.
web control

Now, in the URL field type this URL:

http://188.166.206.43/ YourAuthTokenHere / update / DigitalPinToBeUpdateHere
 we should use V2 and V0 as digital pin i.e GPIO2 and GPIO0.

Next, Select the ‘Method’ field as PUT

Select ‘Content type’ as Application/JSON.

For the ‘Body’ type this: [“0”]

Here ‘0’ means to turn on, so we are basically saying Blynk to turn on relay that is connected to pin V2, which in our case is Bulb.
Now click on ‘Create Action’ and then Finish.

Similarly, we create another applet to turn off the relay. Repeat all the steps above.
For the ‘Body’ type this: [“1”].

Congrats, Now you've created a voice assistant based home automation system with Internet of things features embedded.

Like

Discussions