ESP32-CAM: Integrating a Tiny Surveillance Camera with Home Assistant

Integrate ESP32-CAM with Home Assistant for a cost-effective, customizable home security solution. No external cloud services; control privacy and functionality.

Let's explore the process of integrating an ESP32-CAM with Home Assistant. This guide is aimed at those who are interested in adding a smart camera functionality to their home automation setup.

The ESP32-CAM is a compact, low-cost microcontroller board featuring a camera module. It's part of the ESP32 family, known for its Wi-Fi and Bluetooth capabilities. The "CAM" in its name indicates its most prominent feature: an onboard camera, making it suitable for a wide range of applications involving photography or video streaming.

Why?

  • Enhanced Home Security: By streaming live video, the ESP32 CAM can function as a security camera, letting you monitor your home remotely. Please note that this approach does not save any footage, it is only used for live view. To set up a proper CCTV and NVR solution, take a look at this post.
  • Cost-Effective Solution: Compared to commercial smart cameras, the ESP32 CAM is much more affordable, providing a budget-friendly way to add surveillance to your home.
  • Customization and Flexibility: The module is considerably smaller than a CCTV camera, and it is suitable for discreet surveillance applications in various settings, such as monitoring specific rooms in a home or keeping an eye on pets. The image (OS/configuration) created for the ESP32 is completely customizable, ensuring it only does what you instruct it to do.
  • Privacy Control: There are no external cloud services involved in this setup and data is not being recorded anywhere.

This guide demonstrates the use of an ESP32-CAM module, specifically the version with the ESP32-CAM-MB development board attached. It covers controlling the onboard LED and accessing the camera stream through Home Assistant.

Getting Started with ESPHome and Home Assistant

This guide assumes Home Assistant is already set up and the ESPHome plugin is installed.

  1. Create a New Device in ESPHome: In Home Assistant, go to ESPHome and create a new device. Enter a name for your device and select ESP32 as your board.
  2. Configuring ESP32 Cam: Skip the installation after selecting your board. Instead, edit the configuration. This configuration includes essential components such as WiFi settings, OTA updates, and basic camera settings, such as the HTTP stream and controlling the onboard LED:
esphome:

  name: TestCam
  friendly_name: Test Cam

esp32:
  board: esp32dev
  framework:
    type: arduino

logger:

api:
  encryption:
    key: "yynsHmZsR4TwfqwFR2a9D2xaEbkhBwi2=dTg4MDRqtsesMqZ7oBe"

ota:
  password: "acYD5z97Gy4zq8oiUV2FvWowcmmy9eiiMA9hRenhc9ENH4qnMBn"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  ap:
    ssid: "Test Fallback Hotspot"
    password: "giopnajpnijwnIJNAPS"

captive_portal:

esp32_camera:
  external_clock:
    pin: GPIO0
    frequency: 20MHz

i2c_pins:
    sda: GPIO26
    scl: GPIO27
  data_pins: [GPIO5, GPIO18, GPIO19, GPIO21, GPIO36, GPIO39, GPIO34, GPIO35]
  vsync_pin: GPIO25
  href_pin: GPIO23
  pixel_clock_pin: GPIO22
  power_down_pin: GPIO32

name: Test Camera

esp32_camera_web_server:
  - port: 8080
    mode: stream
  - port: 8081
    mode: snapshot

output:
  - platform: gpio
    pin: GPIO4
    id: gpio_4

light:
  - platform: binary
    output: gpio_4
    name: flashlight
  1. Compiling and Uploading the Firmware: Click ‘Install’ and then ‘Manual Download’ in ESPHome. This step will compile the binary image for your ESP32 Cam. Save the .bin file in the Modern Format.
  2. Flashing the ESP32 Cam: Go to https://web.esphome.io/. While holding the IO0 button on your ESP32, connect it to your computer. Follow the website's instructions to connect your device and flash the .bin image.
  3. Setting Up the Camera: Once flashed, find the IP address of your ESP32 Cam and set a static IP for it. Test the stream by visiting http://<Your-ESP32-IP>:8080 from your browser. Create a new rule in your router's firewall settings to restrict the device's network traffic exclusively to Home Assistant.

Home Assistant Integration

Now that your ESP32 Cam is set up and streaming, it's time to integrate it with Home Assistant:

  1. Discovering the Camera in Home Assistant: Navigate to 'Settings' > 'Devices & Services'. Home Assistant should automatically discover your new ESP32 cam.
  2. Adding the Camera to the Dashboard: To add the camera to your Home Assistant dashboard, use the following Lovelace card. This setup allows you to view the live feed from your camera and includes a tap action to toggle a the built-in LED light:
show_state: false
show_name: true
camera_view: live
type: picture-entity
entity: camera.test_camera
name: Test Cam
tap_action:
  action: call-service
  service: light.toggle
  target:
    entity_id: light.test_flashlight
  data: {}
show_state: false
show_name: true
camera_view: live
type: picture-entity
entity: camera.test_camera
name: Test Cam
tap_action:
  action: call-service
  service: light.toggle
  target:
    entity_id: light.test_flashlight
  data: {}

A Note on Limitations

The ESP32 Cam has its limitations, particularly when trying to integrate with more advanced systems like Frigate. The ESP32 Cam streams in MJPEG format, which is not directly compatible with systems that require an H264 stream. Converting the stream is challenging and often results in unstable results. For more information on this issue, you can refer to this GitHub issue.

Another limitation is ESPHome not supporting SD cards for saving footage or images. For my use case (keeping an eye on pets), none of the above matters as I only needed a live feed, but for a more serious security camera setup, check out this guide.

Conclusion

Setting up an ESP32 Cam with Home Assistant is an extremely straightforward and cost-effective solution. It's also a great way to enhance your home's security and automation capabilities. Happy tinkering!