logo
NodeMCU Relay GPO
Dual Relay GPO Box

This little project with the NodeMCU and a dual relay that can be turn ON and OFF with a IFTTT WEBHOOK
Here in this project I am switching a power outlet ON and OFF with one of the relays
A word of WARNING when working with mains voltage, you should be competent to do so, it is very DANGEROUS.
I'm using a phone charger inside the box to power the unit and not the programming port.
You will have to setup portforwarding on your router and if you don't have a static public IP address
you will need somthing like No-IP I have a little No-IP updater I made from a NodeMCU.

PARTS

 USB2.0 Type-A Plug 4-pin Male
WeMos D1 USB NodeMcu Lua V3 CH340G ESP8266 Wireless Internet Development 5-9V
5V Two 2 Channel Relay Module With Optocoupler
100x100x50mm IP55 ABS Enclosure Junction Box Adaptable Waterproof Weatherproof
 2N2222 Bipolar Transistor NPN 40V 0.6A TO-92
2 x 1k Resister Ohm
3 x 470 Resister Ohm
4 x 10k Resister Ohm
3 x LEDs
2 x PUSH BUTTONs
Small 5v phone charger, i like to use a genuine brand
Surface Sockets Plug Base

NOTE: I have had problem with Windows 10 on and off, the sketch would test and upload to this type of Nodemcu but after connecting to the wifi the Nodemcu would crash
If I used Linux to upload the sketch it would work fine or if I used the Nodemcu with the square silver chip that was ok with Windows 10.


nodemcu_relay_gpo_top_on        nodemcu_relay_gpo_box_open

nodemcu_relay_gpo_pcb_relay        nodemcu_relay_gpo_pcb

The below pictures are of the dual relay, it comes with two isolation cut-outs, but as I am using one of the
relays for mains voltage I decided to cut one more between the two relays

nodemcu_relay_gpo_relay1        nodemcu_relay_gpo_relay2

nodemcu_relay_gpo_wiring_s
click picture to enlarge

Below is a Sketch that works, it may not be the best put you can change it to suit your own designs
/*
 MerrittSoft
 NodeMCU Relay GPO
 9/03/2018
*/
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
ESP8266WebServer server;
uint8_t gpo = D1;       // gpo output pin
uint8_t relay = D2;     // relay output pin
char* ssid = "your-wifi"; // att your wifi name here
char* password = "your-wifi-password"; // add your wifi password here
int gpobutton = D6;         // gpo button pin in
int relaybutton = D7;       // relay button pin in
int switchState = 0;
int oldSwitchState = 0;
int lightsOn = 0;
int switchState2 = 0;
int oldSwitchState2 = 0;
int lightsOn2 = 0;
void setup()
{
 pinMode(gpo, OUTPUT);
 digitalWrite(gpo, LOW);
 pinMode(relay, OUTPUT);
 digitalWrite(relay, LOW);
 pinMode(gpobutton, INPUT);
 pinMode(relaybutton, INPUT);
 WiFi.begin(ssid, password);
 WiFi.mode(WIFI_STA); // added works with some boards to stop the wifi broadcast.
 Serial.begin(115200);
 while (WiFi.status() != WL_CONNECTED)
 {
   Serial.print(".");
   delay(500);
 }
 Serial.println("");
 Serial.print("IP Address: ");
 Serial.println(WiFi.localIP());
 server.on("/", []() {
   server.send(200, "text/plain", "NodeMCU Relay");
 });
 // below are the commands that activate the relays
 // eg. the /GPO=OFF is the command that you have programmed to be sent to to unit from IFTTT Webhook
 server.on("/GPO=OFF", gpoOFF);
 server.on("/GPO=ON", gpoON);
 server.on("/RELAY=OFF", relayOFF);
 server.on("/RELAY=ON", relayON);
 server.begin();
}
void loop() {
 if (WiFi.status() != WL_CONNECTED)
 {
   ESP.reset();
 }
 server.handleClient();
 // ******** gpobutton
 delay(200);
 switchState = digitalRead(gpobutton); // read the pushButton State
 if (switchState != oldSwitchState) // catch change
 {
   oldSwitchState = switchState;
   if (switchState == HIGH)
   {
     // toggle
     lightsOn = !lightsOn;
   }
 }
 if (lightsOn)
 {
   digitalWrite(gpo, HIGH); // set the gpo relay on
 } else {
   digitalWrite(gpo, LOW); // set the gpo relay off
 }
 // ********* relaybutton
 switchState2 = digitalRead(relaybutton); // read the pushButton State
 if (switchState2 != oldSwitchState2) // catch change
 {
   oldSwitchState2 = switchState2;
   if (switchState2 == HIGH)
   {
     // toggle
     lightsOn2 = !lightsOn2;
   }
 }
 if (lightsOn2)
 {
   digitalWrite(relay, HIGH); // set the relay2 on
 } else {
   digitalWrite(relay, LOW); // set the relay2 off
 }
}
void gpoOFF()
{
 digitalWrite(gpo, LOW);
 lightsOn = 0;
 server.send(204, "");
}
void gpoON()
{
 digitalWrite(gpo, HIGH);
 lightsOn = 1;
 server.send(204, "");
}
void relayOFF()
{
 digitalWrite(relay, LOW);
 lightsOn2 = 0;
 server.send(204, "");
}
void relayON()
{
 digitalWrite(relay, HIGH);
 lightsOn2 = 1;
 server.send(204, "");
}
www.000webhost.com