PirCam With TelegramBot

by jcherreradev in Circuits > Microcontrollers

33 Views, 1 Favorites, 0 Comments

PirCam With TelegramBot

Screenshot_0.jpg
Screenshot_3.jpg
Screenshot_1.jpg
Screenshot_2.jpg

It consists of a security monitoring system based on an ESP32-CAM, which interacts with a Telegram bot. The device captures photos using the built-in camera and sends them to registered users when it detects motion using a PIR sensor. In addition, it has multiple functionalities controllable through Telegram, such as activating or deactivating the surveillance mode, taking photos on demand, streaming video on the local network or controlling the energy saving mode.


⚠️ On power up, the ESP32-CAM attempts to load the stored configurations. If it does not find the bot token, Wi-Fi credentials or at least one user, it enters configuration mode using WiFiManager to obtain these data to get them.

Supplies

PirCam_TelegramBot_ESPCAM.png

Environment Configuration

Software

  1. IDE: Arduino v1.8.x or v2.3.x
  2. Framework: Arduino ESP32 v3.0.7
  3. Board: AI Thinker ESP32-CAM
  4. Partition: Regular 4MB with spiffs(1.2MB APP/1.5MB SPIFFS).
  5. In case of using OTA function: Minimal SPIFFS(1.9 APP with OTA/ 190KB SPIFFS).

Libraries

  1. WiFiManager v2.0.17: To manage Wi-Fi connection.
  2. UniversalTelegramBot v1.3.0: For integration with Telegram bot.
  3. ArduinoJson v7.4.1: For handling JSON in the communication with the Telegram bot.
  4. TelnetStream v1.3.0: Use only if you do not have serial communication.

Hardware

  1. 1x ESP32-CAM board and external antenna (optional).
  2. 1x PIR motion sensor HC-SR501.
  3. 1x Step-Up MT3608 DC-DC
  4. 1x Charging module TP4056.
  5. 1x Battery holder 18650.
  6. 1x 18650 battery.
  7. 1x DPDT 6P switch.
  8. 1x LED diode.
  9. Resistors 100 kohm, 10 kohm and 100 ohm.

Check PSRAM

⚠️ First verify that the PSRAM on the ESP32-CAM board is working properly using the board model: AI Thinker ESP32-CAM as some units seem to have this defect from the factory.

#include <Arduino.h>
void setup() {
Serial.begin(115200);
delay(1000);

if (psramFound()) {
Serial.println("PSRAM está habilitada y funcionando.");
} else {
Serial.println("PSRAM no está habilitada o disponible.");
}
}

void loop() {
}

If failures occur such as Error starting the camera, Error capturing the photo or restarts due to WDT, we recommend disabling the Watchdogs function.

Image and Video Quality

Image quality must be changed from void configCamara() higher image quality and size means higher power consumption and lower performance of the esp32-cam.

config.frame_size = FRAMESIZE_VGA; // FRAMESIZE_ + QVGA|CIF|VGA|SVGA|XGA|SXGA|UXGA|UXGA
config.jpeg_quality = 30; // 0-63 a lower number means higher image quality.

Script Proxy_Video_ESP32.py

Screenshot_6.png
Screenshot_5.jpg

This script is a proxy server in Flask to relay the ESP32-CAM video through a custom web interface which allows you to view the video in Picture-in-Picture (PiP), Full Screen, Capture Image and Record Video if you do not require these functions simply enter: http://espcam-bot.local or the esp32-cam IP.

Install Python:

Make sure you have Python 3.10 or higher installed on your system. You can download it from python.org.

Install the dependencies:

Run the following command to install the necessary libraries, flask v3.1.x and requests v2.25.x and ffmpeg v4.4.x for video:

sudo apt install ffmpeg
pip install flask requests

Run the script: Start the server by running the script:

python3 Proxy_Video_ESP32.py

Access the server: To access, open http://localhost:5000 in your browser.

Operation Flow

Screenshot_4.jpg
  1. Power On and Load Configuration: On power up, the device loads the configuration from flash memory. If the data is not available (bot token, Wi-Fi credentials, or registered user), it enters configuration mode (AP mode) using WiFiManager to collect the information.
  2. Interaction with the Bot: Once configured, the Telegram bot interacts with the device through defined commands. Each command has a specific action, such as changing the surveillance mode, taking photos or modifying power saving settings.
  3. Events: If there was an unexpected reboot report the causes or of some other problem referred to the startup data.
  4. Notifications: The system sends notifications to registered users in case the battery drops below 15%, overheating and when the PIR sensor detects motion.

Considerations

Conmutacion.jpg
voltimetro.png

⚡ Switching between external power supply and battery

Circuit to switch the power supply between the battery and an external 5V source.

⚠️ If the device is connected to an external power supply, it is recommended not to use the battery simultaneously. The TP4056 module is not designed to charge the battery and power the device at the same time, which could cause overcharging and be dangerous. Use the battery only when an external power supply is not available.


🔋 Battery level

⚠️ Using GPIO33 to measure battery level on ESP32-CAM

On the ESP32-CAM board, only the ADC2 pins are available externally. However, the ADC2 pins are used internally by the Wi-Fi controller or by the microSD, so they cannot be used for analog readings while Wi-Fi is enabled.

By default, the GPIO33 pin which is ADC1 is connected to the on-board integrated LED. To use this pin as an analog input to measure the battery level, you must desolder the integrated LED that is connected to GPIO33. This way, you will be able to connect the resistive divider for battery voltage measurement to this pin.

Code