In this tutorial we will show how to build WiFi internet controlled switch connected to EasyIoT Cloud with 4 relays and one ESP8266. We will use Arduino IDE to program ESP8266

This internet connected switch is simplified version of ESP8266 internet connected switch. Program code is simplified - it's not plug and play, with hard coded configuration, but it uses 4 relays instead of just one. You can easy extend program to control more relays with single ESP8266. Relays can be controlled from anywhere in the world - you just need internet connection.

Devices can be controlled over EasyIoT Cloud interface or native Android Application.

 

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

6. Buying guide

 

Introduction

  

Materials

 

-ESP8266 WiFi module

ESP8266 ESP-12 Serial WIFI Wireless Transceiver ModuleESP8266 ESP-12 Serial WIFI Wireless Transceiver Module
$2.08
$1.95
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
$2.72

 

-Relay module

1 Channel Isolated 5V Relay Module1 Channel Isolated 5V Relay Module
$1.34
$0.58
2 Channel 5V Relay Module With optocoupler2 Channel 5V Relay Module With optocoupler
$1.48
$0.91
4 Channel 5V Relay Module With optocoupler4 Channel 5V Relay Module With optocoupler
$2.5
$1.77
8 Channel 5V Relay Module With optocoupler8 Channel 5V Relay Module With optocoupler
$4.85
$3.90
 

-3.3V power supply

5pcs 3.3V regulator module 800mA5pcs 3.3V regulator module 800mA
$1.12
$1.58
 

 

-4.7K 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
$1.8
$3.99

 

-NPN Transistor TO-92 2N2222

100Pcs NPN Transistor TO-92 2N2222100Pcs NPN Transistor TO-92 2N2222
$3.42
$1.00

 

See buying guide at the end of tutorial for details.

EasyIoT Cloud configuration 

First register to EasyIoT Cloud service. We will add DO modules manually. Go to Configure->Modules. If there are demo modules you do not need delite them all. Then press button Add Module. Change module type to Digital Output DO (MT_DIGITAL_OUTPUT) and save module. It's important to remember module ID. If this is your first module in your configuration, module ID should be 1. Next add more DO modules - in our case 3 more, but it can be dependent on your program and number of relays.

 

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"

 

Also set up your WiFi access point name and password:

#define AP_SSID "xxx"
#define AP_PASSWORD "xxx"  

If you use different DO pins than default D0,D1,D2,D3 change those pins.

In program also change module ID to correspond to those in user interface:

#define MODULE_ID_1     1

#define MODULE_ID_2     2

#define MODULE_ID_3     3

#define MODULE_ID_4     4

 

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 NODEMCU ESP8266, but you can use any other type of ESP8266 with all pins exposed. In our program we connect D0,D1, D2 and D3 to circuit which controls relays. Relays are using 5V ESP8266 is using 3.3V. To connect 3.3V ESP8266 to 5V relay module we use one NPN transistor 2N2222. Connection is shown below:

 

 

Ground is common, ESP8266 is on 3.3V, relay module is on 5V. You can use relay module with more relays (2,4,8).

 

EasyIoT Cloud web interface

 

EasyIoT Cloud Android application

 

Modules can be manually renamed in EasyIoT Cloud configuration to suits your need.

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 Wifi Relay Module

ESP8266 Wifi Relay Module Transmission Distance 400m 10A/30V DC For Smart Home

ESP8266 Network Relay WIFI Module

ESP8266 10A 220V Network Relay WIFI Module Input DC 7V~30V

ESP8266 ESP-12 Serial WIFI Wireless Transceiver Module

ESP8266 serial Port WIFI wireless module ESP-12

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

1 Channel Isolated 5V Relay Module

Be able to control various appliances, and other equipments with large current.

2 Channel 5V Relay Module With optocoupler

Be able to control various appliances, and other equipments with large current. 2 channel.

4 Channel 5V Relay Module With optocoupler

Be able to control various appliances, and other equipments with large current. 4 channel.

8 Channel 5V Relay Module With optocoupler

Be able to control various appliances, and other equipments with large current. 8 channel.

100Pcs NPN Transistor TO-92 2N2222

All purpose NPN transistor.

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

5pcs 3.3V regulator module 800mA

Basic regulator module based on AMS1117.

MB102 Breadboard Power Supply Module 3.3V 5V Arduino Board+Breadboard 830 Point

Breadboard Power Supply Module 3.3V 5V +Breadboard 830 Point

40PCS Jumper Cable Male to Female

Jumper Cable Male to Female

 


Comments   

#3 Jon 2017-03-20 12:06
Quoting Super User:
Quoting Jon:
I get an error when compiling the sketch...

'D0' was not declared in this scope


Did you select NodeMCU board in Boards manager?


No I used generic ESP-8266. I will give that a try.
#2 Super User 2017-03-20 09:45
Quoting Jon:
I get an error when compiling the sketch...

'D0' was not declared in this scope


Did you select NodeMCU board in Boards manager?
#1 Jon 2017-03-17 13:32
I get an error when compiling the sketch...

'D0' was not declared in this scope

You have no rights to post comments