Hi,
I've a project with a bare 8266 unit. The 8266 gets a signal from another web based device ( a "control by web" X-310) to turn its output on/off once per day. The command signal takes the form of that shown in notes at the top of the sketch and is repeated every couple of minutes (turn on , turn on ,turn on..... turn off turn off...). So my 8266 is actually pretending to be a reomte "control by web" product.
This works fine but after a few weeks it stops switching and has to be powered down and up again. So I'm guessing some form of memory or stack creep ? The 8266 has a 100uf capacitor added across its power pins.
I did add, as can be seen , a counter so the 8266 has to receive two "turn off" signals before it acted -I thought this might be the problem ,as when it stop running its usually with the output "ON".
Any nice comments welcomed.
thx.
My sketch is below and an adaptation of a library example ( I've tasken out the SSID and password!!):
/* Server to be driven by "control by Web" device to appear as a remote relay in its configuration.
run with serial monitor at 115200
Note doesn't connect to the 5G network only 2G
This version is for the little board bought from Ebay with intergral relay
relay corresponds to GPIO0 connection,
The "control by web" device needs to set a remote relay (relay "3" works !!) . There is no password.
Control by web output is of the form:
(IPof 8266)/state.xml?relayState=0” for relay on and...
(IPof 8266)/state.xml?relayState=0 for relay off.
*/
#include <ESP8266WiFi.h>
const char* ssid = "######"; // blanked out here
const char* password = "########";//blanked out here
int counter = 0;
WiFiServer server(80);
void setup() {
Serial.begin(115200);
delay(10);
pinMode(0, OUTPUT);
digitalWrite(0, HIGH);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
//setup for static IP:
IPAddress ip(192, 168, 0, 25); // where this is the desired IP Address to be accessed by the CBW device.
IPAddress gateway(192, 168, 0, 1);
Serial.print(F("Setting static ip to : "));
Serial.println(ip);
IPAddress subnet(255, 255, 255, 0); // set subnet mask to match
WiFi.config(ip, gateway, subnet);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.print("Use this URL : ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");
}
void loop() {
//re connect wifi if not connected
if (WiFi.status() != WL_CONNECTED)
{
delay(1);
Serial.print(">");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi re-connected");
// Start the server
server.begin();
Serial.println("Server re-started");
return;
}
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
//Serial.println("server lost");
return;
}
// Wait until the client sends some data
Serial.println("new client");
while (!client.available()) {
delay(1);
}
// Read the first line of the request
String request = client.readStringUntil('\r');
Serial.println(request);
client.flush();
// Match the request
if (request.indexOf("State=1") > -1) { // see if output is "ON". This is a small part of the string sent by the control by web device
// CBW relay set to on switch on the LED
digitalWrite(0, LOW); //LED on
Serial.println("ON");
delay(50); //for debugging
counter = 0;
}
if (request.indexOf("State=0") > -1)
{
// see if output is "ON". This is a small part of the string sent by the control by web device
// CBW relay set to on switch on the LED
counter = counter + 1;
if ( counter > 2) // need several switch off signals to turn it off
{
digitalWrite(0, HIGH);
Serial.println("OFF");
delay(50); //for debugging
counter = 0;
}
}
} //end of void loop