Are you looking to program your ESP8266 wifi module using micropython?
No wonder, you have finally decided to realize the potential of Python for the hardware devices, and specifically for the WiFi enabled NodeMCU ESP8266.
Believe me! You won’t be disappointed…
With the prevalence of “the most popular language” in Data Science, Machine Learning, and Artificial Intelligence, the first question should be asking: “Is this even possible, buddy?”
Yes… This is definitely possible!
However, you will need to divert your focus from ‘Python’ to ‘MicroPython’, which is just a miniature version of the original language to support the hardware devices with minimum resources.
In this tutorial, I will show you how to program ESP8266 using MicroPython so that you can leverage the power of Python for your next IoT project.
Before diving into the nitty-gritty details, let’s explore a little bit about the MicroPython itself.
Micropython is a lean and efficient implementation of the Python 3 programming language. It includes a small subset of the standard Python library, and is optimized to run on microcontrollers in constrained environments.
It is completely compatible with the Python, and you can transfer your code across Desktop or an embedded system with no inherent issues.
In addition to the core Python libraries, micropython comes with additional modules to efficiently access and manipulate the low-level hardware.
Micropython is a full Python compiler that comes with an interactive prompt (REPL) to execute commands in run-time. The REPL has history, tab completion, auto-indent, and paste mode for an interactive user experience.
Micropython has a dedicated firmware for various microcontroller boards listed below. You can download and flash the firmware from the official repository of the Micropython:
- pyboard v1
- pyboard D Series
- STM32 Nucleo and Discovery Boards
- Espruino Pico
- Raspberry Pi Pico
- WiPy Module
- ESP8266 Module
- ESP32 Module
For this tutorial, we will be flashing the NodeMCU ESP8266 with the micropython firmware and blink the onboard LED.
If you are interested in learning more about the ESP8266 WiFi module and its programming using Arduino IDE, you can refer to the following article.
For this tutorial, we will be flashing the onboard LED of the NodeMCU ESP8266 dev kit. You need to have the following equipment before moving on with this tutorial:
- A PC/Laptop running Windows/Linux
- USB Type-A to Micro-B Cable
- NodeMCU ESP8266-12E DevKit
Connect the NodeMCU DevKit with the PC using USB Type-A to Micro-B cable, and proceed with the steps outlined herein.
ESP8266 Micropython Programming
To flash the NodeMCU ESP8266 with Micropython firmware and then program it using Micropython API, you need to follow two discrete steps:
- Flashing the Micropython firmware onto ESP8266 Wi-Fi Module
- Programming ESP8266 Wi-Fi Module with Micropython Programming Language
Let’s now explore each of these steps in detail…
Flashing the Micropython firmware
Before you can start programming the ESP8266 WiFi Module using Micropython, you need to upload the relevant firmware within the flash memory of the ESP8266 module.
In order to do so, download the latest stable build of the Micropython firmware for the generic ESP8266 module from the downloads sections of the official website:
For this tutorial, I will use esptool to flash the micropython firmware onto the esp8266 boards. esptool is an open-source, python-based utility to communicate with the ROM bootloader, and flash the firmware onto ESP8266 and ESP32 series chips.
Let’s explore how you can use esptool to flash the firmware onto esp8266 for both Windows/Linux based systems.
STEP 1: Install CP2102 Driver
NodeMCU ESP8266-12E module contains an FTDI Serial Programmer. To use this programmer, you need to download and install the CP210x USB to UART Bridge VCP Driver from the official website of SilLabs:
After you have installed the driver, the device should appear in the device manager of your PC:
If the USB-to-UART converter appears in Ports (COM and LPT), you are good to go. Else, you need to reinstall the driver before you can move onto the next step.
There is no need to install explicit drivers in case of Linux based machine. You just need to check if the serial device is detected or not by running the following command in terminal:
If the serial device appears in the monitor, you are good to go. Otherwise, reconnect the device so that it is recognized by the linux system.
STEP 2: Install Python Essentials
Since esptool is a python-based platform, you need to install ‘Python’ and the package manager ‘pip’ alongside with it.
Download and install the latest stable build of the python from the official website:
If the python is installed successfully, it should be recognized in the command prompt and an interpreter should be launched upon typing the following command in the Windows Terminal:
If you downloaded the python from official resources, it comes pre-packaged with the ‘Pip Package Manager’. You need to ensure that ‘pip’ is installed alongside python using the following command:
If both ‘pip’ and ‘python’ are recognized in your system, you are good to go. Else, you can download the software again and make sure that both of these are installed successfully and recognized in your system before you can move onto the next steps.
Most of the latest linux distributions (Ubuntu, Debian) come pre-installed with the Python 3. Open the terminal, and check whether python is already installed:
If you see the error as “Command not found”, you can install the python using the following command:
sudo apt install python3
Once the python is installed, you need to install the ‘Pip Package Manager’. For this, write the following command in the linux terminal:
sudo apt install python3-pip
Before moving onto the next steps, make sure that both ‘python’ and ‘pip’ are installed and recognized in your system:
python3 --version pip3 --version
STEP 3: Install esptool
Install the esptool using pip package manager:
Windows CMD: Pip install esptool
Linux Terminal: sudo pip3 install esptool
Once the installation is completed, you can verify whether the esptool is installed successfully or not using the following command:
STEP 4: Flash Micropython Firmware
Now you are all geared up to flash the micropython firmware onto the ESP8266 module. If you have still not downloaded the Micropython firmware, now is the time to do that.
Before you go on to the flash the new firmware, you need to erase the old firmware:
esptool.py --port <serial_port> erase_flash
Now you can upload the latest firmware within the flash of ESP8266 Module:
esptool.py --port <serial_port> write_flash --flash_size=detect -fm dio 0x00000 <micropyhton-firmware>.bin
<serial_port> = COM Port in Windows (e.g., COM5) or /dev/ttyUSB* port in Linux (e.g., /dev/ttyUSB0)
Note that the option –flash_size=detect is mandatory, else it will lead to a corrupted firmware
To find the serial port with which ESP8266 module is connected, you can see the Device Manager-> (COM and LPT)
Erase the flash:
esptool.py --port COM5 erase_flash
Flash the new firmware
esptool.py --port COM5 write_flash --flash_size=detect -fm dio 0x00000 esp826620210418-v1.15.bin
Run the command ls /dev/tty*to see the port. If you have only one USB connected, it will mostly be /dev/ttyUSB0
Insert an image for serial monitor /dev/ttyUSB0
Download the latest micropython firmware using the wget command:
wget -c https://micropython.org/resources/firmware/esp8266-20210418-v1.15.bin
Erase the old firmware:
esptool.py --port /dev/ttyUSB0 erase_flash
Flash the new firmware:
esptool.py --port /dev/ttyUSB0 write_flash --flash_size=detect -fm dio 0x00000 esp8266-20210418-v1.15.bin
If you get the error permission denied; then you need to run the command:
sudo chmod 666 /dev/ttyUSB0
Programming ESP8266 Wi-Fi Module with Micropython
To program esp8266 with the micropython, you can use either of two approaches:
- Use the Micropython Firmware REPL
- Use Thonny IDE
I prefer the IDE approach because it provides everything contained in a single package. In this article, however, I will explain both the approaches.
Micropython Firmware REPL
To write and flash a program to blink the onboard LED, I am going to use the REPL provided within the Micropython Firmware.
To do so, I need to communicate with the ESP8266 using a dedicated serial port. For this purpose, I will use the Putty terminal-emulator and serial console application for both the Windows and Linux.
Firstly, you need to download and install the Putty from the official website. Once installed, you can access the REPL using a Serial Communication with a dedicated Serial Port (e.g., COM5) and a Baud Rate of 115200:
Once the connection is established, you need to press the RST button on NodeMCU ESP8266 to issue commands on the REPL:
To download the Putty serial console in linux, you need to run the following command in the linux terminal:
sudo apt-get install putty putty
Use a dedicated Serial Port (e.g., /dev/ttyUSB0) and the Baud Rate of 115200 to access the REPL:
Press the RST button on the NodeMCU dev kit, and you are all set to issue the real-time commands onto REPL:
Now that you have accessed the Micropython REPL, it’s time to write the code to turn the onboard LED on/off.
Issue the following commands one by one onto the REPL, and you will be able to manipulate the output status of the onboard LED:
>>> import machine >>> pin=machine.Pin(2, machine.Pin.OUT) >>> pin.value(0) >>> pin.value(1)
Thonny IDE is a great place to start learning about python, let alone micropython. This IDE also comes pre-installed with the Raspberry-Pi, and has a great community support.
In this tutorial, we will explore Thonny IDE in the context of programming esp8266 board using micropython language.
Step 1: Install Thonny IDE
Visit the official website of the Thonny IDE, and download the installer for Windows:
Run the installer with admin privileges and launch the IDE once installed. The interface should look like this:
To install the thonny IDE in linux, you need to execute the following command within the terminal:
sudo apt install thonny
Launch the Thonny IDE from the terminal:
Step 2: Configure Thonny IDE
To upload the micropython program in the esp8266 wifi board, you need to select the appropriate interpreter within the Thonny IDE.
Go to Run->Select Interpreter…
From the dropdown menu, select Micropython (ESP8266) and then select the appropriate Serial Port (COM for Windows, /dev/ttyUSB0 for Linux) from the Port dropdown menu:
Once you click OK, you will see the Thonny IDE is connected to ESP8266 board via the flashed firmware.
Step 3: Upload First Micropython Script using Thonny IDE
Once the connection is established, you can write the commands into REPL window or prepare a dedicated micropython script to be loaded within the esp8266 wifi module.
Paste the following script in the ‘untitled’ file and save it in the Micropython Device with the name ‘main.py’:
from machine import Pin from time import sleep led = Pin(2, Pin.OUT) while True: led.value(0) sleep(0.5) led.value(1) sleep(0.5)
Now you can press the ‘Run’, and the onboard LED must start blinking after every 0.5 sec. You can change the blinking duration, and play around with different settings to get yourself acquainted to this platform.
To make changes to the program, you should press the ‘Stop’ button. After updating the program, save it and then ‘Run’ it to see the effect of changes.
Micropython is a miniature version of the python language to program the microcontrollers which can only operate with constrained resources. In this tutorial, NodeMCU ESP8266 V1.0 dev kit was programmed using Micropython to blink the onboard LED.
Esptool was used to flash the esp8266 with the micropython firmware. Thonny IDE was used to establish a serial connection with the ESP8266 Wifi module, and upload a script to make the onboard blink after sometime.
Can you tell me some projects which can be implemented using micropython on esp8266?
He is the owner and founder of Embedded Robotics and a health based start-up called Nema Loss. He is very enthusiastic and passionate about Business Development, Fitness, and Technology. Read more about his struggles, and how he went from being called a Weak Electrical Engineer to founder of Embedded Robotics.
Subscribe for Latest Articles
Don't miss new updates on your email!