MicroPython and the NodeMCU ESP8266

In a previous post, I looked briefly at MicroPython and it’s place and role in the Internet of Things (IoT) arena. In this post, I’d like to walk through the process of getting a NodeMCU ESP8266 device flashed and up and running with MicroPython. Then we’ll do the, almost required, IoT version of “Hello World”… a blinking LED light.

There are multiple micro-controller options available for using MicroPython so why, you may ask, have I decided on the NodeMCU? First and foremost they are inexpensive. You can find the NodeMCU ESP8266 development boards on eBay for under $5.00. Granted that is from a factory overseas, but for under $10.00 you can get one on Amazon. The second reason I like this board, in particular, is that it has LED lights built into the board so I don’t necessarily need to break out LED bulbs and resistors. Another big plus is that it is WiFi ready and with MicroPython that means that one can set up a web interface with the board and also readily make truly connected projects without additional hardware.

All that said and done I’ll be using a basic NodeMCU ESP8266 device for this tutorial. Feel free to use a different physical device with the same chip, just know that some settings may be different based on the physical device configuration. The board has a USB interface which, when connected to a PC provides power.


We obviously need to get a copy of the MicroPython firmware. It comes in a .bin file format that can be loaded directly to the ESP8266 device and can be downloaded from the MicroPython site here. For this tutorial, I will be using the esp8266-20170108-v1.8.7.bin version of firmware. You will want to download that to your computer and remember where it is saved.

Now we need to deploy the firmware to our device. If you are using the above-mentioned board, deploying the firmware should be relatively straight forward. If you are using a different ESP8266 board, check with the manufacturer of your board for the proper flashing technique. For the rest of us, we will want to start with the esptool and use it to move the firmware over to our device. The tool can either be downloaded from GitHub directly or can be installed using pip with:

python -m pip install esptool

This version of esptool supports Python version 3.4 or newer.

Serial Ports

Once we have that installed on our system and the device connected via the USB port, we should, for best practice, clear the device by erasing its current state. Assuming that the port name of the device is COM4, we can accomplish that with the following command:

esptool.py --port COM4 erase_flash

Just substitute COM4 for whichever communication port your particular device is using. It may be something similar to /dev/ttyUSB1 on a non Windows machine and from a terminal port, you can use the command ls /dev/tty.* to detect the port. Also, depending on your ESP8266 device you might need to install the USB to UART Bridge VCP Drivers the be able to detect the device via a serial connection. Once the device has been flashed (it takes less than 10 seconds on my machine) we upload the firmware to the device with the following command:

esptool.py --port COM4 --baud 460800 write_flash --flash_size=detect 0 esp8266-20170108-v1.8.7.bin

The filename of the firmware should match the downloaded firmware. Assuming you don’t get any errors during the flashing process, you now have a NodeMCU ESP8266 device running MicroPython. Pretty slick, eh?

Serial Terminal

We can now connect to the device using a serial terminal tool, like PuTTY for Windows, the screen program on Mac, or something like picocom for Linux. I’ll be using PuTTY since I’m currently on a Windows 10 machine.

We’ll want to select our serial port again, in my case it is COM4 and set the speed, or baud rate, to 115200. Then we can open the connection and we should be prompted with a Python read-evaluate-print-loop (REPL) interface as shown:

We can start entering in Python commands on the device itself. Let’s have our device do the IoT version of “Hello World” and turn on one of the on-board LED lights. These lights are controlled through General-Purpose Input/Output (GPIO) pins and there are two of them on board. GPIO 2 controls a small blue LED on the board.

In the REPL then, we want to be able to turn the light on and off. That can be done by altering the state (electrical charge) provided to the GPIO pins, typically by changing the state from low to high and back to low.

Here is something that I discovered when working with this particular LED and GPIO setting and board. On this board, the LED on GPIO 2 (and 16 for that matter) is wired between the pin and the power, so when we set the pin state to low, the light is activated, and when set to high, it is off. There is active development on this issue and firmware updates may have already addressed the issue. However, if one connects an external LED light to, say GPIO 5 along with the required resistor (~300+ ohms), we get the expected light on with a high state, and off with a low state.

MicroPython code – Try it Out!

Okay, so technical issues aside, let’s look at the code necessary to turn on our LED. First, we’ll need access to the board’s hardware. We can import a package called machine which provides the necessary software interface. Then we can change the state of a given GPIO pin…

>>> import machine
>>> pin = machine.Pin(2, machine.Pin.OUT)
>>> pin.high()  # light off
>>> pin.low()  # light on
>>> pin.high() # light off again

There we have it! We have taken a NodeMCU 8266 device, flashed it with MicroPython, accessed it through the serial port, and with minimal code, turned the on-board LED lights on and off.

Congratulations on your first venture into IoT and Python! There is a lot more that can be done with this $5.00 board and I think I will spend some time experimenting with it a bit. I’ll post my results and findings here as I work through the process.

Follow me on Twitter @kenwalger to get the latest updates on my postings.


New version… new features.

If you get involved in the software development industry, or heck, even just use a computer, you know that vendors pump out new versions of their software very frequently. It often seems like just when you get a handle on version 10, version 11 is introduced. Making the learning curve start all over. I’m so stubborn that I’m personally still on Microsoft Office 2003… I know, don’t laugh.

Sometimes new versions offer nothing but bug fixes and additional headaches. Other times new software versions come packed with a wide assortment of immediately useful new features. Such is the case of the latest release of MongoDB, 3.4, released in December of 2016.

I’d like to take a look at some of the included new features in this latest version for developers. There are several new features more suited for a discussion from an administrator standpoint, such as zones, elastic scalability enhancements, consistency control, and improvements to security for database access. For this post though, let’s stick to a discussion about some of the development-oriented features.

Version 3.4 provides some excellent improvements for data processing and aggregation, international language considerations, and third-party application connection tools.


They have brought the ability to do graph processing directly into MongoDB with the new $graphLookup aggregation stage. This new stage allows MongoDB to recursively lookup documents based on a specific relationship to a starting document. There are, of course, many uses for this feature alone. Such as graphing social networks, business entities, genealogical family trees, etc. One can now utilize the power of a graph database natively within MongoDB. For those that have used a separate graph database, like Neo4j, they can now process their data using a single MongoDB datastore and get graphs along with other business critical data. With the rise of applications that are connected globally, being able to graph relationships is becoming a larger and larger need.

Additionally, it is becoming more and more critical to be able to offer localized language considerations to your application. My family just got done hosting an exchange student from Germany. She let us know that German phone books and German dictionaries sort words differently. Well, it would be great to be able to have the ability to account for these language rules and nuances. MongoDB 3.4 has support for over 100 different languages and locales. One can specify a collation based on collection, index, or view. Further, several operations support collation as well, such as find, aggregate, and update.

Big Data Tools

A couple of additional things which personally got me excited were some updates to the MongoDB Connector for BI and the MongoDB Connector for Apache Spark. As someone who enjoys being able to visualize data, the Connector for BI is very exciting. The performance improvements have been made by moving more query execution into MongoDB processes to reduce latency and bandwidth. Other improvements around the Connector for BI include a simplification of the installation, configuration process, authorization, and support for Windows.

The Spark Connector has received some nice attention as well with updates to support the latest Spark 2.0 release. For those familiar with Spark, the connector allows us access to Spark’s libraries for Scala, Java, Python, and R and brings MongoDB data in as DataFrames and Datasets. This allows us to utilize data already in MongoDB to be analyzed through Spark’s tools for machine learning, graph, streaming, and SQL APIs and provide shorter turnaround times for data scientists and engineers.

Wrap Up

There are several other features new in 3.4 as well, such as faceted navigation, enhancements to the aggregation pipeline, and a support for the decimal data type. In previous posts, I have mentioned some of the advancements to MongoDB Compass and their DBaaS offering Atlas. There are a lot of exciting things in all of these new and/or enhanced features that make version 3.4 worth examining further.

I am excited to continue to examine the possibilities especially around $graphLookup, multi-language collations, and the various connectors. I know that not every organization is in a place and able to upgrade to 3.4 today, but it is definitely worth a look. You can download it in their download center here.

Follow me on Twitter @kenwalger to get the latest updates on my postings.