Probably everybody wanted to have a “smart home” or at least “smart room”. And in this post I’d show you a simple and cheap way to build a remote-controlled switch. Which you can easily control via WiFi. It is based on ESP8266, a powerful WiFi chip with quite low price. 

You would be able to control the remote switch over the Internet by connecting to WiFi.

Components  

  • ESP8266 
  • 5V single relay 
  • Transistor 2N2222 or 2N2222A
  • 4.7KOhm resistor 
  • 5V power adapter 
  • 12V/220V bulb to test (optional)

IMG_20180424_210627_HDR

Wiring up 

Be careful with transistor pinout. Before the start, check what type do you have. Then I’d recommend you to look up on the Internet the datasheet for the transistor to get the most accurate pinout information. Here are the most common ones.

Pinouts

For this example I’ve used a Witty Cloud version of ESP8266, you can also use another one! Just connect VCC to VIN and GND to ground pin. If you have some questions feel free to write them in the comments.

To power it, connect the 5V power adapter.

RemoteSwitchSchematics

Programming

Now we are ready to program it! And here is the code (you can upload it in Arduino IDE):

//Including libraries
#include <ESP8266WiFi.h>
#include <WiFiClient.h> 
#include <ESP8266WebServer.h>

//Defining pin for relay, I picked GPIO5 or easily 5
#define RelayPin 5

//WiFi credentials (You can easily change it to yours)
const char *ssid = "andagent.com";
const char *password = "andagent";

//Defines library class
ESP8266WebServer server(80);

//HTML part of the webpage
String head = "<html><head><title>Remote Switch</title></head><body><a href='/on'>Turn on</a><br><a href='/off'>Turn off</a></body></html>";

//Called when someone clicked OFF
void RelayOFF() {
  server.send(200, "text/html",head+"<br><br> State: OFF");
  digitalWrite(RelayPin, 0);
}

//Called when someone clicked ON
void RelayON() {
  server.send(200, "text/html",head+"<br><br> State: ON");
  digitalWrite(RelayPin, 1);
}

//Called when page is loaded
void Root(){
  server.send(200, "text/html",head);
}


void setup() {
  pinMode(RelayPin, OUTPUT); //Defines pin for relay as output
  WiFi.softAP(ssid, password); //Connects to the WiFi
  Serial.begin(115200);
  IPAddress myIP = WiFi.softAPIP();
  Serial.print(myIP);
  //Setting up the Event Listeners
  server.on("/", Root);
  server.on("/on", RelayON);
  server.on("/off", RelayOFF);
  //Starting the webserver
  server.begin();
}

void loop() {
  //Running the webpage
  server.handleClient();
}

 

To test it, connect to the WiFi network – andagent.com and enter the password – andagentandagent . Then enter this IP in your browser’s address bar  – 192.168.4.1 . On this webpage you will see two links: Turn on and Turn off. By clicking one of the links the remote switch will turn on or off. 

Testing

Now power everything up and open your browser. Then try to click on one of the links. You may hear some clicks from the relay, if you hear it, congratulations you made a remote switch (and it works)! You can also try to connect a bulb to the relay, by connecting one wire to the COM and NO ports.

Note: Don’t connect + and – to COM and NO, you need to connect + and + or – and – . Relay is like a standard switch! If you will connect + and – , you will make a shortcut!

Conclusion

This is it! Now you can implement this inside your room. For example to control the light. If you have some questions or ideas feel free to ask me in the comments.

Happy coding and building!

Did you enjoy this article?
Signup today and receive free updates straight in your inbox. We will never share or sell your email address.
I agree to have my personal information transfered to MailChimp ( more information )