ESP32 – Getting started with MicroPython

Micropython is a lean and efficient implementation of the Python 3 programming language that includes a small subset of the Python standard library and is optimised to run on microcontrollers and in constrained environments. ESP32 Wroom supports Micropython. In this blog, we will see how to flash  Micropython firmware on ESP 32

Software needed:

ESPTOOL

Putty/Teraterm

Python 3 environment

USB drivers for silicon labs CP2102

Setting up:

  1. Download EspTool from github site .The version used in this tutorial was v2.1

Unzip the folder and run setup.py to create esptool

(f:\Anaconda3) F:\esptool-2.1>python setup.py install

or you can install it with pip as

pip install esptool

2. Download micropython firmware from micropython site for ESP32

3. Connect ESP32 using usb cable. Find the com port number from device manager

4.  Erase the current firmware by issuing this command

esptool.py --port COM3 erase_flash
5. Flash the firmware by running this command
F:\esptool-2.1>esptool.py --chip esp32 --port COM3 --baud 115200 
write_flash -z 0x1000 esp32-20170827-v1.9.1-439-g6c62e70b.bin

6. Open serial terminal by giving COM port number and baud rate

Testing

7. Everything is fine, you will see this window.

8. Print(“Hello world”) from the python command prompt to see the result

9. Check Wifi accesspoints available to connect by issuing wifi_scan command

10. Then connect LED on GPIO 23 and issue commands

>>> import machine
>>> pin23 = machine.Pin(23, machine.Pin.OUT)
>>> pin23.value(1)
>>> pin23.value(0)
>>> pin23.value(1)
>>>

For clarity showing the output of terminal

ets Jun 8 2016 00:22:57

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
ets Jun 8 2016 00:22:57

rst:0x10 (RTCWDT_RTC_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0008,len:8
load:0x3fff0010,len:3408
load:0x40078000,len:9488
load:0x40080000,len:252
entry 0x40080034
I (2227) cpu_start: Pro cpu up.
I (2228) cpu_start: Single core mode
I (2230) heap_init: Initializing. RAM available for dynamic allocation:
I (2266) heap_init: At 3FFAE2A0 len 00001D60 (7 KiB): DRAM
I (2322) heap_init: At 3FFD4FD8 len 0000B028 (44 KiB): DRAM
I (2380) heap_init: At 3FFE0440 len 00003BC0 (14 KiB): D/IRAM
I (2439) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (2499) heap_init: At 400920A0 len 0000DF60 (55 KiB): IRAM
I (2558) cpu_start: Pro cpu start user code
I (2716) cpu_start: Starting scheduler on PRO CPU.
OSError: [Errno 2] ENOENT
MicroPython v1.9.1-439-g6c62e70b on 2017-08-27; ESP32 module with ESP32
Type "help()" for more information.
>>> print "Hello World from MicroPython"
Traceback (most recent call last):
 File "<stdin>", line 1
SyntaxError: invalid syntax
>>> print ("Hello World from MicroPython")
Hello World from MicroPython

>>> import network
I (638039) wifi: wifi firmware version: f092575
I (638039) wifi: config NVS flash: enabled
I (638039) wifi: config nano formating: disabled
I (638039) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE
I (638049) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE
I (638069) wifi: Init dynamic tx buffer num: 32
I (638069) wifi: Init dynamic rx buffer num: 64
I (638069) wifi: wifi driver task: 3ffd79a4, prio:23, stack:4096
I (638079) wifi: Init static rx buffer num: 10
I (638079) wifi: Init dynamic rx buffer num: 0
I (638079) wifi: Init rx ampdu len mblock:7
I (638089) wifi: Init lldesc rx ampdu entry mblock:4
I (638089) wifi: wifi power manager task: 0x3ffde454 prio: 21 stack: 2560
I (638099) wifi: wifi timer task: 3ffdf4bc, prio:22, stack:3584
W (638099) phy_init: failed to load RF calibration data (0x1102), falling back to full calibration
I (638259) phy: phy_version: 355.1, 59464c5, Jun 14 2017, 20:25:06, 0, 2
I (638269) wifi: mode : null
>>> sta_if = network.WLAN(network.STA_IF); sta_if.active(True)
I (668049) wifi: mode : sta (30:ae:a4:02:f6:38)
I (668049) wifi: STA_START
True
>>> sta_if.scan()
I (686769) wifi: event 1
[(b'VarshNet', b'T\xb8\nWU,', 7, -37, 2, False), (b'DangerVirus', b'\x1c_+ Sq\x7f', 1, -48, 4, False), (b'Rama', b'\xc0\xff\xd4\x91\xe6\xc6', 6, -86, 3, False), (b'KKS_New', b'\x00\x1f3%\xb0~', 11, -91, 1, False)]
>>> I (638039) wifi: config nano formating: disabled


Few more blogs on MicroPython and ESP32

building IoT application in 10 steps

For ESP32 micro python docs, refer this link

One Comment

Add a Comment

Your email address will not be published. Required fields are marked *