Introduction: ESP8266 Controlling 4 Relays

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

Step 1: Materials

-ESP8266 WiFi module

-Relay module

-3.3V power supply

-4.7K Resistor

-NPN Transistor TO-92 2N2222

Step 2: EasyIoT Cloud Configuration

First register to EasyIoT Cloud. 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.

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:

#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

Step 4: 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.

Microcontroller Contest 2017

Participated in the
Microcontroller Contest 2017