ESP8266 Relay-control

by HeinzA1 in Circuits > Wireless

12856 Views, 165 Favorites, 0 Comments

ESP8266 Relay-control

DSC_0311.JPG
DSC_0304.JPG
DSC_0308.JPG
DSC_0310.JPG
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

Let's Get Started!

ESPlorer.PNG

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.

Something About the Code...

Screenshot_20161230-231454.png
Screenshot_20161231-180649.png
Screenshot_20161231-180658.png
Screenshot_20161231-180704.png
Win2.PNG
Win1.PNG
Win3.PNG

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.

Downloads

The Ending....

So, that's all.

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

-Have fun! :D