Introduction: NodeMCU - Smart Home Switch

Hi everybody,

This guide purpose is to show how you could control any remote switch remotely over the internet without maintenance any static IP address.

This guide is not required any previous knowledge, but basic code knowledge is recommended.

Before starting read this article, I want to clarify that the writer of this article is not take any responsible of outcome using this guide, If you are using a high voltage it is in your own risk!

you can look at the pictures and movie above to see the final result.

So lets see what is the necessary ingredient in the next chapter...

Step 1: What Do We Need

Ingredients

So first of all we need this items:

  • NodeMCU (3.5$)
  • Relay Module (1$)
  • Wires (less than 1$)
  • Extension cord (should be found in every house)
  • Hands and a little working brain :)

1. I bought my NodeMCU at this link but it can be found in a lot of place and variant:

http://www.ebay.com/itm/NodeMcu-Lua-WIFI-Internet-...

2. Similarly you can use any relay module you wish, I bought this item:

http://www.ebay.com/itm/5V-One-1-Channel-Relay-Mod...

3. Regarding the wires I am preferring using a jumper wires (maybe you can find a cheaper seller):

http://www.ebay.com/itm/10-x-40Pcs-Dupont-Wire-Jum...

So when you have the ingredients I think we can start to prepare the software framework.

Step 2: Preparing the Software Framework

Download this software:

1.Python

2.esptool

3.ESPlorer

4.pyserial

I am not pretend to be professional in NodeMCU so I am recommend to read NodeMCU documentation here for any other explanation.

https://nodemcu.readthedocs.io/en/master/

in the next chapter we flash the firmware into our NodeMCU...

Step 3: Flashing the Firmware

There is several ways flashing the firmware, I am recommended using ESPtools that you have downloaded in the previous step, you can read more about the other method here:

https://nodemcu.readthedocs.io/en/master/en/flash/

preparing the firmware:

So before flashing the firmware we need to prepare the firmware, there are also several ways:

https://nodemcu.readthedocs.io/en/master/en/build/

I am recommend using this method:

https://nodemcu-build.com/

I already prepared for you the necessary build, you can download it here:

https://www.dropbox.com/s/yw4477zxot5q7ll/nodemcu-...

if the download is not working or you want to see how it is done, go to this link, insert your email and choose this components (see also picture):

adc, bit, crypto, dht, enduser_setup, file, gpio, http, i2c, mdns, mqtt, net, node, pwm, sntp, spi, tmr, uart, wifi.

and click the "start build" button.

when the build finish, you will be notifies to your email with the download link.

Flashing the firmware:

Verify that you connect your NodeMCU by USB to your computer, locate esptool software that you have downloaded copy the firmware you downloaded to this folder, and by console (in windows start>run>cmd) navigate to the esptool folder and write:

esptool.py --port COM write_flash -fm dio 0x00000 Firmware.bin

Where:

  • in replace it with the com number the NODEMCU is connected to.
  • in Firmware.bin replace it with the name of the firmware name you downloaded

Congratulation, we flash the firmware :)

Step 4: Coding NodeMCU

Now open ESPlorer Folder you downloaded before, the screen of the software should be as the screen above.

click the Open button and you probably see a gibberish, in the NodeMCU should be a reset button, click it and click at the Send button in the software several times until you get a real answer.

in code section of the ESPlorer software insert this code found here:

http://codepad.org/pdwoBKd9

or here:

http://212.150.158.230/~itzikc/nodeMCU/init.lua

Note that you need to change the fields:

  • WIFI_SSD - to your WiFi name
  • WIFI_PASS - to your WiFi password

and in this line:

http.get("http://your_host_url/setHomeExternalIP.php?token=a1a1a1a1&ip=http://"..externalIP.."", nil, function(code, data)

  • replace "your_host_url" to your server.

save the file as init.lua this file is run automatically when NodeMCU is power up.

in the ESplorer application open the saved file and click the button "save to ESP", make sure that your script is compiled fine and no error accrued.

in this step you can locate the internal ip of the NODEMCU by open the port and listening to the output string.

when you enter to this ip on the same wifi you can see your website as the image above.

in the next step we will discuss how to see your website outside your network.

Step 5: Preparing the Website

So now coming the trick, if you will connect a led to pin number 6 you can turn off and on the Led, but only if you will be at the same sub network.

If we want to control our switch when we are not at home the website will not display.

So, My trick requires:

  • Server supported PHP+MYSQL

There is a lot of free server you can found on the net, so if you don't have already one, google it.

Create the DB:

using the website panel create a sql database, we need to hold the external IP of our home where the NodeMCU server found, such that when a person will look at the DB he will know what is the external ip of our home network.

if you will look at the picture above you will see only one column called home_ip which hold my external IP address.

Uploading the PHP files

there are two file we need to upload:

  • setHomeExternalIP.php - this file will get the external IP from NodeMCU every 10 secound and update the DB.
  • index.php - will search for the server that NodeMCU holds and if it exist will Iframe it, else notify of an error that the device is not found.

You can found this 2 files in the link below where I put all my project with the necessary code and software:

Download all the project here:

http://212.150.158.230/~itzikc/nodeMCU/NodeMCU_Rem...

just make sure you editing this file with your own db user, db table and db password in this two files on the relevant fields.

Don't forget to forwarding port 80 to the NodeMCU internal IP adress like the picture above, if you dont know what is the ip address of your router, type ipconfig at the command line and search the default gateway ip adress.

Hope this guide was helpfully, leave a comments here if you have any question or email me at:

itzikc20@gmail.com

Comments

author
DIY+Hacks+and+How+Tos made it!(author)2017-04-08

Nice system. I would love to have a setup like this. I am slowly trying to convert my house into a DIY smart house.