· last year · Feb 12, 2025, 03:50 AM
1#include "esp_camera.h"
2#include <WiFi.h>
3#include <HTTPClient.h>
4#include <WebServer.h>
5
6#define CAMERA_MODEL_AI_THINKER
7#include "camera_pins.h"
8
9// WiFi credentials
10const char* ssid = "AOBY_2.4";
11const char* password = "PASSWORD_1995";
12
13// Flask server URL
14const char* flaskServerUrl = "http://192.168.254.108:5000/capture";
15
16// Web server on port 80
17WebServer server(80);
18
19void handleCapture() {
20 Serial.println("[INFO] Received HTTP request on /capture");
21
22 // Capture an image
23 camera_fb_t *fb = esp_camera_fb_get();
24 if (!fb) {
25 Serial.println("[ERROR] Camera capture failed");
26 return;
27 }
28 Serial.printf("[INFO] Captured image size: %d bytes\n", fb->len);
29
30
31 // Send POST request to Flask server
32 HTTPClient http;
33 http.begin(flaskServerUrl);
34 http.addHeader("Content-Type", "application/octet-stream");
35
36 Serial.println("[INFO] Sending image to Flask server...");
37 int httpResponseCode = http.POST(fb->buf, fb->len);
38
39 if (httpResponseCode > 0) {
40 String response = http.getString();
41 Serial.printf("[SUCCESS] Flask server response: %s\n", response.c_str());
42 server.send(200, "application/json", response);
43 } else {
44 Serial.printf("[ERROR] HTTP POST failed: %s\n", http.errorToString(httpResponseCode).c_str());
45 server.send(500, "text/plain", "Error sending image to Flask");
46 }
47
48 http.end();
49 esp_camera_fb_return(fb); // Release buffer only after HTTP request completes
50}
51
52void setup() {
53 Serial.begin(115200);
54 Serial.println("\n[INFO] Starting ESP32-CAM...");
55
56 // Initialize Camera
57 camera_config_t config;
58 config.ledc_channel = LEDC_CHANNEL_0;
59 config.ledc_timer = LEDC_TIMER_0;
60 config.pin_d0 = Y2_GPIO_NUM;
61 config.pin_d1 = Y3_GPIO_NUM;
62 config.pin_d2 = Y4_GPIO_NUM;
63 config.pin_d3 = Y5_GPIO_NUM;
64 config.pin_d4 = Y6_GPIO_NUM;
65 config.pin_d5 = Y7_GPIO_NUM;
66 config.pin_d6 = Y8_GPIO_NUM;
67 config.pin_d7 = Y9_GPIO_NUM;
68 config.pin_xclk = XCLK_GPIO_NUM;
69 config.pin_pclk = PCLK_GPIO_NUM;
70 config.pin_vsync = VSYNC_GPIO_NUM;
71 config.pin_href = HREF_GPIO_NUM;
72 config.pin_sccb_sda = SIOD_GPIO_NUM;
73 config.pin_sccb_scl = SIOC_GPIO_NUM;
74 config.pin_pwdn = PWDN_GPIO_NUM;
75 config.pin_reset = RESET_GPIO_NUM;
76 config.xclk_freq_hz = 20000000;
77 config.pixel_format = PIXFORMAT_JPEG;
78
79 if (psramFound()) {
80 config.frame_size = FRAMESIZE_UXGA;
81 config.jpeg_quality = 10;
82 config.fb_count = 2;
83 } else {
84 config.frame_size = FRAMESIZE_SVGA;
85 config.jpeg_quality = 12;
86 config.fb_count = 1;
87 }
88
89 if (esp_camera_init(&config) != ESP_OK) {
90 Serial.println("[ERROR] Camera initialization failed!");
91 return;
92 }
93 Serial.println("[INFO] Camera initialized");
94
95 // Connect to WiFi
96 WiFi.begin(ssid, password);
97 Serial.print("[INFO] Connecting to WiFi...");
98 while (WiFi.status() != WL_CONNECTED) {
99 delay(500);
100 Serial.print(".");
101 }
102
103 Serial.println("\n[INFO] WiFi connected!");
104 Serial.print("[INFO] ESP32-CAM IP Address: ");
105 Serial.println(WiFi.localIP());
106
107 // Web Server
108 server.on("/capture", HTTP_GET, handleCapture);
109 server.begin();
110 Serial.println("[INFO] Web server started!");
111}
112
113
114void loop() {
115 server.handleClient();
116}
117192.168.254.108