[ESP32] Chicken house door with a stepper motor problem

Hello,

I post here because I have a problem with my chicken house door managed by an ESP32 and a 12v stepper motor.
Here are some pictures of my setup. You can see a zigbee door sensor, it gives me the status of the door.

Here is a part of my code :

cover:
  - platform: template
    name: "Poulailler"
    id: poulailler
    device_class: door
    open_action:
      - stepper.set_target:
          id: my_stepper
          target: 0
      - sensor.template.publish:
          id: position
          state: !lambda return id(my_stepper).target_position;
    close_action:
      - stepper.set_target:
          id: my_stepper
          target: 50500
      - sensor.template.publish:
            id: position
            state: !lambda return id(my_stepper).target_position;
    stop_action:
      - stepper.set_target:
          id: my_stepper
          target: !lambda return id(my_stepper).current_position;
      - sensor.template.publish:
            id: position
            state: !lambda return id(my_stepper).current_position;
    optimistic: true
    assumed_state: true

When the door is open, the “position” value is 0
When the door is closed, the “position” value is 50500

Last night, I don’t know why, the ESP32 became unavailable. Here are details of the “position” state.

This morning, I think because of a reboot around 0:15 AM, the value should be 50500 but it was 0. So the door didn’t open, poor chickens were stuck in their house !

Why does the ESP32 become unavailable/undefined ? Do you think it reboots ?
Is there a way to “push” this value according to the door state ? Maybe it’s possible to set this “position” value when the ESP32 is available again ?

Thank you for your help

You haven’t said whether you use ESPHome for your project but if you do there should be a way to set the internal position here:

Yes I’m using ESPhome.
I will check your link.
Thank you @finity

1 Like

I didn’t find how to push a position when the esp32 starts.

However, why do the sensor become randomly unavailable? What could be the reason?

hi,

I would add 2 magnetic contact door sensors so you know the door is open/closed or in between. Define 2 input pins with internal pullup - a wire connected to ground and the other to your input pin from the sensor.

magneetcontact-0.1a-30v-dc-no-montage-opbouw

Then use the sun platform in ESPHome and open the door after sunrise (based on a condition the door is closed or not open).

ESPHome can work offline so your feathered friends will not depend on wifi connectivity. I realize I haven’t answered your original question but wifi disconnects can happen and in that case it is good to have a fall back.

Good luck!

Thank you for your suggestions, that’s very interesting !
I will check this after my holidays.
Would someone give some code to do this ?

I think it would be good if someone could offer a suggestion on the motor that you are using?

I’ve used a stepper motor for a sliding door type while this looks like a drawbridge. That means your stepper motor would have to be powered constantly or the door would slowly open because of gravity. For a portcullis type of door I have used a wormgear motor as that stays in position while powered off.

Code for opening, to stop the motor and to check the sensor which stops the motor at door open/close, so you’ll need to duplicate closing and one more sensor for open/close. I only open if not already open and close if not already closed. A lot off mqtt status publishing too which you can ignore.

stepper:
  - platform: a4988
    id: my_stepper
    step_pin: GPIO26
    dir_pin: GPIO25
    max_speed: 600 steps/s
    sleep_pin: GPIO27
    acceleration: 100     // use low acceleration as motor does not have torque at high speed
    deceleration: inf

switch:
  - platform: template
    id: esp32lr5_op
    name: "Openen"
    restore_state: false
    turn_on_action:
     if:
      condition:
       binary_sensor.is_off: sensorGeopend   // open ony if the open sensor is not on
      then:
      - switch.template.publish:
         id: esp32lr5_op
         state: on    
      - stepper.report_position:
         id: my_stepper
         position: 0
      - stepper.set_target:           // run the motor for a long time
         id: my_stepper
         target: 50000
      - stepper.set_speed:
         id: my_stepper
         speed: 600 steps/s

  - platform: template        // stop and deactivate motor - stop action
    id: esp32lr5_stop
    name: "Stop"
    restore_state: false
    turn_on_action:
    - stepper.report_position:
        id: my_stepper
        position: 0
    - stepper.set_target:
        id: my_stepper
        target: 0
    - stepper.set_speed:
        id: my_stepper
        speed: 1 steps/s    
    - switch.template.publish:
       id: esp32lr5_op
       state: off   
    - switch.template.publish:
       id: esp32lr5_neer
       state: off    


binary_sensor:

  - platform: gpio
    pin:
      number: GPIO32
      mode: INPUT_PULLUP   // if contact is closed then pin is connected to ground so inverted sensor
      inverted: true
    name: "Geopend"
    id: sensorGeopend
    filters:
      - delayed_on_off: 300ms    
    on_press:
      then:
          - switch.turn_on: esp32lr5_stop     // if open/close sensor is on then perform stop action
          - mqtt.publish:
             topic: home/open/status
             payload: Aan
             retain: true
    on_release:
      then:
       - mqtt.publish:
           topic: home/open/status
           payload: Uit
           retain: true

1 Like

I’ve made a similar chicken door control, that operates according to the light levels, ie dusk and it closes, sunrise opens …also this does not connect to homeassistant as its located far away from server, therefore once programmed via USB and installed, it generates it’s own local hotspot for controlling the open / close / stop / action from your phone, when you are close by. Connected to a car battery which is topped up from solar panel and has been running without any problems for the last 6 months.

esphome:
  name: "chickens"

esp8266:
  board: d1_mini


network:
wifi:
  ap:
    ssid: "chickens"
    password: "n2354op"

web_server:
  local: true
  port: 80

   

i2c:
  sda: D3
  scl: D4
  scan: true
  id: bus_a


cover:
  - platform: template
    name: "My Blind"
    id: my_blind
    device_class: blind
    open_action:
      - stepper.set_target:
          id: my_stepper
          target: 9000
      - sensor.template.publish:
          id: position
          state: !lambda return id(my_stepper).target_position;
    close_action:
      - stepper.set_target:
          id: my_stepper
          target: 0
      - sensor.template.publish:
            id: position
            state: !lambda return id(my_stepper).target_position;
    stop_action:
      - stepper.set_target:
          id: my_stepper
          target: !lambda return id(my_stepper).current_position;
      - sensor.template.publish:
            id: position
            state: !lambda return id(my_stepper).current_position;
    optimistic: true
    assumed_state: true

sensor:
  - platform: template
    name: "My Blind Position"
    id: position

# light sensor    
  - platform: bh1750
    name: "chickens BH1750 Illuminance"
    address: 0x23
    update_interval: 10s 
########################################     when light drops below 12 close above 12 open
    on_value_range:
     - below: 12
       then:
        - stepper.set_target:
            id: my_stepper
            target: 0
     - above: 12
       then:
        - stepper.set_target:
            id: my_stepper
            target: 9000
   
#############################################
stepper:
  - platform: a4988
    id: my_stepper
    step_pin: D6
    dir_pin: D5
    sleep_pin: D7
    max_speed: 800
1 Like

Some weeks ago, I’ve bought a new WiFi AP, there are no disconnection anymore.
Thank you for your suggestions, very interesting.