BLE UART Communication between ESP32-C3 (Xiao ESP32C3 and NodeMCU ESP-C3-32S-Kit)

The previous post show a exercise of "BLE UART Communication between XIAO ESP32C3 (client/central) and nRF52840 (server/peripheral), in Arduino Framework".

This exercise implement the server/peripheral side on NodeMCU ESP-C3-32S-Kit with SSD1331 SPI Color OLED.


The client/central side, XESP32C3_BLE_client_OLED_Terminal.ino, run on XIAO ESP32C3 with SSD1306 OLED, refer to the previous exercise.

ESP32C3_BLE_uart_server_SSD1331.ino, server/peripheral side on NodeMCU ESP-C3-32S-Kit with SSD1331 SPI Color OLED.
/*
 * Modify Examples for ESP32C3 Dev Module
 *        > ESP32 BLE Arduino
 *        > BLE_uart
 * Run on NodeMCU ESP-C3-32S-Kit.
 * With UUID of Nordic UART Service (NUS), to work with 
 * XESP32C3_BLE_client_OLED_Terminal run on Xiao ESP32C3.
 */
 
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1331.h>
#include <SPI.h>

// connection to SSD1331 SPI Color OLED
// using software SPI
#define sclk 10
#define mosi 9
#define cs   6
#define rst  8
#define dc   7

Adafruit_SSD1331 display = Adafruit_SSD1331(cs, dc, mosi, sclk, rst);

// Color definitions
#define BLACK           0x0000
#define BLUE            0x001F
#define RED             0xF800
#define GREEN           0x07E0
#define CYAN            0x07FF
#define MAGENTA         0xF81F
#define YELLOW          0xFFE0
#define WHITE           0xFFFF

#define LED_Warm  18

BLEServer *pServer = NULL;
BLECharacteristic * pTxCharacteristic;
bool deviceConnected = false;
bool oldDeviceConnected = false;

// UUID of Nordic UART Service (NUS)
#define SERVICE_UUID           "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID
#define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
#define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"


class MyServerCallbacks: public BLEServerCallbacks {
    void onConnect(BLEServer* pServer) {
      deviceConnected = true;
      digitalWrite(LED_Warm, HIGH);
    };

    void onDisconnect(BLEServer* pServer) {
      deviceConnected = false;
      digitalWrite(LED_Warm, LOW);
    }
};

class MyCallbacks: public BLECharacteristicCallbacks {
    void onWrite(BLECharacteristic *pCharacteristic) {
      std::string rxValue = pCharacteristic->getValue();

      if (rxValue.length() > 0) {
        Serial.println("*********");
        Serial.print("Received Value: ");
        for (int i = 0; i < rxValue.length(); i++){
          Serial.print(rxValue[i]);
          display.print(rxValue[i]);
        }
        Serial.println();
        Serial.println("*********");

      }
    }
};


void setup() {
  Serial.begin(115200);

  pinMode(LED_Warm, OUTPUT);
  digitalWrite(LED_Warm, LOW);

  display.begin();
  display.fillScreen(BLUE);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.print("BLE Server run on ESP32C3 with SSD1331 Color OLED");
  display.print("\n\n");

  // Create the BLE Device
  BLEDevice::init("UART Service");

  // Create the BLE Server
  pServer = BLEDevice::createServer();
  pServer->setCallbacks(new MyServerCallbacks());

  // Create the BLE Service
  BLEService *pService = pServer->createService(SERVICE_UUID);

  // Create a BLE Characteristic
  pTxCharacteristic = pService->createCharacteristic(
										CHARACTERISTIC_UUID_TX,
										BLECharacteristic::PROPERTY_NOTIFY
									);
                      
  pTxCharacteristic->addDescriptor(new BLE2902());

/*
  BLECharacteristic * pRxCharacteristic = pService->createCharacteristic(
                      CHARACTERISTIC_UUID_RX,
											BLECharacteristic::PROPERTY_WRITE
										);
*/
  BLECharacteristic * pRxCharacteristic = pService->createCharacteristic(
                      CHARACTERISTIC_UUID_RX,
                      BLECharacteristic::PROPERTY_READ   |
                      BLECharacteristic::PROPERTY_WRITE  |
                      BLECharacteristic::PROPERTY_NOTIFY |
                      BLECharacteristic::PROPERTY_INDICATE
                    );
                    
  pRxCharacteristic->setCallbacks(new MyCallbacks());

  // Start the service
  pService->start();

  /*
  // Start advertising
  pServer->getAdvertising()->start();
  */

  /*
   * this part follow BLE_server example
   */
  // Start advertising
  BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  pAdvertising->addServiceUUID(SERVICE_UUID);
  //pAdvertising->setScanResponse(false);
  pAdvertising->setScanResponse(true);
  //pAdvertising->setMinPreferred(0x0);  // set value to 0x00 to not advertise this parameter
  pAdvertising->setMinPreferred(0x06);  // functions that help with iPhone connections issue
  pAdvertising->setMinPreferred(0x12);
  
  BLEDevice::startAdvertising();
  
  Serial.println("Waiting a client connection to notify...");
}

void loop() {

    if (deviceConnected) {

        if (Serial.available() > 0) {
          uint8_t incomingByte = Serial.read();
          pTxCharacteristic->setValue(&incomingByte, 1);
          pTxCharacteristic->notify();
        }
        /*
        pTxCharacteristic->setValue(&txValue, 1);
        pTxCharacteristic->notify();
        txValue++;
        */
		//delay(1000); // bluetooth stack will go into congestion, if too many packets are sent
	}

    // disconnecting
    if (!deviceConnected && oldDeviceConnected) {
        delay(500); // give the bluetooth stack the chance to get things ready
        pServer->startAdvertising(); // restart advertising
        Serial.println("start advertising");
        oldDeviceConnected = deviceConnected;
    }
    // connecting
    if (deviceConnected && !oldDeviceConnected) {
		// do stuff here on connecting
        oldDeviceConnected = deviceConnected;
    }
}



Comments

Popular posts from this blog

480x320 TFT/ILI9488 SPI wih EP32C3 (arduino-esp32) using Arduino_GFX Library

my dev.tools - FNIRSI 2C23T 3-in-1 Dual Channel Oscilloscope/Multimeter/Signal Generator