· 8 months ago · Mar 11, 2025, 03:25 PM
1#include "esp_camera.h"
2#include <WiFi.h>
3#include <HTTPClient.h>
4#include <ArduinoJson.h>
5//
6// WARNING!!! PSRAM IC required for UXGA resolution and high JPEG quality
7// Ensure ESP32 Wrover Module or other board with PSRAM is selected
8// Partial images will be transmitted if image exceeds buffer size
9//
10// You must select partition scheme from the board menu that has at least 3MB APP space.
11// Face Recognition is DISABLED for ESP32 and ESP32-S2, because it takes up from 15
12// seconds to process single frame. Face Detection is ENABLED if PSRAM is enabled as well
13
14// ===================
15// Select camera model
16// ===================
17//#define CAMERA_MODEL_WROVER_KIT // Has PSRAM
18//#define CAMERA_MODEL_ESP_EYE // Has PSRAM
19//#define CAMERA_MODEL_ESP32S3_EYE // Has PSRAM
20//#define CAMERA_MODEL_M5STACK_PSRAM // Has PSRAM
21//#define CAMERA_MODEL_M5STACK_V2_PSRAM // M5Camera version B Has PSRAM
22//#define CAMERA_MODEL_M5STACK_WIDE // Has PSRAM
23//#define CAMERA_MODEL_M5STACK_ESP32CAM // No PSRAM
24//#define CAMERA_MODEL_M5STACK_UNITCAM // No PSRAM
25//#define CAMERA_MODEL_M5STACK_CAMS3_UNIT // Has PSRAM
26#define CAMERA_MODEL_AI_THINKER // Has PSRAM
27//#define CAMERA_MODEL_TTGO_T_JOURNAL // No PSRAM
28//#define CAMERA_MODEL_XIAO_ESP32S3 // Has PSRAM
29// ** Espressif Internal Boards **
30//#define CAMERA_MODEL_ESP32_CAM_BOARD
31//#define CAMERA_MODEL_ESP32S2_CAM_BOARD
32//#define CAMERA_MODEL_ESP32S3_CAM_LCD
33//#define CAMERA_MODEL_DFRobot_FireBeetle2_ESP32S3 // Has PSRAM
34//#define CAMERA_MODEL_DFRobot_Romeo_ESP32S3 // Has PSRAM
35#include "camera_pins.h"
36
37// ===========================
38// Enter your WiFi credentials
39// ===========================
40const char *ssid = "Redmi 8A";
41const char *password = "sahar3010";
42
43void startCameraServer();
44void setupLedFlash(int pin);
45
46void setup() {
47 Serial.begin(115200);
48 Serial.setDebugOutput(true);
49 Serial.println();
50
51 camera_config_t config;
52 config.ledc_channel = LEDC_CHANNEL_0;
53 config.ledc_timer = LEDC_TIMER_0;
54 config.pin_d0 = Y2_GPIO_NUM;
55 config.pin_d1 = Y3_GPIO_NUM;
56 config.pin_d2 = Y4_GPIO_NUM;
57 config.pin_d3 = Y5_GPIO_NUM;
58 config.pin_d4 = Y6_GPIO_NUM;
59 config.pin_d5 = Y7_GPIO_NUM;
60 config.pin_d6 = Y8_GPIO_NUM;
61 config.pin_d7 = Y9_GPIO_NUM;
62 config.pin_xclk = XCLK_GPIO_NUM;
63 config.pin_pclk = PCLK_GPIO_NUM;
64 config.pin_vsync = VSYNC_GPIO_NUM;
65 config.pin_href = HREF_GPIO_NUM;
66 config.pin_sccb_sda = SIOD_GPIO_NUM;
67 config.pin_sccb_scl = SIOC_GPIO_NUM;
68 config.pin_pwdn = PWDN_GPIO_NUM;
69 config.pin_reset = RESET_GPIO_NUM;
70 config.xclk_freq_hz = 20000000;
71 config.frame_size = FRAMESIZE_UXGA;
72 config.pixel_format = PIXFORMAT_JPEG; // for streaming
73 //config.pixel_format = PIXFORMAT_RGB565; // for face detection/recognition
74 config.grab_mode = CAMERA_GRAB_WHEN_EMPTY;
75 config.fb_location = CAMERA_FB_IN_PSRAM;
76 config.jpeg_quality = 12;
77 config.fb_count = 1;
78
79 // if PSRAM IC present, init with UXGA resolution and higher JPEG quality
80 // for larger pre-allocated frame buffer.
81 if (config.pixel_format == PIXFORMAT_JPEG) {
82 if (psramFound()) {
83 config.jpeg_quality = 10;
84 config.fb_count = 2;
85 config.grab_mode = CAMERA_GRAB_LATEST;
86 } else {
87 // Limit the frame size when PSRAM is not available
88 config.frame_size = FRAMESIZE_SVGA;
89 config.fb_location = CAMERA_FB_IN_DRAM;
90 }
91 } else {
92 // Best option for face detection/recognition
93 config.frame_size = FRAMESIZE_240X240;
94#if CONFIG_IDF_TARGET_ESP32S3
95 config.fb_count = 2;
96#endif
97 }
98
99#if defined(CAMERA_MODEL_ESP_EYE)
100 pinMode(13, INPUT_PULLUP);
101 pinMode(14, INPUT_PULLUP);
102#endif
103
104 // camera init
105 esp_err_t err = esp_camera_init(&config);
106 if (err != ESP_OK) {
107 Serial.printf("Camera init failed with error 0x%x", err);
108 return;
109 }
110
111 sensor_t *s = esp_camera_sensor_get();
112 // initial sensors are flipped vertically and colors are a bit saturated
113 if (s->id.PID == OV3660_PID) {
114 s->set_vflip(s, 1); // flip it back
115 s->set_brightness(s, 1); // up the brightness just a bit
116 s->set_saturation(s, -2); // lower the saturation
117 }
118 // drop down frame size for higher initial frame rate
119 if (config.pixel_format == PIXFORMAT_JPEG) {
120 s->set_framesize(s, FRAMESIZE_QVGA);
121 }
122
123#if defined(CAMERA_MODEL_M5STACK_WIDE) || defined(CAMERA_MODEL_M5STACK_ESP32CAM)
124 s->set_vflip(s, 1);
125 s->set_hmirror(s, 1);
126#endif
127
128#if defined(CAMERA_MODEL_ESP32S3_EYE)
129 s->set_vflip(s, 1);
130#endif
131
132// Setup LED FLash if LED pin is defined in camera_pins.h
133#if defined(LED_GPIO_NUM)
134 setupLedFlash(LED_GPIO_NUM);
135#endif
136
137 WiFi.begin(ssid, password);
138 WiFi.setSleep(false);
139
140 Serial.print("WiFi connecting");
141 while (WiFi.status() != WL_CONNECTED) {
142 delay(500);
143 Serial.print(".");
144 }
145 Serial.println("");
146 Serial.println("WiFi connected");
147
148 startCameraServer();
149
150 Serial.print("Camera Ready! Use 'http://");
151 Serial.print(WiFi.localIP());
152 Serial.println("' to connect");
153
154
155const String firebaseURL = "https://facerecognition-tank-default-rtdb.firebaseio.com";
156const String firebaseNode = "espcam";
157const String firebaseAPIKey = "y74ChPC3AwpFmJm87loprONAV3IvBEzAiNxzCH3y";
158
159StaticJsonDocument<200> jsonDoc;//זה הגודל של הג'יסון
160jsonDoc["IP"] = WiFi.localIP().toString();
161HTTPClient http;
162String url = firebaseURL + "/" + firebaseNode+"/espServerIP" + ".json?auth=" + firebaseAPIKey;
163String jsonData;
164serializeJson(jsonDoc, jsonData);
165Serial.println("firebaseNode: " + url);
166
167http.begin(url);
168http.addHeader("Content-Type", "application/json");
169int httpResponseCode = http.PUT(jsonData);
170if (httpResponseCode > 0) {
171 String response = http.getString();
172 Serial.println("HTTP PUT Response code: " + String(httpResponseCode));
173 Serial.println("Response: " + response);
174} else {
175 Serial.println("Error in sending POST: " + String(httpResponseCode));
176}
177
178}
179
180void loop() {
181 // Do nothing. Everything is done in another task by the web server
182 delay(10000);
183}