Skip to content

Commit

Permalink
Allow setting hostname and auto-led (#75)
Browse files Browse the repository at this point in the history
Implement automatic Lamp illumination only while camera is active. (#72 thanks @TodWulff )
Allow Hostname setting (#74 thanks @mrmessy )
A couple of small fixes and some unnecessary debug output removed too.
  • Loading branch information
easytarget authored Oct 25, 2020
1 parent e2dfb77 commit 244fbb3
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 39 deletions.
45 changes: 35 additions & 10 deletions app_httpd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ extern int streamPort;
extern char httpURL[];
extern char streamURL[];
extern char default_index[];
extern int8_t streamCount;
extern int myRotation;
extern int lampVal;
extern bool autoLamp;
extern int8_t detection_enabled;
extern int8_t recognition_enabled;
extern bool filesystem;
Expand Down Expand Up @@ -257,7 +259,7 @@ static esp_err_t capture_handler(httpd_req_t *req){
esp_err_t res = ESP_OK;

Serial.println("Capture Requested");

if (autoLamp && (lampVal != -1)) setLamp(lampVal);
flashLED(75); // little flash of status LED

int64_t fr_start = esp_timer_get_time();
Expand All @@ -266,6 +268,7 @@ static esp_err_t capture_handler(httpd_req_t *req){
if (!fb) {
Serial.println("Camera capture failed");
httpd_resp_send_500(req);
if (autoLamp && (lampVal != -1)) setLamp(0);
return ESP_FAIL;
}

Expand All @@ -291,7 +294,10 @@ static esp_err_t capture_handler(httpd_req_t *req){
}
esp_camera_fb_return(fb);
int64_t fr_end = esp_timer_get_time();
Serial.printf("JPG: %uB %ums\n", (uint32_t)(fb_len), (uint32_t)((fr_end - fr_start)/1000));
if (debugData) {
Serial.printf("JPG: %uB %ums\n", (uint32_t)(fb_len), (uint32_t)((fr_end - fr_start)/1000));
}
if (autoLamp && (lampVal != -1)) setLamp(0);
return res;
}

Expand All @@ -300,6 +306,7 @@ static esp_err_t capture_handler(httpd_req_t *req){
esp_camera_fb_return(fb);
Serial.println("dl_matrix3du_alloc failed");
httpd_resp_send_500(req);
if (autoLamp && (lampVal != -1)) setLamp(0);
return ESP_FAIL;
}

Expand All @@ -314,6 +321,7 @@ static esp_err_t capture_handler(httpd_req_t *req){
dl_matrix3du_free(image_matrix);
Serial.println("to rgb888 failed");
httpd_resp_send_500(req);
if (autoLamp && (lampVal != -1)) setLamp(0);
return ESP_FAIL;
}

Expand All @@ -336,13 +344,15 @@ static esp_err_t capture_handler(httpd_req_t *req){
dl_matrix3du_free(image_matrix);
if(!s){
Serial.println("JPEG compression failed");
if (autoLamp && (lampVal != -1)) setLamp(0);
return ESP_FAIL;
}

int64_t fr_end = esp_timer_get_time();
if (debugData) {
Serial.printf("FACE: %uB %ums %s%d\n", (uint32_t)(jchunk.len), (uint32_t)((fr_end - fr_start)/1000), detected?"DETECTED ":"", face_id);
}
if (autoLamp && (lampVal != -1)) setLamp(0);
return res;
}

Expand All @@ -361,10 +371,10 @@ static esp_err_t stream_handler(httpd_req_t *req){
int64_t fr_encode = 0;
int64_t fr_ready = 0;


Serial.println("Stream requested");

flashLED(75); // double flash of status LED
if (autoLamp && (lampVal != -1)) setLamp(lampVal);
streamCount = 1; // at present we only have one stream handler..
flashLED(75); // double flash of status LED
delay(75);
flashLED(75);

Expand All @@ -375,6 +385,7 @@ static esp_err_t stream_handler(httpd_req_t *req){

res = httpd_resp_set_type(req, _STREAM_CONTENT_TYPE);
if(res != ESP_OK){
if (autoLamp && (lampVal != -1)) setLamp(0);
return res;
}

Expand Down Expand Up @@ -497,6 +508,9 @@ static esp_err_t stream_handler(httpd_req_t *req){
}
}

if (autoLamp && (lampVal != -1)) setLamp(0);
streamCount = 0; // at present we only have one stream handler..
Serial.println("Stream ended");
last_frame = 0;
return res;
}
Expand Down Expand Up @@ -577,9 +591,23 @@ static esp_err_t cmd_handler(httpd_req_t *req){
detection_enabled = val;
}
}
else if(!strcmp(variable, "autolamp") && (lampVal != -1)) {
autoLamp = val;
if (autoLamp) {
if (streamCount > 0) setLamp(lampVal);
else setLamp(0);
} else {
setLamp(lampVal);
}
}
else if(!strcmp(variable, "lamp") && (lampVal != -1)) {
lampVal = constrain(val,0,100);
setLamp(lampVal);
if (autoLamp) {
if (streamCount > 0) setLamp(lampVal);
else setLamp(0);
} else {
setLamp(lampVal);
}
}
else if(!strcmp(variable, "save_face")) {
if (filesystem) saveFaceDB(SPIFFS);
Expand Down Expand Up @@ -619,6 +647,7 @@ static esp_err_t status_handler(httpd_req_t *req){
char * p = json_response;
*p++ = '{';
p+=sprintf(p, "\"lamp\":%d,", lampVal);
p+=sprintf(p, "\"autolamp\":%d,", autoLamp);
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 @@ -987,10 +1016,6 @@ void startCameraServer(int hPort, int sPort){
// Face ID list (settings + pointer to the data allocation)
face_id_init(&id_list, FACE_ID_SAVE_NUMBER, ENROLL_CONFIRM_TIMES);
// The size of the allocated data block; calculated in dl_lib_calloc()
id_list_alloc = FACE_ID_SAVE_NUMBER * sizeof(dl_matrix3d_t *) + sizeof(void *);
Serial.print("FACE DB SIZE: ");
Serial.println(id_list_alloc);
Serial.printf("FACE DB POINTER: %p\n", id_list.id_list);

config.server_port = hPort;
config.ctrl_port = hPort;
Expand Down
44 changes: 30 additions & 14 deletions esp32-cam-webserver.ino
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ int stationCount = sizeof(stationList)/sizeof(stationList[0]);
#else
char default_index[] = "simple";
#endif


// DNS server
const byte DNS_PORT = 53;
Expand All @@ -118,6 +117,9 @@ char apName[64] = "Undefined";
char httpURL[64] = {"Undefined"};
char streamURL[64] = {"Undefined"};

// Count number of active streams
int8_t streamCount = 0;

// This will be displayed to identify the firmware
char myVer[] PROGMEM = __DATE__ " @ " __TIME__;

Expand All @@ -140,6 +142,7 @@ int myRotation = CAM_ROTATION;
#else
int lampVal = -1; // no lamp pin assigned
#endif
bool autoLamp = false; // Automatic lamp (auto on while camera running)

int lampChannel = 7; // a free PWM channel (some channels used by camera)
const int pwmfreq = 50000; // 50K pwm frequency
Expand Down Expand Up @@ -281,9 +284,14 @@ void WifiSetup() {
Serial.println("Static IP settings requested but not defined in config, falling back to dhcp");
#endif
}

#if defined(HOSTNAME)
WiFi.setHostname(HOSTNAME);
#endif

// Initiate network connection request
WiFi.begin(stationList[bestStation].ssid, stationList[bestStation].password);

// Wait to connect, or timeout
unsigned long start = millis();
while ((millis() - start <= WIFI_WATCHDOG) && (WiFi.status() != WL_CONNECTED)) {
Expand Down Expand Up @@ -377,7 +385,7 @@ void setup() {
Serial.println("No wifi ssid details have been configured; we cannot connect to WiFi or start our own AccessPoint");
while (true) delay(1000);
}

#if defined(LED_PIN) // If we have a notification LED, set it to output
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LED_ON);
Expand Down Expand Up @@ -515,14 +523,15 @@ void setup() {
Serial.println("No Internal Filesystem, cannot save preferences or face DB");
}

/*
/*
* Camera setup complete; initialise the rest of the hardware.
*/

// Initialise and set the lamp
if (lampVal != -1) {
ledcSetup(lampChannel, pwmfreq, pwmresolution); // configure LED PWM channel
setLamp(lampVal); // set default value
if (autoLamp) setLamp(0); // set default value
else setLamp(lampVal);
ledcAttachPin(LAMP_PIN, lampChannel); // attach the GPIO pin to the channel
} else {
Serial.println("No lamp, or lamp disabled in config");
Expand All @@ -536,16 +545,23 @@ void setup() {

// Now we have a network we can start the two http handlers for the UI and Stream.
startCameraServer(httpPort, streamPort);

// Construct the app and stream URLs
if (httpPort != 80) {
sprintf(httpURL, "http://%d.%d.%d.%d:%d/", ip[0], ip[1], ip[2], ip[3], httpPort);
} else {
sprintf(httpURL, "http://%d.%d.%d.%d/", ip[0], ip[1], ip[2], ip[3]);
}

#if defined(URL_HOSTNAME)
if (httpPort != 80) {
sprintf(httpURL, "http://%s:%d/", URL_HOSTNAME, httpPort);
} else {
sprintf(httpURL, "http://%s/", URL_HOSTNAME);
}
sprintf(streamURL, "http://%s:%d/", URL_HOSTNAME, streamPort);
#else
if (httpPort != 80) {
sprintf(httpURL, "http://%d.%d.%d.%d:%d/", ip[0], ip[1], ip[2], ip[3], httpPort);
} else {
sprintf(httpURL, "http://%d.%d.%d.%d/", ip[0], ip[1], ip[2], ip[3]);
}
sprintf(streamURL, "http://%d.%d.%d.%d:%d/", ip[0], ip[1], ip[2], ip[3], streamPort);
#endif
Serial.printf("\nCamera Ready!\nUse '%s' to connect\n", httpURL);
// Construct the Stream URL
sprintf(streamURL, "http://%d.%d.%d.%d:%d/", ip[0], ip[1], ip[2], ip[3], streamPort);
Serial.printf("Stream viewer available at '%sview'\n", streamURL);
Serial.printf("Raw stream URL is '%s'\n", streamURL);

Expand Down
8 changes: 4 additions & 4 deletions index_other.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ const uint8_t index_simple_html[] = R"=====(<!doctype html>
applyRotation();
} else if(el.id === "stream_url"){
streamURL = value;
streamButton.setAttribute("title", `Start the stream (${streamURL})`);
streamButton.setAttribute("title", `Start the stream :: {streamURL}`);
console.log('Stream URL set to:' + value);
}
}
Expand Down Expand Up @@ -190,20 +190,20 @@ const uint8_t index_simple_html[] = R"=====(<!doctype html>
})
// Put some helpful text on the 'Still' button
stillButton.setAttribute("title", `Capture a still image (${baseHost}/capture)`);
stillButton.setAttribute("title", `Capture a still image :: ${baseHost}/capture`);
const stopStream = () => {
window.stop();
streamButton.innerHTML = 'Start Stream';
streamButton.setAttribute("title", `Start the stream (${streamURL})`);
streamButton.setAttribute("title", `Start the stream :: ${streamURL}`);
hide(viewContainer);
}
const startStream = () => {
view.src = streamURL;
view.scrollIntoView(false);
streamButton.innerHTML = 'Stop Stream';
streamButton.setAttribute("title", `Stop the stream (${streamURL})`);
streamButton.setAttribute("title", `Stop the stream`);
show(viewContainer);
}
Expand Down
21 changes: 16 additions & 5 deletions index_ov2640.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ const uint8_t index_ov2640_html[] = R"=====(<!doctype html>
<input type="range" id="lamp" min="0" max="100" value="0" class="default-action">
<div class="range-max">Full</div>
</div>
<div class="input-group hidden" id="autolamp-group">
<label for="autolamp">Auto Lamp</label>
<div class="switch">
<input id="autolamp" type="checkbox" class="default-action" title="Lamp only on when camera active">
<label class="slider" for="autolamp"></label>
</div>
</div>

<div class="input-group" id="framesize-group">
<label for="framesize">Resolution</label>
<select id="framesize" class="default-action">
Expand Down Expand Up @@ -288,6 +296,7 @@ const uint8_t index_ov2640_html[] = R"=====(<!doctype html>
const settings = document.getElementById('sidebar')
const waitSettings = document.getElementById('wait-settings')
const lampGroup = document.getElementById('lamp-group')
const autolampGroup = document.getElementById('autolamp-group')
const streamGroup = document.getElementById('stream-group')
const camName = document.getElementById('cam_name')
const codeVer = document.getElementById('code_ver')
Expand Down Expand Up @@ -358,8 +367,10 @@ const uint8_t index_ov2640_html[] = R"=====(<!doctype html>
} else if(el.id === "lamp"){
if (value == -1) {
hide(lampGroup)
hide(autolampGroup)
} else {
show(lampGroup)
show(autolampGroup)
}
} else if(el.id === "cam_name"){
camName.innerHTML = value;
Expand All @@ -375,10 +386,10 @@ const uint8_t index_ov2640_html[] = R"=====(<!doctype html>
streamURL = value;
viewerURL = value + 'view';
stream_url.innerHTML = value;
stream_link.setAttribute("title", "Open stream viewer (" + viewerURL + ")");
stream_link.setAttribute("title", `Open the standalone stream viewer :: ${viewerURL}`);
stream_link.style.textDecoration = "underline";
stream_link.style.cursor = "pointer";
streamButton.setAttribute("title", `Start the stream (${streamURL})`);
streamButton.setAttribute("title", `Start the stream :: ${streamURL}`);
show(streamGroup)
console.log('Stream URL set to: ' + streamURL);
console.log('Stream Viewer URL set to: ' + viewerURL);
Expand Down Expand Up @@ -438,20 +449,20 @@ const uint8_t index_ov2640_html[] = R"=====(<!doctype html>
})

// Put some helpful text on the 'Still' button
stillButton.setAttribute("title", `Capture a still image (${baseHost}/capture)`);
stillButton.setAttribute("title", `Capture a still image :: ${baseHost}/capture`);

const stopStream = () => {
window.stop();
streamButton.innerHTML = 'Start Stream';
streamButton.setAttribute("title", `Start the stream (${streamURL})`);
streamButton.setAttribute("title", `Start the stream :: ${streamURL}`);
hide(viewContainer);
}

const startStream = () => {
view.src = streamURL;
view.scrollIntoView(false);
streamButton.innerHTML = 'Stop Stream';
streamButton.setAttribute("title", `Stop the stream (${streamURL})`);
streamButton.setAttribute("title", `Stop the stream`);
show(viewContainer);
}

Expand Down
Loading

0 comments on commit 244fbb3

Please sign in to comment.