#include #include #include // See the following for generating UUIDs: // https://www.uuidgenerator.net/ #define SERVICE_UUID "b58ac4ec-7c6d-4ca2-b678-2b709db8c65e" #define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8" int LED=D9; class MyCallbacks: public BLECharacteristicCallbacks { //Class for the callbacks (any exchange of information) void onWrite(BLECharacteristic *pCharacteristic) { //Method to enter when receiving information String value = pCharacteristic->getValue(); //getValue is a function to receive the information as strings String zero = "0"; //Create a string variable to compare (this was easier for me to visualize it) String one = "1"; //Second string variable if(value==zero){ //Comparison from the value received and a value I expect digitalWrite(LED,LOW); //Turn off the LED } else if(value==one){ //Comparison from the value received and another value I expect digitalWrite(LED,HIGH); //Turn on the LED } if (value.length() > 0) { //Comparator for when there is something received (this is written in the Xiao wiki and decided to conserve it) Serial.println("*********"); //Print a line of asterisks to separate things Serial.print("New value: "); //Print for (int i = 0; i < value.length(); i++) //Go over each character of the value received Serial.print(value[i]); //Print each character Serial.println(); //Give a line break Serial.println("*********"); //Print another line of asterisks } } }; void setup() { Serial.begin(115200); //Begin serial communication to be able to print in the serial monitor pinMode(LED,OUTPUT); //Establish the pin mode of the LED port BLEDevice::init("DanisitaBBQ_2"); //Name server BLEServer *pServer = BLEDevice::createServer(); //Set BLE device as a server BLEService *pService = pServer->createService(SERVICE_UUID); //Create a service for the UUID BLECharacteristic *pCharacteristic = pService->createCharacteristic( //Set the server characteristic CHARACTERISTIC_UUID, BLECharacteristic::PROPERTY_READ | //Give the property to read BLECharacteristic::PROPERTY_WRITE //Give the property to write ); pCharacteristic->setCallbacks(new MyCallbacks()); //Enter the function of MyCallbacks to exchange information pCharacteristic->setValue("Buenas tardes"); //Set a value to be read when it connects pService->start(); //Begin the service BLEAdvertising *pAdvertising = pServer->getAdvertising(); //Start service and advertising pAdvertising->start(); } void loop() { // put your main code here, to run repeatedly: delay(2000); //Delay to avoid a stack of information }