/* 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 //Pre-setup for the OLED #include #include #ifdef U8X8_HAVE_HW_SPI #include #endif #ifdef U8X8_HAVE_HW_I2C #include #endif U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE); #define LCDWidth u8g2.getDisplayWidth() BLEServer *pServer = NULL; BLECharacteristic *pTxCharacteristic; bool deviceConnected = false; bool oldDeviceConnected = false; uint8_t txValue = 0; // See the following for generating UUIDs: // https://www.uuidgenerator.net/ #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" int led= 20; int LED_PIN= 10; int LED_COUNT= 3; int colorIndex = 0; //looper for color and the name char a; //varaiable for button ID char c; //variable for button push or release ID Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800); // RGB color values (we'll handle GRB conversion manually for 2nd LED) uint8_t colorTable[][3] = { {255, 0, 0}, // Red {255, 165, 0}, // Orange {255, 255, 0}, // Yellow {0, 255, 0}, // Green {0, 255, 255}, // Cyan {0, 0, 255}, // Blue {255, 0, 255}, // Magenta {128, 0, 128}, // Purple {255, 255, 255}, // White {0, 0, 0} // Off }; // Color names for OLED display const char* colorNames[] = { "Red", "Orange", "Yellow", "Green", "Cyan", "Blue", "Magenta", "Purple", "White", "Off" }; const int totalColors = sizeof(colorTable) / sizeof(colorTable[0]); const char* intro = "Device Ready"; class MyServerCallbacks : public BLEServerCallbacks { void onConnect(BLEServer *pServer) { deviceConnected = true; }; void onDisconnect(BLEServer *pServer) { deviceConnected = false; } }; class MyCallbacks : public BLECharacteristicCallbacks { void onWrite(BLECharacteristic *pCharacteristic) { 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]); Serial.println("*********"); } char a= rxValue[2]; char c= rxValue[3]; Serial.print("a= "); Serial.println(a); Serial.print("c= "); Serial.println(c); Serial.println(); Serial.println("*********"); // If the up button (#5) is pushed in Bluefruit app then increment the color table if (a=='5' and c=='1'){ colorIndex = (colorIndex + 1) % (sizeof(colorTable) / sizeof(colorTable[0])); Serial.print("Color Index Increased to "); Serial.println(colorIndex); } // If the up button (#6) is pushed in Bluefruit app then decrement the color table if (a=='6' and c=='1'){ colorIndex = (colorIndex - 1 + totalColors) % totalColors; //colorIndex = (colorIndex - 1) % (sizeof(colorTable) / sizeof(colorTable[0])); Serial.print("Color Index Increased to "); Serial.println(colorIndex); } // Get RGB values for current color uint8_t r = colorTable[colorIndex][0]; uint8_t g = colorTable[colorIndex][1]; uint8_t b = colorTable[colorIndex][2]; // First LED is RGB (even though strip is NEO_GRB by default) strip.setPixelColor(0,r, g, b); // Second LED is GRB, so swap r and g strip.setPixelColor(1,g, r, b); strip.setPixelColor(2,g, r, b); strip.show(); // Send color text to the OLED u8g2.clearBuffer(); // clear the internal memory int len = strlen(colorNames[colorIndex]); int x = (LCDWidth - 6*len) / 2; //Serial.print(LCDWidth); //Serial.print(" "); //Serial.print(len); //Serial.print(" "); //Serial.println(len); Serial.println(len); u8g2.drawStr(x,10,colorNames[colorIndex]); // write something to the internal memory u8g2.sendBuffer(); // transfer internal memory to the display } } }; void setup() { Serial.begin(115200); // Setup for Display u8g2.begin(); u8g2.setFont(u8g2_font_ncenB08_tr); // choose a suitable font u8g2.setFlipMode(1); u8g2.drawStr(0,10,intro); // Create the BLE Device BLEDevice::init("Cromira"); // 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); pRxCharacteristic->setCallbacks(new MyCallbacks()); // Start the service pService->start(); // Start advertising pServer->getAdvertising()->start(); Serial.println("Waiting a client connection to notify..."); } void loop() { if (deviceConnected) { pTxCharacteristic->setValue(&txValue, 1); pTxCharacteristic->notify(); txValue++; delay(10); // 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; } }