in Automation, Electronic

First steps with the ESP8266 micro controller

ESP8266 is a micro controller made by chinese Espressif…
It’s based on the famous ESP8266 micro controller and can be used for a variety of task.
Even if it’s not powerful, it’s capable enough of running different language interpreters like Node, Python, Basic and even C with Arduino compatibility.

ESP8266 micro controller

I have no previous electronic skills, but as a programmer, I found very easy to start working with nodeMcu through the use of different libraries, most of them written in MicroPython

There are different boards, but I started experimenting using this
which has a usb port for power and programming, plus already soldered pins.
And yes, it’s less than 3 Euros!

You can find online also cheaper boards (less than 2 euros), without usb, good for production but less good for prototyping.
They have pins for analogic and digital read/write

NodeMCU Pinout

GPIO number is used in the script and corresponds to a I/O Index Number written on the board.

Software for MAC

To follow this post you’ll need a bunch of software. all for Mac (it’s my Os of choice).

Drivers for Mac:
Adafruit ampy:

Getting the firmware for the ESP8266 micro controller

Even if my NodeMcu come with a pre-installed version a Lua firmware, reading on Internet I found that some other chips are shipped without firmware.

All the example in this post are written for MicroPython, so we need first fo flash the firmware.

Here you can find a list of firmwares you can flash: Lua, MicroPython (our choice), Espruino (Node), Basic and C for Arduino IDE.

Cloud build service (Lua):
Arduino IDE:

Flash the firmware

Flashing the firmware on the NodeMcu is the same operation regardless the firmware of your choice.

1 – Install drivers for OsX

Simply double click the installer you can find in the zip linked below.
It will create a virtual port in /dev/cu.SLAB_USBtoUART.

2 – Install

To flash your firmware you need a tool, and it’s
It will provide a CLI to do many operations with your NodeMcu like erasing the memory or uploading a new firmware.
I suppose you have python installed (you’re on a Mac), so you can simply install it using pip

pip install esptool

3 – Prepare your NodeMcu for flashing.

Ok, now you have your Mac set up and you have downloaded your firmware of choice.
Next step is to prepare NodeMcu for flashing.
First connect the USB cable then old down the FLASH button on the device and hit the RST button while doing so.
Good, you’re in flash mode now.

4 – Delete pre-existing firmware

To be sure you have a good environment, first delete the flash:

python --port /dev/cu.SLAB_USBtoUART erase_flash

5 – Flash the firmware

Now, to flash a new firmware, according to the firmware you choose, type the following command:

python --port=/dev/cu.SLAB_USBtoUART  write_flash  -fm=dio -fs=32m 0x00000 [image name]

… where [image name] is the firmware you choose.

6 – Done!

You’re done! Now you can reset (or simply unplug and plug) the NodeMcu and you are ready to enjoy.

Connecting via terminal

To connect via USB to your ESP8266 micro controller you need to use a terminal program, I suggest Coolterm (see the link above).

ESP8266 micro controller

ESP8266 micro controller

First script(s)

So, once you are connected you see the REPL prompt (>>>) where you can type some code:

print(“Hello world”)

… great!
Now, let’s try something more complex, with more lines.
To paste more lines at once in the REPL you need to enter the “Paste mode”, to tell MicroPython not to execute the code after each return.
To do that press CTRL+E and then CTRL+D to exit paste mode and execute the code.

For example you can try with:

import time 
for x in range((,)10):

So, remember some easy key combinations:

CTRL + E – Paste mode
CTRL + D – Exit paste mode
CTRL + C – Break program and/or cancels any input
CTRL + D – Soft reset

… it will help you working on the NodeMcu!

Connecting to a WiFi

Now, as a next step you want to connect to a wifi, how can you do that?
Easily MicroPython has a module written to manage WiFi on the ESP8266 micro controller.
So type:

import network
wlan = network.WLAN(network.STA_IF)
wlan.connect('ssid', 'password')

… and you are connected!

But what if I want to connect automatically after each boot?

Boot sequence and transferring files

If you want to read a detailed explanation on how boot process works, you can read it here:

At the moment, just remember that if you want some code to be executed at boot time, you need to write it in ‘’ (well, it’s not exactly a best practice as you can read in the link above, but it’s enough for our purposes).

So on your Mac create a file with the lines show above for connecting to the WiFi (and if you want you can type also the easy program that counts to ten, just to see something happening).

Now that you have your file you need to upload it… but how?

First method: you can write a python script to write python scripts!

filename = ''
content = """
import network
wlan = network.WLAN(network.STA_IF)
wlan.connect('ssid', 'password')
with open(filename,'w') as f:

Enter PASTE MODE (CTRL+E) on the ESP8266 micro controller, paste it, exit PASTE MODE (CTRL+D) and you’ll have your script on filesystem, ready to be launched at each boot.

But of course, there’s something smart: an utility released by Adafruit called ‘ampy’.

pip install adafruit-ampy

Now you can list files…

ampy --port /dev/tty.SLAB_USBtoUART ls

… put files (from Mac to NodeMcu)…

ampy --port /dev/tty.SLAB_USBtoUART  put [filename]

… get files (from NodeMcu to Mac)…

ampy --port /dev/tty.SLAB_USBtoUART get [filename]

… but also delete files…

ampy --port /dev/tty.SLAB_USBtoUART rm [filename]

… and of course run files:

ampy --port /dev/tty.SLAB_USBtoUART run [filename]

Note 1:
If you don’t want to type port name each time you can do this trick:

export AMPY_PORT=/dev/tty.SLAB_USBtoUART

… and then just use:

ampy ls

Note 2:
To use ampy with the ESP8266 micro controller you need that no other app is using the NodeMCU connected USB port, so remember to close CoolTerm before running it

  • Related Content by Tag