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

 

Contents

1. Introduction

2. Materials

3. EasyIoT Cloud configuration 

4. Program

5. Hardware

5. Configuring ESP8266 Internet switch

6. Buying guide

 

Introduction

 

ESP8266 internet switch

Finished ESP8266 internet controlled switch

 

Materials

-ESP8266 WiFi module

ESP8266 ESP-01 Serial WIFI Wireless Transceiver ModuleESP8266 ESP-01 Serial WIFI Wireless Transceiver Module
$2.45
$7.65
ESP8266 ESP-03 Serial WIFI Wireless Transceiver ModuleESP8266 ESP-03 Serial WIFI Wireless Transceiver Module
$2.07
ESP8266 ESP-12 Serial WIFI Wireless Transceiver ModuleESP8266 ESP-12 Serial WIFI Wireless Transceiver Module
$2.09
ESP8266 ESP-05 Serial WIFI Wireless Transceiver ModuleESP8266 ESP-05 Serial WIFI Wireless Transceiver Module
$1.88
$3.89
ESP8266 ESP-07 Serial WIFI Wireless Transceiver ModuleESP8266 ESP-07 Serial WIFI Wireless Transceiver Module
$2.18
$1.87
ESP8266 Lua Nodemcu WIFI Network Development BoardESP8266 Lua Nodemcu WIFI Network Development Board
$7.43
$3.20

 

-Solid State Relay 2A 240V

Solid State Relay 2A 240VSolid State Relay 2A 240V
$1.13
$1.93
 

 

-3.3V 600mA AC-DC step down module

3.3V 600mA AC-DC step down module3.3V 600mA AC-DC step down module
$1.99
$2.24

 

-Push Button switch

10Pcs Push Button switch10Pcs Push Button switch
$1.67

 

-NPN Transistor TO-92 2N2222

100Pcs NPN Transistor TO-92 2N2222100Pcs NPN Transistor TO-92 2N2222
$1.13

 

-Electrolytic capacitor

210Pcs 25 Value 0.1uF-220uF Electrolytic Capacitors Assortment Kit Set210Pcs 25 Value 0.1uF-220uF Electrolytic Capacitors Assortment Kit Set
$3.78
$5.52

-Resistor

400X 0.25w 1/4w Metal Film Resistor Pack Kit 1% 20 Value Each 20 Pcs 10 ~ 1M ohm400X 0.25w 1/4w Metal Film Resistor Pack Kit 1% 20 Value Each 20 Pcs 10 ~ 1M ohm
$3.55
$2.99

 

See buying guide at the end of tutorial for details.

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.

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.

 

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.

 

ESP8266 internet switch schematic 

 

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

ESP8266 internet switch

Android application

 

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

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.

ESP8266 internet switch configuration

See more tutorials at http://iot-playground.com/build

Buying guide

To support this site and EasyIoT framework development please buy in our store.

ESP8266 ESP-01 Serial WIFI Wireless Transceiver Module

ESP8266 Serial WIFI Wireless Transceiver Module

ESP8266 ESP-12 Serial WIFI Wireless Transceiver Module

ESP8266 serial Port WIFI wireless module ESP-12

ESP8266 ESP-03 Serial WIFI Wireless Transceiver Module

ESP8266 serial WIFI model ESP-03

ESP8266 ESP-05 Serial WIFI Wireless Transceiver Module

ESP8266 ESP-05 Serial WIFI Wireless Transceiver Module

ESP8266 ESP-07 Serial WIFI Wireless Transceiver Module

ESP8266 with external antenna

ESP8266 Lua Nodemcu WIFI Network Development Board

ESP8266 Lua Nodemcu WIFI Network Development Board

Solid State Relay 2A 240V

G3MB-202P-5VDC DC-AC SSR In 5V DC Out 240V AC 2A Solid State Relay

3.3V 600mA AC-DC step down module

3.3V 600mA AC-DC step down module

10Pcs Push Button switch

10Pcs Push Button switch

100Pcs NPN Transistor TO-92 2N2222

All purpose NPN transistor.

210Pcs 25 Value 0.1uF-220uF Electrolytic Capacitors Assortment Kit Set

400X 0.25w 1/4w Metal Film Resistor Pack Kit 1% 20 Value Each 20 Pcs 10 ~ 1M ohm

 


Comments   

#22 Bruno 2017-01-22 01:13
Has anyone used EasyIoT server instead of the cloud server ?? What changes are needed in the code ?
+1 #21 Dave Brunner 2016-12-25 05:43
Quoting Adrian:
Can you recommend a Solid State Relay that will work with ESP8266 & U.S. 120V power? Would this work - Omron G3MC-202P-VD DC5

I would think any 100-240VAC relay will work but here is one.
https://www.amazon.com/CJRSLRB-8-channel-Resistive-Duemilanove-Mega2560/dp/B00SGHM3GI/ref=sr_1_1?s=pc&ie=UTF8&qid=1482640934&sr=1-1&keywords=Solid+state+5V+relay+arduino
+1 #20 Dave Brunner 2016-12-25 05:40
Quoting Aleksandar Aleksandrov:
Quoting Steve:
Has anybody modified this project to work with a relay instead of a solid state relay? The similar server (non cloud) project is that way. I would like to control some motors that are larger than 2A.


You can just add a mono phase or 3 phase contactor after the Solid state relay.


I'm using a standard relay w/o a problem. I have GPIO going right to the relay input terminal. My problem is my relay coil is engaged when grounded (LOW) but the GUI
has LOW as off but in my case it's actaully ON.
#19 Dave Brunner 2016-12-25 05:37
Do you know if there is a way to change the switch "Off" status to have GPIO2 HIGH instead of LOW. My relays are switched on w/ a LOW signal. I could use a relay to resolve this but I'm hoping to find a way to do this w/in the sketch. I looked through it and also the web GUI and could not find a way to change the default power on to LOW and off to HIGH.
#18 Aleksandar Aleksandrov 2016-11-22 00:24
Quoting Steve:
Thanks Aleksandar.

How would that work? The SSR is rated for 240 V AC. Most of the relays I have seen have an input voltage of 5 or 7 volts DC. Can the SSR control 5-7 volts DC?


You just need:
SSR (input:5V,output 240V/2A)
Contactor (Xamps ,1phase or 3 phase depent on your motor with control circuit 240V)
Connect:
Wifi Module to SSR (3,3V to 240V/2A)
SSR(240V/2A) to Contactor control coil
Contactor to the motor
#17 Steve 2016-11-21 16:20
Thanks Aleksandar.

How would that work? The SSR is rated for 240 V AC. Most of the relays I have seen have an input voltage of 5 or 7 volts DC. Can the SSR control 5-7 volts DC?
#16 Aleksandar Aleksandrov 2016-11-20 15:21
Quoting Steve:
Has anybody modified this project to work with a relay instead of a solid state relay? The similar server (non cloud) project is that way. I would like to control some motors that are larger than 2A.


You can just add a mono phase or 3 phase contactor after the Solid state relay.
#15 Aleksandar Aleksandrov 2016-11-20 15:08
I made an upgrade to HTML so now you can type in your Cloud username and password.But i want to make a box in the web to change the ModuleID.This "THING-" is very annoying.Can you help me please.Sorry for my bad English.
#14 Steve 2016-11-08 16:49
Has anybody modified this project to work with a relay instead of a solid state relay? The similar server (non cloud) project is that way. I would like to control some motors that are larger than 2A.
#13 PC 2016-10-19 17:30
Hi

just found this nice work you did but I likely have misunderstood something.
You mentionned a 1000µF capacitor is required and you give link to buy 210Pcs 25 Value 0.1uF-220uF Electrolytic Capacitors Assortment Kit Set.
What did I miss ?
Thank you

You have no rights to post comments