Security camera project with esp32cam module

I bought few esp32cam modules with miscro SD card slot and I am trying to turn them into security cameras. The problem is that code that can be found on the Interent does not provide all features I need to have on each of them.
I am fairly new into this and don't have any knowledge of C++ language so it will be quite challenging to achive what I want in this project.

My expectations of features are and description whether I know how to do it:

  • Stream over WIFI - easy doable
  • Baterry back up - easy doable
  • Motion detection - somehow doable, but more code will be needed or I will use 3rd party PIR sensor.
  • Saving pictures on SD card in the case of power outage - should be easy, but I don't have all the necessary code for that
  • Upload pictures on FTP/SMB after power restore - I have no idea how to do that, but found some code.
  • Delete oldest pictures on SD card in order to fit the new ones in the case of full storage. - No idea.

I am planning to use some 3rd party tools like Home Assistant or Zone Minder to control the camera features, but I need to put together some code that will provide those basic features I listed above.

I plan to run cameras in 2 modes:

  1. Normal mode - WIFI network and power is on, only motion detection will trigger pictures saved on FTP. This can be triggered by the server.
  2. Alarm mode - WIFI network and power is off (in the case intruder turned off power) - start saving pictures on SD card until power is restored, then upload to FTP server.

Question 1. Are all these features possible to be combined into the esp32cam module? I am not sure if I am not hitting some memory limitations.

Q2: What is the best source of learning the code related to all features listed above? So far I found everything on esp32cam | Random Nerd Tutorials, but these guides are pretty much copy and paste without going deeper into what each code does, while I want to learn bit more to be able to tweak the code to my needs.

Furthermore I have some inital code that I put together from what I found, it would be great if someone helps me to make it working corectly:

#include <WiFi.h>
#include <SD.h>
#include <ESP32_FTPClient.h>

// WiFi credentials
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";

// FTP credentials
const char* ftp_server = "YOUR_FTP_SERVER";
const char* ftp_user = "YOUR_FTP_USERNAME";
const char* ftp_pass = "YOUR_FTP_PASSWORD";

FTPClient ftp(ftp_server, ftp_user, ftp_pass);

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

  // Initialize WiFi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("WiFi connected");

  // Initialize SD card
  if (!SD.begin()) {
    Serial.println("Card Mount Failed");
    return;
  }

  uint8_t cardType = SD.cardType();
  if (cardType == CARD_NONE) {
    Serial.println("No SD card attached");
    return;
  }

  Serial.println("SD card initialized");
}

void loop() {
  // Upload files to FTP server
  uploadFileToFTP("/test.txt");
  
  // Add your other code here

  delay(60000); // Wait a minute before next upload
}

void uploadFileToFTP(const char* filename) {
  File file = SD.open(filename);
  if (!file) {
    Serial.println("Failed to open file for reading");
    return;
  }

  if (WiFi.status() == WL_CONNECTED) {
    ftp.OpenConnection();
    ftp.InitFile("Type I");
    ftp.ChangeWorkDir("/"); // Set your target directory on the FTP server
    ftp.NewFile(filename);

    byte clientBuf[512];
    int clientCount = 0;

    while (file.available()) {
      clientBuf[clientCount] = file.read();
      clientCount++;
      if (clientCount > 511) {
        ftp.WriteData(clientBuf, 512);
        clientCount = 0;
      }
    }

    if (clientCount > 0) {
      ftp.WriteData(clientBuf, clientCount);
    }

    ftp.CloseFile();
    ftp.CloseConnection();
    file.close();

    Serial.println("File uploaded successfully");
  } else {
    Serial.println("WiFi not connected");
  }
}

Hi @viktorp75 ,

Welcome to the forum..

I working on something..
Esp32Cam Server
Still a work in progress..
Just some open source fun..

I don't see any show stoppers in your questions..
Monitoring the power, just reading a pin attached to a wall wart..
haven't messed with ftp..
SD card should work, a bit power hungry and the power is off, so you need a UPS to keep cam going..

the best source?? idk, matter of opinion..
This forum is a wealth of knowledge..
Search on GitHub, there's quite a few project that might be what you're looking for or at least gives some pointers..
And there's the Esp32 Forum..

you get stuck, ask here..

good luck.. ~q

1 Like

I moved your topic to a more appropriate forum category @viktorp75.

The Nano ESP32 category you chose is only used for discussions directly related to the Arduino Nano ESP32 board.

In the future, please take the time to pick the forum category that best suits the subject of your question. There is an "About the _____ category" topic at the top of each category that explains its purpose.

Thanks in advance for your cooperation.

hi @qubits-us

Looks interesting, definitelly going to try to play with it when I find some time for this project.

Nice to hear and I can now also see that its realistic, even tought I thought there is not many options out there to work with.
I have just found many projects on Github dealing with this and I can see the problem is actually that there are too many options, lot of code to go through.

I am plannig to have a small power bank connected to each module and power bank will be plugged into a charger. If power disconnects then module should still work and I don't think it will be required to monitor the power outage, storing to SD card will be trigered when connection to WIFI is lost.

There will be many way how to go on about it and I can see already that it will take me a long time before I get it to the stage when I am happy with it.
I have like 5 of these camera modules so I don't think I will loose motivation to work on this as I don't want them to end up in my drawer and also I have quite good motivation right now to place few of these cameras around my garden to monitor our ducks and find out where they hide the eggs as its almost impossible to discover their hiding places :slight_smile:

1 Like

something to consider, the above will happen..
your loop should be able to reconnect wifi..

they are fun.. :slight_smile:

good luck.. ~q

1 Like

I have a sketch which I think does a lot of what you require which may be of interest: HERE

If you search eBay for "18650 shield", I have used these on a couple of my projects, using them as a UPS.

I have found your project yesterday when browsing github. Looks like a lot from it is what I want and I have saved it. I have also seen the YT video about changing lenses. Good job!

As for the shield module, I have a couple of tiny 2200mA/h power banks that supply 1A. I am planning to try those, I think min current requirement for esp32cam is 500mA. I will power supply it by a long couple of wires from untwisted UTP cable. Something like a fake PoE. I expect that it should work fine. If not then I will consider the 18650 shield module.

Thank you.

so I got bit of time trying to test your code and hitting some rookie problems in both Plaformio and Arduino IDE:

Compiling .pio/build/esp32cam/src/main.cpp.o
In file included from /home/vito/Documents/PlatformIO/Projects/camerawifimotion/src/CameraWifiMotion.ino:183:
src/wifi.h:86:10: fatal error: WiFiManager.h: No such file or directory

*********************************************************************
* Looking for WiFiManager.h dependency? Check our library registry!
*
* CLI  > platformio lib search "header:WiFiManager.h"
* Web  > https://registry.platformio.org/search?q=header:WiFiManager.h
*
*********************************************************************

 #include <WiFiManager.h>                  // https://github.com/tzapu/WiFiManager
          ^~~~~~~~~~~~~~~
compilation terminated.
In file included from src/main.cpp:181:
src/wifi.h:86:10: fatal error: WiFiManager.h: No such file or directory

*********************************************************************
* Looking for WiFiManager.h dependency? Check our library registry!
*
* CLI  > platformio lib search "header:WiFiManager.h"
* Web  > https://registry.platformio.org/search?q=header:WiFiManager.h
*
*********************************************************************

 #include <WiFiManager.h>                  // https://github.com/tzapu/WiFiManager
          ^~~~~~~~~~~~~~~
compilation terminated.
Compiling .pio/build/esp32cam/lib276/Update/Updater.cpp.o
*** [.pio/build/esp32cam/src/CameraWifiMotion.ino.cpp.o] Error 1
*** [.pio/build/esp32cam/src/main.cpp.o] Error 1
============================================================================= [FAILED] Took 4.58 seconds =============================================================================

 *  The terminal process "platformio 'run'" terminated with exit code: 1. 
 *  Terminal will be reused by tasks, press any key to close it. 

Looks like I am missing some libraries, however shouldn't they be installed automatically?
Tried to follow instructions on PlatformIO Registry but it does not seem to have helped.
Any advice on what I should do?

How do you use that as UPS, I checked the specs of the shield module and charging current is only 0.5 A max while output is 3A so if I use it to power up the esp32 then it will slowly deplete the battery. ESP32CAM actually needs 5V&2A minimum so my power bank idea won't work either.
On the top of it apparently if power input goes off then the whole unit turns off and you need to use the button to put it back on.
So the shield does not sound like a good UPS solution for ESP32cam module. Unless you're talking about different shield module. But I have not seen any other yet that would be different from what I said above.

They keep up with my esp32 project ok as it only draws a low current on average.
You can adjust the charging current by replacing a resistor (change R2 from 2k to 1.2k to give 1amp charge and 100ma low charge)

See: https://www.youtube.com/watch?v=joAkJ9QA2bw

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.