Arduino & ESP32 - Basic Bluetooth LE ( + Nordic UART Service / NUS)
-
GUI-O + Arduino + ESP32
Developer manual: https://www.gui-o.com/assets/gui-o_developer_manual.pdf
A DEMO project to help you quickly get started with GUI-O and Bluetooth Low Energy (BLE) using Esp32 board. You can expand the example for your specific project.
Download BasicBLE.ino sketch from:
https://drive.google.com/file/d/1a0Bu12UIyjZ9aeZ2VZ5yV6gpKvtLbs2A/view?usp=sharingFor NUS (Nordic UART Service):
https://drive.google.com/file/d/1OVhITiGA2CAfbz2kE0QTNLCD5d2YBUAu/view?usp=sharingAdditional Boards Manager URLs:
https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json/* * GUI-O Basic BLE example * * Copyright (C) 2021, GUI-O Team * * SPDX-License-Identifier: BSD-3-Clause */ #include <BLEDevice.h> #include <BLEServer.h> #include <BLEUtils.h> #include <BLE2902.h> namespace led { static const uint8_t LED_BUILTIN = 2; static const uint8_t LED_CHANNEL = 0; static const double LED_FREQ = 5000.0; static const uint8_t LED_BITS = 8; } // namespace led namespace uuid { static const char *SERVICE_UUID = "6E400001-B5A3-F393-E0A9-E50E24DCCA9E"; static const char *CHARACTERISTIC_UUID = "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"; } // namespace uuid // forward declare parser for incoming messages void parseGuioMsg(const String &msg); // custom handling of server callbacks class CustomBLEServerCallbacks: public BLEServerCallbacks { void onConnect(BLEServer* pServer) { Serial.println("Connected!"); }; void onDisconnect(BLEServer* pServer) { Serial.println("Disconnected!"); } }; // custom handling of characteristic callbacks class CustomBLECharacteristicCallbacks: public BLECharacteristicCallbacks { void onWrite(BLECharacteristic *pCharacteristic) { std::string msg = pCharacteristic->getValue(); // parse message string parseGuioMsg(String(msg.c_str())); } }; // global ptr BLECharacteristic *pCharacteristic; void setup() { // debug output Serial.begin(115200); // setup builtin led ledcSetup(led::LED_CHANNEL, led::LED_FREQ, led::LED_BITS); // channel, freq, resolution_bits ledcAttachPin(led::LED_BUILTIN, led::LED_CHANNEL); // create device BLEDevice::init("BasicBLE"); // create server and register callback BLEServer *pServer = BLEDevice::createServer(); pServer->setCallbacks(new CustomBLEServerCallbacks()); // create service BLEService *pService = pServer->createService(uuid::SERVICE_UUID); // crate characteristic, register callback and add descriptor pCharacteristic = pService->createCharacteristic(uuid::CHARACTERISTIC_UUID, BLECharacteristic::PROPERTY_NOTIFY | BLECharacteristic::PROPERTY_INDICATE | BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_READ); pCharacteristic->setCallbacks(new CustomBLECharacteristicCallbacks()); pCharacteristic->addDescriptor(new BLE2902()); // start the service and start advertising pService->start(); pServer->getAdvertising()->start(); } void loop() { } /***************************/ /* IMPLEMENT YOUR GUI HERE */ /***************************/ void sendMsg(const String &msg) { pCharacteristic->setValue(std::string(msg.c_str())); pCharacteristic->notify(); delay(50); } void parseGuioMsg(const String &msg) { if(msg.startsWith("@init")) { Serial.println("GUI-O app is requesting INITIALIZATION!"); // clear screen and set background sendMsg("@cls\r\n"); sendMsg("@guis BGC:#FFFFFF\r\n"); delay(100); // initialize simple example GUI sendMsg("|LB UID:title X:50 Y:15 TXT:\"Simple light switch\" FFA:\"font8\" FSZ:3.5\r\n"); sendMsg("|LB UID:tap_me X:50 Y:70 TXT:\"TAP ME!\" FFA:\"font8\" FSZ:3 FFA:\"font5\"\r\n"); sendMsg("|CB UID:brightness X:50 Y:50 W:90 BTH:5 HAH:8 HAW:8 VIS:0 STA:135 ENA:45 FGC:#000000 SFGC:#FFFF00 BGC:#CBCBCB\r\n"); sendMsg("|IM UID:light_off X:50 Y:50 IP:\"https://i.imgur.com/3VbsS0Z.png\" VIS:1\r\n"); sendMsg("|IM UID:light_on X:50 Y:50 IP:\"https://i.imgur.com/gNdck9A.png\" VIS:0\r\n"); } else if(msg.startsWith("@light_off")) { Serial.println("GUI-O app is requesting LIGHT ON!"); // "drive GUI-O app" sendMsg("@light_off VIS:0\r\n"); sendMsg("@light_on VIS:1\r\n"); sendMsg("@brightness VIS:1 VAL:100\r\n"); // led enable ledcWrite(led::LED_CHANNEL, 255); } else if(msg.startsWith("@light_on")) { Serial.println("GUI-O app is requesting LIGHT OFF!"); // "drive GUI-O app" sendMsg("@light_off VIS:1\r\n"); sendMsg("@light_on VIS:0\r\n"); sendMsg("@brightness VIS:0 VAL:0\r\n"); // led disable ledcWrite(led::LED_CHANNEL, 0); } else if(msg.startsWith("@brightness")) { const int idx = msg.indexOf(' '); if(idx > 0) { const int val = msg.substring(idx + 1).toInt(); if(val >= 0 && val <= 100) { Serial.print("GUI-O app is requesting LIGHT BRIGHTNESS: "); Serial.println(val); // led drive ledcWrite(led::LED_CHANNEL, static_cast<uint8_t>(val * 2.55)); } } } }