-1

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() {
 Serial.begin(9600);
 delay(10);
 pinMode(ledPin, OUTPUT);
 digitalWrite(ledPin, LOW);
 pinMode(ledPin2, OUTPUT);
 digitalWrite(ledPin2, LOW);
 pinMode(conectado, OUTPUT);
 pinMode(desconectado, OUTPUT);
 digitalWrite(desconectado, HIGH);
 connectWifi(); 
 Serial.print(F("Setting static ip to : "));
 Serial.println(WiFi.localIP()); 
 // Start the server
 server.begin();
 Serial.println("Server started");
}
void loop() {
 delay(200);
 if (WiFi.status() != WL_CONNECTED) {
 delay(100);
 digitalWrite(conectado, LOW);
 digitalWrite(desconectado, HIGH);
 connectWifi(); 
 }
 // Check if a client has connected
 client = server.available();
 if (!client) {
 return;
 }
 // Wait until the client sends some data
 Serial.println("new client");
 int t = millis();
 while(!client.available()){
 delay(1);
 if (millis()-t>130) return;
 }
 // Read the first line of the request
 String request = client.readStringUntil('\r');
 Serial.println(request);
 client.flush();
 // Match the request
 if (request.indexOf("/switch") != -1) {
 digitalWrite(ledPin, !digitalRead(ledPin));
 digitalWrite(ledPin2, !digitalRead(ledPin2));
 printSwitch();
 }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){
 printIP();
 }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) { 
 client.print("Off");
 } else {
 client.print("On"); 
 }
 }else if (request.indexOf("/canaldos") != -1){
 if(digitalRead(ledPin2) == HIGH) { 
 client.print("Off");
 } else {
 client.print("On");
 }
 }
// Set ledPin according to the request
//digitalWrite(ledPin, value);
 // Return the response 
 delay(1);
 Serial.println("Client disonnected");
 Serial.println("");
}
void printSwitch(){ 
 if(digitalRead(ledPin) == LOW) {
 client.print("Canal 1 Encendido");
 } else {
 client.print("Canal 1 Apagado");
 }
 Serial.println("");
 if(digitalRead(ledPin2) == LOW) {
 client.print("Canal 2 Encendido");
 } else {
 client.print("Canal 2 Apagado");
 }
}
void printIP(){
 delay(100); 
 client.print(WiFi.localIP());
}
void connectWifi()
{
 Serial.print("Connecting to "+*ssid);
 WiFi.begin(ssid, password);
 while (WiFi.status() != WL_CONNECTED) {
 delay(500);
 Serial.print(".");
 } 
 // 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);
 Serial.println("");
 Serial.println("Connected");
 Serial.println(""); 
 // Print the IP address
 Serial.print("Use this URL to connect: ");
 Serial.print("http://");
 Serial.print(WiFi.localIP());
 Serial.println("/");
}//end connect
VE7JRO
2,51519 gold badges27 silver badges29 bronze badges
asked Aug 13, 2017 at 14:46
4
  • 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. Commented Aug 13, 2017 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. Commented Aug 13, 2017 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 192.168.4.1 and it responded, after trying that, I tried again with static IP and worked fine, it seems like it sleeps or something programatically Commented Aug 13, 2017 at 15:40
  • The answer here might help you: arduino.stackexchange.com/questions/75494/… Commented May 15, 2020 at 11:30

1 Answer 1

0

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

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

then handle the filled request buffer like

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

This should keep you esp running

answered Apr 11, 2020 at 18:41
2
  • 1
    That doesn't compile. You need a closing round bracket in your while loop. I also get: error: empty character constant char c = ''; Commented May 5, 2020 at 11:18
  • 1
    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. Commented Oct 2, 2020 at 18:43

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.