The Spy Car

I made a little remote controlled spy-car running on an ESP32Cam. It is remotely controlled with a separate ESP32 with buttons. It’s 3D-printed, but mostly built around a cheap USB-battery bank. This was more than anything a test running ESPHome with a motor controller. It’s got some power issues, may need a bigger capacitor somewhere.


Parts:

STL-files here: https://www.thingiverse.com/thing:4744538

ESPHome config:

 esphome:
  name: spycam
  platform: ESP32
  board: esp32cam

wifi:
  ssid: "SSID"
  password: "PASSWORD"

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Spycam Fallback Hotspot"
    password: "Password"

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:
  services:
  # Motor A #
    - service: control_motor_a_left
      variables:
        level: int
      then:
        - output.turn_on: motorAleft
        - output.turn_off: motorAright
    - service: control_motor_a_right
      variables:
        level: int
      then:
        - output.turn_off: motorAleft
        - output.turn_on: motorAright
    - service: control_motor_a_stop
      variables:
        level: int
      then:
        - output.turn_off: motorAleft
        - output.turn_off: motorAright
  # Motor B #
    - service: control_motor_b_left
      variables:
        level: int
      then:
        - output.turn_on: motorBleft
        - output.turn_off: motorBright
    - service: control_motor_b_right
      variables:
        level: int
      then:
        - output.turn_off: motorBleft
        - output.turn_on: motorBright
    - service: control_motor_b_stop
      variables:
        level: int
      then:
        - output.turn_off: motorBleft
        - output.turn_off: motorBright
ota:

esp32_camera:
  name: doorcam
  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
  jpeg_quality: 10
  max_framerate: 30 fps
  resolution: 320x240
  horizontal_mirror: true
  vertical_flip: false

    
output:
- platform: ledc
  pin: 12
  id: motorAleft
  frequency: 1000 Hz

- platform: ledc 
  pin: 13
  id: motorAright
  frequency: 1000 Hz

- platform: ledc
  pin: 15
  id: motorBleft
  frequency: 1000 Hz

- platform: ledc
  pin: 14
  id: motorBright
  frequency: 1000 Hz

Script:

motor_a_left:
  alias: Motor A Left
  sequence:
    - service: esphome.spycam_control_motor_a_left
      data:
        level: '100'

motor_a_right:
  alias: Motor A Right
  sequence:
    - service: esphome.spycam_control_motor_a_right
      data:
        level: '100'

motor_a_stop:
  alias: Motor A Stop
  sequence:
    - service: esphome.spycam_control_motor_a_stop
      data:
        level: '0'

motor_b_left:
  alias: Motor B Left
  sequence:
    - service: esphome.spycam_control_motor_b_left
      data:
        level: '100'

motor_b_right:
  alias: Motor B Right
  sequence:
    - service: esphome.spycam_control_motor_b_right
      data:
        level: '100'

motor_b_stop:
  alias: Motor B Stop
  sequence:
    - service: esphome.spycam_control_motor_b_stop
      data:
        level: '0'

motor_a_and_b_forward:
  alias: Motor A and B Forward
  sequence:
    - service: esphome.spycam_control_motor_a_left
      data:
        level: '100'
    - service: esphome.spycam_control_motor_b_left
      data:
        level: '100'

motor_a_and_b_backward:
  alias: Motor A and B Backward
  sequence:
    - service: esphome.spycam_control_motor_a_right
      data:
        level: '100'
    - service: esphome.spycam_control_motor_b_right
      data:
        level: '100'

motor_a_and_b_right:
  alias: Motor A and B Right
  sequence:
    - service: esphome.spycam_control_motor_a_left
      data:
        level: '100'
    - service: esphome.spycam_control_motor_b_stop
      data:
        level: '0'

motor_a_and_b_left:
  alias: Motor A and B Left
  sequence:
    - service: esphome.spycam_control_motor_b_left
      data:
        level: '100'
    - service: esphome.spycam_control_motor_a_stop
      data:
        level: '0'

motor_a_and_b_stop:
  alias: Motor A and B Stop
  sequence:
    - service: esphome.spycam_control_motor_b_stop
      data:
        level: '0'
    - service: esphome.spycam_control_motor_a_stop
      data:
        level: '0'

Automation:

- alias: Motor go forward when button pushed
  trigger:
    platform: state
    entity_id: binary_sensor.forward
    from: 'off'
    to: 'on'
  action:
    - service: homeassistant.turn_on
      entity_id: script.motor_a_and_b_forward

- alias: Motor stop when button released
  trigger:
    platform: state
    entity_id:
      - binary_sensor.forward
      - binary_sensor.right
      - binary_sensor.left
    from: 'on'
    to: 'off'
  action:
    - service: homeassistant.turn_on
      entity_id: script.motor_a_and_b_stop

- alias: Motor go left when button pushed
  trigger:
    platform: state
    entity_id: binary_sensor.left
    from: 'off'
    to: 'on'
  action:
    - service: homeassistant.turn_on
      entity_id: script.motor_a_and_b_left

- alias: Motor gpo right when button pushed
  trigger:
    platform: state
    entity_id: binary_sensor.right
    from: 'off'
    to: 'on'
  action:
    - service: homeassistant.turn_on
      entity_id: script.motor_a_and_b_right
4 Likes

This is a great project! Thanks for sharing. Do you think that instead of the buttons you could use the following joystick?

I’m sure that could be done, although it would need a different approach, code-wise.

1 Like