Greetings.. :o
So I am trying to build a remote control project.
I want to switch a light on/off using a relay when a value on SQL Database changes.
(I am using a NodeMCU)
Currently I am using code which monitors the value in every iteration, which works, but is very inefficient as this sends WAY too many requests to my server.
Here is my code:
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
const char *ssid = "SSID"; // replace with your wifi ssid and wpa2 key
const char *pass = "PASSWORD";
WiFiClient client;
void setup()
{
Serial.begin(9600);
delay(10);
pinMode(LED_BUILTIN,OUTPUT);
pinMode(D0,OUTPUT);
Serial.println("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
}
void loop()
{
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin("http://SERVERADDRESS/updateDB/view.php?id=1");
int httpCode = http.GET();
if (httpCode > 0) { //Check the returning code
String payload = http.getString(); //Get the request response payload
Serial.println(payload);
if(payload == "0"){
digitalWrite(LED_BUILTIN,HIGH);
digitalWrite(D0,HIGH);
}
else if(payload == "1"){
digitalWrite(LED_BUILTIN,LOW);
digitalWrite(D0,LOW);
}
//Print the response payload
}
http.end(); //Close connection
}
}
I was wondering if instead of this, is there any way the server could NOTIFY the nodeMCU when the value changes so that it does not have to check value every time?
I realize this is possible if I host a local server using the NodeMCU but I want to be able to control the device from outside the local network.
Any suggestions would be appreciated.