chegewara / esp32-snippets

Sample ESP32 snippets and code fragments
https://leanpub.com/kolban-ESP32
Apache License 2.0
9 stars 4 forks source link

mbedtls handshake after BLE init fails #2

Open wegunterjr opened 6 years ago

wegunterjr commented 6 years ago

This is my heap readout during each step of connection. It gets to the handshake part with about 68k, but it fails there.. any suggestions? `

Heap beginning of setup() AWS IOT using WiFiClientSecure program: 152068 Heap afger BLE init: 105212 ****Heap after WiFi begin: 68236 Heap after rootCABuff: 68236 Heap after certificateBuff: 68236 Heap after privateKeyBuff: 68236 Heap afger setting certs: 68236 Heap after trying to connect: 68236 Checking wifi... connecting...[E][ssl_client.cpp:33] handle_error(): RSA - The private key operation failed : BIGNUM - Memory allocation failed [E][ssl_client.cpp:35] handle_error(): MbedTLS message code: -17168

`

chegewara commented 6 years ago

Like i said before, you have to change order you are doing your calls.

You need at least 75kB free heap to even start with mbedTLS (or maybe even more). To get even more free ram i had to delete default task in arduino:

Also, like i said before (maybe its bad idea), i have task which is doing some operations and create another task and delete itself. This way im in position where my last task is using minimum amount of heap.

There is still big issue, when your aws IoT disconnecting for some reason, you wont be able to connect because there is not enough free heap. Stopping ble, deleting ble task or any other actions on ble wont help because bluedroid stack (esp-idf ble) is statically load into ram.

wegunterjr commented 6 years ago

@chegewara WOW! 75K.... I did release bt classic memory in setup(), and that is a little better, but still... wow!

chegewara commented 6 years ago

Its mostly because mbedTLS handshake requires 2 buffers (TX and RX) 15kB each. Then library itself and probably aes library, its a lot.

wegunterjr commented 6 years ago

How do you get 200k?

Hi, its very hard to make it both work together. Its because ble is using a lot of ram and mbedTLS (ssl) needs a lot of ram for handshake. You can make it work togheter, but you need to free as much heap as you can before you even start, then connect with aws mqtt and when you are connected then you can finally connect to ble peer device. In my app i am starting with over 200kB free heap and when i am connected to ble device i have about 20kB free heap.

wegunterjr commented 6 years ago

my challenge is that the user workflow i am working on now has them find the device using BLE then connect to wifi... I guess I could consider alternative workflow...

chegewara commented 6 years ago

I dont remember, but right now just to check it and i have less:

rst:0x10 (RTCWDT_RTC_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:952
load:0x40078000,len:6084
load:0x40080000,len:7936
entry 0x40080310
FREE stack: 7492
FREE heap: 183108

Here is few lines of my setup():

void setup() {
  Serial.begin(115200);
  esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT);
      Serial.print("FREE stack: ");
      Serial.println(uxTaskGetStackHighWaterMark(NULL));
      Serial.print("FREE heap: ");
      Serial.println(esp_get_free_heap_size());
  BLEDevice::init("");

And all includes, but im not sure i am using all right now:

#include <HTTPClient.h>
#include <WiFiClientSecure.h>
#include <MQTTClient.h>   //you need to install this library: https://github.com/256dpi/arduino-mqtt
#include <ArduinoJson.h>
#include "BLEDevice.h"
#include <freertos/event_groups.h>