Introduction: Program ESP8266 - MicroPython

About: " Work until you no longer have to introduce yourself " Show some love on Instagram @makers_bee & Motivate me on YouTube @MakersBee

MicroPython is a project that allows you to run a miniature version of python 3 on microcontrollers and embedded boards. It has growing support of microcontroller boards and rather than installing a full Linux distro onto the board it only gives a minimized version of python with respect to the board, with a python shell and you can upload small python files on to the board and run it.

In this instructable I will be showing you how to use MicroPython on a NodeMCU, the NodeMCU is a development board based on esp8266-12.

Step 1: Bill of Materials

For this project, you will need a,

  • NodeMCU
  • LED
  • Breadboard
  • Micro USB cable

Step 2: Installing MicroPython

To install micropython on the esp8266, I'm using the esp8266-12 version board. To install micropython you will need esptool you will need to download and install python and pip, to install the esptool.

Run the below command on a terminal or cmd to install esptool.

pip install esptool

Next, you can visit micropython website and download the latest firmware for the esp8266, after downloading it open up a terminal in the same directory as the firmware file and then run the below command.

esptool.py --port /dev/ttyUSB0 erase_flash

esptool.py --port /dev/ttyUSB0 --baud 460800 write_flash --flash_size=detect 0 esp8266-xxxxx-vxxxx.bin

You will need to change the port based on your PC. After this, you should have successfully installed micropython.

Step 3: Testing Blink Program

Now that you have successfully installed micropython it is time to try out a few test programs to do this we need to open up the python shell using a serial monitor, I use putty on a windows machine to open up a serial monitor on the com port the esp8266 is assigned to.

This python shell is similar to that of the python 3 shell, run the below script to blink an led connected to the esp8266.

import esp
pin = machine.Pin(0) pin = machine.Pin(0, machine.Pin.OUT)

Then running the below line of python script will turn on the led and the second line would turn it off.

pin.value(1)
pin.value(0)

alternatively, you could also run these lines to do the same.

pin.off()
pin.on()

Step 4: Using WebREPL

Now let's enable the micropython WebREPL which allows us to upload scripts to the esp8266 over WiFi hence eliminating the need of wires.

First, we need to enable WebREPL, open up a serial terminal and execute the below line, this setup's the webrepl and asks you to set up a password to improve security.

import webrepl_setup

Next, you should see a WiFi access point named MicroPython-xxxxxx, connect to it and open up an internet browser and visit the webREPL webpage. Now you should get a web page, hit connect and enter the password you created. Now you can execute scripts on the esp8266 wireless.

Now that you got micropython up and running you can execute scripts on it, similar to what you do a raspberry pi. There are a lot of modules available for micropython to work with and you can get good documentation of it in the micro python official website under esp8266 category.

If you have any queries, feel free to leave a comment below or PM me and I would try to help you.