Introduction: ESP8266 $5 Internet Connected Switch - Improved

In this tutorial we will show how to build WiFi internet controlled switch connected to EasyIoT Cloud. This tutorial is improved version of ESP8266 internet connected switch. We will use ESP8266, solid state relay and Arduino IDE. AP name and password can be configured in WEB interface.

This internet controlled switch uses few components and it's connected to EasyIoT Cloud. You can control your devices on computer in EasyIoT Cloud WEB interface or with native Android application. Internet controlled switch is connected to internet with WiFi connection. Switch can also be controlled locally with push button.

This tutorial is improved version of ESP8266 internet connected switch. It works with latest version of EasyIoT Cloud MQTT broker. Two main improvements regarding previous version of switch are that this uses switch information to update state of UI interface. If connection to switch is broken, then state in UI will not update. Next improvement is that in this switch you can configure AP username and password with mobile phone or computer. This internet switch is "plug and play" - it will automatically set all EasyIoT Cloud settings, so no configuration in EasyIoT Cloud is needed.

WARNING!! You will play with LIVE MAINS!! Deadly zone!! If you don't have any experience and are not qualified for working with MAINS power I will not encourage you to play around! Do NOT use it without proper Knowledge about MAINS circuits ! Do NOT use it without a proper FUSE on MAINS line! Max current for solid state in this tutorial is 2A - suitable for room light only.

Step 1: Materials

Materials:

  • ESP8266 WiFi module
  • Solid State Relay 2A 240V
  • 3.3V 600mA AC-DC step down module
  • Push Button switch
  • NPN Transistor TO-92 2N2222
  • 1000uF electrolytic capacitor
  • resistor, 1K, 47K

Step 2: EasyIoT Cloud Configuration

No EasyIoT Cloud configuration is needed, just register to EasyIoT Cloud service. You will need username and password to access your internet switch on WEB application or in Android application. Switch will be automatically added to EasyIoT Cloud and will be visible in WEB interface or in Android application after power on.

Step 3: Program

Program is written in Arduino ESP8266 IDE. See Arduino ESP8266 IDE tutorial how to connect ESP8266 module to computer to upload program. Program can be downloaded from our GitHub. You will also need MQTT client library. Add this library to library folder in Arduino IDE. Program uses EasyIoT Cloud MQTT API.

In program change following lines EasyIoT Cloud username and password:

#define EIOTCLOUD_USERNAME "xxx"
#define EIOTCLOUD_PASSWORD "xxx"

You can also change access point name and password, but this is not necessary. You can later change AP username and password in WEB interface.

At beginning of the program it reads EEPROM settings. Then it tries to connect to AP. If connection to AP is not successful after 10 s switch to AP mode to change configuration. If connection to AP is successful switch checks module ID. If module ID is 0 it means switch is not configured on EasyIoT Cloud. In this case it adds module to EasyIoT Cloud and saves module ID to EEPROM settings. After successful configuration module subscribe to specific topic to listen switch changes.

The easiest way to test program is to use ESP8266 LUA node MCU board. Just connect it to USB port and you can test program. If you use this board you can also uncomment #define DEBUG to see debug messages. Flash button on ESP8266 board will work as button to manually change switch state. Onboard LED displays switch state.

Step 4: Hardware

Picture of Hardware

In our case we use ESP8266 01, but you can use any other type of ESP8266. GPIO2 is connected to NPN transistor to control SSR. Max current for our type of solid state relay is 2A - this is suitable for room light and not for applications which consume more power - for example heater.

For power supply we use 3.3V step down module. It's very important to add 1000uF capacitor to 3.3V power line - in our case switch didn't work if we skip this capacitor. GPIO0 is connected to push button for local control of internet switch.

After power on, switch will be automatically added to EasyIoT Cloud and it will be visible in WEB interface or Android application.

You can manually rename name of your switch and group in EasyIoT Cloud configuration to suits your need.

Step 5: Configuring ESP8266 Internet Switch

Picture of Configuring ESP8266 Internet Switch

When internet switch is switched on it tries to connect to AP. If after 10 s connection to AP is not successful it will switch in AP mode. With mobile phone search for AP named Thing-xx-xx-xx-xx where xx-xx-xx-xx is random value. Connect to this AP and then in WEB browser address enter 192.168.4.1. In WEB page displayed you can configure AP name and password. After changing AP name and password click submit button and internet switch will reboot and connect to AP.

Comments

DennisT33 (author)2016-08-28

There should be no need to use an IoT cloud for this. One 8266 module as 'server' to control a relay, another receiving pushbutton input, and an android device that can program either and control the circuit remotely. The future is not turning on a desk lamp from your smartphone, but designing houses that don't need 14ga copper going to both the ceiling lamp and two separate wall switches.

I want to be able to turn on my lights even if easyIoTCloud goes out of business!

alifasya (author)DennisT332017-05-18

Hi dennisT33

I have the same idea with you.

Have you tried your idea.... then please send your idea result to my email amalifasya@gmail.com...

Thanks

amatos4 (author)2017-03-18

Hello

Congratulations on the project, would you like to know how to use the server made with raspberry?

This same project with the status of the button is very interesting, even if it is not in the same environment, I can know if the lights are on. And even without connection to the internet or network, I can directly call the button, this is perfect.

Now to get better, I wanted to use the local raspberry server.

Thank you so much

Alexandre

shivayla (author)2017-01-09

Hi, its very interesting that you've updated the project and it sounds very very cool, I've been trying the tutorial but I'm stuck in the Arduino IDE as it says error as follows.

exit status 1

Error compiling for board Generic ESP8266 Module.

I've used the MQTT lib file which you have specified, but nothing seems to work, please help me out.I've tried in 1.6.5 and 1.6.9 versions of Arduino.

PaulD328 (author)2016-12-29

Hi, question on the SSR. Should it be a "non zero cross" version. So in your example use an Omron G3MC-202PL instead of a -202P ? I'm kinda new to electronics so just trying to figure the differences out before I order the parts. thanks.

praanesh (author)2016-11-27

And after i connect my phone to Ai- thinker wifi that is available, i cant connect to the ip 192.168.4.1 please help me

praanesh (author)2016-11-27

Hello I cant connect my 8266 to the server, it is not showing the switch. after i uploaded the program i connected the gpio to 0 and reset the chip i get only junk values in the serial monitor. I don't know what to do after that please help me

stetrault (author)2016-11-18

Hi there, i whant to build this circuit but i want to replace a wall switch with it instead of a socket. Kind of belkin wemo WI-FI wall switch clone. I am not really good in electronic so i need your help. Here is my custom diagram for it. Please let me know about it.

- I changed the step down module for a better one witch is insulated. ( HLK-PM03 3W 3.3v )

- Also i dont want the SSR relay to switch OFF and ON the cold wire ( WHITE ). I want it to turn OFF and ON the hot wire ( BLACK )

- Also how can i correct the circuit to be shure we can use any of the 2 BLACK wire in the wall. ( One arriving from the main ( braker ), other one going to the lignt ). Currently i need to be shure the black coming from the main is feeding the relay and load of the SSR.

- Also i would like to add a led to be switched on on the swich when relay is open. To find the switch on the wall in the dark.

I hope you can help me and correct me. I also want to do a real shematic with correct diode flow direction.

FEEL free to contact me if you want to work with me in this project.

stetrault (author)stetrault2016-11-18

Also you can contact me by email if you whant to work with me on this belkin wemo wifi wall switch project. steve@lannex.com

stetrault (author)stetrault2016-11-18

HI forget to tell i live in canada and here it is 120v ac.

But i think this circuit can handle it

mirceaC (author)2016-11-11

I made this and work on nodemcu lua but please help me to add a temeprature sensor like dht11 or 18b20 to this project ! Thank you ! mirceacra@gmail.com

marcelode1 (author)2016-09-03

Do you have any other sample or could guide me to add more relays and buttons, i also want to be able to sense a temperature of a water. I need to turn on a motor and 3 actionaters depending upon watet temperature.
Thanks

pardhiatno (author)2016-07-28

Can i change the SSR with normal relay so i can use bigger ampere?

EasyIoT (author)pardhiatno2016-07-28

I guess yes. Use 3,3V relay.