-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathesp_now_ctrl.h
332 lines (265 loc) · 8.61 KB
/
esp_now_ctrl.h
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
const int MAX_FOLLOWERS = 10; // Maximum number of follower devices
typedef struct struct_message {
byte devCode;
float base;
float shoulder;
float elbow;
float hand;
byte cmd;
char message[210];
} struct_message;
struct_message espNowMessage;
struct_message espNowMegsRecv;
esp_now_peer_info_t peerInfo;
#define ESP_NOW_CHANNEL 0
#define ESP_NOW_ENCRYPT false
uint8_t thisDevMac[6];
uint8_t singleFollowerDev[6];
// input a mac[6]:{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}
// return String: "FF:FF:FF:FF:FF:FF"
String macToString(uint8_t mac[6]) {
char macStr[18]; // 6 pairs of 2 characters + null terminator
snprintf(macStr, sizeof(macStr), "%02X:%02X:%02X:%02X:%02X:%02X",
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
return String(macStr);
}
void getThisDevMacAddress() {
// WiFi.macAddress(thisDevMac);
esp_err_t ret = esp_wifi_get_mac(WIFI_IF_STA, thisDevMac);
String getInfoJsonString = macToString(thisDevMac);
jsonInfoHttp.clear();
jsonInfoHttp["mac"] = getInfoJsonString;
Serial.println(getInfoJsonString);
}
void changeEspNowMode(byte inputMode) {
switch(inputMode) {
case 0: espNowMode = inputMode;
if (InfoPrint == 1) {Serial.println("esp-now mode: none");}
screenLine_3 = "ESP-NOW: NONE";
oled_update();
jsonInfoHttp.clear();
jsonInfoHttp["info"] = "esp-now mode: none";
break;
case 1: espNowMode = inputMode;
espNowMessage.cmd = 0;
if (InfoPrint == 1) {Serial.println("esp-now mode: flow-leader(group)");}
screenLine_3 = "ESP-NOW: F-LEADER-B";
oled_update();
jsonInfoHttp.clear();
jsonInfoHttp["info"] = "esp-now mode: flow-leader(group)";
break;
case 2: espNowMode = inputMode;
espNowMessage.cmd = 0;
if (InfoPrint == 1) {Serial.println("esp-now mode: flow-leader(single)");}
screenLine_3 = "ESP-NOW: F-LEADER-S";
oled_update();
jsonInfoHttp.clear();
jsonInfoHttp["info"] = "esp-now mode: flow-leader(single)";
break;
case 3: espNowMode = inputMode;
if (InfoPrint == 1) {Serial.println("esp-now mode: follower");}
screenLine_3 = "ESP-NOW: > FOLLOWER <";
oled_update();
jsonInfoHttp.clear();
jsonInfoHttp["info"] = "esp-now mode: follower";
break;
}
}
// callback when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
char macStr[18];
Serial.print("Packet to: ");
// Copies the sender mac address to a string
snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
Serial.print(macStr);
Serial.print(" send status:\t");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
}
void macStringToByteArray(const String& macString, uint8_t* byteArray) {
for (int i = 0; i < 6; i++) {
byteArray[i] = strtol(macString.substring(i * 3, i * 3 + 2).c_str(), NULL, 16);
}
return;
}
void OnDataRecv(const esp_now_recv_info_t *info, const unsigned char* incomingData, int len) {
// void OnDataRecv(const unsigned char* mac, const unsigned char* incomingData, int len) {
const uint8_t *mac = info->src_addr;
if (espNowMode != 3){
return;
}
if (!ctrlByBroadcast) {
if (memcmp(mac, mac_whitelist_broadcast, 6) != 0) {
return;
}
}
memcpy(&espNowMegsRecv, incomingData, sizeof(espNowMegsRecv));
if (InfoPrint == 1) {
Serial.print("Bytes received: ");Serial.println(len);
}
DeserializationError err = deserializeJson(jsonCmdReceive, espNowMegsRecv.message);
switch(espNowMegsRecv.cmd) {
case 0:
RoArmM2_allJointAbsCtrl(espNowMegsRecv.base,
espNowMegsRecv.shoulder,
espNowMegsRecv.elbow,
espNowMegsRecv.hand,
0,
0);break;
case 1:
if (err == DeserializationError::Ok) {
jsonCmdReceiveHandler();
};break;
case 2:
if (err == DeserializationError::Ok) {
runNewJsonCmd = true;
};break;
case 3:
Serial.println(espNowMegsRecv.message);
}
}
void initEspNow() {
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
// register esp-now sending call back function.
esp_now_register_send_cb(OnDataSent);
// register esp-now receving call back function.
esp_now_register_recv_cb(OnDataRecv);
// register peer
peerInfo.channel = ESP_NOW_CHANNEL;
peerInfo.encrypt = ESP_NOW_ENCRYPT;
}
void registerNewFollowerToPeer(String inputMac) {
if (inputMac.length() != 17) {
Serial.println("invalid MAC address format.");
return;
}
uint8_t macArray[6];
macStringToByteArray(inputMac, macArray);
for (int i = 0; i < 6; i++) {
singleFollowerDev[i] = macArray[i];
}
memcpy(peerInfo.peer_addr, macArray, 6);
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
Serial.println("Failed to add peer: " + inputMac);
jsonInfoHttp.clear();
jsonInfoHttp["info"] = "Failed to add peer";
jsonInfoHttp["mac"] = inputMac;
return;
}
Serial.println("add peer: " + inputMac);
jsonInfoHttp.clear();
jsonInfoHttp["info"] = "add peer";
jsonInfoHttp["mac"] = inputMac;
}
void deleteFollower(String inputMac) {
if (inputMac.length() != 17) {
Serial.println("invalid MAC address format.");
return;
}
uint8_t macArray[6];
macStringToByteArray(inputMac, macArray);
esp_now_del_peer(macArray);
jsonInfoHttp.clear();
jsonInfoHttp["info"] = "delete peer";
jsonInfoHttp["mac"] = inputMac;
if (InfoPrint == 1) {
Serial.println("delete peer: " + inputMac);
}
}
void espNowGroupSend(byte devCodeIn, float bIn, float sIn, float eIn, float hIn, byte cmdIn, String messageIn) {
espNowMessage.devCode = devCodeIn;
espNowMessage.base = bIn;
espNowMessage.shoulder = sIn;
espNowMessage.elbow = eIn;
espNowMessage.hand = hIn;
espNowMessage.cmd = cmdIn;
// espNowMessage.message = messageIn;
strcpy(espNowMessage.message, messageIn.c_str());
esp_err_t result = esp_now_send(0, (uint8_t *) &espNowMessage, sizeof(struct_message));
if (result == ESP_OK) {
Serial.println("Sent with success");
}
else {
Serial.println("Error sending the data");
}
}
void espNowSingleDevSend(String inputMac, byte devCodeIn, float bIn, float sIn, float eIn, float hIn, byte cmdIn, String messageIn){
if (inputMac.length() != 17) {
Serial.println("invalid MAC address format.");
return;
}
espNowMessage.devCode = devCodeIn;
espNowMessage.base = bIn;
espNowMessage.shoulder = sIn;
espNowMessage.elbow = eIn;
espNowMessage.hand = hIn;
espNowMessage.cmd = cmdIn;
// espNowMessage.message = messageIn;
strcpy(espNowMessage.message, messageIn.c_str());
uint8_t macArray[6];
macStringToByteArray(inputMac, macArray);
for (int i = 0; i < 6; i++) {
singleFollowerDev[i] = macArray[i];
}
esp_err_t result = esp_now_send(
macArray,
(uint8_t *) &espNowMessage,
sizeof(struct_message));
if (result == ESP_OK) {
Serial.println("Sent with success");
}
else {
Serial.println("Error sending the data");
}
}
void espNowSingleDevFlowCtrl() {
espNowMessage.base = radB;
espNowMessage.shoulder = radS;
espNowMessage.elbow = radE;
espNowMessage.hand = radG;
esp_err_t result = esp_now_send(singleFollowerDev,
(uint8_t *) &espNowMessage,
sizeof(struct_message));
if (result == ESP_OK) {
Serial.println("Sent with success");
}
else {
Serial.println("Error sending the data");
}
}
void espNowGroupDevsFlowCtrl() {
espNowMessage.base = radB;
espNowMessage.shoulder = radS;
espNowMessage.elbow = radE;
espNowMessage.hand = radG;
esp_err_t result = esp_now_send(0, (uint8_t *) &espNowMessage, sizeof(struct_message));
if (result == ESP_OK) {
Serial.println("Sent with success");
}
else {
Serial.println("Error sending the data");
}
}
void changeBroadcastMode(bool inputMode, String inputMac) {
ctrlByBroadcast = inputMode;
jsonInfoHttp.clear();
jsonInfoHttp["mode"] = inputMode;
uint8_t macArray[6];
macStringToByteArray(inputMac, macArray);
for (int i = 0; i < 6; i++) {
mac_whitelist_broadcast[i] = macArray[i];
}
if (InfoPrint == 1) {
if (ctrlByBroadcast) {
Serial.println("it can be ctrl by esp-now broadcast cmd.");
jsonInfoHttp["info"] = "it can be ctrl by esp-now broadcast cmd.";
jsonInfoHttp["leader mac"] = inputMac;
} else {
Serial.println("it won't be ctrl by esp-now broadcast cmd.");
jsonInfoHttp["info"] = "it won't be ctrl by esp-now broadcast cmd, leader mac: "+inputMac;
}
}
}