8266 lockup

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

Hi Hammy. I can think of a few things that might be worth trying.

Your code is not reading all the lines of the request from the client, just the first line. I don't think flush() discards any remaining lines. It just sends any part of the response out that is still in the output buffer. At that point in the code, no part of the response has been written, so I don't think flush() is doing anything.

Come to that, you are not sending any response to the client. Even if the client is not expecting one, its useful for debugging to send at least a short one echoing the request parameters.

Once there is no more of the client's request to be read, and the client is no longer connected (check client.connected()), and the response has been sent, it might be a good idea to do a client.stop().

If the WiFi connection had been lost, maybe do server.end() (.stop()?) before doing server.begin() again.

Maybe restructure your code, using nested if and else, to remove those return commands, so that your code always passes a certain point regardless of what has happened, so that any tidying/closing always gets done and is only in one place. I'm getting really picky now.

I have no certainty that any of these suggestions will make the slightest difference, but these are the things that would I would be suspicious of if it were my code.

Thx Paul , a few things for me to look into