CanSat-India-Updated / ESPVIDEO_FINAL.ino
ESPVIDEO_FINAL.ino
Raw
#include "esp_camera.h"
#include "FS.h"
#include <WiFi.h>
#include <HTTPClient.h>
#include "SD_MMC.h"

// Pin definition
#define 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

#define sgn 12

// Camera config
const 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_QVGA,
    .jpeg_quality   = 12,
    .fb_count       = 2
};

const unsigned long RECORD_DURATION_MS = 20000; // Recording duration in milliseconds (e.g., 10 seconds)
unsigned long recordStartTime = 0; // Variable to store the time when recording started
bool isRecording = false;

// Initialize camera
void init_camera() {
    esp_err_t err = esp_camera_init(&camera_config);
    if (err != ESP_OK) {
        Serial.printf("Camera init failed with error 0x%x\n", err);
        ESP.restart();
    }
}

// Initialize SD card
bool init_sd_card() {
    if (!SD_MMC.begin()) {
        Serial.println("Card Mount Failed");
        return false;
    }
    uint8_t cardType = SD_MMC.cardType();
    if (cardType == CARD_NONE) {
        Serial.println("No SD card attached");
        return false;
    }
    Serial.println("SD card initialized");
    return true;
}

// Start video recording
void start_recording() {
    isRecording = true;
    File videoFile = SD_MMC.open("/video.avi", FILE_APPEND);

    if (!videoFile) {
        Serial.println("Failed to open video file");
        return;
    }
    Serial.println("Recording video...");
    int x=0;
  
    
    while (isRecording) {
      File videoFile = SD_MMC.open("/video.avi", FILE_APPEND);
      if (!videoFile) {
          Serial.println("Failed to open video file");
          return;
      }


        camera_fb_t *fb = esp_camera_fb_get();
        if (!fb) {
            Serial.println("Camera capture failed");
            continue;
        }

        
        videoFile.write(fb->buf, fb->len);
        esp_camera_fb_return(fb);
        x++;
        videoFile.close();
        if(x==46000){
          stop_recording();
          break;
        }
    }

    
}

// Stop video recording
void stop_recording() {
    isRecording = false;
}

void setup() {
    Serial.begin(115200);

    pinMode(sgn, INPUT);

    // Initialize SD card
    if (!init_sd_card()) {
        Serial.println("Failed to initialize SD card");
        return;
    }

    // Initialize camera
    init_camera();
    start_recording();
}

void loop() {
}