ESP device dropping API with relay switch trigger

Hi, I have a intermittent issue with my ESP32Thingplus disconnecting from my HA/ESPHome API when I trigger an ‘off’ command to a Gravity relay I have connected to it. I know that the device is not rebooting because the relay is physically set to ‘normally closed’ and the YAML on switch on the device is also set to restore off.

switch:
  - platform: gpio
    pin: GPIO26
    # wire on N/C
    inverted: true
    restore_mode: RESTORE_DEFAULT_OFF
    name: "reserve tank switch"

As I said it’s intermittent. The Wi-Fi signal is good.

  • note this gravity card did not feature a flyback diode in the hook-up guide.

Electromagnetic interference (EMI) from your relay switching is interfering with your wifi signal causing the connection to drop.

Does your relay coil have a flyback diode?

If not, add one.

If it does then you will need some other sort of EMI suppression. An RC snubber across the relay contacts and/or some ferrite cores around the contact cabling. Physical distance between the relay and ESP may also help. It is a complicated subject and there is no simple answer to this.

It took me a great deal of time to stop this happening with my ESPHome irrigation controller. I used ferrite cores on all the valve relay cabling and moved the ESP antenna to an external one located away from the relays and valves.

2 Likes

Tom, it seems like that’s the obvious answer. particularly given it’s intermittent. This is the relay (below) Cant see a flyback diode mentioned. seems really strange it doesn’t have one built in given its for Arduino.

https://wiki.dfrobot.com/Gravity__Digital_Relay_Module__Arduino_%26_Raspberry_Pi_Compatible__SKU__DFR0473

It does have a flyback diode (though not a particularly well rated one):

Try switching your relay many times without a load connected. Does the issue still occur?

If not, then it is arcing of the relay contacts, or the contact cabling that is generating EMI. In which case the RC snubber and ferrites may help.

If it does still occur then the EMI is coming from the relay coil and the only options you have are distance and shielding.

1 Like

Tom, its a bit of a mad box spaghetti. the relay is close to the ESP32, very close.


Try cable managing that spaghetti away from the ESP antenna with some cable ties. Keep your high current cabling from the relay away from your signalling cabling too (as much as possible).

Shorten them up as much as possible too. Every single one of those wires is an antenna for sending and receiving EMI.

1 Like

yes. it’s a bit of a shocker. its weatherproof though! ROFL.

so after a serious wire haircut and the inclusion of a copper ‘avengers style prison’ for the relay, i can operate the relay without losing connectivity, although i get this curious messages in the log immediately after doing so:

12:51:44][D][pulse_counter:174]: 'tank litres per minute': Retrieved counter: 0.00 pulses/min
[12:51:44][D][sensor:094]: 'tank litres per minute': Sending state 0.00000 L/min with 2 decimals of accuracy
[12:51:47][D][switch:012]: 'tank switch on off' Turning ON.
[12:51:47][D][switch:055]: 'tank switch on off': Sending state ON
[12:52:06][D][esp32.preferences:114]: Saving 1 preferences to flash...
[12:52:06][D][esp32.preferences:143]: Saving 1 preferences to flash: 0 cached, 1 written, 0 failed
[12:52:33][D][sensor:094]: 'tank sensor voltage': Sending state 3.14853 V with 2 decimals of accuracy

IMG_7208 Medium

still doing it with a 1N4007 flyback diode installed.

could it be the ESP noting the restore mode?

restore_mode: RESTORE_DEFAULT_OFF