Having said that, I have discovered there are a lot of things that can make it not work. I am currently using an ESP-12 (that I got from Seeedstudio via Amazon. It took me awhile to figure out all of the HW mods I needed to make it work (pull up and pull down resistors, as specified in making it reliable). I believe that is as it should be with:
Pull up: CH_PD, Reset, GPIO_0
Pull down: GPIO_15
That has helped tremendously. However, this morning it stopped updating the web. The serial output has stopped and I noticed that the esp8266 is now in AP mode and not station mode. AP mode is working, in that I can get an IP address and ping the esp8266, but it doesn't serve up a web page. My code doesn't set up AP mode, it does station mode:
void setup() {
Serial.begin(115200);
gasMeter.init();
display.init();
display.setCursor(0, 0);
display.print("Connecting to");
display.setCursor(1, 0);
display.print(ssid);
WiFi.begin(ssid, password);
display.setCursor(2, 0);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
display.print(".");
}
display.setCursor(3, 0);
display.print("WiFi connected:");
display.setCursor(4, 0);
display.print(WiFi.localIP());
delay(5000);
}
The code that uses the WiFi client looks like this:
int HttpClient::PostOrPut(WiFiClient client, const char* server, String Uri, boolean isPost, String Headers, String PostData) {
boolean connected;
int counter = 0;
int retries = 0;
int badUpdate = 0;
do {
connected = client.connect(server, 80);
if (connected) { break; }
client.stop();
Alarm.delay(1000);
Serial.println("retry");
retries++;
counter++;
} while (!connected && counter <5);
if (connected) {
if (isPost){
Serial.println("POST");
Serial.print("POST " + Uri + " HTTP/1.1\n");
client.print("POST " + Uri + " HTTP/1.1\n");
}
else {
Serial.println("PUT");
Serial.print("PUT " + Uri + " HTTP/1.1\n");
client.print("PUT " + Uri + " HTTP/1.1\n");
}
client.print("Host: ");
client.print(server);
client.print("\n");
client.print("Connection: close\n");
client.print(Headers);
client.print("Content-Length: ");
client.print(PostData.length());
client.print("\n\n");
client.print(PostData);
Serial.print("Host: ");
Serial.print(server);
Serial.print("\n");
Serial.print("Connection: close\n");
Serial.print(Headers);
Serial.print("Content-Length: ");
Serial.print(PostData.length());
Serial.print("\n\n");
Serial.print(PostData);
int c = '\0';
unsigned long startTime = millis();
unsigned long httpResponseTimeOut = 10000; // 10 sec
while (client.connected() && ((millis() - startTime) < httpResponseTimeOut)) {
if (client.available()) {
c = client.read();
Serial.print((char)c);
}
else {
Serial.print(".");
Alarm.delay(100);
}
}
}
else {
badUpdate++;
Serial.println("bad Update");
}
client.stop();
if (badUpdate > 0) {
return -retries;
}
else {
return retries;
}
}
Yes, it uses a lot of String manipulation. Yes, I have checked the heap usage. In fact, I graph that (along with the number of successful iterations it has made). All of this is working and it has long periods of consistent heap utilization (12 hours or more). It is interesting that there appears to be a leak, but it is not consistent and sometimes it is recovered. You can see the heap utilization here: https://smartenergygroups.com/groups/frogmore
But, I have never been able to get the esp8266 to stay working for more than a few days. It has what seems like a lot of remaining heap (28KBytes). I have not updated the esp8266 library in a few weeks, but I didn't see any changes that seemed related to this issue.
Any ideas?