//Server Sending Information #include #include #include //Pin numbers int LED = D9; int JoyX = A2; int JoyY = A1; int BUTTON1 = D8; int BUTTON2 = D10; //BT #define SERVICE_UUID "b58ac4ec-7c6d-4ca2-b678-2b709db8c65e" #define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8" class MyCallbacks: public BLECharacteristicCallbacks { void onWrite(BLECharacteristic *pCharacteristic) { //Method to read information written by others String value = pCharacteristic->getValue(); //Read the value sent String zero = "0"; //String variable to compare String one = "1"; //String variable to compare if(value==zero){ //Compares the value received with the "zero" variable digitalWrite(LED,LOW); //Turns LED off } else if(value==one){//Compares the value received with the "one" variable digitalWrite(LED,HIGH); //Turns LED off } } void onRead(BLECharacteristic *pCharacteristic) { //Method to send information to be read by others String Joy = "Wola"; //Create a string variable //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 = "Stop"; } else if (ValorX==511){ //UP Joy = "Up"; } else if(ValorX==0){ //DOWN Joy = "Down"; } else if(ValorY==0){ //LEFT Joy = "Left"; } else if(ValorY==511){ //RIGHT Joy = "Right"; } pCharacteristic->setValue(Joy); //Sends value } }; //--------------------------------------------------------------------------------- void setup() { //Serial monitor Serial.begin(115200); //Begin serial communication //pinModes pinMode(LED,OUTPUT); pinMode(JoyX, INPUT); pinMode(JoyY, INPUT); pinMode(BUTTON1,INPUT); pinMode(BUTTON2,INPUT); //BT pinMode(LED,OUTPUT); BLEDevice::init("DanisitaBBQ"); //Name device 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 | BLECharacteristic::PROPERTY_WRITE ); pCharacteristic->setCallbacks(new MyCallbacks()); //Call the class to read and write information pCharacteristic->setValue("Holis"); pService->start(); //Begin the bluetooth service BLEAdvertising *pAdvertising = pServer->getAdvertising(); //Start service and advertising pAdvertising->start(); //Begin the advertising of the bluetooth service } //--------------------------------------------------------------------------------- void loop() { delay(10); // bluetooth stack will go into congestion, if too many packets are sent }