Songle Internet Controlled Relay from ESP8266
Unfortunately it just didn't work that easy. The digital GPIO signal from the ESP8266 is 3.3v and the relay just won't read that input on the IN1 or IN2 channel.
So you have to build a driver circuit for it. I used a 2N3904 transistor that I got from RadioShack here. Thank goodness my town still has a RadioShack.
Here's a rough and dirty detail.
This project uses Digital Pin 5 which is actually Pin D1 on the NodeMCU 1.0. Yeah, that's intuitive.
This is all from the flat part of the resistor towards you.
- From D1 a 1K ohm resistor goes to the base of the transistor. That is the middle leg.
- The Emitter, left leg, is tied directly to ground.
- The collector, right leg, goes to IN1 or IN2 on the relay. Also from the collector a 1N1004 diode goes to the 5v power source with the banded end at the 5v input. Put it in backwards and you are creating the condition to fry your transistor.
- The NodeMCU requires 3.3 volts (sharp eyes will see I'm using 5V to drive mine, and it works but it's wrong, kinda, sorta.)
- The relay Vcc requires 5 volts and of course ground is tied to ground.
Here it is on a breadboard layout. Nothing to it.
And here it is in action.
To drive both relays though you need to build another driver circuit for the other relay input. Also you need firmware to control 2 relay channels. I used this code I got from here. With this you just find the IP address of your ESP8266 and go to that page in a browser and you get 2 sets of on and off controls.
Also you can configure this in Home Assistant to make dashboard switches.
switch 5: platform: command_line switches: arest_pin_two: command_on: "/usr/bin/curl -X GET http://192.168.20.102/digital/2/1" command_off: "/usr/bin/curl -X GET http://192.168.20.102/digital/2/0" friendly_name: Relay 1 switch 6: platform: command_line switches: arest_pin_zero: command_on: "/usr/bin/curl -X GET http://192.168.20.102/digital/0/1" command_off: "/usr/bin/curl -X GET http://192.168.20.102/digital/0/0" friendly_name: Relay 2