Go Down

Topic: Memory leak with client.connect() when in main loop, or no connect joy (Read 123 times) previous topic - next topic

Sophi

Hello! I am using the Arduino IDE on 2 ESP8266s, which are configured one as client and one as server.

The client sends on/off pin data to the server, which takes that data and turns other things on/off.

When the client has this
Code: [Select]
client.connect(server, 80); in the main loop, data is sent back and forth but eventually loses connection due to a memory leak.

When the client has that line
Code: [Select]
client.connect(server, 80); in the setup loop (where it should be), the connection on the server side times out and it never connects to the client.

I have read the ESP8266Wifi library docs and have been trying different things for a bit. I'm hoping someone here can help me to troubleshoot.

I think what is happening is that the server connects to the client, then drops, and doesn't have a way to reconnect. How can I troubleshoot (server, 80) to see what is happening? There are error codes, but I don't know how to access them.
On the client side, checking for a drop and reconnecting in the main loop results in the heap leak.

Client code:
Code: [Select]

#include <ESP8266WiFi.h>
extern "C" {
#include "user_interface.h"
}
long last_output = 0;
String val4;
String val5;
byte ledPin = 2;
int clientConnected;
bool showCount = true;

char ssid[] = "x";                        // put your SSID of home WiFi here
char pass[] = "xx";                     // password of home WiFi
unsigned long askTimer = 0;

IPAddress server(192,168,1,213);     // fixed IP of ESP server
WiFiClient client;

void setup() {
  Serial.begin(115200);                    // only for debug
  WiFi.begin(ssid, pass);                   // connects to the WiFi router
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
  Serial.println("connected to WiFi!");

  while (!client.connected()) {
  Serial.println("no connection to server");
  client.connect(server, 80);
  delay(500);
  }
  Serial.println("connection to server ok");
  delay(5000);

  pinMode(0, OUTPUT); // LED
  pinMode(ledPin, OUTPUT); // LED pin 2
  pinMode(4,  INPUT_PULLUP);   // + input to U6, up/down motor
  pinMode(5,  INPUT_PULLUP);   // - input to U6
}

void loop () {
/*  the following lines result in memory leak
  while (!client.connected()) {
  Serial.println("no connection to server");
  client.connect(server, 80);
  delay(500);
 }  */

  if (millis() - last_output > 1000) // compare diff of now vs. timer
  {   
    uint32_t free = system_get_free_heap_size(); // get free ram   
    Serial.println(free); // output ram to serial   
    last_output = millis(); // reset timer
  }

    val4 = digitalRead(4);          // read the input pin
    val5 = digitalRead(5);          // read the input pin
    client.println(val4);       
    client.println(val5);       
    client.println('y');                // send the data
}


Server code
Code: [Select]
#include <SPI.h>
#include <ESP8266WiFi.h>

char ssid[] = "x";                     // SSID of home WiFi
char pass[] = "xx";
WiFiServer server(80);

IPAddress ip(192,168,1,213);         // IP address of the ESP
IPAddress gateway(192,168,1,1);        // gateway of your network netstat -nr | grep default
IPAddress subnet(255,255,255,0);       // subnet mask of your network

String request;
String request4;
String request5;
int ledPin = 2;

void setup() {
  Serial.begin(115200);                   // only for debug
  WiFi.config(ip, gateway, subnet);       // forces fixed IP
  WiFi.begin(ssid, pass);                 // connects to the WiFi router
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
 
  Serial.println("Connected to wifi");
  server.begin();

  pinMode(0, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(4, OUTPUT);   
  pinMode(5, OUTPUT);     
  digitalWrite(0,  HIGH);
  digitalWrite(2, HIGH);
  digitalWrite(4,  LOW);
  digitalWrite(5,  LOW); 
}

void loop () {
  WiFiClient client = server.available();
  if (client) {
    if (client.connected()) {
      Serial.println("Connected");
      String request = client.readStringUntil('y');
      request4 = request.charAt(0);
      request5 = request.charAt(3);   

     if (request4 == "0" && request5 == "1"){    // left motor
        digitalWrite(4, LOW);   //U6
        digitalWrite(5, HIGH);  //U6
        Serial.println("request4 is on");
      }
     if (request5 == "0" && request4 == "1"){    // left motor
        digitalWrite(5, LOW);   //U6
        digitalWrite(4, HIGH);  //U6
        Serial.println("request5 is on");
      }
     if (request5 == "1" && request4 == "1"){
        digitalWrite(4, LOW);  //U6
        digitalWrite(5, LOW);  //U6
        Serial.println("request5/ request 4 are off");
      }
   }
 }
}

Sophi

This has been resolved. The ESP8266 libraries 2.4.1 that currently come with the library manager introduced a leak. I rolled back to 2.4.0 and the leak is gone.
More info here

Go Up