//Official Server #include #include #include #include //Pin numbers int LED = D9; int JoyX = A2; int JoyY = A1; //Variable int Joy = 0; //BLE Server name (name of the other Xiao) #define bleServerName "Danisita_BBQ" BLECharacteristic *pCharacteristic; //Look for the object in the BLECharacteristic class bool deviceConnected = false; //Boolean variable for the connection of the device class MyServerCallbacks: public BLEServerCallbacks { //Class for the callbacks void onConnect(BLEServer* pServer) { //Function for a connected device deviceConnected = true; //Establish the positive value when a device is connected digitalWrite(LED, HIGH); }; void onDisconnect(BLEServer* pServer) { //Function for a disconnected device deviceConnected = false; //Establish the negative value when a device is connected digitalWrite(LED, LOW); } }; void setup() { Serial.begin(115200); //Serial communication to observe in serial monitor //pinModes pinMode(LED,OUTPUT); pinMode(JoyX, INPUT); pinMode(JoyY, INPUT); BLEDevice::init(bleServerName); //Give the established name to the server BLEServer *pServer = BLEDevice::createServer(); //Create the server pServer->setCallbacks(new MyServerCallbacks()); //Call function to know if connected or not BLEService *pService = pServer->createService(BLEUUID((uint16_t)0x181A)); //Service UUID pCharacteristic = pService->createCharacteristic( //Give the characteristics to the service BLEUUID((uint16_t)0x2A59), //Characteristic UUID BLECharacteristic::PROPERTY_NOTIFY //Establish the notifiy property to the server ); pCharacteristic->addDescriptor(new BLE2902()); //Give a descriptor to allow the client to receive notifications pService->start(); //Begin the service BLEAdvertising *pAdvertising = BLEDevice::getAdvertising(); //Access the advertising class to allow it pAdvertising->addServiceUUID(pService->getUUID()); //Method to get the UUID of the service pAdvertising->setScanResponse(true); //Allow scanning pAdvertising->setMinPreferred(0x0); //Method to establish the minimum interval to be advertised pAdvertising->setMinPreferred(0x1F); BLEDevice::startAdvertising(); //Begin the advertising of the server } void loop() { if (deviceConnected) { //Enter to the following only if it is connected //Read analog values from joystick int ValorX = analogRead(JoyX)/8; int ValorY = analogRead(JoyY)/8; //Assign string values to the "Joy" variable if((ValorX>=490 && ValorX<=494)&&(ValorY>=468 && ValorY<=474)){ //STOP Joy = 0; } else if (ValorX==511){ //UP Joy = 1; } else if(ValorY==511){ //RIGHT Joy = 2; } else if(ValorX==0){ //DOWN Joy = 3; } else if(ValorY==0){ //LEFT Joy = 4; } else{ Joy = 0; } pCharacteristic->setValue(Joy); pCharacteristic->notify(); delay(10); // bluetooth stack will go into congestion, if too many packets are sent } }