-
Notifications
You must be signed in to change notification settings - Fork 9
/
Bluetooth.cpp
227 lines (159 loc) · 6.11 KB
/
Bluetooth.cpp
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Bluetooth.cpp
#include "Bluetooth.h"
#include <Arduino.h>
int clutch_pin = 15;
#define AP_SERVICE_UUID "ab0828b1-198e-4351-b779-901fa0e0371e"
#define UUID_RECEIVE "4ac8a682-9736-4e5d-932b-e9b31405049c"
BLEService *Bluetooth::pService = nullptr;
//all ota stuff
#define OTA_SERVICE_UUID "c8659210-af91-4ad3-a995-a58d6fd26145"
#define UUID_FW "c8659211-af91-4ad3-a995-a58d6fd26145"
#define UUID_VERSION "c8659212-af91-4ad3-a995-a58d6fd26145"
BLECharacteristic *Bluetooth::Version_Characteristic = nullptr;
BLECharacteristic *Bluetooth::FW_Characteristic = nullptr;
BLEService *Bluetooth::pOTAService = nullptr;
#define FULL_PACKET 512
#define CHARPOS_UPDATE_FLAG 5
esp_ota_handle_t otaHandler = 0;
bool updateFlag = false;
size_t totalSize = 0;
size_t remainingSize = 0;
//end ota
#define LED 2
bool Bluetooth::deviceConnected = false;
bool Bluetooth::oldDeviceConnected = false;
BLEServer *Bluetooth::pServer = nullptr; // Initialize the static member variable
class MyServerCallbacks : public BLEServerCallbacks {
void onConnect(BLEServer *pServer) {
Bluetooth::deviceConnected = true;
Serial.println("connected");
digitalWrite(LED, HIGH);
};
void onDisconnect(BLEServer *pServer) {
Bluetooth::deviceConnected = false;
Serial.println("Disconnected");
digitalWrite(LED, LOW);
delay(1000);
pServer->getAdvertising()->start();
Motor::set(0, 0);
}
};
class MyCallbacks : public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
//if you get an error, you didnt install the esp32 boards manager Version 3. Go to tools->boards manager and search for esp32. Install the esp32 by Espressif Systems.
String rxValue = pCharacteristic->getValue();
if (rxValue.length() > 0) {
String rxString = rxValue.c_str();
Serial.print("Received Value: ");
Serial.println(rxString);
//do not change because there is old hardware.
if (rxString.startsWith("motor")) {
rxString.remove(0, 5);
int motor_speed = rxString.toInt();
int motor_direction = 1;
if (motor_speed < 0) {
motor_speed *= -1;
motor_direction = 0;
}
Motor::set(motor_speed, motor_direction);
return;
}
int splitpoint = rxString.indexOf(",");
String key = rxString.substring(0, splitpoint);
String value = rxString.substring(splitpoint+1);
if (key == "apOn") {
if(value == "0") digitalWrite(clutch_pin, LOW);
else digitalWrite(clutch_pin, HIGH);;
}
}
}
};
class otaCallback : public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
String rxDataStr = pCharacteristic->getValue();
int rxDataSize = rxDataStr.length();
const uint8_t* rxData = (const uint8_t*)rxDataStr.c_str();
if (!updateFlag) { // If it's the first packet of OTA since bootup, begin OTA
totalSize = rxData[0] | (rxData[1] << 8) | (rxData[2] << 16) | (rxData[3] << 24);
remainingSize = totalSize;
Serial.println("BeginOTA");
Serial.print("Total size: ");
Serial.println(totalSize);
esp_err_t err = esp_ota_begin(esp_ota_get_next_update_partition(NULL), totalSize, &otaHandler);
if (err != ESP_OK) {
Serial.print("esp_ota_begin failed: ");
Serial.println(err);
return;
}
updateFlag = true;
uint8_t txData[5] = {1, 2, 3, 4, 5};
//delay(1000);
pCharacteristic->setValue((uint8_t*)txData, 5);
pCharacteristic->notify();
return;
}else if(rxDataSize > 0) {
esp_err_t err = esp_ota_write(otaHandler, rxData, rxDataSize);
if (err != ESP_OK) {
Serial.print("esp_ota_write failed: ");
Serial.println(err);
return;
}
remainingSize -= rxDataSize;
//Serial.print("Remaining: ");
//Serial.println(remainingSize);
if (remainingSize <= 0) { // End OTA if all data is received
esp_ota_end(otaHandler);
Serial.println("EndOTA");
if (ESP_OK == esp_ota_set_boot_partition(esp_ota_get_next_update_partition(NULL))) {
delay(2000);
esp_restart();
} else {
Serial.println("Upload Error");
}
}
}
}
};
void Bluetooth::notify(BLECharacteristic *characteristic, String value) {
static char charBuff[15];
value.toCharArray(charBuff, 15);
characteristic->setValue(charBuff);
characteristic->notify();
}
void Bluetooth::setup() {
pinMode(clutch_pin, OUTPUT);
digitalWrite(clutch_pin, LOW);
BLEDevice::init("AutoPilot");
pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
pService = pServer->createService(AP_SERVICE_UUID);
BLECharacteristic *pRxCharacteristic = pService->createCharacteristic(UUID_RECEIVE, BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_WRITE_NR);
pRxCharacteristic->setCallbacks(new MyCallbacks());
pService->start();
//OTA
pOTAService = pServer->createService(OTA_SERVICE_UUID);
Version_Characteristic = pOTAService->createCharacteristic(UUID_VERSION, BLECharacteristic::PROPERTY_READ);
Version_Characteristic->addDescriptor(new BLE2902());
FW_Characteristic = pOTAService->createCharacteristic(UUID_FW,BLECharacteristic::PROPERTY_NOTIFY | BLECharacteristic::PROPERTY_WRITE| BLECharacteristic::PROPERTY_WRITE_NR);
FW_Characteristic->addDescriptor(new BLE2902());
FW_Characteristic->setCallbacks(new otaCallback());
pOTAService->start();
pServer->getAdvertising()->addServiceUUID(OTA_SERVICE_UUID);
pServer->getAdvertising()->start();
Serial.println("Waiting a client connection to notify...");
uint8_t firmwareVersion[1] = {FIRMWARE_VERSION};
Version_Characteristic->setValue((uint8_t*)firmwareVersion, 1);
pinMode(LED, OUTPUT);
}
void Bluetooth::loop() {
if (!deviceConnected && oldDeviceConnected) {
delay(500);
pServer->startAdvertising();
Serial.println("start advertising");
oldDeviceConnected = deviceConnected;
}
if (deviceConnected && !oldDeviceConnected) {
oldDeviceConnected = deviceConnected;
Serial.println("Connecting...");
}
}