Skip to content

Instantly share code, notes, and snippets.

@scottyob
Created February 28, 2025 01:12
Show Gist options
  • Save scottyob/bb35ccc4771b44efb5a5f165689eece5 to your computer and use it in GitHub Desktop.
Save scottyob/bb35ccc4771b44efb5a5f165689eece5 to your computer and use it in GitHub Desktop.
#include <Arduino.h>
#include <WiFi.h>
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#include "esp_heap_caps.h"
#include "esp_system.h"
BLEServer* pServer = nullptr;
BLECharacteristic* pCharacteristic = nullptr;
bool deviceConnected = false;
uint32_t counter = 0;
#define SERVICE_UUID "12345678-1234-5678-1234-56789abcdef0"
#define CHARACTERISTIC_UUID "abcdef01-1234-5678-1234-56789abcdef0"
void printMemoryStats() {
// Heap memory info
uint32_t freeHeap = esp_get_free_heap_size();
uint32_t minFreeHeap = esp_get_minimum_free_heap_size();
uint32_t usedHeap = heap_caps_get_total_size(MALLOC_CAP_INTERNAL) - freeHeap;
uint32_t totalHeap = freeHeap + usedHeap;
Serial.println("=== Memory Stats ===");
Serial.printf("Total Heap: %u bytes\n", totalHeap);
Serial.printf("Free Heap: %u bytes\n", freeHeap);
Serial.printf("Used Heap: %u bytes\n", usedHeap);
Serial.printf("Largest Free Block: %u bytes\n", heap_caps_get_largest_free_block(MALLOC_CAP_8BIT));
Serial.printf("Minimum Free Heap Ever: %u bytes\n", minFreeHeap);
Serial.printf("Main Task Stack High Water Mark: %u bytes\n", uxTaskGetStackHighWaterMark(NULL));
#if CONFIG_SPIRAM_USE_MALLOC
Serial.printf("Free PSRAM: %u bytes\n", heap_caps_get_free_size(MALLOC_CAP_SPIRAM));
Serial.printf("Largest Free PSRAM Block: %u bytes\n", heap_caps_get_largest_free_block(MALLOC_CAP_SPIRAM));
#else
Serial.println("PSRAM not available.");
#endif
Serial.println(WiFi.localIP());
Serial.println("====================\n");
}
// BLE Server Callbacks
class MyServerCallbacks : public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
Serial.println("BLE device connected.");
}
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
Serial.println("BLE device disconnected.");
pServer->startAdvertising();
}
};
void setupBLE() {
BLEDevice::init("ESP32_BLE_Test");
pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
BLEService* pService = pServer->createService(SERVICE_UUID);
pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_NOTIFY
);
pService->start();
pServer->getAdvertising()->start();
Serial.println("BLE Server started.");
}
void setupWiFi() {
Serial.print("Connecting to WiFi...");
WiFi.begin();
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi connected.");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
}
void setup() {
Serial.begin(115200);
delay(1000);
setupWiFi();
setupBLE();
}
void loop() {
printMemoryStats();
if (deviceConnected) {
counter++;
pCharacteristic->setValue((uint8_t*)&counter, sizeof(counter));
pCharacteristic->notify();
Serial.println("BLE Notification sent.");
}
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment