ESP8266 $5 Internet Connected Switch - Improved




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


  • 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:


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

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

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 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.



    • Metalworking Contest

      Metalworking Contest
    • Fix It! Contest

      Fix It! Contest
    • Creative Misuse Contest

      Creative Misuse Contest

    22 Discussions

    Can I use Openhab instead of EasyIoT Cloud ?,Because I had an old desktop as an home automation server and EasyIoT cloud usually depends on Internet.

    This is really simple if you use the FW "ESP Easy". Entirely non-cloud-based.

    5 replies

    I used ESPEasy a lot, I even wrote several blog posts about it. However, I find that ESPurna firmware is much much ahead ( for Sonoff devices and few others, of course ) and needs much less tinkering to get something working out of the box. ESPEasy does not have any protection over the HTTP on/off interface, which bugged me a lot!

    We're working on the project a lot but plan to do it even more in the near future. I understand what you're saying about the tinkering and we will start to ship pre-configure bin files for specific hardware/setups. Tasmota and ESPurna have the advantage that they pretty much only support specific hardware whereas we support generic boards and more than 80 sensors and peripheral hardware. Regarding HTTP protection I totally agree and it's in the next big release together with a brand new interface.

    ESPEasy is one of my favorites, because of flexibility, don't get me wrong! However, sometimes I need something very simple, which I can even send to my wife ... like a toggle button - turn on/off :) She can't manage ESPEasy interface at all :)

    We want to get to be the first choice for both sides alike. Later this year we'll release a completely new interface that will hopefully satisfy tinkerers and "noobs" :)

    Thanks for your kind words.

    If you're able to bring SSL into the firmware, that will be already a massive step ahead of everyone else. Wish you all the best with the project and I hope someday I can contribute too ( I'm having an ESP8266 open-source project myself - about sensors. It works with OpenSenseMap and ThingSpeak already, but it's in early alpha, especially the interface. If you're interested - I wrote a bit about it here - ).

    What happens if you lose internet ? . Isn't it preferable to have automation locally so if you lose your internet you be still able to control stuff around the house. I agree you do get a sexy control panel with cloud .

    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!

    1 reply

    Hi dennisT33

    I have the same idea with you.

    Have you tried your idea.... then please send your idea result to my email



    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


    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.

    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.

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

    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

    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.

    2 replies

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

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

    But i think this circuit can handle it

    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 !

    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.