Can NodeMCU act as a web server

How to create an interactive TCP server with NodeMCU on the ESP8266

How to create an interactive TCP server with NodeMCU on the ESP8266

Recommended level



This is part 2 of an advanced series on the Internet of Things. Part 1 is here. This project is relatively straightforward, but it still involves working closely with the Linux terminal. Before doing this project, you should be familiar with both Linux and the Lua scripting language. Here you will find some documentation on Lua as well as the documentation on the NodeMCU API. Remember doctors are your friend!

Last time we discussed how to customize and compile the NodeMCU firmware using open source tools from the source. Assuming a working NodeMCU installation, we will now explore some of the actual capabilities of the device and begin to interact with the "internet" and "thing" aspects of the IoT. Starting with a new flash of the NodeMCU firmware, this project will go through the connection with the device with Screen and use the built-in Lua interpreter to set up the file system and connect to the network. We will then look at how we can automate the scripting process with Luatool and put it all together with a basic TCP server that can interact with a pulse width modulated LED, potentiometer and switch.

Supplies are needed:

  • Linux Computer (Ubuntu 04/15 Vivid Vervet)
  • NodeMCU Devkit v0.9 compiled with the following user modules: Node, file, GPIO, WiFi, Net, PWM, timer, ADC, UART
  • WLAN access
  • 1x LED
  • 1x 100-300 ohm resistor
  • 1x 5K resistor
  • 1x 10K potentiometer
  • 1x normally open button


  • Git
  • Python 2.7
  • Pyserial
  • Linux screen
  • Luatool

To install this in Ubuntu 15.04, type the following into the terminal:

First connection with NodeMCU

1) Connect with NodeMCU:

The entire interaction with NodeMCU takes place via the serial port with 9600 baud. I prefer to use Screen in the terminal, but Minicom or any program that can interact with the serial port will do it. Plug in your NodeMCU devkit and enter the following:

If your NodeMCU devkit is on a different serial port than, use it to find it.

This should just show a simple black terminal screen with no text. After connecting, press the USER button to reset the device. Or type the following into Terminal and hit Enter:

Push this little guy here

You should now get a bit of gibberish (communicating at a different baud rate) and then some sort of welcome message and a '>' prompt. You are now in the Lua interpreter of NodeMCU. The init.lua error is expected as this is a brand new installation and we haven't sent any scripts to the device yet. Init.lua is what NodeMCU does once at startup (similar to the setup () function in Arduino).

The interpreter is located in the computer "background: #eee; border: 1px solid #ccc; padding: 5px 10px;">

When the process is complete, you can view information about the file system using the file.fsinfo () call:

This will show you the total size, free space and used space of the NodeMCU file system only, not the unformatted storage information of the ESP8266. The ESP8266 has 64 KiB of command RAM, 96 KiB of data RAM and about 4 MiB of external flash memory.

Three and a half megabytes? We can work with that.

Note: If you see something like "stdin: 1: unexpected symbol near 'char (27)'" or "stdin: bad header in precompiled chunk" while typing commands, don't worry: it's just a tick from screen. Hannes Lehmann wrote on his website about the topic: "... If you get errors like" stdin: 1: unexpected symbol near 'char (27)' "or" stdin: bad header in precompiled chunk ", then your terminal does that don't support backspace or arrow entry (either copy and paste problem, or you made a correction to your entry). Don't worry, just repeat the command. "

My particular setup with Screen seems to be that it can handle backspace corrections, but it tends to mess up when I try to use the arrow key.

Hello world, hello WiFi!

3) Connect to the WiFi network

Since the main advantage of the ESP8266 is the WiFi stack, we will use the Lua interpreter to connect to the local network and get an IP address.

The interactive Lua terminal on the device is well suited for prototyping small pieces of code. To connect to your local WiFi and view the IP information, enter the following into the terminal:

We are connected!

4) Automation with Luatool

Testing small pieces of code with the interpreter is great, but what if you write something more complicated and want it to run automatically on startup? Github user 4refr0nt wrote a program called Luatool that can upload Lua scripts from your computer to the NodeMCU devkit and save them on the device's file system. Navigate to the Luatool folder you cloned at the top of Github:

It should contain two files in addition to init.lua and main.lua. Use your favorite editor to change the appropriate files as follows:

Download code

Hello, NodeMCU !!!

") End) conn: on (" sent ", function (connection) connection: end () end) end)

Download code

5) Close your current screen session (otherwise Luatool cannot communicate with NodeMCU) and then upload both files to the NodeMCU:

6) Re-establish the connection to the NodeMCU Devkit with Screen and press the USER key to reset the device:

You should then see something like this:

Go to that IP address (in my case, and Voila!

Listen! We have a little, little server!

Now for some hardware

7) Build the circuit and upload the server code

Here is the scheme. This is pretty straightforward as it is mostly software, the hardware designed in Fritzing, my own setup

Now edit the files init.lua and main.lua from before and look like this:

Download code

ESP8266 turn signal thing!

\ n ') - Images ... only because conn: send (' \ n ') - Labels conn: send ('

ADC value: '..adc_value ..'

') conn: send ('

PWM frequency (input high): '..adc_value ..' Hz

') conn: send ('


') conn: send ('

PWM duty cycle (input low): '.. (adc_value * 100/1024) ..'%

') Buttons conn: send (' \ n ') conn: send (' \ n ') conn: send (' \ n ') conn: send (' \ n ') conn: on ("sent", function ( conn) conn: close () end) end) end)

Download code

Close all active screen sessions for Luatool again and upload both files to the NodeMCU:

8) Reconnect to the NodeMCU Devkit with Screen and press the USER key to reset the device:

Here is a video of the project in action:

What's going on here?

When the user presses the "Read ADC" button in the browser, the browser is updated with the current ADC value of the potentiometer, and this value is sent to the serial terminal of the NodeMCU when it is open. If the push button is not pressed, the input pin will be pulled up meaning that the current ADC value will be used to set the PWM frequency of the LED. When it is pressed and the input pin is pulled low, the PWM duty cycle of the LED is set. Depending on which LED parameter is set, you will also get a different image in the browser.

Now let's take a moment to dig through the code to see how this is implemented. Init.lua mainly contains code from the "Hello Word, Hello WiFi" section. It will display some information about the chip and connect to the wireless network. Main.lua is where all the fun happens - it's a modification of the code here. The flow of this script is to print IP information, initialize some global variables, configure the I / O, and then create a TCP server that will listen on port 80. Every time a button is pressed, an HTTP POST method is called by the browser. The string.find () method in the script looks for the HTTP header and tries to find a mention of a button called "mcu_do". When it does, the esp_update () function is called and, depending on the value assigned to mcu_do, it either reads the ADC or updates the parameters of the LED. And there you have it, a bit of hardware that can meaningfully interact with a browser and vice versa.

closing remarks

This project just scratches the surface of what you can do with the ESP8266 and NodeMCU. It can act as an MQTT broker, speak both UDP and TCP, perform cryptography, communicate with peripherals via I2C and SPI, and much more. The NodeMCU Devkit is a powerful hardware device that can enable very powerful IoT applications, but it is by no means the only or even the best solution for all projects. Keep your eyes peeled as new software and hardware solutions dive into the thriving IoT landscape.

Give this project a try for yourself! Get the parts list.