Relays usually run on 5V or higher.
The ESP runs on 3.3V.
This combination runs into problems under certain circumstances.
As you set GPIO2 to "0"" to set the Relay on it is likely the relay uses
a PNP transistor or maybe an opto coupler
The "emitter" of the PNP transistor goes to 5V, The "base" goes to the ESP via an resistor.
This says here is a small remaining voltage from basis to emmiter of 5V - 3.3V = 1,7V
if the ESP output is set to "1".
Depending of the circuit around the transistor this will result in weird behaviour.
Relay may always trigger or it may trigger once and stay triggered as in your case.
To get over this effect there's a simple solution: USe two Diodes (1N4148 or similar)
like shown here:
https://letscontrolit.com/wiki/index.ph ... th_PNP.jpg
There is a long HowTo about relays and troubleshooting in the Wiki:
https://letscontrolit.com/wiki/index.ph ... cs:_Relays
If the module uses an opto coupler the diodes may take up too much power.
In this case you'll have to use a small NPN transistor like a 2N2222 or BC547.
This is shown in the wiki mentioned above:
https://letscontrolit.com/wiki/index.ph ... lution.jpg