I have a problem with the servo.detach command.
I am using a AZ delivery D1 Mini V3 NodeMCU board
When I release the Switch 1, which is connected digital port D1, the servo does not stop, it moves to the end position (-100 or 100)
Does anyone else have the same problem?
Is there any way to stop the servo?
on_release code looks like this (I tried all the different ways I know):
on_release:
then:
- servo.detach: servo1
- lambda: |-
id(servo1).detach();
- logger.log: "done1"
Debug log look like this, on_release seems to work, but servo.detach not:
[21:47:11][D][binary_sensor:036]: 'Switch 1': Sending state ON
[21:47:11][D][servo:062]: Servo new target: -1.000000
[21:47:12][D][binary_sensor:036]: 'Switch 1': Sending state OFF
[21:47:12][D][main:155]: done1
[21:47:16][D][servo:050]: Servo reached target
Here is full code
esphome:
name: pwm-4servo
friendly_name: pwm-4servo
esp8266:
board: d1_mini
# Enable logging
logger:
level: DEBUG
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Pwm-4Servo Fallback Hotspot"
password: "NOJGD83JUJIV"
captive_portal:
# Define global variables
globals:
- id: my_global_int1
type: int
restore_value: no
- id: my_global_int2
type: int
restore_value: no
- id: my_global_int3
type: int
restore_value: no
- id: my_global_int4
type: int
restore_value: no
# Configure API and OTA
api:
services:
- service: control_servo1
variables:
level: float
then:
- servo.write:
id: servo1
level: !lambda 'return level / 100.0;'
- service: control_servo2
variables:
level: float
then:
- servo.write:
id: servo2
level: !lambda 'return level / 100.0;'
- service: control_servo3
variables:
level: float
then:
- servo.write:
id: servo3
level: !lambda 'return level / 100.0;'
- service: control_servo4
variables:
level: float
then:
- servo.write:
id: servo4
level: !lambda 'return level / 100.0;'
ota:
# Configure outputs and servo
output:
- platform: esp8266_pwm
id: pwmout1
pin: D5
frequency: 50 Hz
- platform: esp8266_pwm
id: pwmout2
pin: D6
frequency: 50 Hz
- platform: esp8266_pwm
id: pwmout3
pin: D7
frequency: 50 Hz
- platform: esp8266_pwm
id: pwmout4
pin: D8
frequency: 50 Hz
servo:
- id: servo1
output: pwmout1
transition_length: 5s
auto_detach_time: 1s
- id: servo2
output: pwmout2
transition_length: 5s
auto_detach_time: 1s
- id: servo3
output: pwmout3
transition_length: 5s
auto_detach_time: 1s
- id: servo4
output: pwmout4
transition_length: 5s
auto_detach_time: 1s
# Configure binary sensor and switch
binary_sensor:
- platform: gpio
pin:
number: D1
inverted: true
mode:
input: true
pullup: true
name: "Switch 1"
filters:
- delayed_on: 10ms
on_press:
then:
- servo.write:
id: servo1
level: !lambda |-
if (id(my_global_int1) == 1) {
id(my_global_int1) = 2;
return -1.0;
} else {
id(my_global_int1) = 1;
return 1.0;
}
on_release:
then:
- servo.detach: servo1
- lambda: |-
id(servo1).detach();
- logger.log: "done1"
- platform: gpio
pin:
number: D2
inverted: true
mode:
input: true
pullup: true
name: "Switch 2"
filters:
- delayed_on: 10ms
on_press:
then:
- servo.write:
id: servo2
level: !lambda |-
if (id(my_global_int2) == 1) {
id(my_global_int2) = 2;
return -1.0;
} else {
id(my_global_int2) = 1;
return 1.0;
}
on_release:
then:
- servo.detach: servo2
- logger.log: "done2"
- platform: gpio
pin:
number: D3
inverted: true
mode:
input: true
pullup: true
name: "Switch 3"
filters:
- delayed_on: 10ms
on_press:
then:
- servo.write:
id: servo3
level: !lambda |-
if (id(my_global_int3) == 1) {
id(my_global_int3) = 2;
return -1.0;
} else {
id(my_global_int3) = 1;
return 1.0;
}
on_release:
then:
- servo.detach: servo3
- logger.log: "done3"
- platform: gpio
pin:
number: D4
inverted: true
mode:
input: true
pullup: true
name: "Switch 4"
filters:
- delayed_on: 10ms
on_press:
then:
- servo.write:
id: servo4
level: !lambda |-
if (id(my_global_int4) == 1) {
id(my_global_int4) = 2;
return -1.0;
} else {
id(my_global_int4) = 1;
return 1.0;
}
on_release:
then:
- servo.detach: servo4
- logger.log: "done4"