NodeMCU - Smart Home Switch
by ItzikC1 in Circuits > Wearables
12494 Views, 42 Favorites, 0 Comments
NodeMCU - Smart Home Switch
![photo_2017-04-08_08-05-43.jpg](/proxy/?url=https://content.instructables.com/FF3/1UIV/J16P082R/FF31UIVJ16P082R.jpg&filename=photo_2017-04-08_08-05-43.jpg)
![photo_2017-04-08_08-05-19.jpg](/proxy/?url=https://content.instructables.com/F1V/JMTV/J16P082S/F1VJMTVJ16P082S.jpg&filename=photo_2017-04-08_08-05-19.jpg)
![photo_2017-04-08_08-04-03.jpg](/proxy/?url=https://content.instructables.com/FXQ/OA6H/J16P082V/FXQOA6HJ16P082V.jpg&filename=photo_2017-04-08_08-04-03.jpg)
![smart Home Switch - NodeMCU](/proxy/?url=https://content.instructables.com/FLW/SCPS/J16P083O/FLWSCPSJ16P083O.jpg&filename=smart Home Switch - NodeMCU)
![Smart Switch NODEMCU](/proxy/?url=https://content.instructables.com/F65/KHZT/J16P0855/F65KHZTJ16P0855.jpg&filename=Smart Switch NODEMCU)
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...
What Do We Need
![photo_2017-04-08_08-04-55.jpg](/proxy/?url=https://content.instructables.com/FQY/NR8V/J16P08A8/FQYNR8VJ16P08A8.jpg&filename=photo_2017-04-08_08-04-55.jpg)
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.
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...
Flashing the Firmware
![firmware.PNG](/proxy/?url=https://content.instructables.com/FQB/XC5V/J16P0ABA/FQBXC5VJ16P0ABA.png&filename=firmware.PNG)
![Flashing1.PNG](/proxy/?url=https://content.instructables.com/FVJ/B6GL/J16P0GMR/FVJB6GLJ16P0GMR.png&filename=Flashing1.PNG)
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:
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 :)
Coding NodeMCU
![esplorer.PNG](/proxy/?url=https://content.instructables.com/FG6/I00F/J16P0GP7/FG6I00FJ16P0GP7.png&filename=esplorer.PNG)
![site.PNG](/proxy/?url=https://content.instructables.com/FVY/U7XJ/J16P0H61/FVYU7XJJ16P0H61.png&filename=site.PNG)
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:
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.
Preparing the Website
![db.PNG](/proxy/?url=https://content.instructables.com/FO3/KMRB/J16P0HGL/FO3KMRBJ16P0HGL.png&filename=db.PNG)
![firmware.PNG](/proxy/?url=https://content.instructables.com/F06/53ME/J16P0HZ6/F0653MEJ16P0HZ6.png&filename=firmware.PNG)
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