-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch_dec03b.ino
186 lines (139 loc) · 3.91 KB
/
sketch_dec03b.ino
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*Program to use GATT service on ESP32 to send Battery Level
* ESP32 works as server - Mobile as client
* Program by: B.Aswinth Raj
* Dated on: 13-10-2018
* Website: www.circuitdigest.com
*/
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h> //Library to use BLE as server
#include <BLE2902.h>
#include <ESP32Servo.h>
#define BUILTIN 2
#define INFO 14
#define WARN 27
#define SUCCESS 26
#define N_DATA 2
Servo servo1;
Servo servo2;
bool _BLEClientConnected = false;
String value = "toto";
unsigned int i = 0;
String sx = "";
String sy = "";
int x = 0;
int y = 0;
int lastX = 0;
int lastY = 0;
int deltaX = 0;
int deltaY = 0;
int temp = 0;
int cmt = 10;
bool virg = false;
String data = "";
#define HIDservice BLEUUID((uint16_t)0x1812)
BLECharacteristic HIDCharacteristicTX(BLEUUID((uint16_t)0x2A3D), BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY);
BLECharacteristic HIDCharacteristicRX(BLEUUID((uint16_t)0x2A3D), BLECharacteristic::PROPERTY_WRITE);
BLEDescriptor Descriptor(BLEUUID((uint16_t)0x2901));
void hintLed(int w = 100, int n = 1, int pinout = BUILTIN) {
for(int i = 0;i < n;i++) {
digitalWrite(pinout, HIGH);
delay(w);
digitalWrite(pinout, LOW);
delay(w);
}
}
class MyServerCallbacks : public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
_BLEClientConnected = true;
hintLed(500,2, SUCCESS);
digitalWrite(BUILTIN, HIGH);
};
void onDisconnect(BLEServer* pServer) {
_BLEClientConnected = false;
hintLed(200,4, WARN);
digitalWrite(BUILTIN, LOW);
};
};
class CharacteristicCallbacks: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
std::string rxValue = pCharacteristic->getValue();
// hintLed(200,1, SUCCESS);
if (rxValue.length() > 0) {
value = "";
for (int i = 0; i < rxValue.length(); i++) {
Serial.print(rxValue[i]);
value += rxValue[i];
}
}
// hintLed(300,3,SUCCESS);
}
};
void InitBLE() {
BLEDevice::init("TrickyRoad");
// Create the BLE Server
BLEServer *pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
// Create the BLE Service
BLEService *pBattery = pServer->createService(HIDservice);
// ************** TX ***************
pBattery->addCharacteristic(&HIDCharacteristicTX);
Descriptor.setValue("value");
HIDCharacteristicTX.addDescriptor(&Descriptor);
HIDCharacteristicTX.addDescriptor(new BLE2902());
// ************** RX ***************
pBattery->addCharacteristic(&HIDCharacteristicRX);
HIDCharacteristicRX.setCallbacks(new CharacteristicCallbacks());
pServer->getAdvertising()->addServiceUUID(HIDservice);
pBattery->start();
// Start advertising
pServer->getAdvertising()->start();
}
void setup() {
Serial.begin(115200);
Serial.println("Tricky Road");
pinMode(BUILTIN, OUTPUT);
pinMode(INFO, OUTPUT);
pinMode(WARN, OUTPUT);
pinMode(SUCCESS, OUTPUT);
servo1.attach(13);
servo2.attach(12);
InitBLE();
}
uint8_t level = 57;
String okValue = "ok";
String totoValue = "toto";
void loop() {
// HIDCharacteristic.setValue(level, 1);
// HIDCharacteristic.setValue(value.c_str());
HIDCharacteristicTX.notify();
if(_BLEClientConnected) {
data = value;
i = 0;
sx = "";
sy = "";
virg = false;
while(i < data.length()) {
if(data[i] == ',') virg = true;
else {
if(virg) sy += data[i];
else sx += data[i];
}
i++;
}
x = sx.toInt();
y = sy.toInt();
if(virg && x > 0 && x < 180 && y > 0 && y < 180) {
servo1.write(x);
servo2.write(y);
deltaX = x - lastX;
deltaY = y - lastY;
if(deltaX > deltaY) temp = abs(deltaX);
else temp = abs(deltaX);
// temp = temp * 1.8;
lastX = x;
lastY = y;
delay(temp);
}
}
}