Hi everyone.
I am working on a project where i want to automate my rooms lights ( i will control 9 light in total ), using an Esp8266 and a RF transmitter. Esp8266 is connected to my local webserver, so i am using php and Mysql to store the state of the lights as well as alarm time for on / off.
My issue is a big delay from when i click to the button on my php page until the lights reacts, i have a delay of 12 - 13 sec.
i cannot figure out how so solve that. Please any help ?
( any suggestion on how to optimise the code is appreciated, as i am a newbe learning )
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
#include <RCSwitch.h>
const char* wifiName = "MyWeb";
const char* wifiPass = "";
RCSwitch mySwitch315 = RCSwitch();
RCSwitch mySwitch415 = RCSwitch();
WiFiServer server(80);
WiFiClient client;
const char* host = "http://192.168.0.188";
String get_host = "http://192.168.0.188";
unsigned long currentTime = millis();
unsigned long previousTime = 0;
const long timeoutTime = 1000;
String Id;
String Mode;
String State;
String AlarmON;
String AlarmOFF;
String JustChanged;
String header;
void setup() {
mySwitch315.enableTransmit(0);
mySwitch415.enableTransmit(2);
Serial.begin(115200);
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(wifiName);
WiFi.begin(wifiName, wifiPass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP()); //You can get IP address assigned to ESP
server.begin(); //Start web server
}
void loop(){
get_device_status("S1");
get_device_status("S2");
delay(50);
}
void get_device_status(String device_name) {
HTTPClient http;
String url = get_host+"/lights/getdata.php?Switch_ID="+device_name;
http.begin(url);
int httpCode = http.GET();
String payload = http.getString();
if(httpCode == 200)
{
// Use arduinojson.org/assistant to compute the capacity.
const size_t capacity = JSON_OBJECT_SIZE(5) + 60;
DynamicJsonBuffer jsonBuffer(capacity);
// Parse JSON object
JsonObject& root = jsonBuffer.parseObject(payload);
if (!root.success()) {
Serial.println(F("Parsing failed!"));
return;
}
String Id = root["Id"];
String Mode = root["Mode"];
String State = root["State"];
String AlarmON = root["AlarmON"];
String AlarmOFF = root["AlarmOFF"];
String JustChanged = root["JustChanged"];
if (Mode == "Auto") {
if (AlarmON == "1") {
if (State == "OFF") {
if (JustChanged == "0") {
if (device_name == "S1"){
mySwitch315.send(3038947, 24);
State = "ON";
JustChanged = "1";
}
if (device_name == "S2"){
mySwitch315.send(3038948, 24);
State = "ON";
JustChanged = "1";
}
}
}
}
if (AlarmOFF == "1") {
if (State == "ON") {
if (JustChanged == "1") {
if (device_name == "S1"){
mySwitch315.send(3038947, 24);
State = "OFF";
JustChanged = "1";
}
if (device_name == "S2"){
mySwitch315.send(3038948, 24);
State = "OFF";
JustChanged = "1";
}
}
}
}
}
if (Mode == "Manual") {
if (State == "ON") {
if (JustChanged == "0") {
if (device_name == "S1"){
mySwitch315.send(3038947, 24);
Serial.println("S1 = Manual = ON");
JustChanged = "1";
}
if (device_name == "S2"){
mySwitch315.send(3038948, 24);
Serial.println("S2 = Manual = ON");
JustChanged = "1";
}
}
}
if (State == "OFF") {
if (JustChanged == "1") {
if (device_name == "S1"){
mySwitch315.send(3038947, 24);
Serial.println("S1 = Manual = OFF");
JustChanged = "0";
}
if (device_name == "S2"){
mySwitch315.send(3038948, 24);
Serial.println("S2 = Manual = OFF");
JustChanged = "0";
}
}
}
}
SendResponse(JustChanged, State, device_name);
}
else
{
Serial.println("Error in response");
}
http.end();
}
//Handles http request
void SendResponse(String JustChanged, String State, String device_name) {
WiFiClient client;
if (!client.connect("192.168.0.188", 80)) {
Serial.println("connection failed");
return;
}
client.print(String("GET http://192.168.0.188/lights/post-esp-data.php?") +
("&JustChanged=") + JustChanged +
("&State=") + State + ("&Id=") + device_name +
" HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 500) {
Serial.println(">>> Client Timeout !");
client.stop();
return;
}
}
// Read all the lines of the reply from server and print them to Serial
while(client.available()) {
String line = client.readStringUntil('\r');
}
}