Home / Tutorials / ESP8266 Tutorial / Creating a Simple NodeMCU Web Server
Advertisement
nodemcu-webserver

Creating a Simple NodeMCU Web Server

We're done with basics of the NodeMCU in my last post. Now it's time to use the NodeMCU like how it's meant to be use: as an IoT device. Here I will show you how to create a simple NodeMCU web server which will control a LED attached to one of the NodeMCU's pins. Sounds interesting? Read on!

For this to work, you must have already installed the ESP8266 board development toolkit as shown in my last tutorial. Here's the code for our simple nodemcu web server:

nodemcuwebserver.ino

Notice that I used three headers on the top. These libraries are already installed after adding the ESP8266 toolkit via the board manager. Some notes on the code:

  • Use your own WiFi SSID and password for lines 6 and 7.
  • The string page contains the HTML of our web server.
  • The LED is connected to D7 as it was on the last tutorial.

 

 

What this code does is create a short HTML web page that has two buttons. If you are looking to create a much more good looking website, I suggest you use a microSD card module to house your images, or CSS and Javascript files.

Upload the code to your NodeMCU. After that, open the serial monitor on the Arduino IDE. You should see something like this:

nodemcu web server 1

This means you are successfully connected to the WiFi network! You can also see the IP address assigned to the NodeMCU (192.168.1.7 in this example). Open your preferred web browser and type that IP address on the address bar.

nodemcu web server 2

Click the "On" button and watch as your LED turns on. Click the "Off" button to turn it off.

Troubleshooting

This code has been tested many times without a problem encountered. If you've experienced errors that are displayed on the serial monitor, especially when the exception code is shown, I suggest you use the Exception Decoder.

Easy! Next, you can display data coming from a sensor to a webpage or use WebSockets instead of HTTP!

Check Also

NodeMCU Webserver

NodeMCU MQTT Tutorial

So far, we've been using HTTP in our NodeMCU web servers and while it works, …

12 comments

  1. thanks a lot for the tutorial...this is very nice and apt.

    but i am having a little problem.
    how do i obtian the ip address of my own nodemcu. as this is hindering me from viewing my logs in the webserver.

    i await your response.
    thanks once again

  2. This is a great, simple tutorial but I haven't yet managed to connect to the IP address using my browser.

    This is the third web server tutorial I have tried and though my Node board tells me its IP address and that the "Web server started!" nothing happens when I type its IP address in chrome on my local network.

    Any thoughts?

    Thanks in advance.

    • Maybe you are connecting to different Wifi.
      It is required that your NodeMCU and the device you are accessing that IP on must be connected to same network.

      I faced this problem solved by such simple method.

    • I am also getting same prblm....

  3. Nice code ....

    Thanks

  4. Can you please explain what server.on(....) does and it's syntax, or a possible source to understand this particular function

  5. as I type my ip to browser this shows nothing.............please help me

  6. Worked flawlessly. Thanks!

  7. Awesome sharing! Can you share with me how to change the input variable of ssid and password from web browser in PHP language? Thank you.

  8. This is great, thanks very much. Really nice and simple to follow as an Arduino and NodeMCU beginner. I couldn't get it to work for ages, but then I found I'd put a O instead of a 0 in my SSID, problem solved!

  9. Very good features.

    One question is the size of button in initial . It is so small for me. How to adjust them for bigger ? thank you.

Leave a Reply

Your email address will not be published. Required fields are marked *