You probably know what ESP8266 is and also have heard about Python high level programming language. But did you know there is a special lean and efficient implementation of Python3 that runs on freaking microcontrollers. I know right this is amazing. Now its not full fledged Python3 with all the bells and whistles but its enough to comfortably use Python on MCU-s. Now MicroPython runs within 256k of code space and 16k of RAM. And in this tutorial I will show you how to setup your ESP8266 to run MicroPython.
First you need ESP8266 board. In this example I am using NodeMcu.
- Download latest MicroPython firmware .bin file for ESP8266:
- To flash the esp8266 you need esptool (python program). Now because in Linux where you have python installed its very simple to install esptool. Just run following command in your terminal:
sudo pip install esptool
or (for python 3)
sudo pip3 install esptool
I use pip3.
- Next we will erase the flash with the command:
esptool.py --port /dev/ttyUSB0 erase_flash
the “/dev/ttyUSB0” represents the port you have connected your ESP8266 and may vary. So make sure you have selected the right port.
And the output should be something like this:
esptool.py v2.3.1 Connecting.... Detecting chip type... ESP8266 Chip is ESP8266EX Features: WiFi Uploading stub... Running stub... Stub running... Erasing flash (this may take a while)... Chip erase completed successfully in 8.9s Hard resetting via RTS pin...
- Now we will flash the new firmware. Make sure the bin file is on the same folder where your terminal is set. In time of the making this tutorial, latest firmware is: “esp8266-20171101-v1.9.3”
so the command for me is following:
esptool.py --port /dev/ttyUSB0 --baud 115200 write_flash --flash_size=detect 0 esp8266-20171101-v1.9.3.bin
change the name of the bin file accordingly.
For some boards with a particular FlashROM configuration (e.g. some variants of a NodeMCU board) you may need to use the following command to deploy the firmware (note the
esptool.py --port /dev/ttyUSB0 --baud 115200 write_flash --flash_size=detect -fm dio 0 esp8266-20171101-v1.9.3.bin
And if all things went according to plan the output will be similar to this:
esptool.py v2.3.1 Connecting.... Detecting chip type... ESP8266 Chip is ESP8266EX Features: WiFi Uploading stub... Running stub... Stub running... Configuring flash size... Auto-detected Flash size: 4MB Flash params set to 0x0040 Compressed 600888 bytes to 392073... Wrote 600888 bytes (392073 compressed) at 0x00000000 in 34.7 seconds (effective 138.7 kbit/s)... Hash of data verified. Leaving... Hard resetting via RTS pin...
- Once you have the firmware on the device you can access the REPL (Python prompt) over UART0 (GPIO1=TX, GPIO3=RX), which might be connected to a USB-serial convertor, depending on your board. For me it is so I will use picocom:
picocom /dev/ttyUSB0 -b115200
Now if you get error: “FATAL: cannot open /dev/ttyUSB0: Device or resource busy”
just unplug esp for a second and reconnect to computer. And try again. Output will be something like that:
picocom v1.7 port is : /dev/ttyUSB0 flowcontrol : none baudrate is : 115200 parity is : none databits are : 8 escape is : C-a local echo is : no noinit is : no noreset is : no nolock is : no send_cmd is : sz -vv receive_cmd is : rz -vv imap is : omap is : emap is : crcrlf,delbs, Terminal ready
And that’s it form flashing the ESP8266. now you can use python REPL in your ESP8266.
For more info: