Intro: NodeMCU - Smart Home Switch
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
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:
2. Similarly you can use any relay module you wish, I bought this item:
3. Regarding the wires I am preferring using a jumper wires (maybe you can find a cheaper seller):
So when you have the ingredients I think we can start to prepare the software framework.
Step 2: Preparing the Software Framework
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:
preparing the firmware:
So before flashing the firmware we need to prepare the firmware, there are also several ways:
I am recommend using this method:
I already prepared for you the necessary build, you can download it here:
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
- 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:
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:
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: