Let’s Do Something with the ESP8266 using NodeMCU

By December 10, 2015ESP8266, IoT

Get to a Prompt

Up until now, I’ve been putting out a lot of high-level background information about the ESP8266 and different firmware in order to establish a baseline of knowledge about the platform and its history. Now that you [hopefully] have NodeMCU installed onto your own ESP8266 device, let’s do something with it.

Connect your device to the USB port, and open a serial terminal program.

If you’re looking for a serial terminal for Windows, then you may want to try out RealTerm. It has become my go-to terminal lately, though some things are rough around the edges.


On the Display tab, set the “Display As” to “Ansi”. On the Port tab, change the Baud to 9600, and then pick the COM port attached to your device. I usually click the “Change” button, and then click the “Open” button twice (once to close the current connection, and once again to open it with the parameters set).

Are you using Linux instead? Then screen should be good enough for this exercise (else you likely already have another favorite terminal program).

To exit the screen program, type Ctrl-A then K.

At this point, you should be able to press Enter a few times and see a > prompt. If not, try resetting your ESP8266 by briefly holding the RESET pin low (or press the RESET button, if equipped, which does the same thing). Garbage characters will appear on the screen – this is actually debug data that is sent at 76800 baud during the reset, if you ever wanted to see it. Finally, once NodeMCU starts, the baud rate will be set to 9600 and you’ll see banner text describing the build along with a > prompt.

Execute Some Commands

At this point, you’re in an interactive Lua prompt, or REPL (Read-Eval-Print Loop) where commands that you type will be executed right away.

Let’s see how much RAM is available:

Note: In this tutorial, you type what’s on the lines with the > prompts, and the output from NodeMCU will appear after.

35,368 bytes of RAM in my case (your results will vary based on the firmware that you installed, and how much activity was performed). 35 KB is not a lot of RAM in today’s world where we are used to having gigabytes available. But, compare this to Arduino platforms: the ATmega328 only has 2 KB total, and the ATmega2560 only has 8 KB total. The 35 KB that NodeMCU reported for me was the available memory after the SDK and firmware had taken their share of the total 96k that the ESP8266EX provides.

“node” is a global object that contains functions, and “heap” is one of those functions. The command “=node.heap()” is equivalent to “print(node.heap())” while in the REPL.

Let’s get some more information about our ESP8266 device:

Okay, numbers. But, what do they mean? Well, first, it’s important to understand that functions in Lua can return multiple values. So, what we’re seeing here are eight different values that were returned from that single function call. We can capture the values into variables using this syntax:

Nothing printed. But that’s okay, because each of those numbers from before are now stored in a more meaningful variable name that we can still print out:

Breakdown of the variables:

  • majorVer, minorVer, and devVer allow you to identify the version of the firmware that is running.
  • chipid is a unique number that is assigned to the ESP8266EX during manufacturing
  • flashid is a flash device id. Note: The bytes are in reverse order. This number in hex is 0x1640E0, which means: Manufacturer 0xE0, Device ID 0x4016
  • flashsize is the size of the flash chip, in kilobytes.
  • flashmode of 2 indicates that the system is using Dual IO (2 lines for the data)
  • flashspeed of 40000000 indicates that the system is using a 40 MHz clock to communicate between the ESP8266EX and the flash memory chip

Okay, so that was really boring, and aside from chipid, you’ll probably never use that information again. So in the future, just get the chipid directly:

How about a quick Hello World?

Uh, oh, what have we done? And how do we stop it?

First things first: notice the double >> prompt. While in the REPL, the first line was not a complete statement, and since Lua is not particular about whitespace or linefeeds in the middle of statements, the >> prompt serves as a continuation prompt to allow you to finish working on what was started above.

The “tmr” module stands for timer, and the .alarm() function works like setTimeout in JavaScript. Here, we’re saying to set an alarm on timer #0 (there are 7 total, 0-6) so that every 2000ms, the callback function will execute. The “1” argument stands for repeat (if you use “0” instead, then it will be a one-shot alarm).

So, here we are with “hello world!” showing up in the console every two seconds. How do you stop it? CTRL-C doesn’t work.

The trick is to just keep typing a command while the output is coming – your input will be preserved as a single line even if it’s visually broken up by “hello world!” being printed. To stop a repeating alarm, just use:

(the “0” in this case must match the timer id that you wish to stop).

You probably picked up the ESP8266 because of its Wi-Fi capabilities, right? So how about an introduction to the wifi module:

The ESP8266 can act as both a Wi-Fi Station (client) and Access Point. For configuration purposes, each of these roles are accessed by means of wifi.sta and wifi.ap. In this example, you can see that there are different MAC addresses assigned to each role.

Do you notice anything interesting about the MAC addresses? The last 3 octets (06:28:53) in decimal equals 403539… which happens to be the same thing that node.chipid() returns!

Connecting your ESP8266 to a local Wi-Fi network (802.11 b/g/n on 2.4GHz only) is done with the following:

This will connect to an access point named “SSID” using the password of “password”. Connection takes a couple of seconds, but once connected, we can perform socket operations, like perform a DNS lookup:

These examples and more are available on nodemcu.com and the NodeMCU Firmware GitHub ReadMe.

This was just a taste of the ESP8266 functionality that NodeMCU exposes. In the next post, we’ll take a look at the file system and writing reusable scripts.

The following two tabs change content below.
  • Do you realize that RealTerm is a Delphi app? Delphi has long been an active player in the hardware interfacing space.

  • Jason Follas

    @kylix_rd:disqus Nope, didn’t really think about the technology behind it – I just picked a terminal program from the list of available choices. How appropriate for this blog series, given the building where our office is located (and who first wrote an ActiveX control in Delphi). 🙂

  • Pingback: Let’s Do Something with the ESP8266 using NodeMCU | TRIFORCE STUDIO()