/* Ichikuma added the feature for Module ESP32C3 on May 2023 PC <-- Bluetooth Low Energy --> ESP32C3 <-- Serial1(UART) --> SNTC(Serial Network Train Control) of ik_gadgets */ /* Video: https://www.youtube.com/watch?v=oCMOYS71NIU Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleNotify.cpp Ported to Arduino ESP32 by Evandro Copercini Create a BLE server that, once we receive a connection, will send periodic notifications. The service advertises itself as: 6E400001-B5A3-F393-E0A9-E50E24DCCA9E Has a characteristic of: 6E400002-B5A3-F393-E0A9-E50E24DCCA9E - used for receiving data with "WRITE" Has a characteristic of: 6E400003-B5A3-F393-E0A9-E50E24DCCA9E - used to send data with "NOTIFY" The design of creating the BLE server is: 1. Create a BLE Server 2. Create a BLE Service 3. Create a BLE Characteristic on the Service 4. Create a BLE Descriptor on the characteristic 5. Start the service. 6. Start advertising. In this example rxValue is the data received (only accessible inside that function). And txValue is the data to be sent, in this example just a byte incremented every second. */ #include #include #include #include #include #define BAUD 57600 // Serial1 baud #define RXPIN 20 // GPIO 20 (ESP32C3 8pin) => RX for Serial1 #define TXPIN 6 // GPIO 6 (ESP32C3 5pin) => TX for Serial1 #define LEDPIN 10 std::string BtoU_data; #define BUFSIZE 256 uint8_t UtoB_data[BUFSIZE]; #define rxTimeout 1 BLEServer *pServer = NULL; BLECharacteristic *pTxCharacteristic; BLECharacteristic *pRxCharacteristic; bool deviceConnected = false; bool oldDeviceConnected = false; // See the following for generating UUIDs: // https://www.uuidgenerator.net/ #define SERVICE_UUID "d8668fd8-646f-45a4-a7a5-2cbf7ce534e5" #define CHARACTERISTIC_UUID_RX "52506b1f-01e3-4e83-991f-cacf437af658" #define CHARACTERISTIC_UUID_TX "0a7151b3-d8e6-47bb-b615-e21d0ff1e681" class MyServerCallbacks : public BLEServerCallbacks { void onConnect(BLEServer *pServer) { deviceConnected = true; }; void onDisconnect(BLEServer *pServer) { deviceConnected = false; } }; void UtoB_Function(void) { size_t available = Serial1.available(); if (available > 0) { size_t UtoB_size = Serial1.read(UtoB_data,BUFSIZE); std::string UtoB_str = ""; for (int i=0; i < UtoB_size; i++){ UtoB_str += UtoB_data[i]; } pTxCharacteristic->setValue(UtoB_str); pTxCharacteristic->notify(); } } class MyCallbacks : public BLECharacteristicCallbacks { void onWrite(BLECharacteristic *pCharacteristic) { BtoU_data = pCharacteristic->getValue(); if (BtoU_data.length() > 0) { for (int i = 0; i < BtoU_data.length(); i++) { Serial1.write(BtoU_data[i]); } delay(25); UtoB_Function(); } } }; void setup() { Serial1.begin(BAUD, SERIAL_8N1, RXPIN, TXPIN,true); Serial1.setRxTimeout(rxTimeout); pinMode(LEDPIN, OUTPUT); digitalWrite(LEDPIN, HIGH); // LED off //Serial1.onReceive(UtoB_Function, false); // sets a RX callback function for Serial 1 // Create the BLE Device BLEDevice::init("SNTC"); // 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()); pRxCharacteristic = pService->createCharacteristic( CHARACTERISTIC_UUID_RX, BLECharacteristic::PROPERTY_WRITE); pRxCharacteristic->setCallbacks(new MyCallbacks()); // Start the service pService->start(); // Start advertising pServer->getAdvertising()->start(); //Serial.println("Waiting a client connection to notify..."); } void loop() { // connecting ? if (deviceConnected){ if (!oldDeviceConnected) { oldDeviceConnected = deviceConnected; digitalWrite(LEDPIN, LOW); // LED on } } else{ if (oldDeviceConnected) { // disconnecting --> start advertising delay(500); // give the bluetooth stack the chance to get things ready pServer->startAdvertising(); // restart advertising //Serial.println("start advertising"); oldDeviceConnected = deviceConnected; digitalWrite(LEDPIN, HIGH); // LED off } } }