Code Source
Code Source
Code Source
make2explore.com----------------------------------------------------//
// Project - Home Security System using ESP32 CAM and Telegram App
// Created By - [email protected]
// Version - 1.0
// Last Modified - 22/02/2021 15:00:00 @admin
// Software - C/C++, Arduino IDE, Libraries
// Hardware - Ai Thinker ESP32 CAM, MQ6 Sensor, Flame Sensor, AM312 PIR
Sensor
// Sensors Used - MQ6 LPG/Gas/Smoke Sensor, Flame Sensor, AM312 PIR Sensor
// Source Repo - github.com/make2explore
//
-----------------------------------------------------------------------------------
--------------------//
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include "soc/soc.h"
#include "soc/rtc_cntl_reg.h"
#include "esp_camera.h"
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>
WiFiClientSecure clientTCP;
//CAMERA_MODEL_AI_THINKER
#define PWDN_GPIO_NUM 32
#define RESET_GPIO_NUM -1
#define XCLK_GPIO_NUM 0
#define SIOD_GPIO_NUM 26
#define SIOC_GPIO_NUM 27
#define Y9_GPIO_NUM 35
#define Y8_GPIO_NUM 34
#define Y7_GPIO_NUM 39
#define Y6_GPIO_NUM 36
#define Y5_GPIO_NUM 21
#define Y4_GPIO_NUM 19
#define Y3_GPIO_NUM 18
#define Y2_GPIO_NUM 5
#define VSYNC_GPIO_NUM 25
#define HREF_GPIO_NUM 23
#define PCLK_GPIO_NUM 22
#define FLASH_LED_PIN 4
bool flashState = LOW;
// Motion Sensor
const byte motionSensor = 13;
bool motionDetected = false;
bool motionDetectEnable = false;
// Door Sensor
const byte door1 = 12;
const byte door2 = 2;
bool door1stat = true;
bool door2stat = true;
bool doorLockMonitor = false;
// fire sensor
bool fireDetectMonitor = false;
const byte firePin = 14;
bool fire = false;
void setup(){
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0);
Serial.begin(115200);
// Camera Flash
pinMode(FLASH_LED_PIN, OUTPUT);
digitalWrite(FLASH_LED_PIN, flashState);
// Initialize WiFi
WiFi.mode(WIFI_STA);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println();
Serial.print("ESP32-CAM IP Address: ");
Serial.println(WiFi.localIP());
camera_config_t config;
config.ledc_channel = LEDC_CHANNEL_0;
config.ledc_timer = LEDC_TIMER_0;
config.pin_d0 = Y2_GPIO_NUM;
config.pin_d1 = Y3_GPIO_NUM;
config.pin_d2 = Y4_GPIO_NUM;
config.pin_d3 = Y5_GPIO_NUM;
config.pin_d4 = Y6_GPIO_NUM;
config.pin_d5 = Y7_GPIO_NUM;
config.pin_d6 = Y8_GPIO_NUM;
config.pin_d7 = Y9_GPIO_NUM;
config.pin_xclk = XCLK_GPIO_NUM;
config.pin_pclk = PCLK_GPIO_NUM;
config.pin_vsync = VSYNC_GPIO_NUM;
config.pin_href = HREF_GPIO_NUM;
config.pin_sscb_sda = SIOD_GPIO_NUM;
config.pin_sscb_scl = SIOC_GPIO_NUM;
config.pin_pwdn = PWDN_GPIO_NUM;
config.pin_reset = RESET_GPIO_NUM;
config.xclk_freq_hz = 20000000;
config.pixel_format = PIXFORMAT_JPEG;
// camera init
esp_err_t err = esp_camera_init(&config);
if (err != ESP_OK) {
Serial.printf("Camera init failed with error 0x%x", err);
delay(1000);
ESP.restart();
}
delay(40000); // Give time for PIR and MQ6 Gas Sensor to get Stable
Serial.printf("PIR Sensor Initilized!! \n");
}
void loop(){
if (sendPhoto){
Serial.println("Preparing photo");
sendPhotoTelegram();
sendPhoto = false;
}
String sendPhotoTelegram(){
const char* myDomain = "api.telegram.org";
String getAll = "";
String getBody = "";
camera_fb_t * fb = NULL;
fb = esp_camera_fb_get();
if(!fb) {
Serial.println("Camera capture failed");
delay(1000);
ESP.restart();
return "Camera capture failed";
}
if (clientTCP.connect(myDomain, 443)) {
Serial.println("Connection successful");
esp_camera_fb_return(fb);
if (text == "/photo") {
sendPhoto = true;
bot.sendMessage(chat_id, "Capturing Photo...", "");
Serial.println("New photo request");
}
if (text == "/EnableMotionAlert") {
motionDetectEnable = true;
bot.sendMessage(chat_id, "Motion Alert - Enabled", "");
Serial.println("Enable the Motion alert");
}
if (text == "/DisableMotionAlert") {
motionDetectEnable = false;
bot.sendMessage(chat_id, "Motion Alert - Disabled", "");
Serial.println("Disable the Motion alert");
}
if (text == "/EnableDoorAlert") {
doorLockMonitor = true;
bot.sendMessage(chat_id, "Monitor Doors - Enabled", "");
Serial.println("Enable the Monitor Doors");
}
if (text == "/DisableDoorAlert") {
doorLockMonitor = false;
bot.sendMessage(chat_id, "Monitor Doors - Disabled", "");
Serial.println("Disable the Monitor Doors");
}
if (text == "/EnableFireAlert") {
fireDetectMonitor = true;
bot.sendMessage(chat_id, "Fire Detector - Enabled", "");
Serial.println("Enable the Fire Detector");
}
if (text == "/DisableFireAlert") {
fireDetectMonitor = false;
bot.sendMessage(chat_id, "Fire Detector - Disabled", "");
Serial.println("Disable the Fire Detector");
}
if (text == "/EnableSmokeAlert") {
smokeDetectMonitor = true;
bot.sendMessage(chat_id, "Smoke Detector - Enabled", "");
Serial.println("Enable the Smoke Detector");
}
if (text == "/DisableSmokeAlert") {
smokeDetectMonitor = false;
bot.sendMessage(chat_id, "Smoke Detector - Disabled", "");
Serial.println("Disable the Smoke Detector");
}
if (text == "/start"){
String welcome = "Welcome to the make2explore ESP32-CAM Telegram bot.\n\n";
welcome += "Use the following command to Setup the ESPCAM.\n\n";
welcome += "/flashOn : Turn on the flash LED\n";
welcome += "/flashOff : Turn off the flash LED\n";
welcome += "/photo : takes a new photo\n\n";
welcome += "/EnableMotionAlert : enables the motion Alert System\n";
welcome += "/DisableMotionAlert : disables the motion Alert System\n\n";
welcome += "/EnableDoorAlert : enables the door Open Alert System\n";
welcome += "/DisableDoorAlert : disables the door Open Alert System\n\n";
welcome += "/EnableSmokeAlert : enables the Smoke Alert System\n";
welcome += "/DisableSmokeAlert : disables the Smoke Alert System\n\n";
welcome += "/EnableFireAlert : enables the Fire Alert System\n";
welcome += "/DisableFireAlert : disables the Fire Alert System\n\n";
welcome += "You'll receive a photo whenever motion is detected.\n";
bot.sendMessage(chatId, welcome, "Markdown");
}
}
}
// -------------------------------------# make2explore.comn
#----------------------------------------------//