173 lines
5.1 KiB
C++
173 lines
5.1 KiB
C++
#define CAMERA_MODEL_XIAO_ESP32S3 // PSRAM-enabled camera model
|
|
|
|
#include "esp_camera.h"
|
|
#include <WiFi.h>
|
|
#include "camera_pins.h"
|
|
|
|
// Your Wi-Fi credentials
|
|
const char* ssid = "thetempleofdoom";
|
|
const char* password = "myhandsonmyself";
|
|
|
|
#include "esp_http_server.h"
|
|
#include "esp_camera.h"
|
|
|
|
#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";
|
|
|
|
// Frame buffer conversion function from ESP32 CameraWebServer example
|
|
bool frame2jpg(camera_fb_t * fb, int quality, uint8_t ** out_buf, size_t * out_len) {
|
|
if(!fb || !out_buf || !out_len) return false;
|
|
|
|
*out_buf = NULL;
|
|
*out_len = 0;
|
|
|
|
bool ret = true;
|
|
if(fb->format == PIXFORMAT_JPEG) {
|
|
*out_buf = fb->buf;
|
|
*out_len = fb->len;
|
|
} else {
|
|
if(!fmt2jpg(fb->buf, fb->len, fb->width, fb->height, fb->format, quality, out_buf, out_len)) {
|
|
ret = false;
|
|
}
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
// Implementation of startCameraServer from ESP32 CameraWebServer example
|
|
extern "C" void startCameraServer() {
|
|
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
|
|
config.server_port = 80;
|
|
config.ctrl_port = 80;
|
|
config.max_open_sockets = 3;
|
|
|
|
httpd_handle_t stream_httpd = NULL;
|
|
httpd_handle_t camera_httpd = NULL;
|
|
|
|
if (httpd_start(&stream_httpd, &config) == ESP_OK) {
|
|
httpd_uri_t stream_uri = {
|
|
.uri = "/",
|
|
.method = HTTP_GET,
|
|
.handler = [](httpd_req_t *req){
|
|
camera_fb_t *fb = NULL;
|
|
esp_err_t res = ESP_OK;
|
|
size_t _jpg_buf_len = 0;
|
|
uint8_t *_jpg_buf = NULL;
|
|
char *part_buf[64];
|
|
|
|
res = httpd_resp_set_type(req, _STREAM_CONTENT_TYPE);
|
|
if(res != ESP_OK) return res;
|
|
|
|
while(true) {
|
|
fb = esp_camera_fb_get();
|
|
if (!fb) {
|
|
Serial.println("Camera capture failed");
|
|
res = ESP_FAIL;
|
|
} else {
|
|
if(fb->format != PIXFORMAT_JPEG) {
|
|
bool jpeg_converted = frame2jpg(fb, 80, &_jpg_buf, &_jpg_buf_len);
|
|
if(!jpeg_converted) {
|
|
Serial.println("JPEG compression failed");
|
|
esp_camera_fb_return(fb);
|
|
res = ESP_FAIL;
|
|
}
|
|
} else {
|
|
_jpg_buf_len = fb->len;
|
|
_jpg_buf = fb->buf;
|
|
}
|
|
}
|
|
if(res == ESP_OK) {
|
|
res = httpd_resp_send_chunk(req, (const char *)_jpg_buf, _jpg_buf_len);
|
|
}
|
|
if(fb) {
|
|
esp_camera_fb_return(fb);
|
|
}
|
|
if(res != ESP_OK) break;
|
|
}
|
|
return res;
|
|
},
|
|
.user_ctx = NULL
|
|
};
|
|
httpd_register_uri_handler(stream_httpd, &stream_uri);
|
|
}
|
|
}
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
while (!Serial);
|
|
|
|
// Camera configuration
|
|
camera_config_t config;
|
|
config.ledc_channel = LEDC_CHANNEL_0;
|
|
config.ledc_timer = LEDC_TIMER_0;
|
|
config.pin_d0 = Y2_GPIO_NUM;
|
|
config.pin_d1 = Y3_GPIO_NUM;
|
|
config.pin_d2 = Y4_GPIO_NUM;
|
|
config.pin_d3 = Y5_GPIO_NUM;
|
|
config.pin_d4 = Y6_GPIO_NUM;
|
|
config.pin_d5 = Y7_GPIO_NUM;
|
|
config.pin_d6 = Y8_GPIO_NUM;
|
|
config.pin_d7 = Y9_GPIO_NUM;
|
|
config.pin_xclk = XCLK_GPIO_NUM;
|
|
config.pin_pclk = PCLK_GPIO_NUM;
|
|
config.pin_vsync = VSYNC_GPIO_NUM;
|
|
config.pin_href = HREF_GPIO_NUM;
|
|
config.pin_sccb_sda = SIOD_GPIO_NUM;
|
|
config.pin_sccb_scl = SIOC_GPIO_NUM;
|
|
config.pin_pwdn = PWDN_GPIO_NUM;
|
|
config.pin_reset = RESET_GPIO_NUM;
|
|
config.xclk_freq_hz = 20000000;
|
|
config.pixel_format = PIXFORMAT_JPEG; // for streaming
|
|
config.frame_size = FRAMESIZE_UXGA; // max resolution
|
|
config.grab_mode = CAMERA_GRAB_WHEN_EMPTY;
|
|
config.fb_location = CAMERA_FB_IN_PSRAM; // use PSRAM
|
|
config.jpeg_quality = 12;
|
|
config.fb_count = 1;
|
|
|
|
// Optimize if PSRAM is found
|
|
if (psramFound()) {
|
|
config.jpeg_quality = 10;
|
|
config.fb_count = 2;
|
|
config.grab_mode = CAMERA_GRAB_LATEST;
|
|
}
|
|
|
|
// Initialize the camera
|
|
esp_err_t err = esp_camera_init(&config);
|
|
if (err != ESP_OK) {
|
|
Serial.printf("Camera init failed: 0x%x\n", err);
|
|
return;
|
|
}
|
|
|
|
// Optional sensor settings
|
|
sensor_t* s = esp_camera_sensor_get();
|
|
if (s->id.PID == OV3660_PID) {
|
|
s->set_vflip(s, 1);
|
|
s->set_brightness(s, 1);
|
|
s->set_saturation(s, -2);
|
|
s->set_framesize(s, FRAMESIZE_QVGA);
|
|
}
|
|
|
|
// Connect to Wi-Fi
|
|
WiFi.begin(ssid, password);
|
|
WiFi.setSleep(false);
|
|
Serial.print("Connecting to WiFi");
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
delay(500);
|
|
Serial.print(".");
|
|
}
|
|
Serial.println();
|
|
Serial.println("WiFi connected");
|
|
|
|
// Start the web server
|
|
startCameraServer();
|
|
|
|
Serial.print("Camera Ready! Access at: http://");
|
|
Serial.println(WiFi.localIP());
|
|
}
|
|
|
|
void loop() {
|
|
// All handled in server task
|
|
delay(10000);
|
|
}
|