IoTBoysRajiv SharmaShambhoo kumar
Published

Control Home Lights Using Facebook Chat Messages

Controlling Home Lights using Facebook Chat messages with the help of Arduino, ESP8266 WiFi and a relay board.

AdvancedProtip18 hours7,812
Control Home Lights Using Facebook Chat Messages

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Relay (generic)
×1
ESP8266 ESP-01
Espressif ESP8266 ESP-01
×1
Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×10
AC Bulb
Two Bulb Red and Yellow
×2

Software apps and online services

Arduino IDE
Arduino IDE
Facebook Chat

Story

Read more

Schematics

Circuit Design

Circuit Design

Code

Source Code

C/C++
Source Code for Arduino
#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();
}

Credits

IoTBoys
9 projects • 116 followers
Watch, Learn and Built IoT projects | DIY IoT Projects | IoT Projects for College Student.
Contact
Rajiv Sharma
18 projects • 73 followers
Having more than 10 years of experience in IoT and software technology. Founded IoTBoys to share knowledge with IoT enthusiasts.
Contact
Shambhoo kumar
4 projects • 43 followers
Contact

Comments

Please log in or sign up to comment.