TTGO T-Camera Fridge PIR Security Camera

featured-1.jpg

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 "src/OV2640.h"
#include <WiFi.h>
#include "esp_http_client.h"
#include "src/U8x8lib.h"

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

const char *post_url = "http://domain.dom//posted.php"; // Location where images are POSTED

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

#define PIR_PIN 33

OV2640 cam;

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

  u8x8.begin();
  u8x8.setFont(u8x8_font_amstrad_cpc_extended_r);

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

  cam.init(esp32cam_ttgo_t_config);

  if (init_wifi()) { // Connected to WiFi
    Serial.println("Internet connected");
  } else {
    u8x8.setCursor(4, 2);
    u8x8.print("Lucky!");
    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 ) {
    delay(500);
    Serial.print(".");
    if (connAttempts > 10) return false;
    connAttempts++;
  }
  return true;
}

esp_err_t _http_event_handler(esp_http_client_event_t *evt)
{
  switch (evt->event_id) {
    case HTTP_EVENT_ERROR:
      Serial.println("HTTP_EVENT_ERROR");
      break;
    case HTTP_EVENT_ON_CONNECTED:
      Serial.println("HTTP_EVENT_ON_CONNECTED");
      break;
    case HTTP_EVENT_HEADER_SENT:
      Serial.println("HTTP_EVENT_HEADER_SENT");
      break;
    case HTTP_EVENT_ON_HEADER:
      Serial.println();
      Serial.printf("HTTP_EVENT_ON_HEADER, key=%s, value=%s", evt->header_key, evt->header_value);
      break;
    case HTTP_EVENT_ON_DATA:
      Serial.println();
      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);
      }
      break;
    case HTTP_EVENT_ON_FINISH:
      Serial.println("");
      Serial.println("HTTP_EVENT_ON_FINISH");
      break;
    case HTTP_EVENT_DISCONNECTED:
      Serial.println("HTTP_EVENT_DISCONNECTED");
      break;
  }
  return ESP_OK;
}

void warning_screen()
{
  u8x8.fillDisplay();
  u8x8.clear();
  u8x8.fillDisplay();
  u8x8.clear();
  u8x8.fillDisplay();
  u8x8.clear();
  u8x8.fillDisplay();
  u8x8.clear();

  u8x8.setCursor(6, 0);
  u8x8.print("STEP");
  delay(600);
  u8x8.setCursor(6, 2);
  u8x8.print("AWAY");
  delay(800);
  u8x8.setCursor(4, 4);
  u8x8.print("FROM");
  delay(500);
  u8x8.setCursor(9, 4);
  u8x8.print("THE");
  delay(500);
  u8x8.setCursor(5, 6);
  u8x8.print("FRIDGE");
  delay(1000);
  u8x8.clear();
  u8x8.setCursor(4, 1);
  u8x8.print("PHOTO IN");
  u8x8.setCursor(5, 7);
  u8x8.print("SECONDS");
  delay(500);
  u8x8.setFont(u8x8_font_courB18_2x3_n);
  for (int s = 5; s > 0; s--) {
    u8x8.setCursor(7, 3);
    u8x8.print(s);
    delay(1000);
  }
}

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

  esp_err_t res = ESP_OK;
  cam.run();

  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: ");
    Serial.println(esp_http_client_get_status_code(http_client));
  }

  esp_http_client_cleanup(http_client);
  back_to_sleep();
}

void back_to_sleep()
{
  Serial.println("Device Sleeping...");
  delay(1000);
  u8x8.setPowerSave(true);
  WiFi.mode(WIFI_OFF);
  btStop();
  esp_deep_sleep_start();
}

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

  long timer_millis = millis();
  while (millis() - timer_millis < 3000) { //Test for movement for 3 seconds after warning screen
    if (digitalRead(PIR_PIN)) { // If still movement - Take photo
      take_send_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.

References

Digital Ocean Droplet: https://m.do.co/c/801f9f581abd Cheapest way to get a PHP server. Supposedly this link gives a $100 in credit over 60 days
PHP Email code: https://stackoverflow.com/questions/30295587/sending-email-with-inline-image

Sort list

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

  1. Carsten says:

    Hello,

    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/Arduino.app/Contents/Java/libraries/WiFi
    exit status 1
    ‘internet_connected’ was not declared in this scope
    /Users/cc13/Library/Arduino15/packages/esp32/hardware/esp32/1.0.1/libraries/WiFi/examples/WiFiClient/WiFiClient.ino
    ###

    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?

    Thanks,
    Carsten

    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:

    Hi,

    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.

    Thanks,
    Carsten

  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: https://robotzero.one/category/face-recognition/ 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.

      I’ve updated the code to make it easier to follow. If there is movement during the three seconds after the countdown it will take a picture. If not it will sleep. It also sleeps after taking a picture.

  7. rüştü says:

    I chose esp32 wrover but compilation error
    what should I do

    1. WordBot says:

      What error do you see? I don’t remember which board I chose.

  8. rüştü says:

    can you check and send the code again? Please
    I need a lot of codes

  9. Michel Veron-Durand says:

    bonjour,
    Bien que j’ai configuré la carte comme montré dans un message précedent ,quand je compile moi aussi j’arrive à la fin à:
    Erreur de compilation pour la carte ESP32 Wrover Module
    Merci pour votre aide

    1. WordBot says:

      Hi.. Is there not more information to tell you what the problem is?

  10. Michel Veron-Durand says:

    Changements à venir concernant l’historique
    L’historique de traduction ne sera bientôt disponible que lorsque vous serez connecté. Vous pourrez le gérer dans Mon activité. Votre historique existant sera effacé lors de cette mise à jour. Nous vous invitons à enregistrer les traductions que vous voulez conserver.
    OK
    bonjour,
    merci de me répondre.
    Pour ouvrir un croquis qui fonctionne sur arduino j’ai recopié tous les dossiers SRC dans le même niveau de document que l’ino. (sinon j’ai : src/OV2640.h: No such file or directory.
    Maintenant quand je lance la compilation au bout d’un temps certain elle se bloque .
    j’ai copié les dernières lignes de la complilation:
    Plusieurs bibliothèque trouvées pour “WiFi.h”
    Utilisé : C:\Users\chambre\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\WiFi
    Non utilisé : C:\Program Files (x86)\Arduino\libraries\WiFi
    U8x8lib.cpp:(.text._Z18u8x8_SetPin_KS0108P11u8x8_structhhhhhhhhhhhhhh+0x0): multiple definition of `u8x8_SetPin_KS0108(u8x8_struct*, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char)’

    C:\Users\chambre\AppData\Local\Temp\arduino_build_555784\sketch\U8x8lib.cpp.o:U8x8lib.cpp:(.text._Z18u8x8_SetPin_KS0108P11u8x8_structhhhhhhhhhhhhhh+0x0): first defined here

    C:\Users\chambre\AppData\Local\Temp\arduino_build_555784\sketch\src\U8x8lib.cpp.o: In function `u8x8_SetPin_SED1520(u8x8_struct*, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char)’:

    U8x8lib.cpp:(.text._Z19u8x8_SetPin_SED1520P11u8x8_structhhhhhhhhhhhh+0x0): multiple definition of `u8x8_SetPin_SED1520(u8x8_struct*, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char)’

    C:\Users\chambre\AppData\Local\Temp\arduino_build_555784\sketch\U8x8lib.cpp.o:U8x8lib.cpp:(.text._Z19u8x8_SetPin_SED1520P11u8x8_structhhhhhhhhhhhh+0x0): first defined here

    C:\Users\chambre\AppData\Local\Temp\arduino_build_555784\sketch\src\U8x8lib.cpp.o: In function `u8x8_byte_arduino_hw_i2c’:

    U8x8lib.cpp:(.text.u8x8_byte_arduino_hw_i2c+0x0): multiple definition of `u8x8_byte_arduino_hw_i2c’

    C:\Users\chambre\AppData\Local\Temp\arduino_build_555784\sketch\U8x8lib.cpp.o:U8x8lib.cpp:(.text.u8x8_byte_arduino_hw_i2c+0x0): first defined here

    collect2.exe: error: ld returned 1 exit status

    Utilisation de la bibliothèque WiFi version 1.0 dans le dossier: C:\Users\chambre\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\WiFi
    Utilisation de la bibliothèque SPI version 1.0 dans le dossier: C:\Users\chambre\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\SPI
    Utilisation de la bibliothèque Wire version 1.0.1 dans le dossier: C:\Users\chambre\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\Wire
    exit status 1
    Erreur de compilation pour la carte ESP32 Wrover Module
    2845/5000
    Hello,
    Thank you for answering me.
    To open a sketch that works on arduino I copied all the SRC files in the same document level as the ino. (otherwise I have: src / OV2640.h: No such file or directory.
    Now when I start the compilation after a certain time it freezes.
    I copied the last lines of the compilation:
    Several libraries found for “WiFi.h”
    Used: C: \ Users \ room \ AppData \ Local \ Arduino15 \ packages \ esp32 \ hardware \ esp32 \ 1.0.4 \ libraries \ WiFi
    Not used: C: \ Program Files (x86) \ Arduino \ libraries \ WiFi
    U8x8lib.cpp :(. Text._Z18u8x8_SetPin_KS0108P11u8x8_structhhhhhhhhhhhhhh + 0x0): multiple definition of `u8x8_SetPin_KS0108 (u8x8_struct *, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char. char, unsigned char, unsigned char, unsigned char, unsigned char) ‘

    C: \ Users \ room \ AppData \ Local \ Temp \ arduino_build_555784 \ sketch \ U8x8lib.cpp.o: U8x8lib.cpp :(. Text._Z18u8x8_SetPin_KS0108P11u8x8_structhhhhhhhhhhhhhhh + 0x0): first here

    C: \ Users \ chambre \ AppData \ Local \ Temp \ arduino_build_555784 \ sketch \ src \ U8x8lib.cpp.o: In function `u8x8_SetPin_SED1520 (u8x8_struct *, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char , unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char) ‘:

    U8x8lib.cpp :(. Text._Z19u8x8_SetPin_SED1520P11u8x8_structhhhhhhhhhhhh + 0x0): multiple definition of `u8x8_SetPin_SED1520 (u8x8_struct *, unsigned char, unsigned char, unsigned char, charigned, charigned, chartered, char, unsigned char, unsigned char) ‘

    C: \ Users \ room \ AppData \ Local \ Temp \ arduino_build_555784 \ sketch \ U8x8lib.cpp.o: U8x8lib.cpp :(. Text._Z19u8x8_SetPin_SED1520P11u8x8_structhhhhhhhhhhhh + 0x0): first defined here

    C: \ Users \ room \ AppData \ Local \ Temp \ arduino_build_555784 \ sketch \ src \ U8x8lib.cpp.o: In function `u8x8_byte_arduino_hw_i2c ‘:

    U8x8lib.cpp :(. Text.u8x8_byte_arduino_hw_i2c + 0x0): multiple definition of `u8x8_byte_arduino_hw_i2c ‘

    C: \ Users \ room \ AppData \ Local \ Temp \ arduino_build_555784 \ sketch \ U8x8lib.cpp.o: U8x8lib.cpp :(. Text.u8x8_byte_arduino_hw_i2c + 0x0): first defined here

    collect2.exe: error: ld returned 1 exit status

    Use of the WiFi version 1.0 library in the folder: C: \ Users \ room \ AppData \ Local \ Arduino15 \ packages \ esp32 \ hardware \ esp32 \ 1.0.4 \ libraries \ WiFi
    Use of the SPI version 1.0 library in the folder: C: \ Users \ room \ AppData \ Local \ Arduino15 \ packages \ esp32 \ hardware \ esp32 \ 1.0.4 \ libraries \ SPI
    Use of the Wire version 1.0.1 library in the folder: C: \ Users \ room \ AppData \ Local \ Arduino15 \ packages \ esp32 \ hardware \ esp32 \ 1.0.4 \ libraries \ Wire
    exit status 1
    Compilation error for the ESP32 Wrover Module card

    1. WordBot says:

      Can you try commenting out the U8x8 library include.

  11. Michel Veron-Durand says:

    968/5000
    good evening,
    as you told me i commented on all the lines with u8x8.
    compilation works.
    but when I load the program at the end he says to me:
    esptool.py v2.6
    COM21 serial port
    Connecting …….._____….._____….._____….._____….._____….._____…..____ An error occurred of the sketch transfer
    _

    A fatal error occurred: Failed to connect to ESP32: Timed out waiting for packet header
    Invalid library found in C: \ Users \ room \ Documents \ Arduino \ libraries \ FridgeGuard: no header file (.h) found in C: \ Users \ room \ Documents \ Arduino \ libraries \ FridgeGuard
    Invalid library found in C: \ Users \ room \ Documents \ Arduino \ libraries \ FridgeGuard: no header file (.h) found in C: \ Users \ room \ Documents \ Arduino \ libraries \ FridgeGuard

    I tested with the esp32 wrover card module all partitions
    and also with esp 32 dev module
    as well as TTGO T1
    always the error message.
    hopeless

    1. WordBot says:

      Do you have the FridgeGuard sketch in the /libraries folder? Shouldn’t be there. Do other example sketches upload OK?

  12. Michel Veron-Durand says:

    Hello
    I installed arduino ide on another pc then I didn’t have that sketch and everything worked the first time.
    Thanks for your help.

  13. guy fauquembergue says:

    Hello, I have tried the code with an interrupt triggering PIN33 to send a picture and it does not work with the ov640.h camera config. The interrupt does not detect the motion and reboot the board instead. I am also surprise that Lilygo does not have an example for this board. Having a PIR which cannot send a picture on motion detection is useless if there is no reliable example. Best regards. Guy

    1. WordBot says:

      Hi,

      Are you saying that attaching an interrupt to pin33 crashes the board when the board starts up? How about just looping until pin33 goes high as in my sketch above? The only code I found from Lilygo is here: https://github.com/Xinyuan-LilyGO/LilyGo-Camera-Series/blob/master/sketch/sketch.ino I can’t find where I originally found the code I used.

  14. Hello,
    Thanks for your prompt answer, I have tried again your code and finally it works fine. The issue with LilyGo is that they are not really good in documenting their firmware usage. It seems a common drawback with Chinese products then you need to dig in multiple solutions.
    I just need now to send captured pictures to Telegram or GDrive. Best regards, Guy.

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