Wednesday, March 29, 2023

An Introduction to MicroPython

Ayan Pahwa works as an embedded software engineer at Mentor Graphics

- Advertisement -
 

Loading MicroPython on ESP8266 board

ESP8266 is a cheap Wi-Fi development board. Since MicroPython exposes all required APIs including the networking stack, this chip is the preferred choice for learning how to load MicroPython. ESP8266 raw chip modules require a USB-to-TTL converter and a 3.3-volt stable power source to interact with the computer. Hence, you can use a complete ESP8266 board like NodeMCU or Adafruit Feather HUZZAH, which has all necessary components on the same board.

NodeMCU
Fig. 5: NodeMCU

To flash MicroPython firmware binary on ESP8266 board, a handy command line tool known as esptool can be used. It can be downloaded directly from Python package manager using the following commands:

 

- Advertisement -

sudo pip install esptool
Check for the serial port after
connecting the board it is /dev/tty.
SLAB_USBtoUART in our case
ls /dev/tty* can help you identify it.
esptool.py –port “serial_port” erase_
flash
esptool.py –port “serial_port” –baud
460800 write_flash –flash_size=detect 0
“firmware_file”

Here, .bin firmware file should be kept in the current working directory:

iAyan:~ iAyan$ sudo esptool.py –port /
dev/tty.SLAB_USBtoUART erase_flash
esptool.py v1.2.1
Connecting…
Running Cesanta flasher stub…
Erasing flash (this may take a while)…
Erase took 10.8 seconds
iAyan:~ iAyan$ esptool.py –port /
dev/tty.SLAB_USBtoUART –baud 460800
write_flash –flash_size=detect 0 esp8266-
20161110-v1.8.6.bin
esptool.py v1.2.1
Connecting…
Auto-detected Flash size: 32m
Running Cesanta flasher stub…
Flash params set to 0x0040
Writing 569344 @ 0x0… 569344 (100 %)
Wrote 569344 bytes at 0x0 in 14.1
seconds (324.1 kbit/s)…
Leaving…
iAyan:~ iAyan$

Using the serial REPL

Just like flashing MicroPython to the board, its REPL can be accessed over the serial port. Simply connect your MicroPython compatible board, ESP8266 board in this case, and it will mount as a serial device (COMMxx in Windows and /dev/ttySiLabs in OSX and Linux). Use any serial emulator like Putty on Windows, or minicom or screen on Linux/OSX to access the serial REPL, using the baud rate of 115,200, as follows:

sudo screen /dev/tty.SLAB_USBtoUART
115200
or
sudo minimum -D /dev/ttySLAB_USBtoUART

For Windows, use Putty, for which the serial to be used is COMMxx (xx=COMM port number under Device Manager) and the baud rate is 115,200.

If everything goes well, you will see the familiar Python prompt, at which point you can write Python commands and view the immediate outputs. Type ‘print (“Hello World!”)’, and yes, it is the same old Python that you love.

sudo screen /dev/tty.SLAB_USBtoUART
115200
“press enter”
>>> print(“Hello, MicroPython!”)
Hello, MicroPython!
>>> 2+5
7
>>>

Using WebREPL

One unique feature of MicroPython on the boards that support networking (like ESP8266) is a WebREPL (REPL like a Python command line) that is accessible through a Web page. Instead of using a serial connection to the board, you can run Python code directly from your browser in a simple terminal. You do not even need to connect the board to a Wi-Fi network; it can create its own network, which you can use to access WebREPL. The new releases of MicroPython come with WebREPL disabled by default, so use the following commands to enable it over serialREPL:

>>> import webrepl_setup
WebREPL daemon auto-start status:
disabled
Would you like to (E)nable or (D)isable
it running on boot?
(Empty line to quit)
> E
To enable WebREPL, you must set
password for it
New password: python
Confirm password: python
Changes will be activated after reboot
Would you like to reboot now? (y/n) y

Once enabled, ESP8266 will start in AP mode and create a hotspot. You can connect to it with the password micropythoN and access WebREPL using the IP ws://192.168.4.1:8266/. Enter the password that you set earlier. WebREPL client can be accessed here or downloaded from GitHub (https://codeload.github.com/micropython/webrepl/zip/master).

Tools to make life easy

Adafruit MicroPython Tool (Ampy)

This utility is used to interact with MicroPython board over a serial connection. Ampy is a simple command line tool to manipulate files and run code on MicroPython board over its serial connection. With it, you can send files from your computer to a MicroPython board’s file system, download files from a board to your computer and send a Python script to a board to be executed. Download Ampy using Python Pip.

sudo pip3 install adafruit-amp

Transfer files from a PC to the board using Ampy and vice versa, as follows:

ampy –port “serial_port” put “file_name.py”
ampy –port “serial_port” get “file_name.py”

iAyan:~ iAyan$ ampy –port /dev/tty.SLAB_USBtoUART get boot.py
# This file is executed on every boot (including wake-boot from deepsleep)
#import esp
#esp.osdebug(None)
import gc
import webrepl
webrepl.start()
gc.collect()

iAyan:~ iAyan$ ampy –port /dev/tty.SLAB_USBtoUART put main.py
iAyan:~ iAyan$ ampy –port /dev/tty.SLAB_USBtoUART ls
boot.py
webrepl_cfg.py
main.py

This is handy to transfer main.py file to board and will run as soon as the board boots after boot.py file.

Hardware tinkering with MicroPython

Hardware hacking – GPIO

Connect the positive side, that is, anode of the LED to pin 15 on ESP8266 board via a 330-ohm resistor and the negative side or cathode (shorter leg) to the ground pin.

import machine
import time

pin_number = 15
pin = machine.Pin(pin_number, machine.Pin.OUT)

while True:
pin.high()
time.sleep(1)
pin.low()
time.sleep(1)

Networking with MicroPython

Networking is a powerful feature of microcontrollers, thanks to the Internet of Things. ESP8266 is a low-cost Wi-Fi development board and, because of MicroPython, it exposes the required APIs to access networking on ESP8266.

Connecting to ESP8266
Fig. 6: Connecting to ESP8266

A script to connect your board to your router and open the gate to the Internet is:

>>> import network
>>> AP_NAME = “ “
>>> PASSWORD = “ “
>>> ap = network.WLAN(network.STA_IF)
>>> ap.active(True)
#5 ets_task(4020ed88, 28, 3fff9708, 10)
>>> ap.connect(AP_NAME, PASSWORD)
>>> ap.ifconfig()
(‘192.168.1.127’, ‘255.255.255.0’,
‘192.168.1.1’, ‘192.168.1.1’

Congratulations! ESP8266 is now a device on your local network with just five lines of Python code. Now you can access WebREPL over this IP address.


 

SHARE YOUR THOUGHTS & COMMENTS

 
 

What's New @ Electronicsforu.com?

Truly Innovative Tech

MOst Popular Videos

Electronics Components

Tech Contests