Home Automation Using Facebook Chat And Arduino ESP8266 WiFi Module

In this article we are showing how you can control your home appliances e.g. TV, Fan, Refrigerator, Lights etc. from Facebook chat message with the help of Arduino Uno and ESP8266 WiFi Module. You just need to send instructions like “RED BULB ON, TV ON, Room No-1 Fan Off” in chat and it will control based on your instruction. To accomplish this project we are using Facebook Graph API to communicate with messages and created a middle ware in .NET technology which is responsible for getting the instruction from Facebook Graph API.

Components Required :-

  1. 1 x Arduino uno board
  2. 1 x USB cable
  3. 1 x ESP8266 WiFi Module
  4. 1 x Relay Board
  5. 1 x Jumper wire (Male to Male)
  6. 9 x Jumper wire (Male to Female)
  7. 1 x Breadboard
  8. 1 x Yellow Bulb
  9. 1 x Red Bulb

Connection :-

Source Code :-

#include "SoftwareSerial.h"

String ssid = "YOUR_SSID";
String password = "YOUR_WIFI_PASSWORD";

SoftwareSerial esp(3, 2);// RX, TX

String server = "www.iotboys.com"//YOUR_MIDDLE_WARE;
String uri = "/api/FacebookAutomation/ProcessCommand";//YOUR MIDDLE WARE URI

int RED_BULB = 5;
int YELLOW_BULB = 6;

void setup() {

  pinMode(RED_BULB, OUTPUT);
  pinMode(YELLOW_BULB, OUTPUT);

  digitalWrite(RED_BULB, HIGH);
  digitalWrite(YELLOW_BULB, HIGH);
  esp.begin(9600);

  Serial.begin(9600);

  connectWifi();

  httpget();

  delay(1000);

}
void connectWifi() {

  String cmd = "AT+CWJAP=\"" + ssid + "\",\"" + password + "\"";

  esp.println(cmd);

  delay(4000);

  if (esp.find("OK")) {

    Serial.println("Connected!");

  }

  else {

    connectWifi();

    Serial.println("Cannot connect to wifi");
  }

}
/////////////////////////////GET METHOD///////////////////////////////

void httpget() {
  esp.println("AT+CIPSTART=\"TCP\",\"" + server + "\",80");//start a TCP connection.

  if ( esp.find("OK")) {

    Serial.println("TCP connection ready");

  } delay(1000);

  String getRequest =
    "GET " + uri + " HTTP/1.0\r\n" +
    "Host: " + server + "\r\n" +
    "Accept: *" + "/" + "*\r\n" +
    "Content-Type: application/json\r\n" +
    "\r\n";

  String sendCmd = "AT+CIPSEND=";//determine the number of caracters to be sent.

  esp.print(sendCmd);

  esp.println(getRequest.length() );

  delay(500);

  if (esp.find(">")) {
    Serial.println("Sending..");
    esp.print(getRequest);

    if ( esp.find("SEND OK")) {

      Serial.println("Packet sent");

      while (esp.available()) {

        String response = esp.readString();

        Serial.println(response);
       
        if (response.indexOf("RED BULB ON") > 0)
        {
          digitalWrite(RED_BULB, LOW);
        }
        else if (response.indexOf("RED BULB OFF") > 0)
        {
          digitalWrite(RED_BULB, HIGH);
        }
        else if (response.indexOf("YELLOW BULB ON") > 0)
        {
          digitalWrite(YELLOW_BULB, LOW);
        }
        else if (response.indexOf("YELLOW BULB OFF") > 0)
        {
          digitalWrite(YELLOW_BULB, HIGH);
        }
        else if (response.indexOf("ALL ON") > 0)
        {
          digitalWrite(RED_BULB, LOW);
          digitalWrite(YELLOW_BULB, LOW);
        }
        else if (response.indexOf("ALL OFF") > 0)
        {
          digitalWrite(RED_BULB, HIGH);
          digitalWrite(YELLOW_BULB, HIGH);
        }

      }
      // close the connection
      esp.println("AT+CIPCLOSE");

    }
  }
}

void loop() {
  httpget();
}

You may also like...

18 Responses

  1. Ahmad Sayeed says:

    Something that I have never heard. I know we can operate this trough intrent but never though about Facebook chat.

  2. Anish G says:

    Can you guys please share the middleware .NET code.
    Thanks

  3. Aakash says:

    Great, adding a camera will be more attractive. Can you please do that?

  4. Adnan says:

    Hi, could you please let me know where to download your middleware .net?

    Thanks

  5. Habib says:

    Hi,
    Pls tell me how to connect fb my mobile fb account how to connect

  6. IoT Boys IoT Boys says:

    Hi Habib,
    You need to first create a Facebook page then you can connect with that Facebook page using Facebook Graph API.
    We have used a middle ware developed in .NET to connect with Facebook Graph API .
    Let us know if you have still doubt.

    Best Regards,
    IoTBoys
    https://facebook.com/iotboys
    https://twitter.com/iotboys

  7. ingrid says:

    bonjour le demande si c’est seulement sur le meme seveur on peux envoyer les messages ou si on peux envoyer par notre propre compte facebook

  8. ingrid says:

    I asked how to install the midllware, I downloaded files already,

  9. ingrid says:

    ok how can we modify your code and the herberger or we can host it directly?

Leave a Reply

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