John's Musings

Yeah, Me Neither

Songle Internet Controlled Relay from ESP8266

I bought a Songle 2 channel relay from here with the hopes of hooking it directly to the ESP8266 GPIO and using it over the internet based on this excellent tutorial.

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.

#include <ESP8266WiFi.h>
#include <aREST.h>
#include <aREST_UI.h>

// crée aREST instance
aREST_UI rest = aREST_UI();

// WiFi parameters
const char* ssid = "Your-SSID";
const char* password = "Your-Password";

// port conncetions TCP
#define LISTEN_PORT 80

// crée un instance du server
WiFiServer server(LISTEN_PORT);
void setup(void)
// crée UI
rest.title("Relay ");
// donne nom et ID au module
// connecte au wifi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.println("WiFi connected");
// initialise le serveur
Serial.println("Server started");
// imprimme l'adresse IP

void loop() {
WiFiClient client = server.available();
if (!client) {

Also you can configure this in Home Assistant to make dashboard switches.

switch 5:

  platform: command_line



      command_on: "/usr/bin/curl -X GET"

      command_off: "/usr/bin/curl -X GET"

      friendly_name: Relay 1

switch 6:

  platform: command_line



      command_on: "/usr/bin/curl -X GET"

      command_off: "/usr/bin/curl -X GET"

      friendly_name: Relay 2