#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();
}