194671d387
Hold BOOT (GPIO0) for 10s while running to reboot into the captive portal without needing the current media login (physical access is the proof). This also covers the "forgotten login" case. button.c (new) - button_task polls GPIO0 (pull-up, 50 ms), ignores a press held since boot so download-mode entry is unaffected, and calls wifi_request_portal_physical() after 10s. - Started from app_main() right after wifi_start_provisioning_if_needed(), only when not provisioning, so it also works while the station is retrying an unreachable network. wifi.c - wifi_request_portal_physical(): stores a one-shot "portal requested by button" flag in NVS (namespace "portal") and restarts. NVS is used instead of RTC_DATA_ATTR because the flag was lost across the reset on this board. - wifi_start_provisioning_if_needed(): reads and erases the flag at boot (portal_flag_take) and sets s_physical for that portal session only. Logs the reset reason. - Portal without the button (30 failed reconnects, wifi_request_portal()) stays non-physical and asks for the current login. - /save (Wi-Fi card) now requires cam_auth_verify() unless s_physical. - /auth uses cam_auth_set() when s_physical, else cam_auth_change(). - Shared lockout: 5 wrong logins block /save and /auth until restart (not applied when s_physical). Failed attempts are delayed by 1 s. - Physical portal times out after 180 s (normal portal: 300 s). - Root page gets "var PHYS=0/1" injected before its script via chunked send; with PHYS=1 the "current login" fields are hidden and a banner is shown. - html: added current-login fields (wu/wp) to the Wi-Fi form, wrapped current-login fields in .cur, /save errors are shown to the user. - read_body()/send_text() moved above the handlers. cam_auth.c - New cam_auth_set(): validates and stores a new login with no check of the current one. cam_auth_change() now = cam_auth_verify() + cam_auth_set(). Requirements - cam_auth_load() must run before wifi_start_provisioning_if_needed(), otherwise cam_auth_verify() treats auth as disabled and accepts any login. - cam_auth.h: declare cam_auth_set(); declare wifi_request_portal_physical() where button.c can see it. Tested on board: long press reboots into the AP "ESP32_Setup" instead of reconnecting to the stored network. Not yet tested: Wi-Fi card login gating, lockout, 180 s timeout, portal page rendering on a phone.
178 lines
5.3 KiB
C
178 lines
5.3 KiB
C
#include "esp_http_server.h"
|
|
#include "img_converters.h"
|
|
#include "esp_camera.h"
|
|
#include "camera_pinout.h"
|
|
#include "esp32_s3_cam_ovxxxx.h"
|
|
#include "cam_stream.h"
|
|
#include "board.h"
|
|
|
|
#if defined CONFIG_CAMERA_AF_SUPPORT
|
|
#include "esp_camera_af.h"
|
|
#endif
|
|
|
|
static const char *TAG = "Camera";
|
|
|
|
#define PART_BOUNDARY "123456789000000000000987654321"
|
|
static const char* _STREAM_CONTENT_TYPE = "multipart/x-mixed-replace;boundary=" PART_BOUNDARY;
|
|
static const char* _STREAM_BOUNDARY = "\r\n--" PART_BOUNDARY "\r\n";
|
|
static const char* _STREAM_PART = "Content-Type: image/jpeg\r\nContent-Length: %u\r\n\r\n";
|
|
|
|
#if ESP_CAMERA_SUPPORTED
|
|
static camera_config_t camera_config = {
|
|
.pin_pwdn = CAM_PIN_PWDN,
|
|
.pin_reset = CAM_PIN_RESET,
|
|
.pin_xclk = CAM_PIN_XCLK,
|
|
.pin_sccb_sda = -1,
|
|
.pin_sccb_scl = -1,
|
|
|
|
.pin_d7 = CAM_PIN_D7,
|
|
.pin_d6 = CAM_PIN_D6,
|
|
.pin_d5 = CAM_PIN_D5,
|
|
.pin_d4 = CAM_PIN_D4,
|
|
.pin_d3 = CAM_PIN_D3,
|
|
.pin_d2 = CAM_PIN_D2,
|
|
.pin_d1 = CAM_PIN_D1,
|
|
.pin_d0 = CAM_PIN_D0,
|
|
.pin_vsync = CAM_PIN_VSYNC,
|
|
.pin_href = CAM_PIN_HREF,
|
|
.pin_pclk = CAM_PIN_PCLK,
|
|
|
|
//XCLK 20MHz or 10MHz for OV2640 double FPS (Experimental)
|
|
.xclk_freq_hz = 10000000,
|
|
.ledc_timer = LEDC_TIMER_0,
|
|
.ledc_channel = LEDC_CHANNEL_0,
|
|
|
|
.pixel_format = PIXFORMAT_YUV422, //YUV422,GRAYSCALE,RGB565,JPEG
|
|
.frame_size = FRAMESIZE_QVGA, //QQVGA-UXGA, For ESP32, do not use sizes above QVGA when not JPEG. The performance of the ESP32-S series has improved a lot, but JPEG mode always gives better frame rates.
|
|
|
|
.jpeg_quality = 12, //0-63, for OV series camera sensors, lower number means higher quality
|
|
.fb_count = 2, //When jpeg mode is used, if fb_count more than one, the driver will work in continuous mode.
|
|
.fb_location = CAMERA_FB_IN_PSRAM,
|
|
// .grab_mode = CAMERA_GRAB_WHEN_EMPTY,
|
|
.grab_mode = CAMERA_GRAB_LATEST,
|
|
};
|
|
|
|
esp_err_t init_camera(void)
|
|
{
|
|
//initialize the camera
|
|
esp_err_t err = esp_camera_init(&camera_config);
|
|
if (err != ESP_OK)
|
|
{
|
|
ESP_LOGE(TAG, "Camera Init Failed");
|
|
return err;
|
|
}
|
|
|
|
return ESP_OK;
|
|
}
|
|
|
|
esp_err_t setup_handler(httpd_req_t *req)
|
|
{
|
|
if (!cam_http_check(req)) return ESP_OK;
|
|
httpd_resp_set_type(req, "text/plain; charset=utf-8");
|
|
httpd_resp_send(req, "Rebooting into setup mode. Join the Wi-Fi network ESP32_Setup and open http://192.168.4.1/",
|
|
HTTPD_RESP_USE_STRLEN);
|
|
vTaskDelay(pdMS_TO_TICKS(800));
|
|
wifi_request_portal_physical();
|
|
return ESP_OK;
|
|
}
|
|
|
|
void start_camera_server(void)
|
|
{
|
|
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
|
|
httpd_handle_t server = NULL;
|
|
httpd_uri_t uri_setup = { .uri = "/setup", .method = HTTP_POST, .handler = setup_handler };
|
|
httpd_register_uri_handler(server, &uri_setup);
|
|
if (httpd_start(&server, &config) == ESP_OK) {
|
|
httpd_uri_t uri_capture = { .uri = "/pic", .method = HTTP_GET, .handler = capture_handler };
|
|
httpd_uri_t uri_stream = { .uri = "/stream", .method = HTTP_GET, .handler = stream_handler };
|
|
httpd_register_uri_handler(server, &uri_capture);
|
|
httpd_register_uri_handler(server, &uri_stream);
|
|
}
|
|
}
|
|
|
|
#if defined(CONFIG_CAMERA_AF_SUPPORT) && CONFIG_CAMERA_AF_SUPPORT
|
|
static void maybe_init_autofocus(void)
|
|
{
|
|
sensor_t *s = esp_camera_sensor_get();
|
|
if (!s) {
|
|
ESP_LOGW(TAG, "AF: no sensor handle");
|
|
return;
|
|
}
|
|
|
|
if (!esp_camera_af_is_supported(s)) {
|
|
ESP_LOGI(TAG, "AF: not supported by this sensor");
|
|
return;
|
|
}
|
|
|
|
esp_camera_af_config_t af_cfg = {
|
|
.mode = ESP_CAMERA_AF_MODE_AUTO,
|
|
.timeout_ms = CONFIG_CAMERA_AF_DEFAULT_TIMEOUT_MS,
|
|
};
|
|
|
|
esp_err_t ret = esp_camera_af_init(s, &af_cfg);
|
|
if (ret != ESP_OK) {
|
|
ESP_LOGW(TAG, "AF init failed: %s", esp_err_to_name(ret));
|
|
return;
|
|
}
|
|
|
|
ESP_LOGI(TAG, "AF initialized (AUTO mode)");
|
|
}
|
|
#endif
|
|
#endif
|
|
|
|
esp_err_t capture_handler(httpd_req_t *req)
|
|
{
|
|
uint8_t *jpg = NULL;
|
|
size_t cap = 0;
|
|
uint32_t seq;
|
|
|
|
if (!cam_http_check(req)) return ESP_OK;
|
|
|
|
cam_feed_viewer_add(&seq);
|
|
size_t n = cam_feed_wait(&jpg, &cap, &seq, 3000);
|
|
cam_feed_viewer_remove();
|
|
|
|
if (!n) {
|
|
free(jpg);
|
|
httpd_resp_send_500(req);
|
|
return ESP_FAIL;
|
|
}
|
|
httpd_resp_set_type(req, "image/jpeg");
|
|
esp_err_t r = httpd_resp_send(req, (const char *)jpg, n);
|
|
free(jpg);
|
|
return r;
|
|
}
|
|
|
|
esp_err_t stream_handler(httpd_req_t *req)
|
|
{
|
|
uint8_t *jpg = NULL;
|
|
size_t cap = 0;
|
|
uint32_t seq;
|
|
int idle = 0;
|
|
char part[64];
|
|
|
|
if (!cam_http_check(req)) return ESP_OK;
|
|
esp_err_t res = httpd_resp_set_type(req, _STREAM_CONTENT_TYPE);
|
|
if (res != ESP_OK) return res;
|
|
|
|
cam_feed_viewer_add(&seq);
|
|
while (1) {
|
|
size_t n = cam_feed_wait(&jpg, &cap, &seq, 2000);
|
|
if (!n) {
|
|
if (++idle >= 5) { res = ESP_FAIL; break; } // no frames for 10 s: give up
|
|
continue;
|
|
}
|
|
idle = 0;
|
|
|
|
res = httpd_resp_send_chunk(req, _STREAM_BOUNDARY, strlen(_STREAM_BOUNDARY));
|
|
if (res == ESP_OK) {
|
|
size_t hl = snprintf(part, sizeof(part), _STREAM_PART, (unsigned)n);
|
|
res = httpd_resp_send_chunk(req, part, hl);
|
|
}
|
|
if (res == ESP_OK) res = httpd_resp_send_chunk(req, (const char *)jpg, n);
|
|
if (res != ESP_OK) break; // browser left
|
|
}
|
|
cam_feed_viewer_remove();
|
|
free(jpg);
|
|
return res;
|
|
} |