#include "esp_camera.h"
#include <WiFi.h>

// Wi-Fi 连接信息
const char *ssid = "webserver";
const char *password = "www.nobiggie.cn";

// 定义摄像头模块
#define CAMERA_MODEL_AI_THINKER

// 摄像头引脚定义
#if defined(CAMERA_MODEL_AI_THINKER)
#define PWDN_GPIO_NUM 32
#define RESET_GPIO_NUM -1
#define XCLK_GPIO_NUM 0
#define SIOD_GPIO_NUM 26
#define SIOC_GPIO_NUM 27
#define Y9_GPIO_NUM 35
#define Y8_GPIO_NUM 34
#define Y7_GPIO_NUM 39
#define Y6_GPIO_NUM 36
#define Y5_GPIO_NUM 21
#define Y4_GPIO_NUM 19
#define Y3_GPIO_NUM 18
#define Y2_GPIO_NUM 5
#define VSYNC_GPIO_NUM 25
#define HREF_GPIO_NUM 23
#define PCLK_GPIO_NUM 22
#endif

// 摄像头配置
camera_config_t camera_config = {
   .pin_pwdn = PWDN_GPIO_NUM,
   .pin_reset = RESET_GPIO_NUM,
   .pin_xclk = XCLK_GPIO_NUM,
   .pin_sscb_sda = SIOD_GPIO_NUM,
   .pin_sscb_scl = SIOC_GPIO_NUM,
   .pin_d7 = Y9_GPIO_NUM,
   .pin_d6 = Y8_GPIO_NUM,
   .pin_d5 = Y7_GPIO_NUM,
   .pin_d4 = Y6_GPIO_NUM,
   .pin_d3 = Y5_GPIO_NUM,
   .pin_d2 = Y4_GPIO_NUM,
   .pin_d1 = Y3_GPIO_NUM,
   .pin_d0 = Y2_GPIO_NUM,
   .pin_vsync = VSYNC_GPIO_NUM,
   .pin_href = HREF_GPIO_NUM,
   .pin_pclk = PCLK_GPIO_NUM,
   .xclk_freq_hz = 20000000,
   .ledc_timer = LEDC_TIMER_0,
   .ledc_channel = LEDC_CHANNEL_0,
   .pixel_format = PIXFORMAT_JPEG,
   .frame_size =FRAMESIZE_UXGA,//FRAMESIZE_VAG,
   .jpeg_quality = 12,
   .fb_count = 1
};

void startCameraServer() {
    WiFiServer server(80);
    WiFi.begin(ssid, password);

    while (WiFi.status()!= WL_CONNECTED) {
        delay(1000);
        Serial.println("Connecting to WiFi...");
    }
    Serial.println("Connected to WiFi");
    Serial.println(WiFi.localIP());

    server.begin();

    while (true) {
        WiFiClient client = server.available();
        if (client) {
            Serial.println("New client connected");
            String currentLine = "";
            while (client.connected()) {
                if (client.available()) {
                    char c = client.read();
                    Serial.write(c);
                    if (c == '\n') {
                        if (currentLine.length() == 0) {
                            // 发送 HTTP 响应头
                            client.println("HTTP/1.1 200 OK");
                            client.println("Content-Type: image/jpeg");
                            client.println("Connection: close");
                            client.println();

                            // 获取摄像头图像并发送
                            camera_fb_t *fb = esp_camera_fb_get();
                            esp_camera_fb_return(fb); //清空一下，防止下次还是这个图片
                            fb = esp_camera_fb_get();
                            if (fb) {
                                client.write(fb->buf, fb->len);
                                esp_camera_fb_return(fb);
                            }
                            break;
                        } else {
                            currentLine = "";
                        }
                    } else if (c!= '\r') {
                        currentLine += c;
                    }
                }
            }
            client.stop();
            Serial.println("Client disconnected");
        }
    }
}

void setup() {
    Serial.begin(115200);
    // 初始化摄像头
    esp_err_t err = esp_camera_init(&camera_config);
    if (err!= ESP_OK) {
        Serial.println("Camera init failed");
        return;
    }
    Serial.println("Camera init ok");
    startCameraServer();
}

void loop() {
    // 无需在 loop 中执行其他操作，因为服务器在 setup 中的 while (true) 循环中持续运行
}
