Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

ESP8266 Internet Connected Switch (EasyIoT Cloud MQTT API V1)

DZone's Guide to

ESP8266 Internet Connected Switch (EasyIoT Cloud MQTT API V1)

In this tutorial we will show how to build WiFi internet controlled switch connected to EasyIoT Cloud. We'll use an ESP8266, a solid state relay, and the Arduino IDE.

· IoT Zone ·
Free Resource

It's project time! Today, going to make a switch. This Internet-controlled switch uses few components, and it's connected to EasyIoT Cloud. You can control your devices on a computer in the EasyIoT Cloud WEB interface or with a native Android application. The Internet-controlled switch is connected via Wi-Fi connection. Our switch can also be controlled locally with the push of a button. 

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. 

Introduction

ESP8266 internet switch

Finished ESP8266 Internet-controlled switch

Materials

ESP8266 Wi-Fi module:

ESP8266 ESP-01 Serial WIFI Wireless Transceiver Module ESP8266 ESP-01 Serial WIFI Wireless Transceiver Module

$2.05

$3.00
ESP8266 ESP-03 Serial WIFI Wireless Transceiver Module ESP8266 ESP-03 Serial WIFI Wireless Transceiver Module

$2.45
ESP8266 ESP-12 Serial WIFI Wireless Transceiver Module ESP8266 ESP-12 Serial WIFI Wireless Transceiver Module

$2.33

$2.83
ESP8266 ESP-05 Serial WIFI Wireless Transceiver Module ESP8266 ESP-05 Serial WIFI Wireless Transceiver Module

$1.88

$3.89
ESP8266 ESP-07 Serial WIFI Wireless Transceiver Module ESP8266 ESP-07 Serial WIFI Wireless Transceiver Module

$2.18

$1.87
ESP8266 Lua Nodemcu WIFI Network Development Board ESP8266 Lua Nodemcu WIFI Network Development Board

$7.43

$3.49


Solid State Relay 2A 240V:

Solid State Relay 2A 240V Solid State Relay 2A 240V

$1.14

$1.95
 

3.3V 600mA AC-DC step-down module:

3.3V 600mA AC-DC step down module 3.3V 600mA AC-DC step down module

$1.99

$2.37


Push-button switch:

10Pcs Push Button switch 10Pcs Push Button switch

$1.57

$2.45


NPN Transistor TO-92 2N2222:

100Pcs NPN Transistor TO-92 2N2222 100Pcs NPN Transistor TO-92 2N2222

$1.2

$0.97


Electrolytic capacitor:

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

$3.76

$5.52


Resistor:

400X 0.25w 1/4w Metal Film Resistor Pack Kit 1% 20 Value Each 20 Pcs 10 ~ 1M ohm 400X 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 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.

Program

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.

Hardware

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.

ESP8266 internet switch schematic 

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. 

ESP8266 internet switch


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.

ESP8266 internet switch configuration


Topics:
configuration ,cloud ,wifi ,interface ,android application ,internet ,switch

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}