-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathai-camera-firmware.ino
499 lines (441 loc) · 12.7 KB
/
ai-camera-firmware.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
/*******************************************************************
Camera firmware for serial communication with the lower computer,
and wenbsocket communication with the Sunfounder Controller APP,
targeted at ESP32-CAM and TTGO_CAMERA.
Development test environment:
- Arduino IDE 2.0.3
Board tools:
- Arduino AVR Boards 2.0.3
- esp32 (by Espressif Systems) 2.0.7
Libraries:
- ArduinoJson (by Benoit Blanchon)
- WebSockets (by Markus Sattler) (Links2004)
Version: 1.4.1
-- https://github.com/sunfounder/ai-camera-firmware
Author: Sunfounder
Website: http://www.sunfounder.com
https://docs.sunfounder.com
*******************************************************************/
#define VERSION "1.4.1"
#include "led_status.hpp"
#include "who_camera.h"
#include "camera_server.hpp"
#include "ws_server.h"
#include "wifi_helper.h"
#include "ArduinoJson.h"
#include "soc/soc.h" // disable brownout detector
#include "soc/rtc_cntl_reg.h"
#include "esp32/rom/rtc.h" // rst reason
#include <ESPmDNS.h> // mDNS
#include <Preferences.h>
#include "settings_server.hpp"
// https://github.com/espressif/arduino-esp32/blob/master/libraries/ESP32/examples/ResetReason/ResetReason.ino
/* Select development board */
#define ESP32_CAM
// #define TTGO_CAMERA
#include "pins.h" // after define development board
/* ----------------------- Configuration -------------------------------- */
/* Set Wifi mode, SSID and password */
int mode = AP; // STA or AP
String ssid = "aiCAM";
String password = "12345678";
int apChannel = 1;
// int mode = STA; // STA or AP
// String ssid = "xxxxxx";
// String password = "xxxxxxxx";
/* Set websockets port
Sunfounder Controller APP fixed using port 8765
*/
int port = 8765;
/* Set check info for Sunfounder Controller APP */
String name = "AI Camera";
String type = "AI_Camera";
extern String videoUrl;
#define CHECK_TEXT "SC"
/* Set the Debug Level */
#define DEBUG_LEVEL CAM_DEBUG_LEVEL_INFO
#define CAM_DEBUG_LEVEL_OFF 0
#define CAM_DEBUG_LEVEL_ERROR 1
#define CAM_DEBUG_LEVEL_INFO 2
#define CAM_DEBUG_LEVEL_DEBUG 3
#define CAM_DEBUG_LEVEL_ALL 4
/* Set the camera resolution */
// #define FRAMESIZE FRAMESIZE_QVGA // 320x240
// #define FRAMESIZE FRAMESIZE_HVGA // 480x320
#define FRAMESIZE FRAMESIZE_VGA // 640x480
// #define FRAMESIZE FRAMESIZE_SVGA // 800x600
// #define FRAMESIZE FRAMESIZE_XGA // 1024x768
// #define FRAMESIZE FRAMESIZE_HD // 1280x720
/* Set size of fb_count */
#define FB_COUNT 2
/* Set the camera flip */
#define CAMERA_VERTICAL_FLIP 1
#define CAMERA_HORIZONTAL_FLIP 1
/* Set the SERIAL_TIMEOUT (ms) */
#define SERIAL_TIMEOUT 100 // timeout 100ms
#define CHAR_TIMEOUT 5 // char timeout (ms)
/* ----------------------- Global Variables -------------------------- */
String WIFI_MODES[3] = {"None", "STA", "AP"};
WiFiHelper wifi = WiFiHelper();
WS_Server ws_server = WS_Server();
static QueueHandle_t xQueueHttpFrame = NULL;
bool is_camera_started = false;
String rxBuf = "";
bool settingsStarted = false;
/* ----------------------- Functions -------------------------------- */
#define IsStartWith(str, prefix) (strncmp(str, prefix, strlen(prefix)) == 0)
void camera_init();
String serialRead();
void handleSet(String cmd);
void start();
void handleData(String data);
void debug(String msg);
void info(String msg);
void error(String msg);
void debug(String msg, String data);
void info(String msg, String data);
void error(String msg, String data);
/*--------------------- setup() & loop() ------------------------------*/
void setup() {
Serial.begin(115200);
Serial.setTimeout(SERIAL_TIMEOUT);
Serial.println(F("[Init]"));
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); //disable brownout detector
// Read preferences
Preferences preferences;
preferences.begin("config", false);
apChannel = preferences.getInt("apChannel", 1);
preferences.end();
led_init(CAMERA_PIN_LED); //init status_led
LED_STATUS_DISCONNECTED(); //turn on status_led
pinMode(CAMERA_PIN_FLASH, OUTPUT); // init flash lamp
digitalWrite(CAMERA_PIN_FLASH, 0); // 0:turn off flash lamp
int reason = rtc_get_reset_reason(0); // cpu0
if (reason != 12) { // 12, SW_CPU_RESET, Software reset CPU
Serial.println(VERSION);
} else {
Serial.print(F("[OK] ")); // send [OK] when Software Reset
Serial.println(VERSION);
}
// log_i("psram: %d", psramFound());
// log_i("Total heap: %d", ESP.getHeapSize());
// log_i("Free heap: %d", ESP.getFreeHeap());
// log_i("Total PSRAM: %d", ESP.getPsramSize());
// log_i("Free PSRAM: %d", ESP.getFreePsram());
}
void loop() {
led_status_handler();
ws_server_camera_handler();
serial_received_handler();
settingsHandler();
delay(6);
}
/*--------------------- Functions------------------------------*/
/* websocket loop && camera init */
void ws_server_camera_handler() {
if (wifi.is_connected) {
if (mode == STA) {
wifi.check_status();
}
ws_server.loop();
if (!is_camera_started){
camera_init();
}
}
}
void settingsHandler() {
if (settingsStarted) settingsLoop();
}
void serial_received_handler() {
rxBuf = serialRead();
if (rxBuf.length() > 0) {
debug("RX: ", rxBuf);
if (rxBuf.substring(0, 4) == "SET+") {
handleSet(rxBuf.substring(4));
} else if (rxBuf.substring(0, 3) == "WS+") {
String out = rxBuf.substring(3);
ws_server.send(out);
} else if (rxBuf.substring(0, 4) == "WSB+") {
String _data = rxBuf.substring(4);
size_t len = _data.length();
uint8_t* byte_data = (uint8_t*)(_data.c_str());
ws_server.sendBIN(byte_data, len);
}
}
}
String serialRead() {
String buf = "";
char inChar;
bool is_valid = false;
// -- binary data protocol --
bool is_bin = false;
const uint8_t StartCode = 0x0C;
const uint8_t EndCode = 0x0D;
uint8_t len = 0;
uint8_t data_len = 3; // note that default value need to larger than 3
uint32_t char_time = millis();
while (Serial.available() || millis() - char_time < CHAR_TIMEOUT) {
// ------------ read data --------------------
inChar = (char)Serial.read();
if (is_valid == false) { // check & discard the 0xff at the beginning
if ((int)inChar == 0xff || inChar == '\r' || inChar == '\n') {
len = 0;
continue;
} else {
is_valid = true;
}
}
if((int)inChar == StartCode) {
is_bin = true;
}
// ------------ text data --------------------
if (!is_bin) {
if (inChar == '\n') { // \r\n receive end
break;
} else if (inChar == '\r') {
char_time = millis();
continue;
} else {
buf += inChar;
char_time = millis();
}
}
// ------------ binary data --------------------
else if (is_bin) {
len++;
// get binary data length in 2nd byte
if (len == 2) data_len = (uint8_t)inChar;
if (len <= data_len) {
buf += inChar;
char_time = millis();
}
if (len >= data_len) {
break;
}
}
} // while
// debug(buf);
return buf;
}
void camera_init(){
xQueueHttpFrame = xQueueCreate(2, 2*sizeof(camera_fb_t *));
pixformat_t pixel_format = PIXFORMAT_JPEG;
register_camera(
pixel_format, FRAMESIZE, FB_COUNT, xQueueHttpFrame, CAMERA_VERTICAL_FLIP, CAMERA_HORIZONTAL_FLIP,
CAMERA_PIN_Y2, CAMERA_PIN_Y3, CAMERA_PIN_Y4, CAMERA_PIN_Y5, CAMERA_PIN_Y6,
CAMERA_PIN_Y7, CAMERA_PIN_Y8, CAMERA_PIN_Y9, CAMERA_PIN_XCLK,
CAMERA_PIN_PCLK, CAMERA_PIN_VSYNC, CAMERA_PIN_HREF, CAMERA_PIN_SIOD,
CAMERA_PIN_SIOC, CAMERA_PIN_PWDN, CAMERA_PIN_RESET);
register_httpd(xQueueHttpFrame, NULL, true);
is_camera_started = true;
log_i("Free PSRAM: %d", ESP.getFreePsram());
info("camera stream start on: ", videoUrl);
}
void mDNSInit() {
if (MDNS.begin(name)) { // Set mDNS
settingsStarted = true;
} else {
settingsStarted = false;
Serial.println(F("Warning: Error setting up MDNS responder!"));
}
}
void handleSet(String cmd) {
// ------------ 3 characters command ------------
String _3_chars_cmd = cmd.substring(0, 3);
// PSK
if (_3_chars_cmd == "PSK") {
password = cmd.substring(3);
debug("Set password: ", password);
Serial.println("[OK]");
return;
}
// ------------ 4 characters command ------------
String _4_chars_cmd = cmd.substring(0, 4);
// SSID
if (_4_chars_cmd == "SSID") {
ssid = cmd.substring(4);
debug("Set SSID: ", ssid);
Serial.println("[OK]");
return;
}
// NAME
if (_4_chars_cmd == "NAME"){
name = cmd.substring(4);
debug("Set NAME: ", name);
Serial.println("[OK]");
return;
}
// TYPE
else if (_4_chars_cmd == "TYPE"){
type = cmd.substring(4);
debug("Set TYPE: ", type);
Serial.println("[OK]");
return;
}
// SSID
else if (_4_chars_cmd == "SSID"){
ssid = cmd.substring(4);
debug("Set SSID: ", ssid);
Serial.println("[OK]");
return;
}
// PORT
else if (_4_chars_cmd == "PORT"){
port = cmd.substring(4).toInt();
debug("Set port: ", String(port));
Serial.println("[OK]");
return;
}
// MODE
else if (_4_chars_cmd == "MODE"){
mode = cmd.substring(4).toInt();
debug("Set mode: ", WIFI_MODES[mode]);
Serial.println("[OK]");
return;
}
// LAMP
else if (_4_chars_cmd == "LAMP") {
uint8_t brightness_level = cmd.substring(4).toInt();
// digitalWrite(CAMERA_PIN_FLASH, lamp_sw); // 0:turn off flash lamp, 1:turn on flash lamp
if (brightness_level > 10) brightness_level = 10;
analogWrite(CAMERA_PIN_FLASH, brightness_level*25); // brightness_level: 0~10, to pwm 0 ~ 250
Serial.println("[OK]");
return;
}
// ------------ 5 characters command ------------
String _5_chars_cmd = cmd.substring(0, 5);
// RESET
if (_5_chars_cmd == "RESET") {
debug("Reset");
delay(10);
ESP.restart();
return;
}
// START
else if (_5_chars_cmd == "START") {
start();
return;
}
// ----------- if no retrun before -----------
Serial.println("[ERROR] SET+ Unknown command");
}
String getStrOf(String str, uint8_t index, char divider) {
uint8_t start, end;
uint8_t length = str.length();
uint8_t i, j;
// Get start index
if (index == 0) {
start = 0;
} else {
for (start = 0, j = 1; start < length; start++) {
if (str[start] == divider) {
if (index == j) {
start++;
break;
}
j++;
}
}
}
// Get end index
for (end = start, j = 0; end < length; end++) {
if (str[end] == divider) {
break;
}
}
// Copy result
return str.substring(start, end);
}
void setStrOf(char* str, uint8_t index, String value) {
uint8_t start, end;
uint8_t length = strlen(str);
uint8_t i, j;
// Get start index
if (index == 0) {
start = 0;
} else {
for (start = 0, j = 1; start < length; start++) {
if (str[start] == ';') {
if (index == j) {
start++;
break;
}
j++;
}
}
}
// Get end index
for (end = start, j = 0; end < length; end++) {
if (str[end] == ';') {
break;
}
}
String strValue = String(str).substring(0, start) + value + String(str).substring(end);
strcpy(str, strValue.c_str());
}
void start() {
LED_STATUS_ERROOR();
if (ssid.length() == 0) {
error("Please set ssid");
} else if (password.length() == 0) {
error("Please set password");
} else if (mode == NONE) {
error("Please set mode");
} else if (port == 0) {
error("Please set port");
} else{
bool result = wifi.connect(mode, ssid, password, apChannel);
if (!result) {
error("TIMEOUT");
LED_STATUS_ERROOR();
} else {
LED_STATUS_DISCONNECTED();
mDNSInit();
settingsBegin(VERSION, apChannel);
ws_server.close();
ws_server.begin(port, name, type, CHECK_TEXT);
debug("Websocket on!");
Serial.print("[OK] ");Serial.println(wifi.ip);
videoUrl = String("http://") + wifi.ip + ":9000/mjpg";
}
}
}
void debug(String msg) {
#if (DEBUG_LEVEL >= CAM_DEBUG_LEVEL_DEBUG)
Serial.print(F("[CAM_D] "));
Serial.println(msg);
#endif
}
void info(String msg) {
#if (DEBUG_LEVEL >= CAM_DEBUG_LEVEL_INFO)
Serial.print(F("[CAM_I] "));
Serial.println(msg);
#endif
}
void error(String msg) {
#if (DEBUG_LEVEL >= CAM_DEBUG_LEVEL_ERROR)
Serial.print(F("[CAM_E] "));
Serial.println(msg);
#endif
}
void debug(String msg, String data) {
#if (DEBUG_LEVEL >= CAM_DEBUG_LEVEL_DEBUG)
Serial.print(F("[CAM_D] "));
Serial.print(msg);
Serial.println(data);
#endif
}
void info(String msg, String data) {
#if (DEBUG_LEVEL >= CAM_DEBUG_LEVEL_INFO)
Serial.print(F("[CAM_I] "));
Serial.print(msg);
Serial.println(data);
#endif
}
void error(String msg, String data) {
#if (DEBUG_LEVEL >= CAM_DEBUG_LEVEL_ERROR)
Serial.print(F("[CAM_E] "));
Serial.print(msg);
Serial.println(data);
#endif
}