Motion Triggered Image Capture and Email

11,280

14

19

Introduction: Motion Triggered Image Capture and Email

About: #BnBe is a platform to help teach electronics no matter what the age or skill level. We’re currently designing a wide range of products from beginner level kits to industry-standard microcontroller platforms.

We build upon the previous ESP32-CAM projects and build a motion-triggered image capturing system that also sends an email with the image as an attachment. This build uses the ESP32-CAM board along with a PIR sensor module that is based on the AM312 sensor. The board spends most of the time in sleep mode and wakes up to take an image once motion is detected. In part 1, we modify the previous time-lapse sketch to add the motion detection feature. We then update the sketch and add the email feature in part 2

The video above covers everything you need to know and also explains how the sketch is put together.

Step 1: Gather the Electronics

The ESP32-CAM board already contains the camera module, and microSD card slot that we need for this sketch. In addition to this, you will need a microSD card, a PIR sensor module (based on the AM312 sensor), a general-purpose NPN transistor (BC547, BC548, BC549 or 2N3904), a microUSB breakout board, a 10K Ohm and 1K ohm resistor and also a USB to serial converter to upload the sketch.

Step 2: Upload the Sketch for Part 1

The ESP32-CAM board does not have an onboard USB connector so you need to use an external USB to serial converter to upload the sketch. You can use the wiring connections shown above but make sure that the USB to serial converter is connected in the 3.3V mode.

It is recommended to use an external 5V supply to power the board, particularly if you are using an FTDI breakout board. For the external 5V supply, a simple USB breakout board will do just fine. There has been some success in powering the board directly from the CP2102 breakout board so you can try that first. The board also has a 3.3V power pin if needed.

The jumper is needed to put the board in the download mode. Once you have everything connected, power up the board, open a serial terminal (Tools->Serial Monitor) with a baud rate of 115,200 and press the reset button. You should obtain an output as shown in the image and this will indicate that everything is working as expected.

You can download the sketch using the following link: https://github.com/bnbe-club/motion-triggered-image-camera-diy-10

Step 3: Connect the Circuit and Test

Build the circuit using a breadboard and test to make sure everything works as expected. I added a multimeter to the final sensor output to help determine it's state. Once you're happy with the way everything works, move on to part 2.

Step 4: Install the Mail Client Library

Open up the library manager and type in "ESP32 Mail Client". Install the library that shows up as we need this for the sketch.

Step 5: Upload the Sketch for Part 2

Download the sketch from the following link: https://github.com/bnbe-club/motion-triggered-image-camera-diy-10

Open it up using the Arduino IDE and then update it with your details. You will need to add the network name and password as the board needs to connect to the WiFI network. You will also need to provide an email address along with the password for the board to send the email. I'd recommend creating a new GMAIL account. Once the account is created, you need to enable less secure apps by visiting the following link:

https://myaccount.google.com/lesssecureapps?pli=1

You also need to specify the recipient and you can have multiple if required. Watch the video to learn more. Once all of this is done, upload the sketch to the board and power it ON. I'd recommend connecting the serial terminal and watching the output as this will notify you if there are any errors.

If everything works as expected, then the board should capture, save an image and also send it across as an email.

Step 6: Add the Electronics to an Enclosure

I used the enclosure from the following link: https://www.thingiverse.com/thing:3667886

I added supports and printed it face down, which didn't give a very good finish but this should work for now. Since the PIR sensor was a little too big for the enclosure, I desoldered the sensor and connected it to the PCB using wires. I then created the interfacing circuit using a protoboard and wired it in place. Use the connection diagram shown earlier to connect it all together and you can add some Kapton tape for insulation.

I started by glueing in the PIR sensor, followed by the PIR PCB. I then placed the ESP32 board inside and realised that the case doesn't allow you to access the microSD card but this didn't matter to me as the images would be emailed. I then added the interface board and glued in the microUSB breakout board. Finally, I bent the transistor to allow the cover to close in place. Power on the board and it should take an image when motion is detected.

If you've liked this post, then don't forget to follow us using the links below as we will be building many more projects like this one:

Be the First to Share

    Recommendations

    • Make it Real Student Design Challenge #3

      Make it Real Student Design Challenge #3
    • Box Challenge

      Box Challenge
    • Arduino Contest

      Arduino Contest

    19 Comments

    0
    1305ukgod
    1305ukgod

    Question 1 year ago on Step 6

    Nice project.
    I would like to know the general time it takes between sensing motion and sending a photo. I have yet to receive my esp32 so I cannot answer this question myself until I can run the program.

    I can see before sending a photo the esp32 needs to connect to wifi, configure the cam, and prep the SDcard before taking a snapshot. I am curious about how long the delay is between sensing motion and sending the snapshot...generally. I understand that this time is dependent upon how long the esp32 takes to connect to wifi. But on average with a generally good wifi signal, and decent SDcard?

    Or more specifically, what is the latency between motion trigger and the snapshot?
    thanks all!

    0
    4do
    4do

    Answer 5 months ago

    Boot up to picture taken 5-6 seconds

    0
    pvasala786
    pvasala786

    7 months ago on Step 6

    Can we add facial detection to this project??

    0
    AmbroR
    AmbroR

    1 year ago

    Good project: congratulation !
    There is a thing not clear to me: sketch_1 captures and then save the photo on the SD.
    Sketch_2 read the SD and send a photo by email. Does it means that I do have to upload sketch_1 and then upload sketch_2 for posting ? BUT at this point I have lost sketch_1 and so I will NOT be able to capture again .
    Could you please clarify the matter olease ?
    Thanks for all
    Ambro

    0
    Bnbe_club
    Bnbe_club

    Reply 1 year ago

    Well, I split the project into two as some may not need the mail functionality. Sketch 2 is the complete sketch that does everthing - from capturing the image, saving it and emailing it. So if you want all that functionality then simply use sketch 2.

    0
    refatamin
    refatamin

    1 year ago

    Hi, my esp32 cam can't connect smpt server..

    15851259939194379512275274575264.jpg
    0
    OttoK16
    OttoK16

    1 year ago

    Great project , thanks

    Made same and adapted your code, my ESP Cam starts up , makes a picture at startup amd send to my email, and after that streams Video for a minute and go into deepsleep, my PIR sensor restarts this again.
    Using this code for the stream:

    void startCameraServer(){
    httpd_config_t config = HTTPD_DEFAULT_CONFIG();
    config.server_port = 80;
    httpd_uri_t index_uri = {
    .uri = "/",
    .method = HTTP_GET,
    .handler = stream_handler,
    .user_ctx = NULL
    };

    //Serial.printf("Starting web server on port: '%d'\n", config.server_port);
    if (httpd_start(&stream_httpd, &config) == ESP_OK) {
    httpd_register_uri_handler(stream_httpd, &index_uri);
    }
    }

    I made an android app with android studio to show the stream in a webview widget.
    Works all great.
    Now i want the following , if the PIR sensor is triggeedr which will result in the stream to start and this works, i also want to sent a notification to my app or change a button color inside the app. This works in other apps i made so i know how to code this in Android Studio and Arduino.
    I use ESP8266WebServer.h in case of ESP8266 which works great and for the ESP32 i wanted to use #include <WebServer.h> for ESP32.
    As soon as i do that the compiler crashes and the start is this:

    Arduino: 1.8.9 (Windows 10), Board: "ESP32 Wrover Module, Huge APP (3MB No OTA/1MB SPIFFS), QIO, 80MHz, 115200, None"
    In file included from C:\Users\ottok\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4/tools/sdk/include/esp_http_server/esp_http_server.h:22:0,
    from C:\Arduino_Projects\1 Project under design\CameraProjectOtto\CameraProjectOtto.ino:44:
    C:\Users\ottok\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4/tools/sdk/include/nghttp/http_parser.h:95:6: error: redeclaration of 'HTTP_DELETE'
    XX(0, DELETE, DELETE) \
    So looks likes some parameters are redeclared when i enable WebServer.h, it does not like the webserver used for the streaming video. I had hoped to open two different webservers, one for the streaming video and one for pushing http messages.
    Looks like a challange, does any one know if this at all possible ?

    1
    NursyahmiZ
    NursyahmiZ

    Question 1 year ago

    Hello there, I already made this project but there is one problem. My esp keep waking up from deep sleep and captured image even there are no movement. Can you help me?

    0
    Juanr130
    Juanr130

    Answer 1 year ago

    ls pines son muy sensibles, al conectar 13 a tierra se despierta esp. que transistor usa? yo tengo todo conectado a una bateria de celular y funciona

    0
    ashishgarg2709
    ashishgarg2709

    1 year ago

    Hello Sir, This project is awesome and is the exact thing that I was looking for. But while compiling the program, I am getting an error
    exit status 1
    Error compiling for board ESP32 Wrover Module.

    it stucks after this line
    Linking everything together...


    What I realized that, after adding ESP32_MailClient.h and SD.h libraries it shows me the problem. Because this https://www.bitsnblobs.com/wp-content/media/fw/diy/diy-e10.zip program easily runs. But after adding these libraries it gives me error. I am new in Arduino and coding world.


    How to solve this error.
    Thanks

    Capture.JPG
    0
    ashishgarg2709
    ashishgarg2709

    Reply 1 year ago

    Now I am able to compile and upload the code. Earlier there was error in the libraries. Thanks for this awesome project.

    0
    shounaksaha99
    shounaksaha99

    Reply 1 year ago

    I'm facing the same problem.
    But what are changes you made so that the errors of the packages got rectified?
    If you see this comment, please reply me. also you cam mail me in shounaksaha99@gmail.com.

    0
    ashishgarg2709
    ashishgarg2709

    Reply 1 year ago

    sent you an email.

    0
    Bnbe_club
    Bnbe_club

    Reply 1 year ago

    Thanks for the confirmation. Sorry for the delay in getting back to you - I do not get notifications for comments on this platform.

    Hello,
    Really nice!!
    I have a question, When I upload the sketch2 (send email) I receive the email with the image but the flash doesn't work. Can you please help me?.
    Regards.

    0
    Bnbe_club
    Bnbe_club

    Reply 1 year ago

    Thank you.

    Does the flash work at all, say for the first sketch at least?

    0
    malcolmpalmer.uk
    malcolmpalmer.uk

    1 year ago on Step 6

    Will not work when PIR is triggered a second time.
    This error appears:
    [E][sccb.c:154] SCCB_Write(): SCCB_Write Failed addr:0x72, reg:0xff, data:0x00, ret:-1
    [E][sccb.c:154] SCCB_Write(): SCCB_Write Failed addr:0x72, reg:0x44, data:0x0a, ret:-1
    [E][sccb.c:154] SCCB_Write(): SCCB_Write Failed addr:0x72, reg:0x05, data:0x01, ret:-1
    [E][camera.c:1215] camera_init(): Failed to set frame size
    [E][camera.c:1270] esp_camera_init(): Camera init failed with error 0x20002

    The sketch works once only, then produces these error when camera is woken up again.

    1
    malcolmpalmer.uk
    malcolmpalmer.uk

    Reply 1 year ago

    Solved it myself It was a question of a reliable constant power supply. So, instead of using the laptop or a mains 5v adapter plug to power the device I used a YuRobot 545043 breadboard power supply fixed to a 1/4 size (420 points) breadboard. It now works smoothly at a constant 5v and without any glitches. I've yet to see how long the 9v battery will last!
    Excellent sketch.

    0
    Bnbe_club
    Bnbe_club

    Reply 1 year ago

    Thank you. Didn't realize you left a comment here. I don't seem to be getting notification for new comments here.