forked from lmahmutov/ESP32-JN5169
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ESP32-JN5169.ino
589 lines (548 loc) · 17.3 KB
/
ESP32-JN5169.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
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
#if CONFIG_FREERTOS_UNICORE
#define ARDUINO_RUNNING_CORE 0
#else
#define ARDUINO_RUNNING_CORE 1
#endif
#define UseOled // Есть или нет Экран
#include <FS.h>
#include "time.h"
#include "SPIFFS.h"
#include <sqlite3.h>
#include <WiFiManager.h>
#include <ESPAsyncWebServer.h>
#include <WebSocketsServer.h>
#include "HardwareSerial.h"
HardwareSerial jnSerial(2);
#ifdef UseOled
//OLED Section
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
int displayEnabled = 60;
#endif
unsigned long timing; // Переменная для хранения точки отсчета
//Web server
AsyncWebServer server(80);
WebSocketsServer webSocket = WebSocketsServer(81);
const char* PARAM_MESSAGE = "message";
//---------------------------------
//SQlite
sqlite3 *db;
int rc;
sqlite3_stmt *res;
int rec_count = 0;
const char *tail;
//NTP
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 7200;
const int daylightOffset_sec = 3600;
char dateStringBuff[50]; //50 chars should be enough
char timeStringBuff[50]; //50 chars should be enough
//---------------------------------------
String print_string = "";
String attr_response = "";
bool oledidle = true;
bool joinStarted = false;
int joinSecCounter = 0;
// ------task del from database---------
bool DelCall = false;
uint64_t deletedDevLongAddr = 0;
// ------task get full info ------------
bool new_device_connected = false;
bool connectGood = true;
bool needBind = false;
uint8_t bindEp = 0;
uint16_t new_device_ShortAddr = 0;
uint64_t new_device_LongAddr = 0;
String NewDevName = "";
int counter = 0;
bool EpResponse = false;
bool ClResponse = false;
bool BindResponse = false;
bool DnResponse = false;
byte rxMessageData_newDevice[1000];
byte ClDataNewDevice[1000];
String NewDevComplete = "";
//---------------------------------------
uint64_t u64ExtendedAddr_coord;
// --------------------------------------
uint64_t au64ExtAddr[16];
byte rxMessageData[1024];
byte rxMessageChecksum = 0;
uint16_t rxMessageLength = 0;
uint8_t rxMessageState = 0;
uint16_t rxMessageType = 0;
uint8_t rxMessageCount = 0;
bool rxMessageInEscape = false;
byte rxByte;
#define RXD2 16
#define TXD2 17
void serialEvent() {
webSocket.loop();
while (jnSerial.available())
{
byte rxByte = (byte)jnSerial.read();
if (rxByte == 0x01)
{
// Start character received
rxMessageChecksum = 0;
rxMessageLength = 0;
rxMessageType = 0;
rxMessageState = 0;
rxMessageCount = 0;
rxMessageInEscape = false;
}
else if (rxByte == 0x02)
{
rxMessageInEscape = true;
}
else if (rxByte == 0x03)
{
displayDecodedCommand(rxMessageType, rxMessageLength, rxMessageData);
}
else
{
if (rxMessageInEscape == true)
{
rxByte ^= 0x10;
rxMessageInEscape = false;
}
// Parse character
switch (rxMessageState)
{
case 0:
{
rxMessageType = rxByte;
rxMessageType <<= 8;
rxMessageState++;
}
break;
case 1:
{
rxMessageType |= rxByte;
rxMessageState++;
}
break;
case 2:
{
rxMessageLength = rxByte;
rxMessageLength <<= 8;
rxMessageState++;
}
break;
case 3:
{
rxMessageLength |= rxByte;
rxMessageState++;
}
break;
case 4:
{
rxMessageChecksum = rxByte;
rxMessageState++;
}
break;
default:
{
rxMessageData[rxMessageCount++] = rxByte;
}
break;
}
}
}
}
void TaskDecode( void *pvParameters );
void TaskGetFullInfo( void *pvParameters );
void TaskDelDevice( void *pvParameters );
void TaskOled( void *pvParameters );
//gets called when WiFiManager enters configuration mode
void configModeCallback (WiFiManager *myWiFiManager) {
Serial.println("Entered config mode");
Serial.println(WiFi.softAPIP());
//if you used auto generated SSID, print it
Serial.println(myWiFiManager->getConfigPortalSSID());
#ifdef UseOled
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println(F("Zigbee Gateway v0.1"));
display.setCursor(0, 10);
display.println(F("https://nrf52840.ru"));
display.setCursor(0, 20);
display.println(F("Wifi config mode"));
display.setCursor(10, 30);
display.println(F("Connect to AP"));
display.setCursor(20, 40);
display.println(F("ZigBeeGW"));
display.setCursor(20, 50);
display.println(F("and Setup WiFi"));
display.display();
#endif
}
void UpdateLocalTime()
{
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
Serial.println("Failed to obtain time");
return;
}
strftime(dateStringBuff, sizeof(dateStringBuff), "%d %B %Y", &timeinfo); //"%A, %B %d %Y"
strftime(timeStringBuff, sizeof(timeStringBuff), "%H:%M:%S", &timeinfo);
}
uint32_t getTime() {
time_t now;
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
Serial.println("Failed to obtain time");
return (0);
}
time(&now);
return now;
}
void setup() {
Serial.begin(115200);
Serial.println("Start");
jnSerial.begin(115200, SERIAL_8N1, RXD2, TXD2);
//SPIFFS
if (!SPIFFS.begin()) {
Serial.println("An Error has occurred while mounting SPIFFS");
return;
}
//SQlite
File root = SPIFFS.open("/");
if (!root) {
Serial.println("- failed to open directory");
return;
}
if (!root.isDirectory()) {
Serial.println(" - not a directory");
return;
}
File file = root.openNextFile();
while (file) {
if (file.isDirectory()) {
Serial.print(" DIR : ");
Serial.println(file.name());
} else {
Serial.print(" FILE: ");
Serial.print(file.name());
Serial.print("\tSIZE: ");
Serial.println(file.size());
}
file = root.openNextFile();
}
sqlite3_initialize();
if (db_open("/spiffs/data.db", &db))
return;
//-------------------------------------------------------------------------
#ifdef UseOled
//OLED
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Don't proceed, loop forever
}
#endif
// Wifi Section
WiFiManager wm;
//wm.resetSettings();
wm.setAPCallback(configModeCallback);
// id/name, placeholder/prompt, default, length
if (!wm.autoConnect("ZigBeeGW")) {
Serial.println("failed to connect and hit timeout");
//reset and try again, or maybe put it to deep sleep
ESP.restart();
delay(1000);
}
//init and get the time
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
UpdateLocalTime();
#ifdef UseOled
//Oled Show WIFI
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println(F("Zigbee Gateway v0.1"));
display.setCursor(0, 15);
display.println(F("https://nrf52840.ru"));
display.setCursor(0, 30);
display.println(F("WIFI Connected"));
display.setCursor(0, 45);
display.println(F("IP:"));
display.setCursor(20, 45);
display.println(WiFi.localIP());
display.display();
#endif
//Web Server setup
server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) {
request->send(SPIFFS, "/ws.html", "text/html");
});
server.on ( "/devices", [](AsyncWebServerRequest * request) {
devicesWebpage(request);
} );
server.on("/favicon.ico", HTTP_GET, [](AsyncWebServerRequest * request) {
request->send(SPIFFS, "/", "text/html");
});
server.begin();
delay(1000);
webSocket.begin(); webSocket.onEvent(onWebSocketEvent); // Start WebSocket server and assign callback
////
xTaskCreatePinnedToCore(
TaskDecode
, "TaskDecodeUart"
, 32768
, NULL
, 1
, NULL
, ARDUINO_RUNNING_CORE);
xTaskCreatePinnedToCore(
TaskGetFullInfo
, "GetFullInfoFromConnectedDevice"
, 10000
, NULL
, 1
, NULL
, ARDUINO_RUNNING_CORE);
xTaskCreatePinnedToCore(
TaskOled
, "Oled and join data"
, 10000
, NULL
, 1
, NULL
, ARDUINO_RUNNING_CORE);
xTaskCreatePinnedToCore(
TaskDelDevice
, "Delete from database"
, 10000
, NULL
, 1
, NULL
, ARDUINO_RUNNING_CORE);
//Hard RESET
//transmitCommand(0x0012, 0, 0);
//delay(2000);
//SOFT RESET
//transmitCommand(0x0011, 0, 0);
//Check version of firmware on JN5169
transmitCommand(0x0010, 0, 0);
delay(50);
//Set device type Coordinator
setDeviceType(0);
delay(50);
//Set ZigBee Channel
setChannel(11);
delay(50);
transmitCommand(0x0024, 0, 0);
delay(50);
//transmitCommand(0x0015, 0, 0);
//network state
transmitCommand(0x0009, 0, 0);
delay(50);
DiscoverDevices();
delay(50);
//sendMgmtLqiRequest(0x2FE8, 0);
//delay(50);
setPermitJoin(0xFFFC, 0x1E, 0x00);
delay(50);
transmitCommand(0x0014, 0, 0);
delay(50);
setTime(getTime());
delay(50);
transmitCommand(0x0017, 0, 0);
delay(50);
//sendReadAttribRequest(0x5465, 1, 1 , 0 , 0, 0, 0, 1, 0x0005);
//void sendReadAttribRequest(uint16_t u16ShortAddr, byte u8SrcEndPoint, byte u8DstEndPoint, uint16_t u16ClusterID, byte u8Direction, byte u8ManuSpecific, uint16_t u16ManuID, byte u8AttribCount, uint16_t u16AttribID1)
//sqlite_select_answer("0xf0c8fc06");
}
void OledTimeIP()
{
UpdateLocalTime();
#ifdef UseOled
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(5, 0);
display.println(F("Zigbee Gateway v0.1"));
display.setCursor(10, 12);
display.println(F("IP:"));
display.setCursor(40, 12);
display.println(WiFi.localIP());
display.setCursor(20, 24);
display.println(dateStringBuff);
display.setCursor(44, 36);
display.println(timeStringBuff);
if (joinStarted) {
display.setCursor(20, 48);
display.println("Join enabled " + String(joinSecCounter));
}
else {
display.setCursor(10, 48);
String freeRam = String(ESP.getFreeHeap(), DEC);
display.println("Free RAM : " + freeRam);
}
display.display();
#endif
}
void loop() {
//transmitCommand(0x0017, 0, 0);
delay(1000);
//sendClusterOnOff(2,0x5465,1,1,2);
}
/*--------------------------------------------------*/
/*---------------------- Tasks ---------------------*/
/*--------------------------------------------------*/
void TaskDecode(void *pvParameters) // This is a task.
{
(void) pvParameters;
for (;;) // A Task shall never return or exit.
{
serialEvent();
vTaskDelay(10); // one tick delay (15ms) in between reads for stability
}
}
void TaskOled(void *pvParameters) // This is a task.
{
(void) pvParameters;
for (;;) // A Task shall never return or exit.
{
#ifdef UseOled
OledTimeIP();
#endif
if (joinStarted) {
joinSecCounter--;
if (joinSecCounter < 0) {
Serial.println("Join disabled");
joinStarted = false;
webSocket.broadcastTXT("Join disabled");
}
}
vTaskDelay(1000); // one tick delay (15ms) in between reads for stability
}
}
void TaskGetFullInfo(void *pvParameters) // This is a task.
{
(void) pvParameters;
for (;;) // A Task shall never return or exit.
{
if (new_device_connected && joinStarted) {
new_device_connected = false; // получаем флаг что девайс подключился, и сразу его сбросим
// дальше начнем обработку, проверим есть ли железка в базе
if (sqlite_select_answer(u64toStr(new_device_LongAddr)) == 0) {
NewDevComplete = ""; // очищаем стринг вывода
activeEndpointDescriptorRequest(new_device_ShortAddr); // посылаем в сеть запрос эндпоинтов (0x0045)
counter = 5000; // задерка на 5 сек
while (!EpResponse) { // ждем ответа 0x8045 (копируем данные в новый массив, и флаг ставим true)
delay(1);
if (counter-- == 0) {
connectGood = false;
break;
}
} // получили ответ или закончилось время
delay(50); // На всякий случай подождем, чтобы слишком быстро не слать команду
//Эндпоинты получили, пытаемся узнать имя железки
sendReadAttribRequest(new_device_ShortAddr, 1, rxMessageData_newDevice[1] , 0 , 0, 0, 0, 1, 0x0005); // Запрос атрибута как зовут железку
counter = 5000; // задержка 5 сек
while (!DnResponse) { // ждем ответа 0x8102 (почему то ответчает атрибут репорт.... имя ложим в переменную NewDevName)
delay(1);
if (counter-- == 0) {
connectGood = false;
break;
}
}
delay(50); //На всякий случай подождем, чтобы слишком быстро не слать команду
//
NewDevComplete += "{" + u64toStr(new_device_LongAddr) + ": ";
NewDevComplete += NewDevName + " ; ";
NewDevComplete += u16toStr(new_device_ShortAddr) + " ; ";
for (int i = 0; i < rxMessageData_newDevice[0]; i++)
{
NewDevComplete += " Ep";
NewDevComplete += String(i, DEC) + ":";
NewDevComplete += String(rxMessageData_newDevice[i + 1], DEC) + " ; ";
bindEp = rxMessageData_newDevice[i + 1];
simpleDescriptorRequest(new_device_ShortAddr, bindEp);
counter = 5000;
while (!ClResponse) {
delay(1);
if (counter-- == 0) {
connectGood = false;
break;
}
}
///Get clusters
byte u8Length = 0;
u8Length = ClDataNewDevice[0];
if (u8Length > 0)
{
byte u8InputClusterCount = 0;
u8InputClusterCount = ClDataNewDevice[7];
NewDevComplete += "Clusters ";
for (int i = 0; i < u8InputClusterCount; i++)
{
uint16_t u16ClusterId = 0;
u16ClusterId = ClDataNewDevice[(i * 2) + 8];
u16ClusterId <<= 8;
u16ClusterId |= ClDataNewDevice[(i * 2) + 9];
NewDevComplete += ": " + u16toStr(u16ClusterId);
//Тест механики биндда, если сработает будет круто ) Биндим все кластеры по очереди. и биндим только на новые устройства.
if (connectGood && needBind) {
Serial.println(u64toStr(new_device_LongAddr) + ", " + String(bindEp, DEC) + ", " + u16toStr(u16ClusterId) + ", 3, " + u64toStr(u64ExtendedAddr_coord) + ", 1");
sendBindRequest(new_device_LongAddr, bindEp, u16ClusterId, 3, u64ExtendedAddr_coord, 1 );
counter = 5000;
while (!BindResponse) {
delay(1);
if (counter-- == 0) {
break;
}
}
BindResponse = false;
}
delay(50); //На всякий случай подождем, чтобы слишком быстро не слать команду
// sqlite_insertnewdev(u64toStr(new_device_LongAddr), NewDevName, u16toStr(new_device_ShortAddr));
}
}
delay(50); //На всякий случай подождем, чтобы слишком быстро не слать команду
}
NewDevComplete += " }";
if (connectGood == true) {
webSocket.broadcastTXT(NewDevComplete);
sqlite_insertnewdev(u64toStr(new_device_LongAddr), NewDevName, u16toStr(new_device_ShortAddr));
}
else
{
NewDevComplete = "!!!!!!Add device fail, please try again!!!!!!";
webSocket.broadcastTXT(NewDevComplete);
}
Serial.println(NewDevComplete);
connectGood = true; EpResponse = false; BindResponse = false; bindEp = 0; DnResponse = false; ClResponse = false; NewDevName = ""; //needBind = false;
memset(rxMessageData_newDevice, 0, sizeof(rxMessageData_newDevice));
memset(ClDataNewDevice, 0, sizeof(ClDataNewDevice));
}
else {
Serial.println("Device in base");
}
}
vTaskDelay(100); // one tick delay (15ms) in between reads for stability
}
}
void TaskDelDevice(void *pvParameters) // This is a task.
{
(void) pvParameters;
for (;;) // A Task shall never return or exit.
{
if(DelCall){
sqliteDeleteDevice(u64toStr(deletedDevLongAddr));
Serial.println("Delete device from database");
DelCall = false;
}
vTaskDelay(100); // one tick delay (15ms) in between reads for stability
}
}