#include "esp_camera.h" #include #include #include // Traffic Light Pins #define GREEN_LED 12 #define YELLOW_LED 13 #define RED_LED 14 // Servo Motor Pin #define SERVO_PIN 15 // Wi-Fi Credentials const char* ssid = "YOUR_WIFI_SSID"; const char* password = "YOUR_WIFI_PASSWORD"; // Speed Limit (in km/h) #define SPEED_LIMIT 50 // Global Variables int carCount = 0; Servo barrier; WiFiClient client; HttpClient http(client); // Camera Configuration void setupCamera() { camera_config_t config; config.ledc_channel = LEDC_CHANNEL_0; config.ledc_timer = LEDC_TIMER_0; config.pin_d0 = 5; config.pin_d1 = 18; config.pin_d2 = 19; config.pin_d3 = 21; config.pin_d4 = 36; config.pin_d5 = 39; config.pin_d6 = 34; config.pin_d7 = 35; config.pin_xclk = 0; config.pin_pclk = 22; config.pin_vsync = 25; config.pin_href = 23; config.pin_sscb_sda = 26; config.pin_sscb_scl = 27; config.pin_pwdn = 32; config.pin_reset = -1; config.pin_xclk = 0; config.xclk_freq_hz = 20000000; config.pixel_format = PIXFORMAT_JPEG; if (psramFound()) { config.frame_size = FRAMESIZE_UXGA; config.jpeg_quality = 10; config.fb_count = 2; } else { config.frame_size = FRAMESIZE_SVGA; config.jpeg_quality = 12; config.fb_count = 1; } // Initialize the camera esp_err_t err = esp_camera_init(&config); if (err != ESP_OK) { Serial.printf("Camera init failed with error 0x%x", err); return; } } // Setup Function void setup() { Serial.begin(115200); // Initialize Traffic Light Pins pinMode(GREEN_LED, OUTPUT); pinMode(YELLOW_LED, OUTPUT); pinMode(RED_LED, OUTPUT); // Initialize Servo Motor barrier.attach(SERVO_PIN); // Connect to Wi-Fi WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("\nWi-Fi Connected"); // Initialize Camera setupCamera(); } // Traffic Light Control void controlTrafficLight() { // Green Light digitalWrite(GREEN_LED, HIGH); digitalWrite(YELLOW_LED, LOW); digitalWrite(RED_LED, LOW); delay(5000); // 5 seconds // Yellow Light digitalWrite(GREEN_LED, LOW); digitalWrite(YELLOW_LED, HIGH); digitalWrite(RED_LED, LOW); delay(2000); // 2 seconds // Red Light digitalWrite(GREEN_LED, LOW); digitalWrite(YELLOW_LED, LOW); digitalWrite(RED_LED, HIGH); delay(5000); // 5 seconds } // Car Count Using Motion Detection void countCars() { camera_fb_t *fb = esp_camera_fb_get(); if (!fb) return; // Simple Motion Detection (Compare Frames) // Replace this with actual motion detection logic bool motionDetected = true; // Placeholder for motion detection if (motionDetected) { carCount++; Serial.printf("Car Count: %d\n", carCount); } // Return the frame buffer esp_camera_fb_return(fb); } // Speed Limit Detection void detectSpeed() { float speed = calculateSpeed(); // Implement this function if (speed > SPEED_LIMIT) { sendNotification(speed); } } // Calculate Speed (Placeholder Function) float calculateSpeed() { // Implement speed calculation logic // Measure time between two points in the camera frame return 60; // Example speed } // Send Notification to Traffic Police void sendNotification(float speed) { String url = "http://traffic-police-api.com/notify?speed=" + String(speed); http.get(url); Serial.println("Notification sent!"); } // Servo Motor Control void controlBarrier() { barrier.write(90); // Open barrier delay(5000); barrier.write(0); // Close barrier delay(5000); } // Main Loop void loop() { // Control Traffic Light controlTrafficLight(); // Count Cars countCars(); // Detect Speed detectSpeed(); // Control Barrier (Optional) controlBarrier(); }