s60sc / ESP32-CAM_MJPEG2SD

ESP32 Camera motion capture application to record JPEGs to SD card as AVI files and stream to browser as MJPEG. If a microphone is installed then a WAV file is also created. Files can be uploaded via FTP or downloaded to browser.
GNU Affero General Public License v3.0
885 stars 209 forks source link

How do I put the scheduling function in? #227

Closed beodeulsoft closed 1 year ago

beodeulsoft commented 1 year ago

In order to use this device on the desk in the office, I want to disable motion detection during business hours, so I want to put in a schedule function. esp32s3_schedule

MJPEG2SD.htm

<nav class="quick-nav" id="scheduler" name="weekly-schedule">?</nav> ... <nav class="menu panel" id="weekly-schedule"> <input type="checkbox" id="schedule-cb" class="menu-action"> <div class="pin-menu" title="Pin / unpin menu">?</div> <label for="schedule-cb" class="nav-toggle">?  Weekly Schedule  </label> <div class='addButtons'></div> <div> <div class="input-group" id="week0-group"> <label for="week0">Monday</label> <div class="switch"> <input id="week0" type="checkbox" checked="checked"> <label title="Enable detecting day of the week" class="slider" for="week0"></label> </div> </div> <div class="input-group" id="week1-group"> <label for="week1">Tuesday</label> <div class="switch"> <input id="week1" type="checkbox" checked="checked"> <label title="Enable detecting day of the week" class="slider" for="week1"></label> </div> </div> <div class="input-group" id="week2-group"> <label for="week2">Wednesday</label> <div class="switch"> <input id="week2" type="checkbox" checked="checked"> <label title="Enable detecting day of the week" class="slider" for="week2"></label> </div> </div> <div class="input-group" id="week3-group"> <label for="week3">Thursday</label> <div class="switch"> <input id="week3" type="checkbox" checked="checked"> <label title="Enable detecting day of the week" class="slider" for="week3"></label> </div> </div> <div class="input-group" id="week4-group"> <label for="week4">Friday</label> <div class="switch"> <input id="week4" type="checkbox" checked="checked"> <label title="Enable detecting day of the week" class="slider" for="week4"></label> </div> </div> <div class="input-group" id="week5-group"> <label for="week5">Saturday</label> <div class="switch"> <input id="week5" type="checkbox" checked="checked"> <label title="Enable detecting day of the week" class="slider" for="week5"></label> </div> </div> <div class="input-group" id="week6-group"> <label for="week6">Sunday</label> <div class="switch"> <input id="week6" type="checkbox" checked="checked"> <label title="Enable detecting day of the week" class="slider" for="week6"></label> </div> </div> <div class="vsep"></div> <div class="input-group" id="day00-group"> <label for="day00">00:00 ~ 01:00</label> <div class="switch"> <input id="day00" type="checkbox" checked="checked"> <label title="Enable detecting time of the day" class="slider" for="day00"></label> </div> </div> <div class="input-group" id="day01-group"> <label for="day01">01:00 ~ 02:00</label> <div class="switch"> <input id="day01" type="checkbox" checked="checked"> <label title="Enable detecting time of the day" class="slider" for="day01"></label> </div> </div> <div class="input-group" id="day02-group"> <label for="day02">02:00 ~ 03:00</label> <div class="switch"> <input id="day02" type="checkbox" checked="checked"> <label title="Enable detecting time of the day" class="slider" for="day02"></label> </div> </div> <div class="input-group" id="day03-group"> <label for="day03">03:00 ~ 04:00</label> <div class="switch"> <input id="day03" type="checkbox" checked="checked"> <label title="Enable detecting time of the day" class="slider" for="day03"></label> </div> </div> <div class="input-group" id="day04-group"> <label for="day04">04:00 ~ 05:00</label> <div class="switch"> <input id="day04" type="checkbox" checked="checked"> <label title="Enable detecting time of the day" class="slider" for="day04"></label> </div> </div> <div class="input-group" id="day05-group"> <label for="day05">05:00 ~ 06:00</label> <div class="switch"> <input id="day05" type="checkbox" checked="checked"> <label title="Enable detecting time of the day" class="slider" for="day05"></label> </div> </div> <div class="input-group" id="day06-group"> <label for="day06">06:00 ~ 07:00</label> <div class="switch"> <input id="day06" type="checkbox" checked="checked"> <label title="Enable detecting time of the day" class="slider" for="day06"></label> </div> </div> <div class="input-group" id="day07-group"> <label for="day07">07:00 ~ 08:00</label> <div class="switch"> <input id="day07" type="checkbox" checked="checked"> <label title="Enable detecting time of the day" class="slider" for="day07"></label> </div> </div> <div class="input-group" id="day08-group"> <label for="day08">08:00 ~ 09:00</label> <div class="switch"> <input id="day08" type="checkbox" checked="checked"> <label title="Enable detecting time of the day" class="slider" for="day08"></label> </div> </div> <div class="input-group" id="day09-group"> <label for="day09">09:00 ~ 10:00</label> <div class="switch"> <input id="day09" type="checkbox" checked="checked"> <label title="Enable detecting time of the day" class="slider" for="day09"></label> </div> </div> <div class="input-group" id="day10-group"> <label for="day10">10:00 ~ 11:00</label> <div class="switch"> <input id="day10" type="checkbox" checked="checked"> <label title="Enable detecting time of the day" class="slider" for="day10"></label> </div> </div> <div class="input-group" id="day11-group"> <label for="day11">11:00 ~ 12:00</label> <div class="switch"> <input id="day11" type="checkbox" checked="checked"> <label title="Enable detecting time of the day" class="slider" for="day11"></label> </div> </div> <div class="input-group" id="day12-group"> <label for="day12">12:00 ~ 13:00</label> <div class="switch"> <input id="day12" type="checkbox" checked="checked"> <label title="Enable detecting time of the day" class="slider" for="day12"></label> </div> </div> <div class="input-group" id="day13-group"> <label for="day13">13:00 ~ 14:00</label> <div class="switch"> <input id="day13" type="checkbox" checked="checked"> <label title="Enable detecting time of the day" class="slider" for="day13"></label> </div> </div> <div class="input-group" id="day14-group"> <label for="day14">14:00 ~ 15:00</label> <div class="switch"> <input id="day14" type="checkbox" checked="checked"> <label title="Enable detecting time of the day" class="slider" for="day14"></label> </div> </div> <div class="input-group" id="day15-group"> <label for="day15">15:00 ~ 16:00</label> <div class="switch"> <input id="day15" type="checkbox" checked="checked"> <label title="Enable detecting time of the day" class="slider" for="day15"></label> </div> </div> <div class="input-group" id="day16-group"> <label for="day16">16:00 ~ 17:00</label> <div class="switch"> <input id="day16" type="checkbox" checked="checked"> <label title="Enable detecting time of the day" class="slider" for="day16"></label> </div> </div> <div class="input-group" id="day17-group"> <label for="day17">17:00 ~ 18:00</label> <div class="switch"> <input id="day17" type="checkbox" checked="checked"> <label title="Enable detecting time of the day" class="slider" for="day17"></label> </div> </div> <div class="input-group" id="day18-group"> <label for="day18">18:00 ~ 19:00</label> <div class="switch"> <input id="day18" type="checkbox" checked="checked"> <label title="Enable detecting time of the day" class="slider" for="day18"></label> </div> </div> <div class="input-group" id="day19-group"> <label for="day19">19:00 ~ 20:00</label> <div class="switch"> <input id="day19" type="checkbox" checked="checked"> <label title="Enable detecting time of the day" class="slider" for="day19"></label> </div> </div> <div class="input-group" id="day20-group"> <label for="day20">20:00 ~ 21:00</label> <div class="switch"> <input id="day20" type="checkbox" checked="checked"> <label title="Enable detecting time of the day" class="slider" for="day20"></label> </div> </div> <div class="input-group" id="day21-group"> <label for="day21">21:00 ~ 22:00</label> <div class="switch"> <input id="day21" type="checkbox" checked="checked"> <label title="Enable detecting time of the day" class="slider" for="day21"></label> </div> </div> <div class="input-group" id="day22-group"> <label for="day22">22:00 ~ 23:00</label> <div class="switch"> <input id="day22" type="checkbox" checked="checked"> <label title="Enable detecting time of the day" class="slider" for="day22"></label> </div> </div> <div class="input-group" id="day23-group"> <label for="day23">23:00 ~ 24:00</label> <div class="switch"> <input id="day23" type="checkbox" checked="checked"> <label title="Enable detecting time of the day" class="slider" for="day23"></label> </div> </div> </div>

appSpecific.cpp

else if(!strcmp(variable, "week0")) week[0] = (intVal) ? true : false; else if(!strcmp(variable, "week1")) week[1] = (intVal) ? true : false; else if(!strcmp(variable, "week2")) week[2] = (intVal) ? true : false; else if(!strcmp(variable, "week3")) week[3] = (intVal) ? true : false; else if(!strcmp(variable, "week4")) week[4] = (intVal) ? true : false; else if(!strcmp(variable, "week5")) week[5] = (intVal) ? true : false; else if(!strcmp(variable, "week6")) week[6] = (intVal) ? true : false; else if(!strcmp(variable, "day00")) day[0] = (intVal) ? true : false; else if(!strcmp(variable, "day01")) day[1] = (intVal) ? true : false; else if(!strcmp(variable, "day02")) day[2] = (intVal) ? true : false; else if(!strcmp(variable, "day03")) day[3] = (intVal) ? true : false; else if(!strcmp(variable, "day04")) day[4] = (intVal) ? true : false; else if(!strcmp(variable, "day05")) day[5] = (intVal) ? true : false; else if(!strcmp(variable, "day06")) day[6] = (intVal) ? true : false; else if(!strcmp(variable, "day07")) day[7] = (intVal) ? true : false; else if(!strcmp(variable, "day08")) day[8] = (intVal) ? true : false; else if(!strcmp(variable, "day09")) day[9] = (intVal) ? true : false; else if(!strcmp(variable, "day10")) day[10] = (intVal) ? true : false; else if(!strcmp(variable, "day11")) day[11] = (intVal) ? true : false; else if(!strcmp(variable, "day12")) day[12] = (intVal) ? true : false; else if(!strcmp(variable, "day13")) day[13] = (intVal) ? true : false; else if(!strcmp(variable, "day14")) day[14] = (intVal) ? true : false; else if(!strcmp(variable, "day15")) day[15] = (intVal) ? true : false; else if(!strcmp(variable, "day16")) day[16] = (intVal) ? true : false; else if(!strcmp(variable, "day17")) day[17] = (intVal) ? true : false; else if(!strcmp(variable, "day18")) day[18] = (intVal) ? true : false; else if(!strcmp(variable, "day19")) day[19] = (intVal) ? true : false; else if(!strcmp(variable, "day20")) day[20] = (intVal) ? true : false; else if(!strcmp(variable, "day21")) day[21] = (intVal) ? true : false; else if(!strcmp(variable, "day22")) day[22] = (intVal) ? true : false; else if(!strcmp(variable, "day23")) day[23] = (intVal) ? true : false;

mjpeg2sd.cpp

bool week[7]; bool day[24];

void getWeekHour(tm * timeinfo) { struct timeval tv; gettimeofday(&tv, NULL);

time_t rawtime = tv.tv_sec;
timeinfo = localtime (&rawtime);

} /**** timers & ISRs ****/

static void IRAM_ATTR frameISR() { // check weekly schedule struct tm timeinfo;; getWeekHour(timeinfo); int nWeek = timeinfo->tm_wday; if(!week[nWeek]) { LOG_ERR("frameISR: Week %d is not scheduled", nWeek); delay(1000); return; } int nHour = timeinfo->tm_hour; if(!day[nHour]) { LOG_ERR("frameISR: Time %d is not scheduled", nHour); delay(1000); return; } // interrupt at current frame rate BaseType_t xHigherPriorityTaskWoken = pdFALSE; vTaskNotifyGiveFromISR(captureHandle, &xHigherPriorityTaskWoken); // wake capture task to process frame if (isPlaying) xSemaphoreGiveFromISR (playbackSemaphore, &xHigherPriorityTaskWoken ); // notify playback to send frame if (xHigherPriorityTaskWoken == pdTRUE) portYIELD_FROM_ISR(); } ... static void startSDtasks() { //init schedule for(int i=0; i < 7; i++) week[i] = true; for(int i=0; i < 24; i++) day[i] = true; // tasks to manage SD card operation xTaskCreate(&captureTask, "captureTask", 1024 4, NULL, 5, &captureHandle); xTaskCreate(&playbackTask, "playbackTask", 1024 4, NULL, 4, &playbackHandle); sensor_t s = esp_camera_sensor_get(); fsizePtr = s->status.framesize; setFPS(frameData[fsizePtr].defaultFPS); // initial frames per second
debugMemory("startSDtasks"); }

After working on the above, I uploaded it to the esp32-s3 cam board, and the web server is not working properly. Even if I change the location of the getWeekTime function, I can't save the schedule information even if I press the save button.

define MAX_CONFIGS 130 // > number of entries in configs.txt

I changed the above value but it still doesn't work, is there any other good way?

s60sc commented 1 year ago

add debug logging in app code and use console.log and chrome dev tools inspector for web page

beodeulsoft commented 1 year ago

It's hard because I'm not a web developer. Thank you.

s60sc commented 1 year ago

one thought, the configs.txt file will not be updated unless the data items already exist in it, so you need to ensure you have added the new data items in configs.txt in the correct format, eg: week0~0~98~~na where week0 is var name, 0 is initial value (false)

beodeulsoft commented 1 year ago

Thank you for your advise.

beodeulsoft commented 1 year ago

Maybe it was a pointer problem before, so the mcu kept rebooting, but it works when I fix the function that checks the schedule as below.

int arrWeek[] = {1,1,1,1,1,1,1}; int arrDay[] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};

void* getWeekHour() { struct timeval tv; gettimeofday(&tv, NULL);

return localtime(&tv.tv_sec); }

static boolean processFrame() { if(timeSynchronized) //init ntp server? { // check weekly schedule struct tm timeinfo = (tm)getWeekHour(); int nWeek = timeinfo->tm_wday; if(arrWeek[nWeek] == 0) { LOG_ERR("processFrame: Week %d is not scheduled", nWeek); //delay(100); return false; } int nHour = timeinfo->tm_hour; if(arrDay[nHour] == 0) { LOG_ERR("processFrame: Time %d is not scheduled", nHour); //delay(100); return false; } } ... }