Metainformationen zur Seite
  •  

Dies ist eine alte Version des Dokuments!


ESP32-CAM Dokumentation

esp32cam.jpg

Übersicht

Das ESP32-CAM Board basiert auf dem ESP32-S Chip und bietet eine kostengünstige Lösung für Kameraanwendungen in IoT-Projekten. Es verfügt über einen OV2640 Kamerasensor und unterstützt verschiedene Betriebsmodi.

Hardware Eigenschaften

  • Mikrocontroller: ESP32-S
  • Kamera: OV2640 (2MP)
  • Flashlicht: Integrierte weiße LED
  • GPIO-Pins: Mehrere verfügbare Pins für Erweiterungen
  • Kommunikation: WLAN 2.4 GHz
  • Stromversorgung: 5V

Ersteinrichtung

Flashen des Boards

Das Board muss für die erste Programmierung in den Flash-Modus versetzt werden:

  1. Verbinden Sie das Board über einen USB-zu-TTL-Adapter (z.B. FTDI oder CP2102) mit Ihrem Computer
  2. Stellen Sie folgende Verbindungen her:
    • ESP32-CAM GND → Adapter GND
    • ESP32-CAM 5V/3.3V → Adapter 5V/3.3V (je nach Adapter)
    • ESP32-CAM U0R (RX) → Adapter TX
    • ESP32-CAM U0T (TX) → Adapter RX
  3. Verbinden Sie GPIO0 (IO0) mit GND, um den Flash-Modus zu aktivieren
  4. Drücken Sie kurz den RST-Button auf der Rückseite des Boards
  5. Führen Sie das Flashen mit Arduino IDE, ESPHome oder esptool durch
  6. Nach dem Flashen: Entfernen Sie die Verbindung zwischen GPIO0 und GND
  7. Drücken Sie erneut den RST-Button, um das Board neu zu starten

Integration in Home Assistant mit ESPHome

Basis-Konfiguration

substitutions:
  devicename: esp-cam
  upper_devicename: ESP32-Cam

esphome:
  name: $devicename
  friendly_name: $upper_devicename

esp32:
  board: esp32dev
  framework:
    type: arduino
 
# Aktiviere Logging
logger:
  level: WARN
 
# Externe Pakete einbinden
packages:
  wifi: !include packages/wifi.yaml
  mqtt: !include packages/mqtt.yaml
  ota:  !include packages/ota.yaml

Kamera-Konfiguration

esp32_camera:
  name: $upper_devicename
  id: my_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
  idle_framerate: 0.1fps
  max_framerate: 10fps
 
  # Bildeinstellungen
  contrast: 0
  special_effect: none
  # Belichtungseinstellungen
  aec_mode: auto
  aec2: false
  ae_level: 0
  aec_value: 300
  # Verstärkungseinstellungen
  agc_mode: auto
  agc_gain_ceiling: 2x
  agc_value: 0
  # Weißabgleich-Einstellung
  wb_mode: auto
  # Ausrichtung
  vertical_flip: False
  horizontal_mirror: False

Kamera-Konfiguration

esp32_camera:
  name: $upper_devicename
  id: my_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
  idle_framerate: 0.1fps
  max_framerate: 10fps
 
  # Bildeinstellungen
  contrast: 0
  special_effect: none
  # Belichtungseinstellungen
  aec_mode: auto
  aec2: false
  ae_level: 0
  aec_value: 300
  # Verstärkungseinstellungen
  agc_mode: auto
  agc_gain_ceiling: 2x
  agc_value: 0
  # Weißabgleich-Einstellung
  wb_mode: auto
  # Ausrichtung
  vertical_flip: False
  horizontal_mirror: False

Webserver und LED-Steuerung

# Kamera-Webserver
esp32_camera_web_server:
  - port: 80
    mode: stream
  - port: 81
    mode: snapshot

output:
  - platform: ledc
    pin: GPIO4
    id: gpio_4
    channel: 2
    frequency: 500Hz

light:
  # Für die weiße LED
  - platform: monochromatic
    output: gpio_4
    name: $upper_devicename Flash LED
  # Für die rote Status-LED
  - platform: status_led
    name: $upper_devicename Status LED
    id: cam_streaming
    pin:
      number: GPIO33
      inverted: true

WLAN-Konfiguration (Beispiel)

wifi:
  # Feste IP-Adresse (passen Sie diese an Ihr Netzwerk an)
  manual_ip:
    static_ip: 192.168.178.40
    gateway: 192.168.178.1
    subnet: 255.255.255.0
  # Alternative: Dynamische IP
  # ssid: "IHR_WLAN_NAME"
  # password: "IHR_WLAN_PASSWORT"

Code

esp32-cam-01.yaml
substitutions:
  devicename: esp-cam
  upper_devicename: ESP32-Cam

esphome:
  name: $devicename
  friendly_name: $upper_devicename

esp32:
  board: esp32dev
  framework:
    type: arduino
 
# Enable logging
logger:
  level: WARN

packages:
  wifi: !include packages/wifi.yaml
  mqtt: !include packages/mqtt.yaml
  ota:  !include packages/ota.yaml
 
# Enable Home Assistant API
# Für ESP32 Camera Component siehe: https://esphome.io/components/esp32_camera.html
# Für lambda siehe: https://github.com/esphome/esphome/pull/3090
api:
  services:
    # change camera parameters on-the-fly
    - service: camera_set_param
      variables:
        name: string
        value: int
      then:
        - lambda: |-
            bool state_return = false;
            if (("contrast" == name) && (value>= -2) && (value <= 2)) { id(my_camera).set_contrast(value); state_return = true; }
            if (("brightness" == name) && (value>= -2) && (value <= 2)) { id(my_camera).set_brightness(value); state_return = true; }
            if (("saturation" == name) && (value>= -2) && (value <= 2)) { id(my_camera).set_saturation(value); state_return = true; }
            if (("special_effect" == name) && (value>= 0U) && (value <= 6U)) { id(my_camera).set_special_effect((esphome::esp32_camera::ESP32SpecialEffect)value); state_return = true; }
            if (("aec_mode" == name) && (value>= 0U) && (value <= 1U)) { id(my_camera).set_aec_mode((esphome::esp32_camera::ESP32GainControlMode)value); state_return = true; }
            if (("aec2" == name) && (value>= 0U) && (value <= 1U)) { id(my_camera).set_aec2(value); state_return = true; }
            if (("ae_level" == name) && (value>= -2) && (value <= 2)) { id(my_camera).set_ae_level(value); state_return = true; }
            if (("aec_value" == name) && (value>= 0U) && (value <= 1200U)) { id(my_camera).set_aec_value(value); state_return = true; }
            if (("agc_mode" == name) && (value>= 0U) && (value <= 1U)) { id(my_camera).set_agc_mode((esphome::esp32_camera::ESP32GainControlMode)value); state_return = true; }
            if (("agc_value" == name) && (value>= 0U) && (value <= 30U)) { id(my_camera).set_agc_value(value); state_return = true; }
            if (("agc_gain_ceiling" == name) && (value>= 0U) && (value <= 6U)) { id(my_camera).set_agc_gain_ceiling((esphome::esp32_camera::ESP32AgcGainCeiling)value); state_return = true; }
            if (("wb_mode" == name) && (value>= 0U) && (value <= 4U)) { id(my_camera).set_wb_mode((esphome::esp32_camera::ESP32WhiteBalanceMode)value); state_return = true; }
            if (("test_pattern" == name) && (value>= 0U) && (value <= 1U)) { id(my_camera).set_test_pattern(value); state_return = true; }
            if (("vertical_flip" == name) && (value>= 0U) && (value <= 1U)) { id(my_camera).set_vertical_flip(value); state_return = true; }
            if (("horizontal_mirror" == name) && (value>= 0U) && (value <= 1U)) { id(my_camera).set_horizontal_mirror(value); state_return = true; }
            if (("jpeg_quality" == name) && (value>= 10U) && (value <= 63U)) { id(my_camera).set_jpeg_quality(value); state_return = true; }
 
            if (true == state_return) {
              id(my_camera).update_camera_parameters();
            }
            else {
              ESP_LOGW("esp32_camera_set_param", "Error in name or data range");
            }
wifi:
  manual_ip:
    # Set this to the IP of the ESP
    static_ip: 192.168.178.40
    # Set this to the IP address of the router. Often ends with .1
    gateway: 192.168.178.1
    # The subnet of the network. 255.255.255.0 works for most home networks.
    subnet: 255.255.255.0

captive_portal:

time:
  - platform: homeassistant
    id: esptime

esp32_camera:
  name: $upper_devicename
  id: my_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
  idle_framerate: 0.1fps
  max_framerate: 10fps
 
  # Image settings
  contrast: 0
  special_effect: none
  # exposure settings
  aec_mode: auto
  aec2: false
  ae_level: 0
  aec_value: 300
  # gain settings
  agc_mode: auto
  agc_gain_ceiling: 2x
  agc_value: 0
  # white balance setting
  wb_mode: auto
  # orientation
  vertical_flip: False
  horizontal_mirror: False

  on_stream_start:
    light.turn_on: cam_streaming
  on_stream_stop:
    light.turn_off: cam_streaming
 
# camera webserver
esp32_camera_web_server:
  - port: 80
    mode: stream
  - port: 81
    mode: snapshot

output:
  - platform: ledc
    pin: GPIO4
    id: gpio_4
    channel: 2
    frequency: 500Hz

light:
  # For the white LED.
  - platform: monochromatic
    output: gpio_4
    name: $upper_devicename Flash LED
  # For the red status LED.
  - platform: status_led
    name: $upper_devicename Status LED
    id: cam_streaming
    pin:
      number: GPIO33
      inverted: true

switch:
  - platform: restart
    name: $upper_devicename Restart

binary_sensor:
  - platform: status
    name: $upper_devicename Status

Kamera-Zugriff

Nach erfolgreicher Einrichtung können Sie auf die Kamera zugreifen:

Troubleshooting

Bekannte Probleme

  1. Niedrige Bildrate (FPS)
    • Mögliche Ursache: Interferenzen zwischen IO0 und der WLAN-Antenne
    • Lösung: Abschirmung des IO0-Pins mit antistatischer Folie oder durch Berührung
    • Alternativ: Positionierung des Boards entfernt von Störquellen
  2. Verbindungsprobleme
    • Überprüfen Sie die Stromversorgung (mindestens 5V/1A empfohlen)
    • Stellen Sie sicher, dass sich das Board in Reichweite des WLAN-Routers befindet
    • Versuchen Sie eine Firmware-Aktualisierung