Skip to content

Commit

Permalink
Spiffs basic preferences save and restore (#49)
Browse files Browse the repository at this point in the history
Implement SPIFFS based preferences save and restore, stubs for FaceDB to be aded later.
UI confirmations for Actions
  • Loading branch information
easytarget authored Sep 27, 2020
1 parent a755093 commit 25d5c86
Show file tree
Hide file tree
Showing 10 changed files with 708 additions and 55 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ Compile and upload the code from the IDE, when the `Connecting...` appears in th

Once the upload completes (be patient, it can be a bit slow) open the serial monitor in the IDE and reboot the board again without GPIO0 grounded. In the serial monitor you should see the board start, connect to the wifi and then report the IP address it has been assigned.

If you have a status LED configured it will give a single slow flash when it begins attempting to conenct to WiFi, and three short flashes once it has succeeded. It will also flash briefly when you access the camera to change settings.
If you have a status LED configured it will give a double flash when it begins attempting to conenct to WiFi, and five short flashes once it has succeeded. It will also flash briefly when you access the camera to change settings.

Go to the URL given in the serial output, the web UI should appear with the settings panel open. Click away!

Expand Down
125 changes: 82 additions & 43 deletions app_httpd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "viewers.h"
#include "css.h"
#include "favicons.h"
#include "storage.h"

//#define DEBUG_STREAM_DATA // Debug: dump info for each stream frame on serial port

Expand All @@ -37,6 +38,7 @@ extern int lampVal; // The current Lamp value
extern char streamURL[]; // Stream URL
extern int8_t detection_enabled; // Face detection enable
extern int8_t recognition_enabled; // Face recognition enable
extern bool filesystem; // Save/restore features enabled

#include "fb_gfx.h"
#include "fd_forward.h"
Expand Down Expand Up @@ -342,9 +344,11 @@ static esp_err_t stream_handler(httpd_req_t *req){
int64_t fr_ready = 0;
#endif

Serial.println("Stream started:");
Serial.println("Stream started");

flashLED(75); // little flash of status LED
flashLED(75); // double flash of status LED
delay(75);
flashLED(75);

static int64_t last_frame = 0;
if(!last_frame) {
Expand Down Expand Up @@ -498,6 +502,7 @@ static esp_err_t cmd_handler(httpd_req_t *req){
size_t buf_len;
char variable[32] = {0,};
char value[32] = {0,};
flashLED(75);

buf_len = httpd_req_get_url_query_len(req) + 1;
if (buf_len > 1) {
Expand All @@ -524,11 +529,10 @@ static esp_err_t cmd_handler(httpd_req_t *req){
httpd_resp_send_404(req);
return ESP_FAIL;
}

int val = atoi(value);
sensor_t * s = esp_camera_sensor_get();
int res = 0;

if(!strcmp(variable, "framesize")) {
if(s->pixformat == PIXFORMAT_JPEG) res = s->set_framesize(s, (framesize_t)val);
}
Expand Down Expand Up @@ -570,31 +574,42 @@ static esp_err_t cmd_handler(httpd_req_t *req){
}
}
else if(!strcmp(variable, "lamp") && (lampVal != -1)) {
lampVal = constrain(val,0,100);
setLamp(lampVal);
lampVal = constrain(val,0,100);
setLamp(lampVal);
}
else if(!strcmp(variable, "save_prefs")) {
if (filesystem) savePrefs(SPIFFS);
}
else if(!strcmp(variable, "clear_prefs")) {
if (filesystem) removePrefs(SPIFFS);
}
else if(!strcmp(variable, "reboot")) {
Serial.print("REBOOT requested");
for (int i=0; i<50; i++) {
flashLED(20);
delay(20);
Serial.print('.');
}
Serial.printf("\nThats all folks...\n\n");
ESP.restart();
}
else {
res = -1;
}


if(res){
return httpd_resp_send_500(req);
}

flashLED(75); // little flash of status LED

httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
return httpd_resp_send(req, NULL, 0);
}

static esp_err_t status_handler(httpd_req_t *req){

static char json_response[1024];
sensor_t * s = esp_camera_sensor_get();
char * p = json_response;
flashLED(75);
*p++ = '{';
p+=sprintf(p, "\"lamp\":%i,", lampVal);
p+=sprintf(p, "\"lamp\":%d,", lampVal);
p+=sprintf(p, "\"framesize\":%u,", s->status.framesize);
p+=sprintf(p, "\"quality\":%u,", s->status.quality);
p+=sprintf(p, "\"brightness\":%d,", s->status.brightness);
Expand Down Expand Up @@ -635,6 +650,7 @@ static esp_err_t status_handler(httpd_req_t *req){
}

static esp_err_t info_handler(httpd_req_t *req){
flashLED(75);
static char json_response[256];
char * p = json_response;
*p++ = '{';
Expand All @@ -650,63 +666,89 @@ static esp_err_t info_handler(httpd_req_t *req){
}

static esp_err_t favicon_16x16_handler(httpd_req_t *req){
flashLED(75); // a little feedback to user
delay(75);
flashLED(75);
httpd_resp_set_type(req, "image/png");
httpd_resp_set_hdr(req, "Content-Encoding", "identity");
return httpd_resp_send(req, (const char *)favicon_16x16_png, favicon_16x16_png_len);
}

static esp_err_t favicon_32x32_handler(httpd_req_t *req){
flashLED(75); // a little feedback to user
delay(75);
flashLED(75);

httpd_resp_set_type(req, "image/png");
httpd_resp_set_hdr(req, "Content-Encoding", "identity");
return httpd_resp_send(req, (const char *)favicon_32x32_png, favicon_32x32_png_len);
}

static esp_err_t favicon_ico_handler(httpd_req_t *req){
flashLED(75); // a little feedback to user
delay(75);
flashLED(75);
httpd_resp_set_type(req, "image/x-icon");
httpd_resp_set_hdr(req, "Content-Encoding", "identity");
return httpd_resp_send(req, (const char *)favicon_ico, favicon_ico_len);
}

static esp_err_t save_prefs_handler(httpd_req_t *req){
flashLED(75);
savePrefs(SPIFFS);
httpd_resp_set_type(req, "text/css");
httpd_resp_set_hdr(req, "Content-Encoding", "identity");
char resp[] = "Preferences saved";
return httpd_resp_send(req, resp, strlen(resp));
}

static esp_err_t remove_prefs_handler(httpd_req_t *req){
flashLED(75);
httpd_resp_set_type(req, "text/css");
httpd_resp_set_hdr(req, "Content-Encoding", "identity");
if (filesystem) {
removePrefs(SPIFFS);
char resp[] = "Preferences file removed, reboot will revert to default settings";
return httpd_resp_send(req, resp, strlen(resp));
} else {
char resp[] = "No internal filesystem; save/restore functions disabled";
return httpd_resp_send(req, resp, strlen(resp));
}
}

// DEBUG
extern void dumpPrefs(fs::FS &fs);
static esp_err_t dump_prefs_handler(httpd_req_t *req){
flashLED(75);
dumpPrefs(SPIFFS);
Serial.printf("mtmn_config size: %u :: ra_filter size: %u :: id_list %u\n", sizeof(mtmn_config), sizeof(ra_filter), sizeof(id_list));
httpd_resp_set_type(req, "text/css");
httpd_resp_set_hdr(req, "Content-Encoding", "identity");
char resp[] = "DEBUG: Preferences file Dumped to serial";
return httpd_resp_send(req, resp, strlen(resp));
}
// /DEBUG


static esp_err_t style_handler(httpd_req_t *req){
flashLED(75); // a little feedback to user
delay(75);
flashLED(75);
httpd_resp_set_type(req, "text/css");
httpd_resp_set_hdr(req, "Content-Encoding", "identity");
return httpd_resp_send(req, (const char *)style_css, style_css_len);
}

static esp_err_t miniviewer_handler(httpd_req_t *req){
flashLED(75); // a little feedback to user
delay(75);
flashLED(75);
Serial.println("Simple viewer requested");
httpd_resp_set_type(req, "text/html");
httpd_resp_set_hdr(req, "Content-Encoding", "identity");
return httpd_resp_send(req, (const char *)miniviewer_html, miniviewer_html_len);
}

static esp_err_t streamviewer_handler(httpd_req_t *req){
flashLED(75); // a little feedback to user
delay(75);
flashLED(75);
Serial.println("Stream Viewer requested");
httpd_resp_set_type(req, "text/html");
httpd_resp_set_hdr(req, "Content-Encoding", "identity");
return httpd_resp_send(req, (const char *)streamviewer_html, streamviewer_html_len);
}

static esp_err_t index_handler(httpd_req_t *req){
flashLED(75); // a little feedback to user
delay(75);
flashLED(75);
Serial.println("Index page requested");
httpd_resp_set_type(req, "text/html");
httpd_resp_set_hdr(req, "Content-Encoding", "identity");
sensor_t * s = esp_camera_sensor_get();
Expand All @@ -718,86 +760,82 @@ static esp_err_t index_handler(httpd_req_t *req){

void startCameraServer(int hPort, int sPort){
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
config.max_uri_handlers = 20; // we use more than the default 8...

config.max_uri_handlers = 10; // we use more than the default 8 (on port 80)

httpd_uri_t index_uri = {
.uri = "/",
.method = HTTP_GET,
.handler = index_handler,
.user_ctx = NULL
};

httpd_uri_t status_uri = {
.uri = "/status",
.method = HTTP_GET,
.handler = status_handler,
.user_ctx = NULL
};

httpd_uri_t cmd_uri = {
.uri = "/control",
.method = HTTP_GET,
.handler = cmd_handler,
.user_ctx = NULL
};

httpd_uri_t capture_uri = {
.uri = "/capture",
.method = HTTP_GET,
.handler = capture_handler,
.user_ctx = NULL
};

httpd_uri_t miniviewer_uri = {
.uri = "/view",
.method = HTTP_GET,
.handler = miniviewer_handler,
.user_ctx = NULL
};

httpd_uri_t style_uri = {
.uri = "/style.css",
.method = HTTP_GET,
.handler = style_handler,
.user_ctx = NULL
};

httpd_uri_t favicon_16x16_uri = {
.uri = "/favicon-16x16.png",
.method = HTTP_GET,
.handler = favicon_16x16_handler,
.user_ctx = NULL
};

httpd_uri_t favicon_32x32_uri = {
.uri = "/favicon-32x32.png",
.method = HTTP_GET,
.handler = favicon_32x32_handler,
.user_ctx = NULL
};

httpd_uri_t favicon_ico_uri = {
.uri = "/favicon.ico",
.method = HTTP_GET,
.handler = favicon_ico_handler,
.user_ctx = NULL
};

// DEBUG
httpd_uri_t dump_prefs_uri = {
.uri = "/dump",
.method = HTTP_GET,
.handler = dump_prefs_handler,
.user_ctx = NULL
};
// DEBUG
httpd_uri_t stream_uri = {
.uri = "/",
.method = HTTP_GET,
.handler = stream_handler,
.user_ctx = NULL
};

httpd_uri_t streamviewer_uri = {
.uri = "/view",
.method = HTTP_GET,
.handler = streamviewer_handler,
.user_ctx = NULL
};

httpd_uri_t info_uri = {
.uri = "/info",
.method = HTTP_GET,
Expand All @@ -806,7 +844,6 @@ void startCameraServer(int hPort, int sPort){
};

ra_filter_init(&ra_filter, 20);

mtmn_config.type = FAST;
mtmn_config.min_face = 80;
mtmn_config.pyramid = 0.707;
Expand All @@ -820,7 +857,6 @@ void startCameraServer(int hPort, int sPort){
mtmn_config.o_threshold.score = 0.7;
mtmn_config.o_threshold.nms = 0.7;
mtmn_config.o_threshold.candidate_number = 1;

face_id_init(&id_list, FACE_ID_SAVE_NUMBER, ENROLL_CONFIRM_TIMES);

config.server_port = hPort;
Expand All @@ -836,6 +872,7 @@ void startCameraServer(int hPort, int sPort){
httpd_register_uri_handler(camera_httpd, &favicon_16x16_uri);
httpd_register_uri_handler(camera_httpd, &favicon_32x32_uri);
httpd_register_uri_handler(camera_httpd, &favicon_ico_uri);
httpd_register_uri_handler(camera_httpd, &dump_prefs_uri); // DEBUG
}


Expand All @@ -850,4 +887,6 @@ void startCameraServer(int hPort, int sPort){
httpd_register_uri_handler(stream_httpd, &favicon_32x32_uri);
httpd_register_uri_handler(stream_httpd, &favicon_ico_uri);
}

Serial.printf("mtmn_config size: %u :: ra_filter size: %u :: id_list %u\n", sizeof(mtmn_config), sizeof(ra_filter), sizeof(id_list));
}
Loading

0 comments on commit 25d5c86

Please sign in to comment.