I want to display the state of a BLE connection (connected or not) by using the builtin RGB Led (Green; connected; Red : Disconnected).
To do so i use the RMT as said in this example : https://espressif-docs.readthedocs-host ... /rmt.html.
But this does not seem to work whith BLE.
Here is my code.
Is something wrong, or theses ressources simply cannot work together ? Is ther another way to pilot thr RGB LED ?
Thanks
FB
- #include <BLEDevice.h>
- #include <BLEUtils.h>
- #include <BLEServer.h>
- #define sensorPin 34
- #define SERVICE_UUID "9aab5446-bb20-11ec-8422-0242ac120002"
- #define CHARACTERISTIC_UUID_Read_Write "9aab56bc-bb20-11ec-8422-0242ac120002"
- #define BUILTIN_RGBLED_PIN 8 // RGB builtin Led on ESP32C6
- #define NR_OF_LEDS 1
- #define NR_OF_ALL_BITS 24*NR_OF_LEDS
- int sensorValue=0;
- int incomingByte = 0; // for incoming serial data
- int n=1;
- String LedCommand="";
- int color_On_Connect[] = { 0xFF, 0x00, 0x00 }; // Green Red Blue values
- int color_On_Disconnect[] = { 0x00, 0xFF, 0x00 }; // Green Red Blue values
- int color_On_Data[] = { 0xFF, 0xFF, 0x00 }; // Green Red Blue values
- int color_Off[] = { 0x00, 0x00, 0x00 }; // Green Red Blue values
- rmt_data_t led_data[NR_OF_ALL_BITS];
- void LED_Command(int*);
- BLEServer *BLE_Server = NULL;
- BLECharacteristic *OnReadWriteCharacteristic;
- BLEService *BLE_Service;
- BLEAdvertising *BLE_Advertising;
- bool deviceConnected = false;
- class MyServerCallbacks: public BLEServerCallbacks
- {
- void onConnect(BLEServer* pServer)
- {
- deviceConnected = true;
- BLEDevice::stopAdvertising();
- //Serial.println("BT connecté");
- LED_Command(color_On_Connect);
- delay(1000);
- LED_Command(color_Off);
- };
- void onDisconnect(BLEServer* pServer)
- {
- deviceConnected = false;
- BLEDevice::startAdvertising();
- //Serial.println("BT NON connecté");
- LED_Command(color_On_Disconnect);
- delay(1000);
- LED_Command(color_Off);
- }
- };
- class MyReadWriteCallbacks: public BLECharacteristicCallbacks
- {
- void onWrite(BLECharacteristic *pOnWrite)
- {
- String value = pOnWrite->getValue();
- if (value.length() > 0) {
- LedCommand = "";
- for (int i = 0; i < value.length(); i++){
- LedCommand = LedCommand + value[i];
- }
- if (LedCommand.equals("on")) LED_Command(color_On_Data);
- if (LedCommand.equals("off")) LED_Command(color_Off);
- }
- }
- void onRead(BLECharacteristic *pOnRead)
- {
- /*
- sensorValue=analogRead(sensorPin);
- pOnRead->setValue(("Data Nr " + String(n) + " : " + String(sensorValue)).c_str());
- pOnRead->notify();
- n++;
- */
- }
- };
- void setup() {
- rmtInit(BUILTIN_RGBLED_PIN, RMT_TX_MODE, RMT_MEM_NUM_BLOCKS_1, 10000000);
- LED_Command(color_Off);
- BLEDevice::init("ESP32_BLE_Test");
- BLEDevice::setMTU(256);
- BLE_Server = BLEDevice::createServer();
- BLE_Server->setCallbacks(new MyServerCallbacks());
- BLE_Service = BLE_Server->createService(SERVICE_UUID);
- OnReadWriteCharacteristic = BLE_Service->createCharacteristic(
- CHARACTERISTIC_UUID_Read_Write,
- BLECharacteristic::PROPERTY_READ |
- BLECharacteristic::PROPERTY_WRITE
- );
- OnReadWriteCharacteristic->setCallbacks(new MyReadWriteCallbacks());
- BLE_Service->start();
- //Send_Data_Characteristic->setValue("Bonjour le monde...");
- BLE_Advertising = BLEDevice::getAdvertising();
- BLE_Advertising->addServiceUUID(SERVICE_UUID);
- BLE_Advertising->setScanResponse(true);
- BLE_Advertising->setMinPreferred(0x06); // functions that help with iPhone connections issue //0x06
- BLE_Advertising->setMaxPreferred(0x12); // 0x12
- BLEDevice::startAdvertising();
- }
- void loop() {
- // put your main code here, to run repeatedly:
- }
- void LED_Command(int* color){
- //
- // Note: This example uses Neopixel LED board, 32 LEDs chained one
- // after another, each RGB LED has its 24 bit value
- // for color configuration (8b for each color)
- //
- // Bits encoded as pulses as follows:
- //
- // "0":
- // +-------+ +--
- // | | |
- // | | |
- // | | |
- // ---| |--------------|
- // + + +
- // | 0.4us | 0.85 0us |
- //
- // "1":
- // +-------------+ +--
- // | | |
- // | | |
- // | | |
- // | | |
- // ---+ +-------+
- // | 0.8us | 0.4us |
- // Init data with only one led ON
- int led, col, bit;
- int i=0;
- for (col=0; col<3; col++ ) {
- for (bit=0; bit<8; bit++){
- if ( (color[col] & (1<<(7-bit)))) {
- led_data[i].level0 = 1;
- led_data[i].duration0 = 8;
- led_data[i].level1 = 0;
- led_data[i].duration1 = 4;
- } else {
- led_data[i].level0 = 1;
- led_data[i].duration0 = 4;
- led_data[i].level1 = 0;
- led_data[i].duration1 = 8;
- }
- i++;
- }
- }
- // Send the data and wait until it is done
- rmtWriteAsync(BUILTIN_RGBLED_PIN, led_data, NR_OF_ALL_BITS);
- }