· 4 months ago · May 14, 2025, 10:15 AM
1#include "esp_camera.h"
2#include <WiFi.h>
3
4// Pilih model kamera yang sesuai
5#define CAMERA_MODEL_AI_THINKER // ESP32-CAM AI-THINKER dengan OV2640
6#include "camera_pins.h"
7
8// WiFi credentials
9const char* ssid = "Megabot";
10const char* password = "1234567890";
11
12void startCameraServer();
13
14void setup() {
15 Serial.begin(115200);
16 Serial.setDebugOutput(true);
17 Serial.println();
18
19 // Konfigurasi kamera
20 camera_config_t config;
21 config.ledc_channel = LEDC_CHANNEL_0;
22 config.ledc_timer = LEDC_TIMER_0;
23 config.pin_d0 = Y2_GPIO_NUM;
24 config.pin_d1 = Y3_GPIO_NUM;
25 config.pin_d2 = Y4_GPIO_NUM;
26 config.pin_d3 = Y5_GPIO_NUM;
27 config.pin_d4 = Y6_GPIO_NUM;
28 config.pin_d5 = Y7_GPIO_NUM;
29 config.pin_d6 = Y8_GPIO_NUM;
30 config.pin_d7 = Y9_GPIO_NUM;
31 config.pin_xclk = XCLK_GPIO_NUM;
32 config.pin_pclk = PCLK_GPIO_NUM;
33 config.pin_vsync = VSYNC_GPIO_NUM;
34 config.pin_href = HREF_GPIO_NUM;
35 config.pin_sccb_sda = SIOD_GPIO_NUM;
36 config.pin_sccb_scl = SIOC_GPIO_NUM;
37 config.pin_pwdn = PWDN_GPIO_NUM;
38 config.pin_reset = RESET_GPIO_NUM;
39 config.xclk_freq_hz = 20000000;
40 // config.pixel_format = PIXFORMAT_JPEG;
41 config.pixel_format = PIXFORMAT_RGB565;
42 config.frame_size = FRAMESIZE_QVGA; // Resolusi lebih rendah untuk stabilitas awal
43 config.jpeg_quality = 10; // 10 = kualitas medium, 12 = lebih tinggi
44 config.fb_count = 1;
45
46 // Inisialisasi kamera
47 esp_err_t err = esp_camera_init(&config);
48 if (err != ESP_OK) {
49 Serial.printf("❌ Kamera gagal diinisialisasi, error 0x%x\n", err);
50 return;
51 }
52
53 // Konfigurasi sensor kamera
54 sensor_t *s = esp_camera_sensor_get();
55 if (s->id.PID == OV3660_PID) {
56 s->set_vflip(s, 1); // Flip vertikal
57 s->set_brightness(s, 1); // Tambah kecerahan
58 s->set_saturation(s, -2); // Kurangi saturasi warna
59 }
60
61 // Setup WiFi
62 WiFi.begin(ssid, password);
63 WiFi.setSleep(false);
64 Serial.print("Menghubungkan ke WiFi");
65
66 while (WiFi.status() != WL_CONNECTED) {
67 delay(500);
68 Serial.print(".");
69 }
70 Serial.println("\n✅ WiFi Terhubung!");
71
72 // Jalankan server kamera
73 startCameraServer();
74
75 Serial.print("📷 Kamera Siap! Buka: http://");
76 Serial.print(WiFi.localIP());
77 Serial.println("/");
78}
79
80void loop() {
81 delay(10000); // Loop tidak perlu melakukan apa-apa, server berjalan sendiri
82}
83