ESP8266 Relay-control




About: "Give me a problem, I'll solve it!" is my favorit saying. I try to solve any problem I find and improve nearly everything around me -reinventing things is my thing. So why shouldn't we show it to t...

Update (07.02.2017):

I've updated the Windows-Version of the program to control your ESP8266. I've added a "Small Window"-Mode ("SW"): It will dock a micro-version of the program above your taskbar on the left side so that you can access the relays more easily while you work with other programs. It will also be in the foreground all the time.

Hello, and welcome to my first Instructable!

What will this Instructable show me?

I will show you in this Instructable how to control a relay board with 4 relays -or even more- with an Android-Smartphone or a Windows-PC. If you use 4 Relays or less you can use an App for Android or Windows of mine. If you want to use more, you have to continue to develop the code yourself. So, you will get the code I developed yet.

What do I need?

You will need an ESP8266-Module. I'll use a D1 Mini version of the ESP8266 and I recommend to use this one or a similar too because I don't know if you can program other versions of the ESP8266 as easy as with a D1 Mini. You will also need, of course, a relay module like you see in the pictures.

I bought those: - Relay Module with 4 relays - D1 Mini NodeMCU Module

Step 1: Let's Get Started!

Prepare the ESP8266-Module:

First, download the following files to your Windows-Computer:


ESPlorer (is attached as ZIP-File down)

init.lua (is attached down)

Flashing the ESP8266-Module:

-Connect the ESP8266-Module with your Computer via USB and wait till all drivers are installed (your system will need a serial-to-USB-driver).

-Open ESP8266Flasher.exe from the NodeMCU-Flasher, select the correct COM-port and click "Flash". This could take a while.

Upload the script to the ESP8266-Module:

-Open ESPlorer. You will need Java to open it.

-Select the correct COM-Port in the right tab on the top and click "Open". Don't worry if it says that it can't autodetect the firmware.

-The right gray windows will be a serial monitor, the left one a code editor. Open the init.lua file.

(It should look like this on the screenshot)

-Enter your Wifi-SSID and your Wifi-Password.

-Press the "Upload"-Button.

Step 2: Something About the Code...

The programs:

You can just download the ""-File for Windows and the "EDR.apk"-File for Android.

(This part is about how much relays you need. If you want to use 4 or less, you can skip this step.)

About init.lua:

Part of the code for the relays:


if( == "ON")then
_on = " selected=true"

gpio.write(1, gpio.HIGH)

elseif( == "OFF")then

_off = " selected=\"true\""

gpio.write(1, gpio.LOW)

elseif( == "ON2")then

_on = " selected=true"

gpio.write(2, gpio.HIGH)

elseif( == "OFF2")then

_off = " selected=\"true\""

gpio.write(2, gpio.LOW)

elseif( == "ON3")then

_on = " selected=true"

gpio.write(3, gpio.HIGH)

elseif( == "OFF3")then

_off = " selected=\"true\""

gpio.write(3, gpio.LOW)

elseif( == "ON4")then

_on = " selected=true"

gpio.write(4, gpio.HIGH)

elseif( == "OFF4")then

_off = " selected=\"true\""

gpio.write(4, gpio.LOW)



I am sure, you can find a scheme:

for every Realy there is this block of code:

elseif( == "ON_NUMBER_OF_RELAY")then
_on = " selected=true"

gpio.write(GPIO_NUMBER, gpio.HIGH)

elseif( == "OFF_NUMBER_OF_RELAY")then

_off = " selected=\"true\""

gpio.write(GPIO_NUMBER, gpio.LOW)

Only one more thing. On the beginning of the code is standing:

gpio.mode(1, gpio.OUTPUT)
gpio.mode(2, gpio.OUTPUT)
gpio.mode(3, gpio.OUTPUT)
gpio.mode(4, gpio.OUTPUT)

So will also have to add:


I am sure you can handle this ;-)

About the source code of my Windows-Program:

If you want to edit this, you will have to know something about C# and you have to have a Version of Visual Studio 2015 or compatible. I'll attach the project as ZIP-File too (

About the Android-APP:

If you want the Android-App, you will need the AppInventor2. There is a free online version of it. You can open the attached "EDR.aia"-File there and edit it as you want.

Step 3: The Ending....

So, that's all.

If you have some ideas for this project, just commit it.

-Have fun! :D

First Time Authors Contest 2016

Participated in the
First Time Authors Contest 2016

IoT Builders Contest

Participated in the
IoT Builders Contest



    • Sensors Contest

      Sensors Contest
    • Planter Challenge

      Planter Challenge
    • Stone Concrete and Cement Contest

      Stone Concrete and Cement Contest

    14 Discussions


    Question 8 months ago on Step 3

    Hi Dude,
    It was really nice blog. I want to use window based application for relay on-ff. I have basic idea of C# . so could you please explain little bit how is this project working in C# ? Please explain me . As it has german comments in it :)

    In my case : I am using 2 relays and arduino with ESP8266 . I have created webmethods and .
    when on1 is called relay get on and when off1 then relay get off .
    I want to call these methods from window based application .using C#

    Thanks a lot in advance,

    4 answers

    Answer 8 months ago

    Well, that went faster than I was expecting it...
    I updated the with an english version of my code! You should now be able to understand it completly :-D


    Reply 8 months ago

    Wow ....Thank you so much buddy.
    Let me have look on it :)

    Danke ,


    Answer 8 months ago

    Hi there,
    Thank you for your comment, cool lthat you like it!
    I'll try to make a second version of the C#-Project with english comments in the next few weeks (there's a lot to work at the moment...) so that it become easier to unterstand what I did there ;-)

    But I can give you a little hint right away for calling the URLs from your C#-Application:
    In a "Windows Form"-Project in C# there is an object called "Browser" (or similar). With that you can access URLs in your Form quite easylie :-)


    Answer 8 months ago

    Yes, it is made for one as I wrote in the first paragraph ;-)


    2 years ago

    I am seeing that u r using ESP 8266-12 right!!the firmware u have used to install the Lua script in esp-12 will it work on esp8266-01???

    1 reply

    Reply 2 years ago


    I think it should work too. I'm not totally sure, but it seems like the only different is the pinout and design.
    But anyway, I won't take any warranty ;-)


    2 years ago

    Hi. I made this and it's working. But how can access from internet, not from local wireless?

    3 replies

    Reply 2 years ago

    To Alaccess it outside your lokal network you need a "special" router like Fritz!Box. Your Router needs a Kind of Domain, and if you open a connection to the Domain (e.g. via a browser) the router is forwarding the request to the local device (e.g. your Esp8266). The Problem: Most router don't support this. So you have to look it up for your own router, I'm sorry. If you get it work, let me know ;-)