This tutorial is an improved version of the ESP8266 Internet-connected switch. It works with the latest version of EasyIoT Cloud MQTT broker. Two main improvements are that this uses switch information to update the state of the UI interface. If the connection to switch is broken, then the state in UI will not update. The next improvement is that in this switch, you can configure the AP username and password with a 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.
Finished ESP8266 Internet-controlled switch
ESP8266 Wi-Fi module:
|ESP8266 ESP-01 Serial WIFI Wireless Transceiver Module|
|ESP8266 ESP-03 Serial WIFI Wireless Transceiver Module|
|ESP8266 ESP-12 Serial WIFI Wireless Transceiver Module|
|ESP8266 ESP-05 Serial WIFI Wireless Transceiver Module|
|ESP8266 ESP-07 Serial WIFI Wireless Transceiver Module|
|ESP8266 Lua Nodemcu WIFI Network Development Board|
Solid State Relay 2A 240V:
|Solid State Relay 2A 240V|
3.3V 600mA AC-DC step-down module:
|3.3V 600mA AC-DC step down module|
|10Pcs Push Button switch|
NPN Transistor TO-92 2N2222:
|100Pcs NPN Transistor TO-92 2N2222|
|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|
See buying guide at the end of tutorial for details.
EasyIoT Cloud Configuration
No EasyIoT Cloud configuration is needed, just register to the EasyIoT Cloud service. You will need username and password to access your Internet switch via web application or Android application. The switch will be automatically added to EasyIoT Cloud and will be visible in the web interface or in the Android application after it is powered on.
The program is written in the Arduino ESP8266 IDE. Check out the Arduino ESP8266 IDE tutorial to see how to connect a ESP8266 module to a computer to upload a program. The program can be downloaded from our GitHub. You will also need the MQTT client library. Add this library to the library folder in the Arduino IDE. Our program uses the EasyIoT Cloud MQTT API.
In the program, change the 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 the beginning of the program, it reads EEPROM settings. Then, it tries to connect to the AP. If the connection to the AP is not successful after 10 seconds, switch to AP mode to change the configuration. If the connection to AP is successful, the switch checks the module ID. If the module ID is 0, it means the switch is not configured on EasyIoT Cloud. In this case, it adds the module to EasyIoT Cloud and saves the module ID to the EEPROM settings. After a successful configuration, the module subscribes to a specific topic to listen for switch changes.
The easiest way to test program is to use the ESP8266 LUA node MCU board. Just connect it to the USB port, and you can test program. If you use this board, you can also uncomment #define DEBUG to see debug messages. The flash button on the ESP8266 board will work as the button to manually change the switch state. The onboard LED displays switch state.
In our case, we use ESP8266 01, but you can use any other type of ESP8266. GPIO2 is connected to the NPN transistor to control SSR. The max current for our type of solid state relay is 2A — this is suitable for room light and not for applications that consume more power, like a heater.
For the power supply, we use a 3.3V step-down module. It's very important to add a 1000uF capacitor to the 3.3V power line — when we tried it without the capacitor, the switch didn't work.
GPIO0 is connected to the push button for local control of the Internet switch.
After power on, the switch will be automatically added to EasyIoT Cloud, and it will be visible in the web interface or the Android application.
You can manually rename your switch and group in the EasyIoT Cloud configuration to suit your needs.
Configuring the ESP8266 Internet Switch
When the Internet switch is turned on it tries to connect to AP. If after 10 sseconds the connection to AP is not successful, it will switch to AP mode. With a mobile phone, search for an AP named Thing-xx-xx-xx-xx where xx-xx-xx-xx is random value. Connect to this AP and then, in your web browser, go to 192.168.4.1. In the web page displayed, you can configure the AP name and password. After changing the AP name and password, click submit and the Internet switch will reboot and connect to AP.