Esp32cam pan/tilt

I copy and past this so the dummies have a starting point.
I hope you like it.

esphome:

  name: camera_1

  friendly_name: Camera

esp32:

  board: esp32cam

  framework:

    type: arduino

# Enable logging

logger:

# Enable Home Assistant API

api:

  encryption:

    key: "??????????????"

ota:

wifi:

  ssid: !secret wifi_ssid

  password: !secret wifi_password

  manual_ip:

    static_ip: 192.168.1.87

    gateway: 192.168.1.1

    subnet: 255.255.255.0    

  ap:

    ssid: "Jj Fallback Hotspot"

    password: "???????????"    

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

  resolution: 640x480

  reset_pin: GPIO15

  name: Camera

  id: camera

  jpeg_quality: 10

  special_effect: none

  saturation: 0

  brightness: 1

  contrast: 0

  horizontal_mirror: True

  vertical_flip: False

  agc_mode: auto

  agc_gain_ceiling: 2x

  aec_mode: auto

  agc_value: 0

  wb_mode: auto

  test_pattern: False

  #on_stream_start (Optional, Automation):

  #on_stream_stop (Optional, Automation):  

servo:

  - id: pan_servo_1

    output: pan

    transition_length: 22s # this is for the speed lower is quicker

  - id: tilt_servo_1

    output: tilt  

    transition_length: 22s  

output:

  - platform: ledc

    pin: GPIO4

    channel: 2 # channel 1 is used for esp32_camera

    id: ledlamp

  - platform: ledc

    id: pan

    pin: GPIO13

    frequency: 50 Hz    

  - platform: ledc  

    pin: GPIO12

    #channel: 2 # channel 1 is used for esp32_camera

    frequency: 50 Hz

    id: tilt

number:

  - platform: template

    name: Led

    optimistic: true

    min_value: 0

    max_value: 100

    step: 1

    mode: slider

    on_value:

      then:

        lambda: 'id(ledlamp).set_level(x/100);'

  - platform: template

    name: Pan

    min_value: -100

    initial_value: 0

    max_value: 100

    step: 1

    mode: slider

    optimistic: true

    set_action:

      then:

        - servo.write:

            id: pan_servo_1

            level: !lambda 'return x / 100.0;'

  - platform: template

    name: Tilt

    min_value: -100

    initial_value: 0

    max_value: 100

    step: 10

    mode: slider

    optimistic: true

    set_action:

      then:

        - servo.write:

            id: tilt_servo_1

            level: !lambda 'return x / 100.0;'                

       

#mqtt:

#  broker: 192.168.1.44

#  username: !secret mqtt_username

#  password: !secret mqtt_passwort

#  birth_message:

#  will_message:

#  on_message:

#    - topic: led

#      payload: 'ON'

#      then:  

#      - output.set_level:

#        id: ledlamp

#        level: "100%"

#      - delay: 1000ms  

     
 

#This works for restarting the esp every 30 minutes:

time:

  - platform: sntp

    # ...

    on_time:

      # Every 30 minutes

      - minutes: 30

        then:

          - switch.toggle: restart_cam

switch:

  - platform: restart

    name: "Restart cam"

    id: restart_cam    

  - platform: gpio

    pin: GPIO32

    name: "Pan auto"

    id: pan_auto    

  - platform: gpio

    pin: GPIO33

    name: "Tilt auto"

    id: tilt_auto      

# camera webserver

esp32_camera_web_server:

  - port: 8080

    mode: stream

  - port: 8081

    mode: snapshot  

#### automation.yaml and sure the same for tilt change pan to tilt ########

#alias: Auto Pan Camera 1

#description: ""

#trigger:

#  - platform: state

#    entity_id:

#      - switch.camera_1_pan_auto

#    from: "off"

#    to: "on"

#    for:

#      hours: 0

#      minutes: 0

#      seconds: 1

#condition: []

#action:

#  - repeat:

#      sequence:

#        - device_id: a7e1d6ab0e735ede7c5e2f2c960e7ffe

#          domain: number

#          entity_id: d389ac3c7768143fdf89d5664484b8d2

#          type: set_value

#          value: -100

#        - delay:

#            hours: 0

#            minutes: 0

#            seconds: 22

#            milliseconds: 0

#        - device_id: 18891684c1237bb6cff2994830aaedbc

#          domain: number

#          entity_id: c495fcb21ef93d9503973d2289140b4f

#          type: set_value

#          value: 100

#        - delay:

#            hours: 0

#            minutes: 0

#            seconds: 22

#            milliseconds: 0

#      until:

#        - condition: state

#          entity_id: switch.camera_1_pan_auto

#          state: "off"

#          for:

#            hours: 0

#            minutes: 0

#            seconds: 1

#  - condition: state

#    entity_id: switch.camera_1_pan_auto

#    state: "off"

#    for:

#      hours: 0

#      minutes: 0

#      seconds: 1

#  - device_id: 18891684c1237bb6cff2994830aaedbc

#    domain: number

#    entity_id: c495fcb21ef93d9503973d2289140b4f

#    type: set_value

#    value: 0

#mode: single  

##################  

3 Likes

Apologies for being mentally slow :slight_smile: Is this potentially working code or a dreamed-up API “for the dummies to implement”? Thanks, Chris

You can use it like it is.

Nice! Any pictures from the finished build? Two servos with brackets and a esp32 cam - all that’s needed - correct?

Thanks so much. Will give it a try.

@indeed: I have ordered the adafruit mini pan/tilt and hopefully will be able to try this over the weekened. Will post something.

1 Like



1 Like

Awesome work @jan.mob!

Any comment on the servos used? Are the cheap/small ones (sg-9?) are strong enough for that task?

And is the source for your 3d printed case/aparatus freely available?

And the lens/camera doesn’t look like the default one? Is it a wide range one?

I used mg996r.

1 Like

i use this 3d but i stil need to change the way the bearings are mounted

Yes the lens is a wide one.
And i dont know if the smal sg are strong enough.I bought today 4 metalgear MG 996r for 3.45 euro the one so they are not that expensive also.
Good luck

1 Like