ESP32-CAM as doorbell with camera Topic is solved

Moderator: leecollings

Post Reply
hoeby
Posts: 530
Joined: Saturday 02 June 2018 11:05
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.1
Location: Echt, Netherlands
Contact:

ESP32-CAM as doorbell with camera

Post by hoeby »

8-1-2021 Version V2.0.0 is ESP32-Doorbell is released.

But before getting to the link.
When you have a question about the code. Post it here or in the discussion tab on github, don't make an issue on github for it.
The issues tab is meant for when there is a real issue. Otherwise management is difficult.

When you want to build with us on the code. Do this on the "development" environment
Pulls to the master will be ignored.

Now getting to the beautifull part, the link.
https://github.com/Hoeby/ESP32-Doorbell


Original post
I wanted a doorbell with camera.
But it also needed to trigger a dummy in domoticz, so it could send a telegram message with image (or other things).

Of course there are wonderfull camera doorbells. But the cost a fortune. And they are not open-source.
A few years back a made something with a wemos-D1 and ESPeasy with a lot of extra pcb's .
It was big (12,5cm x 5cm). Needed something smaller and easier.

Then i found the ESP32-cam.
Bought one to experiment on it and liked it.
There are wonderfull programs online, but non was out of the box usefull for domoticz. The most common needed to start the program everytime. Easy to do settings in de left bar. But a lot of overhead which is not needed for a doorbell
Therefor i made my own program. The basic was the common program, and that i changed and added things.

What can my program do
- Stream live camera * http://IP --> for live stream
- Capture a image, needed for integration in domoticz * http://IP/capture --> for image capture, needed for domoticz
- Accepts http command, to activacted outputs on the ESP32-CAM * http://IP/ON or http://IP/OFF --> output LED ON and OFF
- push button input, which can activate output (button with LED) and send http to control IDX number

That are some functions.
But i also tried to putt all variables on top of the script.
So that people wo are not that familair with IDE, don't have to go tru the program, but can do all at the top of the program

Just to say, i am not a programmer. Just like it to do this stuff, but it could be that i made things that are not made by programmer rules.

For now i am making a drawing, to 3D print a housing for it.


Code: Select all

#include "esp_camera.h"
#include <WiFi.h>
#include "esp_timer.h"
#include "img_converters.h"
#include "Arduino.h"
#include "fb_gfx.h"
#include "soc/soc.h" //disable brownout problems
#include "soc/rtc_cntl_reg.h"  //disable brownout problems
#include "esp_http_server.h"
#include <HTTPClient.h>

//Invoeren van je network credentials
const char* ssid       = "WiFi-SSID";             //Wifi SSID waarop ESP32-cam zich moet aanmelden.
const char* password   = "WiFi-Password";      //Bijbehorend wachtwoord welke bij het gekozen SSID behoort.
//#define FixedIP                                   //Haal de // weg om fixed IP te gebruiken, anders staat deze op DHCP.
#define IPaddr            192,168,178,111        //Voer IP adres is, digits tussen komma's invoeren.
#define SubnetAddr        255,255,255,0           //Voer subnet mask in, digits tussen de komma's invoeren.
#define GatewayAddr       192,168,178,254         //Voer Gateway adres in, digits tussen de komma's invoeren. Er moet een gateway ingevoerd worden, of deze ook correct is, wordt niet gecontroleerd.    

//Invoeren van je domoticz credentials
String DomoticzIP      = "192.168.178.110";        //Domoticz Server IP adres.
String DomoticzPort    = "8080";                  //Domoticz Server poort adres.
String DomoticzIDX     = "1";                   //Domoticz IDX nummer welke geschakeld moet worden.
String DomoticzSwitch  = "On";                    //Domoticz IDX state. Bij activeren ingang, moet IDX dan On of Off gestuurd worden?.  
#define DomoticzResetSwitch                       //Domoticz, haal de // weg, dan wordt er een tegengesteld commando van DomoticzSwitch gestuurd nadat de ButtonTimer is verstreken.
                                                  //Voorbeeld als DomoticzResetSwitch defined is; Bij drukken gaat domoticz device op ON, na verstrijken ButtonTimer weer op OFF

//Invoeren van je push-button + LED credentials
const int buttonPin1 = 12;                        //GPIO nummer voor de ingang van drukknop.
const int ledPin1 =  13;                          //De tijd dat LED aangestuurd is. In deze tijd kan de ingang nog wel geactiveerd worden, maar programma doet er niets mee, totdat tijd verstreken is.
const int ledPin2 =  14;                          //De tijd dat LED aangestuurd is. In deze tijd kan de ingang nog wel geactiveerd worden, maar programma doet er niets mee, totdat tijd verstreken is.
const int ledPin3 =  15;                          //De tijd dat LED aangestuurd is. In deze tijd kan de ingang nog wel geactiveerd worden, maar programma doet er niets mee, totdat tijd verstreken is.
#define ButtonTimer      10000                    //De tijd dat LED aangestuurd is. In deze tijd kan de ingang nog wel geactiveerd worden, maar programma doet er niets mee, totdat tijd verstreken is.

int buttonState1 = 0;                              //Zet de ingang op 0, zodat deze niet direct actief is bij opstarten.

#define PART_BOUNDARY "123456789000000000000987654321"

// This project was tested with the AI Thinker Model, M5STACK PSRAM Model and M5STACK WITHOUT PSRAM
#define CAMERA_MODEL_AI_THINKER
//#define CAMERA_MODEL_M5STACK_PSRAM
//#define CAMERA_MODEL_M5STACK_WITHOUT_PSRAM

// Not tested with this model
//#define CAMERA_MODEL_WROVER_KIT

typedef struct {
        httpd_req_t *req;
        size_t len;
} jpg_chunking_t;

static size_t jpg_encode_stream(void * arg, size_t index, const void* data, size_t len){
    jpg_chunking_t *j = (jpg_chunking_t *)arg;
    if(!index){
        j->len = 0;
    }
    if(httpd_resp_send_chunk(j->req, (const char *)data, len) != ESP_OK){
        return 0;
    }
    j->len += len;
    return len;
}

#if defined(CAMERA_MODEL_WROVER_KIT)
  #define PWDN_GPIO_NUM    -1
  #define RESET_GPIO_NUM   -1
  #define XCLK_GPIO_NUM    21
  #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      19
  #define Y4_GPIO_NUM      18
  #define Y3_GPIO_NUM       5
  #define Y2_GPIO_NUM       4
  #define VSYNC_GPIO_NUM   25
  #define HREF_GPIO_NUM    23
  #define PCLK_GPIO_NUM    22

#elif defined(CAMERA_MODEL_M5STACK_PSRAM)
  #define PWDN_GPIO_NUM     -1
  #define RESET_GPIO_NUM    15
  #define XCLK_GPIO_NUM     27
  #define SIOD_GPIO_NUM     25
  #define SIOC_GPIO_NUM     23
  
  #define Y9_GPIO_NUM       19
  #define Y8_GPIO_NUM       36
  #define Y7_GPIO_NUM       18
  #define Y6_GPIO_NUM       39
  #define Y5_GPIO_NUM        5
  #define Y4_GPIO_NUM       34
  #define Y3_GPIO_NUM       35
  #define Y2_GPIO_NUM       32
  #define VSYNC_GPIO_NUM    22
  #define HREF_GPIO_NUM     26
  #define PCLK_GPIO_NUM     21

#elif defined(CAMERA_MODEL_M5STACK_WITHOUT_PSRAM)
  #define PWDN_GPIO_NUM     -1
  #define RESET_GPIO_NUM    15
  #define XCLK_GPIO_NUM     27
  #define SIOD_GPIO_NUM     25
  #define SIOC_GPIO_NUM     23
  
  #define Y9_GPIO_NUM       19
  #define Y8_GPIO_NUM       36
  #define Y7_GPIO_NUM       18
  #define Y6_GPIO_NUM       39
  #define Y5_GPIO_NUM        5
  #define Y4_GPIO_NUM       34
  #define Y3_GPIO_NUM       35
  #define Y2_GPIO_NUM       17
  #define VSYNC_GPIO_NUM    22
  #define HREF_GPIO_NUM     26
  #define PCLK_GPIO_NUM     21

#elif defined(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
#else
  #error "Camera model not selected"
#endif

static const char* _STREAM_CONTENT_TYPE = "multipart/x-mixed-replace;boundary=" PART_BOUNDARY;
static const char* _STREAM_BOUNDARY = "\r\n--" PART_BOUNDARY "\r\n";
static const char* _STREAM_PART = "Content-Type: image/jpeg\r\nContent-Length: %u\r\n\r\n";

httpd_handle_t stream_httpd = NULL;

static esp_err_t stream_handler(httpd_req_t *req){
  camera_fb_t * fb = NULL;
  esp_err_t res = ESP_OK;
  size_t _jpg_buf_len = 0;
  uint8_t * _jpg_buf = NULL;
  char * part_buf[64];

  res = httpd_resp_set_type(req, _STREAM_CONTENT_TYPE);
  if(res != ESP_OK){
    return res;
  }

  while(true){
    fb = esp_camera_fb_get();
    if (!fb) {
      Serial.println("Camera capture failed");
      res = ESP_FAIL;
    } else {
      if(fb->width > 400){
        if(fb->format != PIXFORMAT_JPEG){
          bool jpeg_converted = frame2jpg(fb, 80, &_jpg_buf, &_jpg_buf_len);
          esp_camera_fb_return(fb);
          fb = NULL;
          if(!jpeg_converted){
            Serial.println("JPEG compression failed");
            res = ESP_FAIL;
          }
        } else {
          _jpg_buf_len = fb->len;
          _jpg_buf = fb->buf;
        }
      }
    }
    if(res == ESP_OK){
      size_t hlen = snprintf((char *)part_buf, 64, _STREAM_PART, _jpg_buf_len);
      res = httpd_resp_send_chunk(req, (const char *)part_buf, hlen);
    }
    if(res == ESP_OK){
      res = httpd_resp_send_chunk(req, (const char *)_jpg_buf, _jpg_buf_len);
    }
    if(res == ESP_OK){
      res = httpd_resp_send_chunk(req, _STREAM_BOUNDARY, strlen(_STREAM_BOUNDARY));
    }
    if(fb){
      esp_camera_fb_return(fb);
      fb = NULL;
      _jpg_buf = NULL;
    } else if(_jpg_buf){
      free(_jpg_buf);
      _jpg_buf = NULL;
    }
    if(res != ESP_OK){
      break;
    }
    //Serial.printf("MJPG: %uB\n",(uint32_t)(_jpg_buf_len));
  }
  return res;
}


static esp_err_t ON_handler(httpd_req_t *req){
        #define OKon "HTTP/1.1 200 OK, Output On"
        httpd_resp_send(req, OKon, strlen(OKon));  // Response body can be empty
//        Serial.println("TEST ON"); 
        GpioVarOn();
        }  

static esp_err_t OFF_handler(httpd_req_t *req){
        #define OKoff "HTTP/1.1 200 OK, Output OFF"
        httpd_resp_send(req, OKoff, strlen(OKoff));  // Response body can be empty
//        Serial.println("TEST OFF"); 
        GpioVarOff();
        }  

static esp_err_t capture_handler(httpd_req_t *req){
    camera_fb_t * fb = NULL;
    esp_err_t res = ESP_OK;
    int64_t fr_start = esp_timer_get_time();

    fb = esp_camera_fb_get();
    if (!fb) {
        Serial.println("Camera capture failed");
        httpd_resp_send_500(req);
        return ESP_FAIL;
    }

    httpd_resp_set_type(req, "image/jpeg");
    httpd_resp_set_hdr(req, "Content-Disposition", "inline; filename=capture.jpg");

    size_t out_len, out_width, out_height;
    uint8_t * out_buf;
    bool s;
    {
        size_t fb_len = 0;
        if(fb->format == PIXFORMAT_JPEG){
            fb_len = fb->len;
            res = httpd_resp_send(req, (const char *)fb->buf, fb->len);
        } else {
            jpg_chunking_t jchunk = {req, 0};
            res = frame2jpg_cb(fb, 80, jpg_encode_stream, &jchunk)?ESP_OK:ESP_FAIL;
            httpd_resp_send_chunk(req, NULL, 0);
            fb_len = jchunk.len;
        }
        esp_camera_fb_return(fb);
        int64_t fr_end = esp_timer_get_time();
        Serial.printf("JPG: %uB %ums\n", (uint32_t)(fb_len), (uint32_t)((fr_end - fr_start)/1000));
        return res;
    }
}

void startCameraServer(){
  httpd_config_t config = HTTPD_DEFAULT_CONFIG();
  config.server_port = 80;

  httpd_uri_t index_uri = {
    .uri       = "/",
    .method    = HTTP_GET,
    .handler   = stream_handler,
    .user_ctx  = NULL
  };

  httpd_uri_t capture_uri = {
    .uri       = "/capture",
    .method    = HTTP_GET,
    .handler   = capture_handler,
    .user_ctx  = NULL
  };

  httpd_uri_t ON_uri = {
    .uri       = "/ON",
    .method    = HTTP_GET,
    .handler   = ON_handler,
    .user_ctx  = NULL
  };

  httpd_uri_t OFF_uri = {
    .uri       = "/OFF",
    .method    = HTTP_GET,
    .handler   = OFF_handler,
    .user_ctx  = NULL
  };

  //Serial.printf("Starting web server on port: '%d'\n", config.server_port);
  if (httpd_start(&stream_httpd, &config) == ESP_OK) {
    httpd_register_uri_handler(stream_httpd, &index_uri);
    httpd_register_uri_handler(stream_httpd, &ON_uri);  //voor LED ON
    httpd_register_uri_handler(stream_httpd, &OFF_uri); //voor LED OFF
    httpd_register_uri_handler(stream_httpd, &capture_uri); //Still image capture
  }
}

void setup() {
  WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); //disable brownout detector

  // initialize the LED pin as an output:
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin1, INPUT_PULLUP);

  Serial.begin(115200);
  Serial.setDebugOutput(false);
  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; 
  
  if(psramFound()){
    config.frame_size = FRAMESIZE_SVGA;
    config.jpeg_quality = 10;
    config.fb_count = 2;
  } else {
    config.frame_size = FRAMESIZE_VGA;
    config.jpeg_quality = 12;
    config.fb_count = 1;
  }
  
  // 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;
  }
  // Wi-Fi connection
  WiFi.begin(ssid, password);
  
  #if defined(FixedIP)
    IPAddress ip(IPaddr);   
    IPAddress gateway(GatewayAddr);   
    IPAddress subnet(SubnetAddr);   
    WiFi.config(ip, gateway, subnet);
  #endif

  delay(5000);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  
  Serial.print("Camera Stream Ready! Go to: http://");
  Serial.print(WiFi.localIP());
  
  // Start streaming web server
  startCameraServer();
}

void loop() {
  buttonState1 = digitalRead(buttonPin1);
    
  if (buttonState1 == LOW) {
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, HIGH);
    digitalWrite(ledPin3, HIGH);
    Serial.println("Start Button delay");   

    HTTPClient http;   
    String url="http://" + DomoticzIP + ":" + DomoticzPort + "/json.htm?type=command&param=switchlight&idx=" + DomoticzIDX + "&switchcmd=" + DomoticzSwitch + "";
    http.begin(url);  //Specify destination for HTTP request
    http.addHeader("Content-Type", "text/plain");             //Specify content-type header
    int httpResponseCode = http.POST("POSTING from ESP32");   //Send the actual POST request
  
    delay(ButtonTimer);

    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW);
    Serial.println("Start Button end"); 

    #if defined(DomoticzResetSwitch)
        if (DomoticzSwitch == "On") { 
            String url="http://" + DomoticzIP + ":" + DomoticzPort + "/json.htm?type=command&param=switchlight&idx=" + DomoticzIDX + "&switchcmd=Off";
            http.begin(url);  //Specify destination for HTTP request
            http.addHeader("Content-Type", "text/plain");             //Specify content-type header
            int httpResponseCode = http.POST("POSTING from ESP32");   //Send the actual POST request
        } else {
            String url="http://" + DomoticzIP + ":" + DomoticzPort + "/json.htm?type=command&param=switchlight&idx=" + DomoticzIDX + "&switchcmd=On";
            http.begin(url);  //Specify destination for HTTP request
            http.addHeader("Content-Type", "text/plain");             //Specify content-type header
            int httpResponseCode = http.POST("POSTING from ESP32");   //Send the actual POST request
        }    
    #endif 
    
  }
}

// LED uitgang aan commando: http://ESP32-CAM-IP/ON
void GpioVarOn() {
   Serial.println("Output On");   
   digitalWrite(ledPin1, HIGH);
 }

// LED uitgang aan commando: http://ESP32-CAM-IP/OFF
void GpioVarOff() {
   Serial.println("Output Off");   
   digitalWrite(ledPin1, LOW);
 }
Last edited by hoeby on Friday 08 January 2021 20:25, edited 3 times in total.
Thin-client --> Docker Domoticz main environment
Pi3A+ --> Google home (GAssistPi)
Pi3B+ --> Docker (P1monitor, Domoticz test environment, Ubiquity controller)
peerkersezuuker
Posts: 70
Joined: Monday 14 December 2015 22:16
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Mierlo
Contact:

Re: ESP32-CAM as doorbell with camera

Post by peerkersezuuker »

Hi Hoeby

I am very interested.
Eager to see this in a housing in action.

Regards
Peer
hoeby
Posts: 530
Joined: Saturday 02 June 2018 11:05
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.1
Location: Echt, Netherlands
Contact:

Re: ESP32-CAM as doorbell with camera

Post by hoeby »

I already made a prototype of the housing. But that was a quick one. But the dimensions should be the same.
I made an offset in the front, to make a plate in front of it. That's why the 3D print looks awful, because the support is not the best to print on.

Some idea about the size, next to a normal doorbell
A little bit bigger, but a lot smaller than those expensive doorbells. Of course without audio, but do you need it?
The wood behind the housing is to close it and test it, if it stays working and doesn't overheat.
The 3x screws are now for holding the wooden plate, but in the future are these for mounting. After that a 1mm thick plate will be placed on top whit double side tape.

Why do i have that many cables undernead?
2 are for power (5Vdc + en -
2 are for connecting the normal doorbell chime. This is a wish from my wife. This is made with 2x solid state relay. This will not be default in the design.
1 is the antenne, i orded the wrong esp32-cam with external antenne. There is also 1 with pcb antenna
Image

Mounted is a button with RGB LED in it.
Unfortunally not a WS2812 LED. Tried WS2812, but because of the cam streaming i couldn't get it working nicely. Sometimes data to the WS2812 LED was not correct and the LED started flashing. Another thing, a WS2812 pushbutton is homemade design, this button is just how i boughted it.

This is the standby state
What is on the camera lens, that's dust
Image

This is button pushed state. You could self choose how long this light has to be before it switches back to standby
But be prepared, when the button pushed light is active, the button input doesn't respond untill back to standby.
What is on the camera lens, that's dust
Image

In Domoticz i have a dzvents script which sends telegram image.
This has nothing to do with the ESP32-cam, only that is switches the dummy button
Image
Thin-client --> Docker Domoticz main environment
Pi3A+ --> Google home (GAssistPi)
Pi3B+ --> Docker (P1monitor, Domoticz test environment, Ubiquity controller)
peerkersezuuker
Posts: 70
Joined: Monday 14 December 2015 22:16
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Mierlo
Contact:

Re: ESP32-CAM as doorbell with camera

Post by peerkersezuuker »

Hoeby,
Looks very promising.
The external doorbell chime is no need for me ;-)
I see you have 5v dc power leads (of course) but would it be possible to enclose a 8v ac to 5v dc converter in the housing ?
Because i want to power my doorbel from the original 8v ac power.

(Voor de rest chapeau !!)

regard's
Peer
hoeby
Posts: 530
Joined: Saturday 02 June 2018 11:05
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.1
Location: Echt, Netherlands
Contact:

Re: ESP32-CAM as doorbell with camera

Post by hoeby »

The 8vac to 5vdc is also on my wish list.
But first have to find something that can convert the 8vac to 5vdc and also be small to fit in the housing

8vac converting to vdc. Then the vdc voltage will be +/-11,2vdc
problem is not only to convert to dc, but also stepdown to 5vdc. that all needs to be in this housing.

@Peer, would it be an option that you have a 8vac to 5vdc near your doorbell supply and send the 5vdc over the original cable?

Greetings Paul
Thin-client --> Docker Domoticz main environment
Pi3A+ --> Google home (GAssistPi)
Pi3B+ --> Docker (P1monitor, Domoticz test environment, Ubiquity controller)
freijn
Posts: 536
Joined: Friday 23 December 2016 16:40
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: Netherlands Purmerend
Contact:

Re: ESP32-CAM as doorbell with camera

Post by freijn »

Great idea !

I will upload the code to the ESP and give it a try.

THanks for all the work !
hoeby
Posts: 530
Joined: Saturday 02 June 2018 11:05
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.1
Location: Echt, Netherlands
Contact:

Re: ESP32-CAM as doorbell with camera

Post by hoeby »

Sorry, forgot to mention.
The housing wil be designed to the 120 or 160 degree camera. Not the small 90 degree camera
Thin-client --> Docker Domoticz main environment
Pi3A+ --> Google home (GAssistPi)
Pi3B+ --> Docker (P1monitor, Domoticz test environment, Ubiquity controller)
hoeby
Posts: 530
Joined: Saturday 02 June 2018 11:05
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.1
Location: Echt, Netherlands
Contact:

Re: ESP32-CAM as doorbell with camera

Post by hoeby »

Today i made the start with the new housing.
But i can't finish it yet. Wanting on some new parts, which i need to measure, before i can finish the drawing.
Also looking for the option to add the VAC -> VDC converter, don't know how to solve that.

The drawing idea is made with black housing and aluminium cover.
Some asked to get a copy of the drawings, this is no problem, but than i need to be sure that things could fit. Be patience
Image
Thin-client --> Docker Domoticz main environment
Pi3A+ --> Google home (GAssistPi)
Pi3B+ --> Docker (P1monitor, Domoticz test environment, Ubiquity controller)
azonneveld
Posts: 157
Joined: Wednesday 02 October 2019 7:37
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.4
Location: Netherlands
Contact:

Re: ESP32-CAM as doorbell with camera

Post by azonneveld »

hoeby wrote: Tuesday 05 May 2020 16:48 Also looking for the option to add the VAC -> VDC converter, don't know how to solve that.
You can use the existing wiring from the doorbell. Disconnect it from the 24VAC doorbell adapter and put 5v on it. This will save you money and effort.
Last edited by azonneveld on Friday 08 May 2020 8:21, edited 1 time in total.
rpi4 - zigbee2mqtt - roborock - espeasy - rfxcom - homewizard p1 - otgw - homebridge - surveillance station - egardia - goodwe - open weather map - wol - BBQ detection - rsync backup
azonneveld
Posts: 157
Joined: Wednesday 02 October 2019 7:37
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.4
Location: Netherlands
Contact:

Re: ESP32-CAM as doorbell with camera

Post by azonneveld »

Nice project!

I have it running for a couple of months now.
Works good most of the time, but when the wifi connection is lost, the unit crashes. In that case a power cycle is needed.
I have put a system-alive-checker from domoticz to the doorbell to detect if the unit is crashed. On a crash, a telegram notofication is send.

First I had the unit equipped using the standard camera module, but the viewing angle was too small, so I replaced it by the wide angle version.
Another benefit of the wide angle camera is that the camera is perfectly round, making it easy to fit the camera into the enclosure. I applied some glue on the back of the camera, to make it waterproof.

Inside the enclosure, a bag of silicagel is placed, this absorbs moisture.

Image

housing 83x58x33:
https://www.aliexpress.com/item/32993502008.html
(included screws are not stainless, you need to replace them)

wide angle camera
https://www.aliexpress.com/item/4000130638929.html

Button (5v 19mm):
https://www.aliexpress.com/item/33004311192.html

Script to send image by telegram:

Code: Select all

#!/bin/sh
SnapFile="/var/tmp/camsnapshot_deur.jpg"

# Get image doorbel
wget  -O $SnapFile "http://<doorbel-IP>/capture"
    
# Send Telegram message with image
curl -s -X POST "https://api.telegram.org/<bot id>/sendPhoto" -F chat_id=<chat id> -F photo="@$SnapFile"

# Remove Image
/bin/rm $SnapFile

exit 0
Code for ESP32 Cam: https://www.dropbox.com/s/2ock4a688zxhj ... a.zip?dl=1
The button is lit by default.
when pressed:
1. Notification is send to domoticz
2. Start flashing for a couple of seconds (also helps prevent multiple presses)
3. Domoticz fetches an image from the camera

By the way, this only works if you don't need your original doorbell ringer to work.
I have used a KAKU doorbell (https://www.klikaanklikuit.nl/nl/produc ... urbel.html)
Domoticz sends the command to the KAKU doorbell (using the RFXcom) to make the sound.
A benefit of this setup is, that you can schedule when the sound is allowed in domoticz. Photos and notification are always send using telegram.
Last edited by azonneveld on Monday 21 December 2020 10:49, edited 3 times in total.
rpi4 - zigbee2mqtt - roborock - espeasy - rfxcom - homewizard p1 - otgw - homebridge - surveillance station - egardia - goodwe - open weather map - wol - BBQ detection - rsync backup
supermarcie
Posts: 3
Joined: Thursday 07 May 2020 22:26
Target OS: Linux
Domoticz version:
Contact:

Re: ESP32-CAM as doorbell with camera

Post by supermarcie »

Hi,

never used telegram.
Where do I need to put the scipt to send something with telegram?
azonneveld
Posts: 157
Joined: Wednesday 02 October 2019 7:37
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.4
Location: Netherlands
Contact:

Re: ESP32-CAM as doorbell with camera

Post by azonneveld »

supermarcie wrote: Friday 08 May 2020 9:08 Hi,

never used telegram.
Where do I need to put the scipt to send something with telegram?
First I suggest you get Telegram running from the notifications in domoticz. It is quit a struggle to make your own telegram bot.
Here is something that might be helpful: https://www.domoticz.com/wiki/Telegram_Bot

Next, when you get bot running, you can place the script in fi the domoticz script folder.
Then, you can call it using a dummy switch. on the dummy switch, put on the on command: "script:///home/pi/domoticz/scripts/deurbel.sh"
rpi4 - zigbee2mqtt - roborock - espeasy - rfxcom - homewizard p1 - otgw - homebridge - surveillance station - egardia - goodwe - open weather map - wol - BBQ detection - rsync backup
hoeby
Posts: 530
Joined: Saturday 02 June 2018 11:05
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.1
Location: Echt, Netherlands
Contact:

Re: ESP32-CAM as doorbell with camera

Post by hoeby »

Nice doorbell you made.
azonneveld wrote: Friday 08 May 2020 7:59 I have it running for a couple of months now.
Works good most of the time, but when the wifi connection is lost, the unit crashes. In that case a power cycle is needed.
I have put a system-alive-checker from domoticz to the doorbell to detect if the unit is crashed. On a crash, a telegram notofication is send.
Did you tried to add a device reboot when wifi is lost?
I also have some ESPeasy devices running. These had the some problem, after wifi lost they stopped working.
In ESPeasy i made this script, which prevent me to do the power cycle.

Want to add this to the doorbell script. But need to figger out how to add it to IDE code and not with ESPeasy
On wifi_disconnected it start a timer of 3 minutes. When wifi comes back, the timer is canceled.
If wifi is not back in 3 minutes, than reboot will follow

Code: Select all

on System#Boot do
  Let,1,0                                // set user variable to initial state of 0         
endon

On Wifi#Disconnected do 
  if [VAR#1]=0
  timerSet,1,180
  Let,1,1                                // set user variable 1 to 1 to prevent re-set of var1 on each event.
  endif
endon

on Wifi#Connected do     	 	 // if wifi returns, cancel reboot
  timerSet,1,0                    	 // cancel timer
  Let,1,0                                // set user variable 1 to 0
endon

On Rules#Timer=1 do
  reboot
endon 
Thin-client --> Docker Domoticz main environment
Pi3A+ --> Google home (GAssistPi)
Pi3B+ --> Docker (P1monitor, Domoticz test environment, Ubiquity controller)
azonneveld
Posts: 157
Joined: Wednesday 02 October 2019 7:37
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.4
Location: Netherlands
Contact:

Re: ESP32-CAM as doorbell with camera

Post by azonneveld »

hoeby wrote: Friday 08 May 2020 16:42 Did you tried to add a device reboot when wifi is lost?
Basically I did that, when the ping is not active, domoticz sends a command to another esp8266, which cuts the power to the doorbell.
I have the luxury that another esp8266 is near the powercord of the doorbell, so I added a powercut function to that esp8266 using a NC relays.
I did not manage/try to integrate the reboot function into the doorbel software.
rpi4 - zigbee2mqtt - roborock - espeasy - rfxcom - homewizard p1 - otgw - homebridge - surveillance station - egardia - goodwe - open weather map - wol - BBQ detection - rsync backup
hoeby
Posts: 530
Joined: Saturday 02 June 2018 11:05
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.1
Location: Echt, Netherlands
Contact:

Re: ESP32-CAM as doorbell with camera

Post by hoeby »

That is also a solution to reboot the doorbell.
I will have a look to make it in the script. But how to test could be an issue. Don't want to disable enable my wifi to test it. Maybe need to configure a router for a second temporary network, to test it.

Hou script has a nice feature. The flashing button. Now i have a rgb button, but finding a small rgb button which could fit was not that easy. By letting flashing the light is a nice feature. Are you oke if i copy that part?
Thin-client --> Docker Domoticz main environment
Pi3A+ --> Google home (GAssistPi)
Pi3B+ --> Docker (P1monitor, Domoticz test environment, Ubiquity controller)
azonneveld
Posts: 157
Joined: Wednesday 02 October 2019 7:37
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.4
Location: Netherlands
Contact:

Re: ESP32-CAM as doorbell with camera

Post by azonneveld »

hoeby wrote: Saturday 09 May 2020 8:19 ...The flashing button..... Are you oke if i copy that part?
Thank you! And of course you can use that code. Or maybe improve it. I think my implementation is quite basic...
rpi4 - zigbee2mqtt - roborock - espeasy - rfxcom - homewizard p1 - otgw - homebridge - surveillance station - egardia - goodwe - open weather map - wol - BBQ detection - rsync backup
JediMax
Posts: 14
Joined: Tuesday 23 April 2019 14:35
Target OS: Linux
Domoticz version: 2020.2
Location: Kyiv, Ukraine
Contact:

Re: ESP32-CAM as doorbell with camera

Post by JediMax »

azonneveld wrote: Friday 08 May 2020 7:59 Nice project!

I have it running for a couple of months now.
Works good most of the time, but when the wifi connection is lost, the unit crashes.
...
Hallo!
I add to general code (to the and of loop) follow

Code: Select all

 if ( WiFi.status() ==  WL_CONNECTED ) 
  {
    // WiFi is UP,  do what ever
  } else
  {
    // wifi down, reconnect here
   WiFi.begin(  );
    int WLcount = 0;
    while (WiFi.status() != WL_CONNECTED && WLcount < 200 ) 
    {
      delay( 100 );
         Serial.printf(".");
         if (UpCount >= 60)  // just keep terminal from scrolling sideways
         {
            UpCount = 0;
               Serial.printf("\n");
         }
         ++UpCount;
      ++WLcount;
    }
  }
 // END loop()
and

Code: Select all

int UpCount=0;
to the declare section.
Now ESP32 reconnect to WiFi when lost connection. It's working good 'on the table' :-)
hoeby
Posts: 530
Joined: Saturday 02 June 2018 11:05
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.1
Location: Echt, Netherlands
Contact:

Re: ESP32-CAM as doorbell with camera

Post by hoeby »

Thanks JediMax for your information.
I used it for a start, but needed to change some rows to get it good working in my script.

What have i done today, all tested and works
- Added option that the user can choose between rgb of single led button. When button pushed: for RGB the button works with led colour change, the single led start flashing. (thanks to azonneveld).

- wifi lost and automatic reconnection when wifi signal is back (thanks tot the start JediMax). When wifi is lost the button led starts flashing in a specific way (not like when pushed). So you could see from the outside that wifi signal is gone.
When having a live stream from the camera, you have to refresh the browser, because stream was gone. Could be a problem with dashticz, need to test with integrated to domoticz what happens, that i was forgotten to test

What is on the wish list:
- add flashing led on wifi lost is now only working when it was once connect to your wifi and after that a wifi connection is lost. Want to look to add also for start up.

- add mqtt messaging. Now it works with http json command to domoticz. Want to have both messaging options, that the user can choose. Not everyone uses mqtt.

- need to make the program in modules. That on changing things i am not working in a main program, but in a smaller section of the program
Thin-client --> Docker Domoticz main environment
Pi3A+ --> Google home (GAssistPi)
Pi3B+ --> Docker (P1monitor, Domoticz test environment, Ubiquity controller)
JediMax
Posts: 14
Joined: Tuesday 23 April 2019 14:35
Target OS: Linux
Domoticz version: 2020.2
Location: Kyiv, Ukraine
Contact:

Re: ESP32-CAM as doorbell with camera

Post by JediMax »

It's not working for some long time. Day or two and device will not responding.
Can you share your current code for device?
hoeby
Posts: 530
Joined: Saturday 02 June 2018 11:05
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.1
Location: Echt, Netherlands
Contact:

Re: ESP32-CAM as doorbell with camera

Post by hoeby »

The code is not that fare finished, that i can share it.
It is different with the code from the first message, first need to finish some things. Also like your experiance, because i changed the code i first want to test it for a few days, before sharing the code.
Thin-client --> Docker Domoticz main environment
Pi3A+ --> Google home (GAssistPi)
Pi3B+ --> Docker (P1monitor, Domoticz test environment, Ubiquity controller)
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests