ESP8266 Relay-control

12K16416

Intro: ESP8266 Relay-control

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:

http://ebay.eu/2iQLv3s - Relay Module with 4 relays

http://ebay.eu/2iQUwtr - D1 Mini NodeMCU Module

STEP 1: Let's Get Started!

Prepare the ESP8266-Module:

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

NodeMCU-Flasher: https://github.com/nodemcu/nodemcu-flasher

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 "EDR.zip"-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(_GET.pin == "ON")then
_on = " selected=true"

gpio.write(1, gpio.HIGH)

elseif(_GET.pin == "OFF")then

_off = " selected=\"true\""

gpio.write(1, gpio.LOW)

elseif(_GET.pin == "ON2")then

_on = " selected=true"

gpio.write(2, gpio.HIGH)

elseif(_GET.pin == "OFF2")then

_off = " selected=\"true\""

gpio.write(2, gpio.LOW)

elseif(_GET.pin == "ON3")then

_on = " selected=true"

gpio.write(3, gpio.HIGH)

elseif(_GET.pin == "OFF3")then

_off = " selected=\"true\""

gpio.write(3, gpio.LOW)

elseif(_GET.pin == "ON4")then

_on = " selected=true"

gpio.write(4, gpio.HIGH)

elseif(_GET.pin == "OFF4")then

_off = " selected=\"true\""

gpio.write(4, gpio.LOW)

end

________________________________________________________________

I am sure, you can find a scheme:

for every Realy there is this block of code:

elseif(_GET.pin == "ON_NUMBER_OF_RELAY")then
_on = " selected=true"

gpio.write(GPIO_NUMBER, gpio.HIGH)

elseif(_GET.pin == "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:

gpio.mode(GPIO_NUMBER.OUTPUT)

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 (EDR.zip).

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

14 Comments

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
http://192.168.1.105/off1 and
http://192.168.1.105/on1 .
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,
Well, that went faster than I was expecting it...
I updated the EDR.zip with an english version of my code! You should now be able to understand it completly :-D
Wow ....Thank you so much buddy.
Let me have look on it :)

Danke ,
Hey there,
I unable to see English versoin of code.
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 :-)
can this program be used on a esp8266 module?
Yes, it is made for one as I wrote in the first paragraph ;-)
hello
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???

Hi,

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 ;-)

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

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 ;-)