#include<Servo.h>
#include <WiFi.h>
#include "time.h"
#include <HTTPClient.h>
Servo servo;
const char*ssid = "project"; // your SSID
const char*password = "project1234"; // your SSID Password
WiFiServer server(80);
const String Host;
int servopin=T5;
int bulbpin=T6;
int fanpin=T7;
String keyone="door";
String keytwo="bulb";
String keythree="fan";
String l1value;
String l2value;
String l3value;
int doorstatus=0;
void setup() {
pinMode(bulbpin,OUTPUT);
pinMode(fanpin,OUTPUT);
Serial.begin(115200);
WiFi.disconnect();
delay(10);
WiFi.begin(ssid,password);
// Begin WiFi section
Serial.println();
Serial.println();
Serial.print("\nConnecting to :");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// print out info about the connection:
Serial.print("\nConnected to network");
Serial.print("My IP address is: ");
Serial.println(WiFi.localIP());
//starting the server
server.begin();
digitalWrite(bulbpin,0);
digitalWrite(fanpin,0);
closedoor();
delay(2000);
}
void loop() {
alterscantimer_run();
}
void get_device_status(String table_name, String require_text) {
WiFiClient client = server.available();
HTTPClient http;
String url=getopt+"/modloraget.php?table_name="+table_name+"&task_key="+require_text;
Serial.println(url);
http.begin(url);
//GET method
int httpCode = http.GET();
String payload = http.getString();
Serial.print("PAYLOAD:");Serial.println(payload);
if(require_text==keyone){
l1value=payload;
Serial.print("DOOR VALUE:");Serial.println(l1value);
int doorint=l1value.toInt();
if(doorint==1){
if(doorstatus==0){
doorstatus=1;
opendoor();
}
}
else if(doorint==0){
if(doorstatus==1){
doorstatus=0;
closedoor();
}
}
}
else if(require_text==keytwo){
l2value=payload;
Serial.print("BULB VALUE:");Serial.println(l2value);
int bulbpin=l2value.toInt();
if (bulbpin==1){
digitalWrite(bulbpin,1);
}
else if (bulbpin==0){
digitalWrite(bulbpin,0);
}
}
else if(require_text==keythree){
l3value=payload;
Serial.print("FAN VALUE:");Serial.println(l3value);
int fanpin=l3value.toInt();
if (fanpin==1){
digitalWrite(fanpin,1);
}
else if (fanpin==0) {
digitalWrite(fanpin,0);
}
}
http.end();
}
void getloadsdata(){
get_device_status("voicehomeauto", keyone);
delay(1000);
get_device_status("voicehomeauto", keytwo);
delay(1000);
get_device_status("voicehomeauto", keythree);
delay(1000);
}
String getValue(String data, char separator, int index)
{
int found = 0;
int strIndex[] = { 0, -1 };
int maxIndex = data.length() - 1;
for (int i = 0; i<= maxIndex&& found <= index; i++) {
if (data.charAt(i) == separator || i == maxIndex) {
found++;
strIndex[0] = strIndex[1] + 1;
strIndex[1] = (i == maxIndex) ? i + 1 : i;
}
}
return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}
void closedoor(){
servo.attach(servopin);
delay(500);
servo.write(90);
delay(500);
servo.detach();
delay(500);
}
void opendoor(){
servo.attach(servopin);
delay(500);
servo.write(0);
delay(500);
servo.detach();
delay(500);
}