TTGO T-Camera Fridge PIR Security Camera


Using the TTGO T-Camera module with PIR sensor to display a warning and photograph anyone who opens the fridge

This project uses a PIR movement sensor to wake the on-board ESP32 from deep sleep. Once started the module prints a message to the OLED. If movement continues to be detected a photo is taken and sent to a server on the internet. The server then sends this photograph to an email address.

The TTGO T-Camera is an ESP32 Module with an OV2640 camera and an 0.96 OLED on-board. They have them on AliExpress here. Select the PIR version with the wide-angle lens.

To make it easy to install I’ve zipped up the Sketch and all the needed libraries into one download here. Unzip this folder and copy it to your Arduino folder on your computer. Alternatively copy and paste the script below and install the libaries manually. The SSID, password and server address need to be updated.

#include <OV2640.h>
#include <WiFi.h>
#include "esp_http_client.h"
#include <U8x8lib.h>

const char *ssid =     "NSA";         // Put your SSID here
const char *password = "Orange";     // Put your PASSWORD here

long lastPirDetection = millis();  // variable for last time PIR device woke up system
long stayAwakeInterval = 300000;     // interval after which to sleep (300000ms - 5mins)
const char *post_url = ""; // Location where images are POSTED

U8X8_SSD1306_128X64_NONAME_SW_I2C u8x8(/* clock=*/ 22, /* data=*/ 21);

#define PIR_PIN 33

OV2640 cam;

void setup()


  pinMode(PIR_PIN, INPUT); //pir
  esp_sleep_enable_ext0_wakeup((gpio_num_t )PIR_PIN, HIGH);


  if (init_wifi()) { // Connected to WiFi
    Serial.println("Internet connected");
  } else {
    u8x8.setCursor(4, 2);
    u8x8.setCursor(2, 4);
    u8x8.print("No Internet!");

bool init_wifi()
  int connAttempts = 0;
  Serial.println("\r\nConnecting to: " + String(ssid));
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED ) {
    if (connAttempts > 10) return false;
  return true;

esp_err_t _http_event_handler(esp_http_client_event_t *evt)
  switch (evt->event_id) {
      Serial.printf("HTTP_EVENT_ON_HEADER, key=%s, value=%s", evt->header_key, evt->header_value);
      Serial.printf("HTTP_EVENT_ON_DATA, len=%d", evt->data_len);
      if (!esp_http_client_is_chunked_response(evt->client)) {
        // Write out data
        // printf("%.*s", evt->data_len, (char*)evt->data);
  return ESP_OK;

static esp_err_t take_send_photo()
  Serial.println("Taking picture...");

  esp_err_t res = ESP_OK;;

  esp_http_client_handle_t http_client;

  esp_http_client_config_t config_client = {0};
  config_client.url = post_url;
  config_client.event_handler = _http_event_handler;
  config_client.method = HTTP_METHOD_POST;

  http_client = esp_http_client_init(&config_client);

  esp_http_client_set_post_field(http_client, (char *)cam.getfb(), cam.getSize());

  esp_http_client_set_header(http_client, "Content-Type", "image/jpg");

  esp_err_t err = esp_http_client_perform(http_client);
  if (err == ESP_OK) {
    Serial.print("esp_http_client_get_status_code: ");


void warning_screen()

  u8x8.setCursor(6, 0);
  u8x8.setCursor(6, 2);
  u8x8.setCursor(4, 4);
  u8x8.setCursor(9, 4);
  u8x8.setCursor(5, 6);
  u8x8.setCursor(4, 1);
  u8x8.print("PHOTO IN");
  u8x8.setCursor(5, 7);
  for (int s = 5; s > 0; s--) {
    u8x8.setCursor(7, 3);

void loop()
  Serial.println("Device Woken");

  long timer_millis = millis();
  while (millis() - timer_millis < 3000) { //Test for movement for 3 seconds after warning
    if (digitalRead(PIR_PIN)) { // If still movement - Take photo

    // Back to sleep

Assuming you are using PHP on your server, the code to accept the image and email it looks like this:

This is great little project to check on your friends and family’s fridge habits but the fridge probably isn’t the best place to leave electronics and a power-bank for a long time without some protection.


Digital Ocean Droplet: Cheapest way to get a PHP server. Supposedly this link gives a $100 in credit over 60 days
PHP Email code:

Sort list

11 Replies to “TTGO T-Camera Fridge PIR Security Camera”

  1. Carsten says:


    I tried your example with my board. Download your zip-file, open the .ino file in my Arduino IDE and start to compile.

    I receive an error message:

    /Users/cc13/Documents/Arduino/FridgeGuard/FridgeGuard.ino: In function ‘void setup()’:
    FridgeGuard:32:5: error: ‘internet_connected’ was not declared in this scope
    internet_connected = true;
    Mehrere Bibliotheken wurden für “WiFi.h” gefunden
    Benutzt: /Users/cc13/Library/Arduino15/packages/esp32/hardware/esp32/1.0.1/libraries/WiFi
    Nicht benutzt: /Applications/
    exit status 1
    ‘internet_connected’ was not declared in this scope

    That means Arduino found more then one Wifi library and the used one will not work with “internet_connected”.

    When I open the “WiFiClient”-Example from the folder “/Users/cc13/Library/Arduino15/packages/esp32/hardware/esp32/1.0.1/libraries/WiFi/examples/WiFiClient” I could compile without errors.

    Could you help me please to solve that?


    1. WordBot says:

      Ah sorry.. That line shouldn’t have been in the code. I don’t know how it got there but I’ve removed it now and the code is working.
      Thanks for letting me know.

  2. Carsten says:


    great, now I could compile and transfer the sketch to my board. Next test is if the .php-file is working and sending the mail with the photo.


  3. Cliff says:

    How did you power the device? I’m looking to use it as doorbell, but new to this sort of thing. I have the code like I want it, but want to somehow power the device from the wires going to the current doorbell. Is that possible?

    1. WordBot says:

      Do you know how the current doorbell is powered? It’s often from a transformer from the normal house power. You need 5v DC to power the ESP32. Might be a job for a qualified electrician if you’re not sure.

  4. Rifki says:

    Can i make Email to receive the information about who is Come ?

    1. WordBot says:

      You want the name of the person to be sent in the email? It’s much more complicated. See the first two tutorials here: for some code you could use.

  5. Neo says:

    Could you please guide me as to which board you selected in the arduino IDE to program this board? Thank you.

  6. Antonio Tedesco says:

    Hello, I don’t understand why when the countdown is over everything goes off without any message

    1. WordBot says:

      Hi. Do you have this running on the T-Camera? If you move again when the warning has finished it should then send a picture but I’m looking at the loop and I wonder if I’ve put the closing bracket in the wrong place because the while loop should just be a test for second movement.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

scroll to top