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:
while (!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.
My first memory leak! Hooray! :/
Here is the full code:
#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'
}