HTTP.query({
url: 'https://mdash.net/api/v2/devices/device2?access_token=xxxxxxxxxxx',
headers: { 'Content-Type': 'application/json' },
data: mdata, // { shadow: { state: { desired: { built_in: boolean}}}}
cert: 'cert.pem', // client certificate // just left it if you dont have this file
key: 'key.pem', // client key //just left if you dont has this file
ca_cert: 'ca.pem', // ca bundle to verify server with ""
success: function (body, full_http_msg) { print("body"); },
error: function (err) { print(err); }
});
sometime my device show this in serial monitor and ESP8266 automatically restarted:
The following request decreaase the RAM memory but it doesnot release the memory after the task is finished. I dont know why? Actually i am calling this function every 5 seconds.
when i remove this function the RAM memory does not decrease(while running the sketch).
HTTP.query({
url: 'https://mdash.net/api/v2/devices/device2?access_token=xxxxxxxxxxxxxxx',
headers: { 'Content-Type': 'application/json' },
data: mdata, // Optional. If set, JSON-encoded and POST-ed
cert: 'cert.pem', // client certificate
key: 'key.pem', // client key
ca_cert: 'ca.pem', // ca bundle to verify server with
success: function (body, full_http_msg) { print("success"); },
error: function (err) { print("err"); }
});
This memory should be reclaimed after a couple of minutes, once TCP connection exists TIME_WAIT state.
But i want to reclaimed the memory after the “success” or “error”
The sollution of the memory leak is as follow:
// do this before including other stuff like wificlient.h
#include "lwip/tcp_impl.h"
void tcpCleanup()
{
while(tcp_tw_pcbs!=NULL)
{
tcp_abort(tcp_tw_pcbs);
}
}
Here:
/* HeapCheckV3.ino by Costas 12 June 2017 was originally WebUdater.ino Example
Line 15 of ESP8266HTTPUpdateServer.cpp in
C:\arduino-1.8.3portable\arduino-1.8.3\hardware\esp8266com\esp8266\libraries\ESP8266HTTPUpdateServer\src
changed to: </body></html>)"; // was </body></html>\n)";
To upload through terminal you can use: curl -F "image=@firmware.bin" esp8266-webupdate.local/update
*/
#define HEAPCHECKER 0 // set to 1 to test HEAP loss fix
#ifdef HEAPCHECKER
#include "lwip/tcp_impl.h" // losing bytes work around
#endif
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <ESP8266HTTPUpdateServer.h>
const char* host = "esp8266-webupdate";
const char* ssid = "xxxxxxxxxx";
const char* password = "xxxxxxxxxx";
uint32_t originalram;
ESP8266WebServer httpServer(80);
ESP8266HTTPUpdateServer httpUpdater;
void setup(void){
Serial.begin(115200);
Serial.println();
Serial.println("Booting Sketch...");
WiFi.mode(WIFI_AP_STA);
WiFi.begin(ssid, password);
while(WiFi.waitForConnectResult() != WL_CONNECTED){
WiFi.begin(ssid, password);
Serial.println("WiFi failed, retrying.");
}
MDNS.begin(host);
httpUpdater.setup(&httpServer);
httpServer.begin();
MDNS.addService("http", "tcp", 80);
Serial.printf("HTTPUpdateServer ready! Open http://%s.local/update in your browser\n", host);
originalram = ESP.getFreeHeap();
}
//
void tcpCleanup() // losing bytes work around
{
while(tcp_tw_pcbs!=NULL)
{
tcp_abort(tcp_tw_pcbs);
}
}
//
void loop(void){
httpServer.handleClient();
delay(1000);
uint32_t ram = ESP.getFreeHeap();
Serial.printf("RAM: %d change %d\n", ram, (ram - originalram ));
if(HEAPCHECKER){ // losing bytes work around
tcpCleanup();
Serial.printf("tcpCleanup completed\n");
}
}
So i want to call the simillar function after the success or error. How can i add this?
Okey… I migrated the HTTP.query() in C. I am calling the function “void request(const char *data)” to send the POST request… So How to close the connection??? is the “mg_connectin *nc” is the connection? How to call the close function?