Hello
I'm playing with the Wifi and BLE connexions of the ESP32. I use the BLE libraries from Neil Kolban.
I built a sketch that receives data from an Android app I developed using App Inventor and it works. I wanted to add the possibility to connect on my Wifi network to get the current time, using a sketch which works also on its own.
When joining both sketches, the IDE says that the size is 123% of the available memory, although there are less than or around 50 code lines at all. I had to change the memory option of the Arduino IDE to no OTA to be able to upload it.
Do you know why I had this problem? Is it coming from the libraries used which are too heavy? Can anyone suggest other libraries for BLE and Wifi that are lighter?
The other problem I ran into (but I knew it would be such) is that when I want to connect to the Wifi, it forces the BLE to disconnect. Is there a way to disconnect BLE before connecting to the Wifi and auto-reconnect the BLE after disconnecting from the Wifi, without any action on the Android app side?
Size of a sketch
Re: Size of a sketch
Actually, it's 180 lines...
How can I reduce the size?
Code: Select all
/*
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleWrite.cpp
Ported to Arduino ESP32 by Evandro Copercini
*/
#include <WiFi.h>
#include "time.h"
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
// See the following for generating UUIDs:
// https://www.uuidgenerator.net/
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
const char* ssid = "Lesept";
const char* password = "LeAnaisJuju030198";
const char* ntpServer = "de.pool.ntp.org";
const long gmtOffset_sec = 3600;
const int daylightOffset_sec = 3600;
struct tm timeinfo;
byte n = 0;
/*
struct tm
{
int tm_sec; // Seconds [0,60].
int tm_min; // Minutes [0,59].
int tm_hour; // Hour [0,23].
int tm_mday; // Day of month [1,31].
int tm_mon; // Month of year [0,11].
int tm_year; // Years since 1900.
int tm_wday; // Day of week [0,6] (Sunday =0).
int tm_yday; // Day of year [0,365].
int tm_isdst; // Daylight Savings flag.
}
*/
class MyCallbacks: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
std::string value = pCharacteristic->getValue();
char carac;
if (value.length() > 0) {
Serial.print("New value: ");
for (int i = 0; i < value.length(); i++) Serial.print(value[i]);
Serial.print (" : ");
carac = value[0];
switch (carac) {
case 'N':
Serial.println("Minutes ON");
break;
case 'F':
Serial.println("Minutes OFF");
break;
case 'O':
Serial.println("Fond ON");
break;
case 'E':
Serial.println("Fond OFF");
break;
case 'U':
{
Serial.println("Couleur");
byte red = 0;
int i = 1;
while (value[i] != ',') {
red = red * 10 + value[i] - '0';
i++;
}
Serial.print (red);
Serial.print (" - ");
i++;
byte green = 0;
while (value[i] != ',') {
green = green * 10 + value[i] - '0';
i++;
}
Serial.print (green);
Serial.print (" - ");
i++;
byte blue = 0;
while (value[i] != '*') {
blue = blue * 10 + value[i] - '0';
i++;
}
Serial.println (blue);
break;
}
case 'A':
{
Serial.print("Animation ");
int numAnim = 0;
int i = 1;
while (value[i] != '*') {
numAnim = numAnim * 10 + value[i] - '0';
i++;
}
Serial.println(numAnim);
break;
}
case 'P':
{
Serial.print("Palette ");
int numPal = 0;
int i = 1;
while (value[i] != '*') {
numPal = numPal * 10 + value[i] - '0';
i++;
}
Serial.println(numPal);
break;
}
}
Serial.println();
}
}
};
void printLocalTime()
{
getLocalTime(&timeinfo);
Serial.println(&timeinfo, "%A, %d %B %Y %H:%M:%S");
}
void WifiGetTime () {
//connect to WiFi
Serial.printf("Connecting to %s ", ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(" CONNECTED");
//init and get the time
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
printLocalTime();
//disconnect WiFi as it's no longer needed
WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);
}
void BLEStart () {
Serial.println("Please connect Bluetooth LE device");
BLEDevice::init("MyESP32");
BLEServer *pServer = BLEDevice::createServer();
BLEService *pService = pServer->createService(SERVICE_UUID);
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
);
pCharacteristic->setCallbacks(new MyCallbacks());
pCharacteristic->setValue("Horloge Infinie (Lesept)");
pService->start();
BLEAdvertising *pAdvertising = pServer->getAdvertising();
pAdvertising->start();
}
void setup() {
Serial.begin(115200);
WifiGetTime ();
BLEStart ();
}
void loop() {
delay(1000);
printLocalTime();
n++;
if (n == 60) {
Serial.println(F("Connecting to Wifi to get accurate time"));
WifiGetTime ();
n = 0;
}
}
Re: Size of a sketch
Lesept, it looks like you never received an answer to your question. The problem with the size is simply the size of the code in the include files for the libraries. I have just started working with this library and I am finding that with only the setup coded to initialize BLE, 90% of the programming memory is used. I plan to remove include statements to see if I can do without some of them or I'll look for another library. The one that Neil Kolban wrote is great but it too large. If I see ways to trim or refactor the code, I may fork Neil's code and submit a pull request. I'm trying to work several projects at the moment so trying to refactor working code is near the bottom of the list.
Who is online
Users browsing this forum: No registered users and 50 guests