Hello , Im new to Arduino and i was wondering if someone can help , i can trying to makes a trail cam that gets triggered by a PIR and if another ESP 32 sends the Take Picture command.
Im having a issue where i cant even get the camera to take a picture and save it to the SD card.
Any help would be appreciated
// =====================================================================================
// Options
// =====================================================================================
#define Delay_For_Next_Snap 10000
#define Debounce_Delay 100
#define EEPROM_SIZE 512 // Define EEPROM size
// =====================================================================================
// End of options
// =====================================================================================
#include <WiFi.h>
#include <esp_now.h>
#include <SD_MMC.h>
#include <TFT_eSPI.h>
#include "esp_camera.h"
#include <EEPROM.h> // Read and write from flash memory
// Camera settings for ESP32-CAM
#define CAMERA_MODEL_AI_THINKER
// Manual pin definitions for ESP32-CAM 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
int pictureNumber = 0; // Initialize picture number
// TFT settings
#define TFT_CS 5
#define TFT_RST 16
#define TFT_DC 17
TFT_eSPI tft = TFT_eSPI(); // Instantiate TFT_eSPI object
// Pin definitions
#define PIR_PIN 13
#define PAIR_BUTTON_PIN 12
#define RST_BUTTON_PIN 0 // Define the RST button pin (typically GPIO 0)
#define MAX_DEVICES 20
// Structure to store information about paired devices
typedef struct {
uint8_t mac[6];
} paired_device;
paired_device pairedDevices[MAX_DEVICES];
int pairedDeviceCount = 0;
// Encryption key
uint8_t encryptionKey[16] = {0x1a, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10};
// ESP-NOW callback
void OnDataRecv(const esp_now_recv_info *recv_info, const uint8_t *data, int len)
{
Serial.println("Data received");
// Trigger picture taking function
takePicture();
}
// Function to initialize the camera
void initCamera()
{
camera_config_t config;
config.ledc_channel = LEDC_CHANNEL_0;
config.ledc_timer = LEDC_TIMER_0;
config.pin_d0 = Y2_GPIO_NUM;
config.pin_d1 = Y3_GPIO_NUM;
config.pin_d2 = Y4_GPIO_NUM;
config.pin_d3 = Y5_GPIO_NUM;
config.pin_d4 = Y6_GPIO_NUM;
config.pin_d5 = Y7_GPIO_NUM;
config.pin_d6 = Y8_GPIO_NUM;
config.pin_d7 = Y9_GPIO_NUM;
config.pin_xclk = XCLK_GPIO_NUM;
config.pin_pclk = PCLK_GPIO_NUM;
config.pin_vsync = VSYNC_GPIO_NUM;
config.pin_href = HREF_GPIO_NUM;
config.pin_sccb_sda = SIOD_GPIO_NUM;
config.pin_sccb_scl = SIOC_GPIO_NUM;
config.pin_pwdn = PWDN_GPIO_NUM;
config.pin_reset = RESET_GPIO_NUM;
config.xclk_freq_hz = 20000000;
config.pixel_format = PIXFORMAT_JPEG;
// Init with high specs to pre-allocate larger buffers
if (psramFound()) {
config.frame_size = FRAMESIZE_UXGA; // FRAMESIZE_ + QVGA|CIF|VGA|SVGA|XGA|SXGA|UXGA
config.jpeg_quality = 10;
config.fb_count = 2;
} else {
config.frame_size = FRAMESIZE_SVGA;
config.jpeg_quality = 12;
config.fb_count = 1;
}
// Camera init
esp_err_t err = esp_camera_init(&config);
if (err != ESP_OK) {
Serial.printf("Camera init failed with error 0x%x\n", err);
Serial.println("Check your camera connections and settings.");
}
}
// Function to take picture and save to SD card
void takePicture()
{
camera_fb_t *fb = esp_camera_fb_get();
if (!fb) {
Serial.println("Camera capture failed");
return;
}
// Ensure SD card is mounted before proceeding
if (!SD_MMC.begin()) { // Initialize SD card with default pins
Serial.println("SD Card Mount Failed");
return;
}
// Initialize EEPROM with predefined size
EEPROM.begin(EEPROM_SIZE);
pictureNumber = EEPROM.read(0) + 1;
// Path where new picture will be saved in SD Card
String path = "/picture" + String(pictureNumber) + ".jpg";
File file = SD_MMC.open(path.c_str(), FILE_WRITE);
if (!file) {
Serial.println("Failed to open file in writing mode");
} else {
file.write(fb->buf, fb->len); // payload (image), payload length
Serial.printf("Saved file to path: %s\n", path.c_str());
EEPROM.write(0, pictureNumber);
EEPROM.commit();
}
file.close();
esp_camera_fb_return(fb);
// Turns off the ESP32-CAM white on-board LED (flash) connected to GPIO 4
pinMode(4, OUTPUT);
digitalWrite(4, LOW);
gpio_hold_en(GPIO_NUM_4); // Use gpio_hold_en instead of rtc_gpio_hold_en
delay(2000);
Serial.println("Going to sleep now");
delay(2000);
esp_deep_sleep_start();
Serial.println("This will never be printed");
}
// Interrupt service routine for RST button press
void IRAM_ATTR handleRSTButtonPress()
{
takePicture();
}
// Function to pair new device
void pairNewDevice()
{
Serial.println("Pairing new device...");
// Enable promiscuous mode to listen for pairing requests
WiFi.scanNetworks(true, true);
delay(2000); // Wait for scan to complete
int numNetworks = WiFi.scanComplete();
for (int i = 0; i < numNetworks; i++) {
String ssid = WiFi.SSID(i);
if (ssid.startsWith("ESP32_SENSOR")) {
uint8_t mac[6];
WiFi.BSSID(i, mac);
if (pairedDeviceCount < MAX_DEVICES) {
memcpy(pairedDevices[pairedDeviceCount].mac, mac, 6);
esp_now_peer_info_t peerInfo = {};
memcpy(peerInfo.peer_addr, mac, 6);
peerInfo.channel = 0;
peerInfo.encrypt = true;
memcpy(peerInfo.lmk, encryptionKey, 16);
if (esp_now_add_peer(&peerInfo) == ESP_OK) {
Serial.print("Paired with: ");
for (int j = 0; j < 6; j++) {
Serial.printf("%02X", mac[j]);
if (j < 5) Serial.print(":");
}
Serial.println();
pairedDeviceCount++;
} else {
Serial.println("Failed to add peer");
}
} else {
Serial.println("Max device limit reached");
}
}
}
WiFi.scanDelete();
}
void setup()
{
Serial.begin(115200);
// Camera initialization
initCamera();
// TFT initialization
tft.init();
tft.setRotation(1);
// SD card initialization
if (!SD_MMC.begin()) { // Initialize SD card with default pins
Serial.println("SD Card Mount Failed");
tft.fillScreen(TFT_RED); // Indicate failure on TFT
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.drawString("SD Card Mount Failed", 10, 10);
return;
}
uint8_t cardType = SD_MMC.cardType();
if (cardType == CARD_NONE) {
Serial.println("No SD Card attached");
tft.fillScreen(TFT_RED); // Indicate failure on TFT
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.drawString("No SD Card", 10, 10);
return;
}
tft.fillScreen(TFT_BLACK);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
// PIR sensor setup
pinMode(PIR_PIN, INPUT);
// Button setup
pinMode(PAIR_BUTTON_PIN, INPUT_PULLUP);
// RST button setup
pinMode(RST_BUTTON_PIN, INPUT_PULLUP);
// Initialize GPIO ISR service
static bool isISRServiceInstalled = false;
if (!isISRServiceInstalled) {
gpio_install_isr_service(ESP_INTR_FLAG_LEVEL1);
isISRServiceInstalled = true;
}
attachInterrupt(digitalPinToInterrupt(RST_BUTTON_PIN), handleRSTButtonPress, FALLING);
// ESP-NOW initialization
WiFi.mode(WIFI_STA);
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
esp_now_register_recv_cb(OnDataRecv); // Register the receive callback
// Set the encryption key
if (esp_now_set_pmk(encryptionKey) != ESP_OK) {
Serial.println("Failed to set encryption key");
return;
}
// Pairing setup
attachInterrupt(digitalPinToInterrupt(PAIR_BUTTON_PIN), pairNewDevice, FALLING);
// Initialize EEPROM for picture number storage
EEPROM.begin(EEPROM_SIZE);
pictureNumber = EEPROM.read(0);
}
void loop()
{
if (digitalRead(PIR_PIN) == HIGH) {
delay(Delay_For_Next_Snap);
takePicture();
}
delay(100);
}