Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By sophi
#75528 Hi-
I have 2 ESP8266s talking to eachother, one is client, one is server.
I noticed that they dropped connection after about 2-5 minutes so I checked the heap. The client decreases from heap count of 45568 to around 512, then loses connection with the server. The server's heap is stable.

I think I have isolated the issue to one section of code:
Code: Select allwhile (!client.connect(server, 80)) {
  Serial.println("Connection to server failed");
  delay(1000);
  }


When this section is in the main loop, the memory leaks. When the above snippet is in the setup loop, the memory does not leak, but the client does not connect with the server either. Any insights would be appreciated. :P
My first memory leak! Hooray! :/

Here is the full code:
Code: Select all#include <SPI.h>
#include <ESP8266WiFi.h>
extern "C" {
#include "user_interface.h"
}
long last_output = 0;
String val4;
String val5;
byte ledPin = 2;
int clientConnected;
char ssid[] = "xx";                     // SSID of home WiFi
char pass[] = "xxx";              // 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) { // checks for WiFi connection
    Serial.println(".");
    delay(500);
  }
  delay(1000); 
  Serial.println("WiFi connected: ");
  Serial.print("MAC: ");
  Serial.println(WiFi.macAddress());
  Serial.println("Hello, I am the joystick!");
  delay(1000);
  // lines 42 - 45 are a memory leak problem in the main loop
  while (!client.connect(server, 80)) { // line 42
  Serial.println("Connection to server failed");
  delay(1000);
  }
  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 () { 
  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');             // server look for 'y'
}
User avatar
By schufti
#75532 I don't know why client.connect(server, port) should not work in setup()

but maybe this
Code: Select allwhile (!client.connected()) {
  Serial.println("no connection to server");
  client.connect(server, 80)
  delay(1000);
  }
Serial.println("connection to server ok");

could help to reduce memleakage
User avatar
By sophi
#75543 schufti, thanks for verifying that it should work in setup().

rudy, thanks for the tip. The most recent Arduino ESP8266 library download from the library manager (2.4.1) has a memory leak. I rolled back to 2.4.0, which you can conveniently do from the library manager!

The massive memory leak has been fixed.