I'm controlling some devices through a relay and a NodeMCU, it works fine and reconnects without problem in the meantime I'm testing it, but after I leave it like for 3 or more hours, it stops responding. I need this system to always be working without sleeping or anything. Here is my code:

#include <ESP8266WiFi.h>

const char* ssid = "wifi_Guerrero ";
const char* password = "3245968530";

int ledPin = 13; // GPIO13
int ledPin2 = 12; // GPIO13
int desconectado = 5;
int conectado = 4;
WiFiServer server(80);
WiFiClient client;

int counter = 0;

void setup() {

  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);

  pinMode(ledPin2, OUTPUT);
  digitalWrite(ledPin2, LOW);

  pinMode(conectado, OUTPUT);
  pinMode(desconectado, OUTPUT);
  digitalWrite(desconectado, HIGH);


  Serial.print(F("Setting static ip to : "));

  // Start the server
  Serial.println("Server started");


void loop() {


  if (WiFi.status() != WL_CONNECTED) {
    digitalWrite(conectado, LOW);
    digitalWrite(desconectado, HIGH);

  // Check if a client has connected
  client = server.available();
  if (!client) {

  // Wait until the client sends some data
  Serial.println("new client");
  int t = millis();
  if (millis()-t>130) return;

  // Read the first line of the request
  String request = client.readStringUntil('\r');

  // Match the request
  if (request.indexOf("/switch") != -1)  {
    digitalWrite(ledPin, !digitalRead(ledPin));
    digitalWrite(ledPin2, !digitalRead(ledPin2));
  }else if (request.indexOf("/uno") != -1) {
    client.print("Canal 1 apagagado | Canal 2 encendido");
    digitalWrite(ledPin, HIGH);
    digitalWrite(ledPin2, LOW);
  }else if (request.indexOf("/dos") != -1){
    client.print("Canal 1 encendido | Canal 2 apagado");
    digitalWrite(ledPin, LOW);
    digitalWrite(ledPin2, HIGH);
  }else if (request.indexOf("/tres") != -1){
    client.print("Canal 1 apagado | Canal 2 apagado");
    digitalWrite(ledPin, HIGH);
    digitalWrite(ledPin2, HIGH);
  }else if (request.indexOf("/cuatro") != -1){
    client.print("Canal 1 encendido | Canal 2 encendido");
    digitalWrite(ledPin, LOW);
    digitalWrite(ledPin2, LOW);
  }else if (request.indexOf("/cinco") != -1){
  }else if (request.indexOf("/apagaruno") != -1){
    client.print("Canal 1 apagado");
    digitalWrite(ledPin, HIGH);
  }else if (request.indexOf("/apagardos") != -1){
    client.print("Canal 2 apagado");    
    digitalWrite(ledPin2, HIGH);
  }else if (request.indexOf("/prenderuno") != -1){
    client.print("Canal 1 encendido");
    digitalWrite(ledPin, LOW);
  }else if (request.indexOf("/prenderdos") != -1){
    client.print("Canal 2 encendido");
    digitalWrite(ledPin2, LOW);
  }else if (request.indexOf("/canaluno") != -1){

    if(digitalRead(ledPin) == HIGH) {    
  } else {

  }else if (request.indexOf("/canaldos") != -1){

    if(digitalRead(ledPin2) == HIGH) {    
  } else {


// Set ledPin according to the request
//digitalWrite(ledPin, value);

  // Return the response  

  Serial.println("Client disonnected");


void printSwitch(){ 
  if(digitalRead(ledPin) == LOW) {
    client.print("Canal 1 Encendido");
  } else {
    client.print("Canal 1 Apagado");
  if(digitalRead(ledPin2) == LOW) {
    client.print("Canal 2 Encendido");
  } else {
    client.print("Canal 2 Apagado");

void printIP(){

void connectWifi()
  Serial.print("Connecting to "+*ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {

  // config static IP
  IPAddress ip(192, 168, 0, 8);
  IPAddress gateway(192, 168, 0, 1);
  IPAddress subnet(255, 255, 255, 0);  
  WiFi.config(ip, gateway, subnet);
  digitalWrite(conectado, HIGH);
  digitalWrite(desconectado, LOW);

  // Print the IP address
  Serial.print("Use this URL to connect: ");
}//end connect
  • You need to understand why it is stopping. Start with something like printing serial log messages and capturing them to a log file that you can examine after failure. Preferably do this over a pure UART connection (to a distinct USB to logic-level-serial) without using the on-board USB converter, unless you plan to have the USB connected to a PC in operation. Though you can of course start with the on-board USB and see if you learn something, it's just that there's a risk that having that plugged in could hide power-related failure causes. – Chris Stratton Aug 13 '17 at 15:26
  • You should also consider if there are design errors in your unspecified electrical circuit. Particularly things like insufficient input voltage to a regulator, relay coils without catch diodes, or any sharing of the logic and load power supplies. – Chris Stratton Aug 13 '17 at 15:27
  • I think the problem is not about the circuit or power supply, because I also left it on AP mode and when the static ip I assigned to it stopped responding, I connected directly to it's AP with and it responded, after trying that, I tried again with static IP and worked fine, it seems like it sleeps or something programatically – Javier Guerrero Aug 13 '17 at 15:40
  • The answer here might help you: arduino.stackexchange.com/questions/75494/… – David Klempfner May 15 at 11:30

I think thatthe problem lies with this definition

String request = client.readStringUntil('\r');

which you define dynamically in loop this leads possibly to heap fragmentation, causing resets after sometime
Define a global char array

char request [256] ={'\0'};
char c;
uint cc = 0;

and work instead the String functions with

c = client.read();    
if (c != '\r'){ 
  request[cc] = c;
 else return;

then handle the filled request buffer like

if (strcmp(request,"/switch") != 0)  {

This should keep you esp running

| improve this answer | |
  • That doesn't compile. You need a closing round bracket in your while loop. I also get: error: empty character constant char c = ''; – David Klempfner May 5 at 11:18
  • You need to add a line where you’re reading into that buffer to make sure you don’t overrun. Make sure cc doesn’t go over 255. – Delta_G Oct 2 at 18:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.