Pages

Friday, October 8, 2021

ESP32 Cam doorbell

 For an index to all my stories click this text

The regular readers of this blog know that I wrote a few stories on Blynk. You can find them here:
http://lucstechblog.blogspot.com/2021/03/blynk-part-1-starting-with-blynk.html
http://lucstechblog.blogspot.com/2021/04/blynk-part-2-sending-data-to-blynk.html
http://lucstechblog.blogspot.com/2021/04/blynk-part-3-your-own-blynk-server.html
http://lucstechblog.blogspot.com/2021/06/blynk-part-4-using-blynk-with-multiple.html

And I did some stories on the ESP32 Cam. You can find these here:
http://lucstechblog.blogspot.com/2021/09/esp32cam-programming-board.html
http://lucstechblog.blogspot.com/2021/10/esp32cam-experiment-board.html

So how about combining these two and send pictures from the ESP32Cam to Blynk.

If you have no experience with Blynk I really urge you to read the above mentioned stories first and do some experiments with Blynk. This will make you getting familiar with Blynk which helps a lot with reproducing this project.

As this project uses 800 energy points I really urge you to build your own Blynk server like demonstrated in the 4th story. It is not necessary but there is little energy left for other projects if you use the free Blynk server unless you are willing to spend some money on buying energy.

What is the intention.

I am going to show you how to make a doorbell with an ESP32 Cam that sends a picture to Blynk when someone pushes a button. Next to that in the Blynk app there is a virtual button. When you press that button you will be able to make a picture wether or not there is someone at the door. So you can keep an eye on your door.

There are multiple purposes for this. And I will give you some examples at the end of this story.

The breadboard setup.

For programming the ESP32Cam I used my own ESP32Cam programming board that can be found (and re-build by yourself) here:http://lucstechblog.blogspot.com/2021/10/esp32cam-experiment-board.html

When the programming and testing finished I used my ESP32Cam experimenting board which you can be found (and build by yourself) here :
http://lucstechblog.blogspot.com/2021/10/esp32cam-experiment-board.html



I put the ESP32Cam on the experimenting board and put a small breadboard next to it. On the breadboard I put a pushbutton with a pull-up resistor. The pushbutton is connected to IO pin 13.

Blynk project

For this experiment I started a new project. Surprisingly I called it Espcam doorbell.
If you have no experience with Blynk start with the first of my stories. That story tells you how to add a button and other items to Blynk. You can re-read that story here: http://lucstechblog.blogspot.com/2021/03/blynk-part-1-starting-with-blynk.html



There are just 2 items on the screen.



The first is a button which is attached to GPIO 14. I gave it the name Photo but you can use any name you like. The button was attached to GP14 and is normally low (0) and sends a high signal(1) when pushed. The text is in a large font and colored red. Off the button has the text Photo and when it is pushed the text alters to Done.



The second item is an Image Gallery. You can find that in the Widget Box in the DISPLAYS section.



The Image Gallery is attached to Virtual pin V1. I made the Image Gallery box screen wide and used FIT to make the picture fit into that box.
The most important part is to tell Blynk where to get the picture.

HTTP://192.168.1.86/capture

This is the reference where Blynk can fetch the Photo. The problem at this moment is that you do not know the IP number of the ESP32-Cam. So first we need to flash the software into the ESP32Cam.

The program.

Actually this is the program that is supplied with the ESP32 Cam board in the Arduino IDE.


 

So open the Arduino IDE. Choose as board AI Thinker ESP32-Cam and then load the CameraWebServer example.
To get this working with Blynk we need to make some changes to the program. First I will give you the complete program. You can just copy and paste it if you are not interested in the modifications.

#include "esp_camera.h"
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>

const char* ssid = "YOUR-ROUTERS-NAME";
const char* password = "PASSWORD";
char auth[] = "BLYNK AUTHENTIFICATION CODE"; 

//
// WARNING!!! Make sure that you have either selected ESP32 Wrover Module,
//            or another board which has PSRAM enabled
//

// Select camera model
//#define CAMERA_MODEL_WROVER_KIT
//#define CAMERA_MODEL_ESP_EYE
//#define CAMERA_MODEL_M5STACK_PSRAM
//#define CAMERA_MODEL_M5STACK_WIDE
#define CAMERA_MODEL_AI_THINKER

#include "camera_pins.h"

#define button 13
#define Virtbut 14
#define flashled 4

String camip;
void startCameraServer();

void blynkphoto()
{
  digitalWrite(flashled, HIGH);
  delay(200);
  Blynk.setProperty(V1, "urls", "http://"+camip+"/capture");
  digitalWrite(flashled, LOW);
}

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

  pinMode(flashled,OUTPUT);
  pinMode(button,INPUT_PULLUP);
  Serial.println();

  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;
  //init with high specs to pre-allocate larger buffers
  if(psramFound()){
    config.frame_size = FRAMESIZE_UXGA;
    config.jpeg_quality = 10;
    config.fb_count = 2;
  } else {
    config.frame_size = FRAMESIZE_SVGA;
    config.jpeg_quality = 12;
    config.fb_count = 1;
  }

#if defined(CAMERA_MODEL_ESP_EYE)
  pinMode(13, INPUT_PULLUP);
  pinMode(14, INPUT_PULLUP);
#endif

  // camera init
  esp_err_t err = esp_camera_init(&config);
  if (err != ESP_OK) {
    Serial.printf("Camera init failed with error 0x%x", err);
    return;
  }

  sensor_t * s = esp_camera_sensor_get();
  //initial sensors are flipped vertically and colors are a bit saturated
  if (s->id.PID == OV3660_PID) {
    s->set_vflip(s, 1);//flip it back
    s->set_brightness(s, 1);//up the blightness just a bit
    s->set_saturation(s, -2);//lower the saturation
  }
  //drop down frame size for higher initial frame rate
  s->set_framesize(s, FRAMESIZE_QVGA);

#if defined(CAMERA_MODEL_M5STACK_WIDE)
  s->set_vflip(s, 1);
  s->set_hmirror(s, 1);
#endif

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");

  startCameraServer();

  Serial.print("Camera Ready! Use 'http://");
  Serial.print(WiFi.localIP());
  camip = WiFi.localIP().toString();
  Serial.println("' to connect");
  Blynk.begin(auth, ssid, password, IPAddress(XXX.XXX.XXX.XXX), 8080);
}

void loop()  
{
  Blynk.run();
  if(digitalRead(button) == LOW)
    {; 
    Serial.println("Send Notification");
    Serial.println("Someone rang the bell");
    Serial.println("Getting a photo ");
    blynkphoto();
    }
  if(digitalRead(Virtbut) == HIGH)
    {
    Serial.println("Blynk virtual button pressed");
    Serial.println("Getting the Photo");
    blynkphoto();
    } 
}


The additions for Blynk

Let's have a look at the modifications I made for getting the standard CameraWebServer program.

#include "esp_camera.h"
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>

Next to the Wifi libraries the Blynk library must be included.

const char* ssid = "YOUR-ROUTERS-NAME";
const char* password = "PASSWORD";
char auth[] = "BLYNK AUTHENTIFICATION CODE";


You need to fill in your routers credentials and the Blynk authetification code you received from Blynk when you created the Blynk project.

#define button 13
#define Virtbut 14
#define flashled 4

Here the pins for the buttons are defined. The physical button is attached to GPIO pin 13 at the ESP32Cam just as the breadboard showed. The Virtual pin is attached to GPIO14. Next to that The ESP32 Cam's internal flash led is attached to GPIO 4. I am using the flashlight to brighten te pictures in my rather dark mancave.

String camip;

A String variable is defined in which the IP number of the camera is stored. This is later on going to be used to inform Blynk of the camera's IP number.

void blynkphoto()
{
  digitalWrite(flashled, HIGH);
  delay(200);
  Blynk.setProperty(V1, "urls", "http://"+camip+"/capture");
  digitalWrite(flashled, LOW);
}

This routine is called from the loop() when the physical button or the virtual button is pressed. The flashlight is set ON and thye program waits 200ms to be sure that the led is ON. Then the IP adress of the ESP32-Cam (campip as found in the setup) together with the command /capture. This activates the camera, makes the photo and sends it to the Image Gallery Which has as reference in Blynk V1.

If you do not need the flashlight just remove the digitalWrite(flashled, HIGH); and digitalWrite(flashled, LOW);
Another option is to comment these lines out. Then they are easier to edit when you might need the flashlight again.
You could also add another virtual button to Blynk to set the flashlight on or off.

To test wether taking a photograph works you can put the line:
http://XXX.XXX.XXX.XXX/capture in your browser where you have to replace the XXX.XXX.XXX.XXX with the IP number of the ESP32-Cam. That way you can check wether the ESP32Cam works ok and that the resolution is to your liking.

The cams settings can be altered by putting the ESP32-cam's IP number in your browser. The cams webpage will open and there you can alter all settings like resolution. So next to the Blynk connection the ESP-cam's webpage keeps on functioning. Even the streaming function works although that will be stopped for a split second when you take a picture with Blynk.  

  pinMode(flashled,OUTPUT);
  pinMode(button,INPUT_PULLUP);

These lines are added to the beginning of the setup() routine to set the ESP32 Cam's pins in the right mode.

  Serial.print("Camera Ready! Use 'http://");
  Serial.print(WiFi.localIP());
  camip = WiFi.localIP().toString();
  Serial.println("' to connect");

In these lines the program gets the local IP number of the cam. We will need this to tell Blynk at what IP number the photo to be send can be obtained.

  Blynk.begin(auth, ssid, password, IPAddress(XXX.XXX.XXX.XXX), 8080);

The last line has been added. This line gives all the information to connect to my local Blynk server. Replace the XXX's with the IP number of your own local Blynk server.

Blynk.begin(auth, ssid, pass);

If you do not have your own local Blynk server you can replace the line by the above line. This connects the ESP32 Cam to the cloud based Blynk server.

void loop() 
{

  Blynk.run();
  if(digitalRead(button) == LOW)
    {
    Serial.println("Someone rang the bell");
    Serial.println("Getting a photo ");
    blynkphoto();
    }
  if(digitalRead(Virtbut) == HIGH)
    {
    Serial.println("Blynk virtual button pressed");
    Serial.println("Getting the Photo");
    blynkphoto();
    } 
}

The loop tests wether the physical button on the ESP32-Cam (the doorbell buutton) is pressed. If that is true it calls the blynkphoto() routine that actually sends the photo.
Besides the physical button there is also the virtual button attached to GP14 whic is controlled from the Blynp App. If that is pressed there also will be a photo send to the app.

The doorbell in action

Well not really. This was a proof of concept and I wanted to test wether the ESP32Cam was capable of working as a doorbell. And it is. I the webcams webpage I set the resolution low so pictures would be transferred fast to Blynk. Better resolution is better quality.



Here is a sample picture I took from my 3D printer.

More options.

The doorbell is one way for using this. You could change the physical button by a PIR or a RCWL-0516 radar module to detect movement instead of pushing a button. Use velostat and define an IO pin as an analog input to detect pressure. Use these methods to detect who is stealing your cookies. Or build a bird feeding place that sends you a picture when a bird comes to dinner.
Re-read the story about using multiple ESP's with Blynk. With this information you can build a remote controlled car with a camera on it.

I bet you can come up with a lot of projects too. The price of the ESP32Cam should be no objection.

Till next time
have fun

Luc Volders