ESP 32 AWS_IOT library deinit and get heap back

Ajinkya_777
Posts: 8
Joined: Tue Sep 15, 2020 4:42 am

ESP 32 AWS_IOT library deinit and get heap back

Postby Ajinkya_777 » Mon Oct 19, 2020 6:37 am

Hi,
I'm using AWS_IOT library to send data from esp32 to aws cloud server. when I'm creating a object of AWS_IOT to connect subscribe and publish data to cloud it takes lot of heap memory in esp32 so I deleted the object to free up the heap but it just release around 50000 bytes but when created object it took almost 2,00,000 bytes in heap.

Is their anything I'm doing wrong in it or their is any function to deinit like BLE to clear the stack?

here is the code in arduino I'm doing for aws cloud.

Thank you
Ajinkya G.


#include<stdio.h>
#include "esp_attr.h"
#include<stdlib.h>
#include <ArduinoJson.h>
#include <AWS_IOT.h>
//AWS_IOT myiot; // AWS_IOT instance
#include <WiFi.h>

const char* WIFI_SSID = "*******";
const char* WIFI_PASSWORD = "**##**##**##**";

char HOST_ADDRESS[] = "a1udxktg9dbtpj-ats.iot.ap-south-1.amazonaws.com";
char CLIENT_ID[] = "0e089ed0f06440a0ae741c913e6b8ba1";
char TOPIC_NAME[] = "$aws/things/cat/shadow/update";

int status = WL_IDLE_STATUS; //wifi status
int aws_status = 0;
int data_cnt = 0;

int tick = 0, msgCount = 0, msgReceived = 0;
#define AWS_MAX_RECONNECT_TRIES 5
String wifi_mac = "", storing_data = "",Pulse_count = "", mac = "", Battery_level = "";

void setup()
{
Serial.begin(115200);

Serial.print("Starting = ");
Serial.println(ESP.getFreeHeap());
Serial.println("START");
// Serial.println(micros());
beacon_scan_start = micros();
}

void loop()
{
Serial.println(micros());
Serial.println("before wifi");
Serial.println(ESP.getFreeHeap());
AWS_IOT* myiot = new AWS_IOT();

Serial.println("after myiot created");
Serial.println(ESP.getFreeHeap());
Pulse_count = (String)micros();
wifi_mqtt_send(myiot); // fun to send data to cloud.
delete myiot;
myiot = NULL;
aws_status = 0;
// free(myiot);
delay(2000);
Serial.println("after wifi");
Serial.println(ESP.getFreeHeap());
Serial.print("beacon started again heap = ");
Serial.println(ESP.getFreeHeap());
Serial.println("beacon started again");
beacon_scan_start = micros();
send_cloud_flag = 0;
beacon_fStop = 0;
}

void wifi_mqtt_send(AWS_IOT* myiot)
{
Serial.print("TIME TAKEN TO SEND START = ");
Serial.println(micros());
Serial.println("\n----------------------------------------------------------------");
Serial.print("wifi status :: ");
Serial.println(status);
//check if wifi connected or not.
//if connected then execute if part otherwise it execute else part to connect with wifi and further process2
if (status == WL_CONNECTED)
{
wifi_mac = WiFi.macAddress();
Serial.print("BRIDGE Device ID :: ");////////////////
Serial.println(wifi_mac);
delay(2000);
//if connected then execute if part otherwise else part
if (aws_status == 1)
{
JSON_PACKET(); // JSON_DATA_PACKET STORE AND APEND
aws_data(myiot); //data ready to publish
}
else
{
aws(myiot); // AMAZON CLOUD connect
JSON_PACKET(); // JSON_DATA_PACKET STORE AND APEND
aws_data(myiot); //data ready to publish
}
}
else // first of all connect to wifi and do further process
{
wifi(); // wifi reconnect
if (aws_status == 1) //if connected then execute if part otherwise else part
{
JSON_PACKET(); // JSON_DATA_PACKET STORE AND APEND
aws_data(myiot); //data ready to publish
}
else // firstly create aws connection and then execute rest of like json packet and aws data
{
aws(myiot); // AMAZON CLOUD connect
JSON_PACKET(); // JSON_DATA_PACKET STORE AND APEND
aws_data(myiot); //data ready to publish
}
}

Serial.print("TIME TAKEN TO SEND END = ");
Serial.println(micros());
delay(2500);
Serial.println("before wifi disconnect");
void disconnectCallbackHandler();

Serial.println(ESP.getFreeHeap());

WiFi.disconnect();

Serial.println("WiFi is disconnectd");
Serial.println("after wifi disconnect");
Serial.println(ESP.getFreeHeap());
}

void wifi()
{
//************** wifi *********************************
Serial.print("Connecting to ");
Serial.println(WIFI_SSID);

WiFi.begin(WIFI_SSID, WIFI_PASSWORD);

while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}

Serial.println("");
Serial.println("------------------------------------------------\n");

Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.print("WIFI Strength :: ");
Serial.println( WiFi.RSSI());
Serial.print("WiFi MAC address :: ");
Serial.println(WiFi.macAddress());
Serial.println("------------------------------------------------\n");
}

void aws(AWS_IOT* myiot)
{
// Try to connect to AWS and count how many times we retried.
int retries = 0;
Serial.println("Checking connection for AWS IOT");
delay(2500);// if return value = 0 successfully cloud connect

while (myiot->connect(HOST_ADDRESS, CLIENT_ID) != 0 && retries < AWS_MAX_RECONNECT_TRIES) // Connect to AWS using Host Address and Cliend ID
{
Serial.println(" Connecting to AWS... ");
delay(2000);
retries++;
}

if (myiot->connect(HOST_ADDRESS, CLIENT_ID) == 0)
{
Serial.println(" AWS_cloud is connected ");
aws_status = 1;
delay(100);
return;
}

void aws_data(AWS_IOT* myiot)
{
if (myiot->publish(TOPIC_NAME, payload) == 0) // Publish the message(Temp and humidity)
{
Serial.println("\n------------------------------------------------");
Serial.print("\nPublish Message :: ");
Serial.println(payload);
Serial.print("\nSize of monitor data packet after write :: ");
Serial.print(sizeof(payload));
Serial.println(" bytes");
Serial.println("\n------------------------------------------------");
Serial.println("Published monitor data packet to AWS_CLOUD successfully");
Serial.println("------------------------------------------------");
delay(1000);
memset(payload, 0, 500 * sizeof(payload[0]));
Serial.println("\n------------------------------------------------");
Serial.print("Size of payload after free ::");
Serial.print(sizeof(payload));
Serial.println(" bytes");
delay(1000);
data_cnt++;
}
}

}

void JSON_PACKET()
{
// JSON memory allocation
DynamicJsonDocument doc(350); //data packet buffer
doc["mac_add"] = mac_add;
doc[" Pulse_count"] = Pulse_count;
doc["Battery_level"] = Battery_level ;
Serial.println("JSON DATA PACKET :");
serializeJsonPretty(doc, Serial);
// serializeJson(doc, Serial); //show on serial
serializeJson(doc, payload);//all obj stored from doc to playload
}

chegewara
Posts: 2364
Joined: Wed Jun 14, 2017 9:00 pm

Re: ESP 32 AWS_IOT library deinit and get heap back

Postby chegewara » Mon Oct 19, 2020 4:57 pm

Most of 200kB is used by wifi which you do not deinit/free.

Ajinkya_777
Posts: 8
Joined: Tue Sep 15, 2020 4:42 am

Re: ESP 32 AWS_IOT library deinit and get heap back

Postby Ajinkya_777 » Mon Oct 19, 2020 5:45 pm

chegewara wrote:
Mon Oct 19, 2020 4:57 pm
Most of 200kB is used by wifi which you do not deinit/free.
I want to use BLE to scan and receive data to send by MQTT through wifi and ble need 90k to init, in total I have 233k heap to use after sketch has loaded in the ESP32.

I'm disconnecting Wifi after sending data to cloud, but it also doesn't help to reduce heap, Free heap looks almost the same.
If this is the case then we can not use BLE and Wifi at the same time!

So is their any way to do it so?

chegewara
Posts: 2364
Joined: Wed Jun 14, 2017 9:00 pm

Re: ESP 32 AWS_IOT library deinit and get heap back

Postby chegewara » Tue Oct 20, 2020 7:31 am

From my experience i can say it is possible to use ble + wifi + aws IoT in arduino.

Ajinkya_777
Posts: 8
Joined: Tue Sep 15, 2020 4:42 am

Re: ESP 32 AWS_IOT library deinit and get heap back

Postby Ajinkya_777 » Tue Oct 20, 2020 9:05 am

chegewara wrote:
Tue Oct 20, 2020 7:31 am
From my experience i can say it is possible to use ble + wifi + aws IoT in arduino.
OK Thank You so much.

Anyone else have any suggestion are welcome.

Who is online

Users browsing this forum: username and 85 guests