MystBin
/IonDanceAssistant Created 1 year, 6 months ago...
Raw
Client_Basic_Issue.ino Hide Copy Raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <Arduino.h> #include <BLEDevice.h> #include <BLEUtils.h> #include <BLEClient.h> #include <BLEAdvertisedDevice.h> #include <FastLED.h> #include <SparkFun_OWire_Arduino_Library.h> #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<WS2812B, STRIP_PIN_1, GRB>(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 }
setup.h Hide Copy Raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#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