m5paper_bluetooth_beacon.ino
Transforms the M5Paper into a BLE server to interact with smartphones or other IoT devices.
Uses the native ESP32 BLE stack. Define SERVICE_UUID and CHARACTERISTIC_UUID with unique UUIDs. The screen updates the connection status. BLE mode is power-hungry; combine with deep sleep if waiting is long. Test with an app like 'nRF Connect'.
m5paper_bluetooth_beacon.ino
#include <M5EPD.h>
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
BLEServer *pServer = NULL;
BLECharacteristic *pCharacteristic = NULL;
bool deviceConnected = false;
M5EPD_Canvas canvas(&M5.EPD);
class MyServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
canvas.clear();
canvas.setCursor(10, 10);
canvas.print("Client Connected!");
canvas.pushCanvas(0, 0, UPDATE_MODE_GC16);
}
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
canvas.clear();
canvas.setCursor(10, 10);
canvas.print("Waiting for Client...");
canvas.pushCanvas(0, 0, UPDATE_MODE_GC16);
BLEDevice::startAdvertising();
}
};
void setup() {
M5.begin();
M5.EPD.SetRotation(90);
canvas.createCanvas(540, 960);
canvas.setTextSize(3);
BLEDevice::init("M5Paper_Beacon");
pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
BLEService *pService = pServer->createService(SERVICE_UUID);
pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE
);
pCharacteristic->setValue("Hello World");
pService->start();
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pAdvertising->start();
canvas.setCursor(10, 10);
canvas.print("BLE Ready");
canvas.pushCanvas(0, 0, UPDATE_MODE_GC16);
}
void loop() {
delay(100);
}