It’s a problem with your relay.
I just dug out some relays from my box of bits. I found one that’s identical to the one you’re using, in an unopened packet, so it’s brand new.
It doesn’t work with your code on a D1 Mini, but a better quality two channel opto-isolated relay that I also had works fine.
The relay on the left works fine, the on on the right doesn’t.
I would have spent time messing around with pull-up or pull-down resistors to see if that helped, but TBH I have other things to do.
Get some decent opto-isolated relays and you should be fine.
You might also be interested in this thread:
If you’re interested in hacking existing mains power switches then take a look at this:
Pete.
1 Like