Introduction: NodeMCU, LED & Static IP
On the internet you can find a lot of tutorials on how to program NodeMCU and how to manage modules through a web browser. They are all with a dynamic IP address, which is not bad as a tutorial, but it's not good to create a stand-alone project. Each reset of the router in this case, the IP address changes and it happens that every time after resetting you, we connect NodeMCU from the computer to see the new IP address. The solution is in a static IP address within the home network.
The ingredients for this recipe are:
Connection according to schema. Pins, of course, can be used as desired. I used D7 and D8, or better say GPIO13 and GPIO15. Only pay attention when connecting LEDs. Anode is longer (+) and the cathode is shorter (-).
Step 1: Sketch
Time for sketch. On the start we add a library to work with the ESP8266 module. Further we fill the SSID and the password lines of our home network. The next lines are the addresses. In the first place, we set up a static IP address. When selecting this address, one should keep in mind the limit of the router and the number of connected wi-fi devices. We set gateway and subnet addresses to our router. Next is the definition of output pins and variables that we will need later. Finally, we run the WIfi server. In the void setup section we set the output pins to LOW, we connect the module to the network and all of it is printed on the serial monitor. Serial.println (ip) should print the same address that we set above. In the void loop section except for printing the current state of the module on the serial monitor, which is important for removing any errors, we change the state of the output pins. Depending on what is sent over the web browser, we change each pin from LOW to HIGH and vice versa. The next step is to set up the html page. For this, we use the client.println function. The page can be formatted as desired, but the quotation (") should be keep in mind.
Step 2: Code
You can download .ino file here.
Step 3: Conclusion
By typing the IP address in the web browser's URL, we get to the html page we made. We can access it via any computer, tablet, mobile phone ... as long as they use the same router. Regardless of whether the router will be restarted for some reason, the IP address will remain the same.
As you can see, it's a simple project with plenty of room for expansion. With the basic knowledge of web programming languages, GUI can be made as desired. As an example I added basic CSS, support for UTF8 letters, but in principle the page can be formatted as you like. For a potential project that will be used in the long run, one should consider the responsive template because of the screen of mobile devices. My small flaw is to print the LED status of a web browser, where there is a small bug when another LED is lit after the first one. This requires the use of the sessions and time for necessary programming, and my long-awaited vacation is finally coming. Be free to edit the sketch at will and place the updates below. I wont be mad.
So much from me until the next sketch.
9 Comments
4 years ago on Step 2
any ideas on how to solve mDNS problems on windows 10?
4 years ago on Step 1
i like what you have done ; it has been of great help.thanks
Question 5 years ago on Step 3
I'm using ESP8266_01 module flashing code using Arduino UNO.
facing two problems
1. whenever i flash code into ESP8266_01, using my mobile hotspot as wifi network, it getting connected giving messeges like
"Connecting to MyMoto
.....
WiFi connected
Server started
Type this address in URL to connect: http://192.168.0.107
/
"
but on opening the link on my mobile(same mobile which i'm using for Hotspot or any other mobile which is connected via same wifi network) giving error message : "connection refused or connection time out"
and i can see a new wifi network named "ESP8266"
2. Everytime if i switch off the power supply of circuit, i have to re-flash the code.
5 years ago
You must set static IP according your router settings. I changed my router so my static IP now should be 192.168.2.107
Check your router settings first.
5 years ago
Hello Dekip75,
after I uploaded your code, my ad in the serial monitor looks just like yours. However, if I want to open the IP 192.168.0.107 in my browser, I get no connection. Do you have a tip where I have to change something? At my router? At the IP addresses? In the code?
//????????
IPAddress ip(192, 168, 0, 107); //set static ip
IPAddress gateway(192, 168, 0, 1); //set getteway
IPAddress subnet(255, 255, 255, 0);//set subnet
Many Thanks!
Cheers, Merzo
5 years ago
Many Thanks man, I have this NodeMcu controlling some ligths on my house and since I have noticed some days stops working, I wii add a statip ip adress.
Thanks a lot!.
-Alex.
5 years ago
Thanks. I am glad you like it. Please don't forget to share. I have bunch of ideas to made an instructable. Your feedback means a lot.
5 years ago
Thanks...very helpful!
5 years ago
Thanks for sharing :)