MicroPython and the NodeMCU ESP8266
Tags: micropython, nodemcu, iot, esp8266
Published: 2017-05-03 14:01:51

---
title: MicroPython and the NodeMCU ESP8266
published: true
description: Installing MicroPython on a NodeMCU ESP8266
tags: MicroPython, NodeMCU, IoT, ESP8266
---

In a <a href="https://www.kenwalger.com/blog/python/an-overview-of-micropython/">previous post</a> I looked briefly at <a href="http://www.micropython.org">MicroPython</a> 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 <a href="http://nodemcu.com/index_en.html">NodeMCU</a> 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 <a href="https://www.ebay.com/p/?iid=191607499973&amp;lpid=82&amp;&amp;&amp;ul_noapp=true&amp;chn=ps">eBay</a> for under $5.00.Â Granted that is from a factory overseas, but for under $10.00 you can get one on <a href="https://www.amazon.com/gp/product/B01N0B48NI/ref=as_li_tl?ie=UTF8&tag=kenwalgersite-20&camp=1789&creative=9325&linkCode=as2&creativeASIN=B01N0B48NI&linkId=dc1e9f82e2beb42bca36616a22b8820d">Amazon</a>. 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 setup 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 <a href="https://www.amazon.com/gp/product/B01N0B48NI/ref=as_li_tl?ie=UTF8&tag=kenwalgersite-20&camp=1789&creative=9325&linkCode=as2&creativeASIN=B01N0B48NI&linkId=dc1e9f82e2beb42bca36616a22b8820d">NodeMCU ESP8266</a>Â device for this tutorial. Feel free to use a different physical device with the same chip, just know that some settingsÂ <em>may</em> be different based on the physical device configuration. The board has a USB interface which, when connected to a PC provides power.
<h3>Firmware</h3>
We obviously need to get a copy of the MicroPython firmware. It comes in a <code>.bin</code> file format that can be loaded directly to the ESP8266 device and can be downloaded from the MicroPython site <a href="http://micropython.org/download#esp8266">here</a>. For this tutorial I will be using the <code>esp8266-20170108-v1.8.7.bin</code> 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 <a href="https://github.com/espressif/esptool/">esptool</a> 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:
<pre>python -m pip install esptool
</pre>
This version of esptool supports Python version 3.4 or newer.
<h4>Serial Ports</h4>
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 it's current state. Assuming the the port name of the device is <code>COM4</code>, we can accomplish that with the following command:
<pre>esptool.py --port COM4 erase_flash</pre>
Just substitute COM4 for whichever communication port your particular device is using. It may be something similar to <code>/dev/ttyUSB1</code> on a non Windows machine and from a terminal port you can use the command <code>ls /dev/tty.*</code> to detect the port. Also, depending on your ESP8266 device you might need to install the <a href="https://www.silabs.com/products/development-tools/software/usb-to-uart-bridge-vcp-drivers">USB to UART Bridge VCP Drivers</a>Â 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:
<pre>esptool.py --port COM4 --baud 460800 write_flash --flash_size=detect 0 esp8266-20170108-v1.8.7.bin
</pre>
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?
<h3>Serial Terminal</h3>
We can now connect to the device using a serial terminal tool, like <a href="http://www.putty.org/">PuTTY</a> 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:

<a href="https://www.kenwalger.com/blog/wp-content/uploads/2017/05/REPL-prompt.png"><img class="alignnone size-medium wp-image-217" src="https://www.kenwalger.com/blog/wp-content/uploads/2017/05/REPL-prompt-300x190.png" alt="" width="300" height="190" /></a>

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.
<h3>MicroPython code - Try it Out!</h3>
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 <code>machine</code> which provides the necessary software interface. Then we can change the state of a given GPIO pin...
<pre>&gt;&gt;&gt; import machine
&gt;&gt;&gt; pin = machine.Pin(2, machine.Pin.OUT)
&gt;&gt;&gt; pin.high() Â # light off
&gt;&gt;&gt; pin.low() Â # light on
&gt;&gt;&gt; pin.high() # light off again
</pre>
There we have it! We have take 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.

<hr />

<em>Follow me on Twitter <a href="https://www.twitter.com/kenwalger">@kenwalger</a> to get the latest updates on my postings.</em>