PirCam With TelegramBot
by jcherreradev in Circuits > Microcontrollers
33 Views, 1 Favorites, 0 Comments
PirCam With TelegramBot




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

Environment Configuration
Software
- IDE: Arduino v1.8.x or v2.3.x
- Framework: Arduino ESP32 v3.0.7
- Board: AI Thinker ESP32-CAM
- Partition: Regular 4MB with spiffs(1.2MB APP/1.5MB SPIFFS).
- In case of using OTA function: Minimal SPIFFS(1.9 APP with OTA/ 190KB SPIFFS).
Libraries
- WiFiManager v2.0.17: To manage Wi-Fi connection.
- UniversalTelegramBot v1.3.0: For integration with Telegram bot.
- ArduinoJson v7.4.1: For handling JSON in the communication with the Telegram bot.
- TelnetStream v1.3.0: Use only if you do not have serial communication.
Hardware
- 1x ESP32-CAM board and external antenna (optional).
- 1x PIR motion sensor HC-SR501.
- 1x Step-Up MT3608 DC-DC
- 1x Charging module TP4056.
- 1x Battery holder 18650.
- 1x 18650 battery.
- 1x DPDT 6P switch.
- 1x LED diode.
- 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.
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.
Script Proxy_Video_ESP32.py


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:
Run the script: Start the server by running the script:
Access the server: To access, open http://localhost:5000 in your browser.
Operation Flow

- 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.
- 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.
- Events: If there was an unexpected reboot report the causes or of some other problem referred to the startup data.
- Notifications: The system sends notifications to registered users in case the battery drops below 15%, overheating and when the PIR sensor detects motion.
Considerations


⚡ 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.