# MystBin ! - Client_Basic_Issue.ino #include #include #include #include #include #include #include #include "setup.h" BLEClient* pClient; bool connected = false; BLEUUID sMainUUID(S_MAIN_UUID); BLEUUID cPowerUUID(C_POWER_UUID); BLEUUID cColourUUID(C_COLOUR_UUID); BLEUUID cModeUUID(C_MODE_UUID); OWIRE myLED; CRGB leds[STRIP_LENGTH]; static void powerNotifyCallback(BLERemoteCharacteristic* pBLERemoteCharacteristic, uint8_t* pData, size_t length, bool isNotify) { int value = *pData; Serial.print("Setting Striplight brightness: "); Serial.println(value); fill_solid(leds, STRIP_LENGTH, CRGB::Red); FastLED.setBrightness(value); } static void colourNotifyCallback(BLERemoteCharacteristic* pBLERemoteCharacteristic, uint8_t* pData, size_t length, bool isNotify) { int value = *pData; Serial.print("Setting Striplight brightness: "); Serial.println(value); } static void modeNotifyCallback(BLERemoteCharacteristic* pBLERemoteCharacteristic, uint8_t* pData, size_t length, bool isNotify) { int value = *pData; Serial.print("Setting Striplight brightness: "); Serial.println(value); } class MyClientCallback : public BLEClientCallbacks { void onConnect(BLEClient* pclient) { connected = true; myLED.setModeAndColor(OW_SOLID, OW_RED); } void onDisconnect(BLEClient* pclient) { ESP.restart(); } }; void setup() { Serial.begin(115200); myLED.begin(4, false); myLED.setModeAndColor(OW_SOLID, OW_BLUE); FastLED.addLeds(leds, STRIP_LENGTH); Serial.println("Booting Client..."); BLEDevice::init("Light Pole Client"); pClient = BLEDevice::createClient(); pClient->setClientCallbacks(new MyClientCallback()); while (!connected) { BLEScan* pBLEScan = BLEDevice::getScan(); pBLEScan->setActiveScan(true); pBLEScan->setInterval(100); pBLEScan->setWindow(99); BLEScanResults scanResults = pBLEScan->start(5); for (int i = 0; i < scanResults.getCount(); i++) { BLEAdvertisedDevice device = scanResults.getDevice(i); BLEUUID service_uuid = device.getServiceUUID(); String device_name = device.getName().c_str(); Serial.print("Device Found! Name: '"); Serial.print(device_name); Serial.print("' Advertising Service: '"); Serial.print(service_uuid.equals(sMainUUID)); Serial.print("' Service UUID: '"); Serial.print(service_uuid.toString().c_str()); Serial.println("'"); Serial.println(); if (device.haveServiceUUID() && device.isAdvertisingService(sMainUUID)) { Serial.println("Found Server, attempting to connect..."); pClient->connect(&device); break; } } if (!connected) { Serial.println("Connection failed. Waiting 15 seconds before retrying."); myLED.setModeAndColor(OW_SOLID, OW_YELLOW); delay(15000); // Wait for 15 seconds before retrying myLED.setModeAndColor(OW_SOLID, OW_BLUE); } } if (connected) { BLERemoteService* sMain = pClient->getService(sMainUUID); if (sMain) { BLERemoteCharacteristic* cPower = sMain->getCharacteristic(cPowerUUID); if (cPower) { cPower->registerForNotify(powerNotifyCallback); cPower->getDescriptor(BLEUUID((uint16_t)0x2902))->writeValue((uint8_t*)"\x01", 1); } else { Serial.println("Power Characteristic not found."); } BLERemoteCharacteristic* cColour = sMain->getCharacteristic(cColourUUID); if (cColour) { cColour->registerForNotify(colourNotifyCallback); cColour->getDescriptor(BLEUUID((uint16_t)0x2902))->writeValue((uint8_t*)"\x01", 1); } else { Serial.println("Colour Characteristic not found."); } BLERemoteCharacteristic* cMode = sMain->getCharacteristic(cModeUUID); if (cMode) { cMode->registerForNotify(modeNotifyCallback); cMode->getDescriptor(BLEUUID((uint16_t)0x2902))->writeValue((uint8_t*)"\x01", 1); } else { Serial.println("Mode Characteristic not found."); } } else { Serial.println("Main Service not found."); } } } void loop() { // Nothing to do in the loop for this example } # MystBin ! - setup.h #ifndef Setup_h #define Setup_h /* The setup file for the Light Pole! */ /* Server Only */ /* PINS */ // IR_PIN - The pin the IR Reciever is connected too. #define IR_PIN 3 // 34, 3 // REMOTE_TYPE - The type of IR remote that you will use. #define REMOTE_TYPE 1 /* SETTINGS */ // DEFAULT_BRIGHTNESS - The brightness that the server will use on startup. (must be 0 to 255) #define DEFAULT_BRIGHTNESS 127 /* Client Only */ // STRIP_LENGTH - how many leds are in the strips. #define STRIP_LENGTH 36 // STRIP_TYPE - how many strips you have, 1 (STRIP_PIN_1), 2 (STRIP_PIN_1, STRIP_PIN_2) or 4 (all STRIP_PIN numbers). makes the effects look better. #define STRIP_TYPE 4 // STRIP_PIN_X - the strip light pins. #define STRIP_PIN_1 5 #define STRIP_PIN_2 6 #define STRIP_PIN_3 7 #define STRIP_PIN_4 8 /* Both */ /* PINS */ // STATUS_LED_PIN - The pin the status led is connected too. #define STATUS_LED_PIN 4 // 13, 4 // POWER_BUTTON_PIN - The pin the power button is attached too. #define POWER_BUTTON_PIN 3 // 27, 5 /* BLE - UUID'S */ // S_MAIN_UUID - The services' main UUID #define S_MAIN_UUID "00c6273c-70d9-11ee-b962-0242ac120002" // C_POWER_UUID - The power UUID, where 00 is off, and 01, to FF is the brightness #define C_POWER_UUID "068ed628-70d9-11ee-b962-0242ac120002" // C_COLOUR_UUID - The colour UUID, which sets the colour of the lights, or the mode. #define C_COLOUR_UUID "09e45eba-70d9-11ee-b962-0242ac120002" // C_MODE_UUID - The mode UUID, which sets the mode of the lights. If its 00, then it just shows the default colour. #define C_MODE_UUID "09e45eba-70d9-11ee-b962-0242ac120002" /* DEBUG */ // SERIAL_SPEED - The speed at which the serial data is transmitted. #define SERIAL_SPEED 115200 // ENABLE_DEBUG - If true, debug messages will be sent. If disabled, only serious errors will be sent. #define ENABLE_DEBUG true #endif