Tags: , ,

, ,

We’ve seen how easy it is to program the ESP8266 NodeMCU using the Arduino IDE. Let’s see how to program it with MicroPython. The advantage of using Python to program an ESP8266 is that you can use it to its full potential.

Before following this tutorial, you need to install Python 3.

In this tutorial, we’ll look at how to configure the ESP8266 and use a terminal to test Python commands. Finally, we’ll look at two methods for loading and running Python scripts on the microcontroller.

Introducing MicroPython

MicroPython is an implementation of Python 3 designed to operate in the microcontroller environment and its constraints. It has a subset of the Python libraries, as well as libraries for interfacing with low-level hardware and network interfaces. (e.g. Wifi)

Boards supporting MicroPython:

  • ESP32 Boards (e.g. NodeMCU ESP32)
  • PyBoard
  • Micro:Bit
  • Teensy 3.X
  • WiPy – Pycom

Installing firmware on the ESP8266

To use MicroPython, we’ll erase the ESP8266’s flash memory and install the firmware. To do this, we’ll use the esptool tool (.py or .exe depending on what you want to use).

  • Install esptool.py
python3 -m pip install esptool

or esptool.exe (available when you install the esp package on Arduino IDE)

  • Erase flash memory (Warning: the port name may be different from yours)
python3 -m esptool --port COM9 erase_flash
  • Download the firmware, then enter the following line of code (Warning: the port name and file address may be different from yours)
python3 -m esptool --port COM9 --baud 460800 write_flash --flash_size=detect 0 C:\Users\ADMIN\Downloads\esp8266-20210202-v1.14.bin

To be able to communicate and program the ESP with MicroPython, we need to install a terminal. Here we’ll look at the use of PuTTy and TeraTerm.

Installing TeraTerm Terminal on Windows

Download and install TeraTerm

Open TeraTerm and select the serial port corresponding to the microcontroller.

Then configure serial communication in Setup> Serial Port …

You can now use the terminal to enter Python commands and execute them on the ESP8266

PuTTy terminal installation

Download and install PuTTy

Open Putty and select the serial port corresponding to the microcontroller.

Next, configure serial communication. To communicate with the ESP8266, select None in Flow control under Serial

You can now use the terminal to enter Python commands and execute them on the ESP8266

The PuTTy and TeraTerm terminals are two good options for testing a few Python commands, but not for building a complete program.

Creating a Python script and downloading it with ampy

We’re going to write some simple code to test the uploading of code to ESP8266. There are two files which are treated differently by the ESP8266: boot.py and main.py. By default, only boot.py is present. Boot.py is executed first, followed by main.py. If you wish to create a script that runs at startup, you must call it main or run it in main. If you don’t want it to run at startup, call it something else.

import sys
import time

def main():
    print("Program is running ")

if __name__=="__main__":
  print("{} initialized".format(sys.platform))
    except KeyboardInterrupt:
      print("Program stopped")

Next, we’ll install ampy, which will allow us to load a file onto the microcontroller.

python3 -m pip install adafruit-ampy

Once ampy is installed, you can use it in a command prompt with the “put” function to load a file by specifying its path and name.

ampy -d 0.5 --port COM9 put \main.py

N.B.: If you get the error “ampy.pyboard.PyboardError: could not enter raw repl”, you can increase the delay to 1 or 2 (i.e. “-d 1” or “-d 2”).

If you connect to the ESP8266 with a terminal, you’ll see the program running (you can stop the script with Ctrl+C).

The main.py script will run on each reboot. To delete it, enter the following command in the terminal

import os

Using the uPyCraft IDE

When you’re developing code and need to test it, it’s complicated to go through the command prompt to load it each time you try. There’s an IDE for Python, similar to the Arduino IDE: uPyCraft, which you can use as an editor and terminal to interface with the microcontroller.

  • In Tools> Preferences, check the serial port configuration
  • In Tools>Serial, select the serial port used by the microcontroller

Once the microcontroller has been connected to the interface, you can use the console as a terminal.

You can also create and edit a file containing the Python script. Once the code has been written, you can load and execute it by pressing F5. To validate the code upload, you may need to press the reset button on the board. You can stop the code by pressing Ctrl+C in the console.

N.B.: You can restart the code without a reset by entering the command

exec(open('main.py').read(), globals())

Delete file

If you don’t want the script to be triggered on reboot, you need to delete the main.py code using the command in a terminal or in the IDE


Reset to default configuration

To program the microcontroller with Arduino IDE, you need to delete the main.py file if it exists and upload Arduino code. This will reload the bootloader in flash.

To use MicroPython again, you’ll need to erase the flash and load the firmware.


How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?