Skip to content

MicroPython: REPL

Leo Vidarte edited this page Mar 15, 2017 · 7 revisions

Getting a MicroPython REPL prompt

REPL stands for Read Evaluate Print Loop, and is the name given to the interactive MicroPython prompt that you can access on the ESP8266. Using the REPL is by far the easiest way to test out your code and run commands.

Also known as an interactive toplevel or language shell, REPL is a simple, interactive computer programming environment that takes single user inputs (i.e. single expressions), evaluates them, and returns the result to the user; a program written in a REPL environment is executed piecewise.

https://repl.it/ - Online REPL, Compiler & IDE

There are two ways to access the REPL: either via a wired connection through the UART serial port, or via WiFi.

REPL over the serial port

The REPL is always available on the UART0 serial peripheral, which is connected to the pins GPIO1 for TX and GPIO3 for RX. The baudrate of the REPL is 115200.

Press Crtl-d (C-d) to reboot MicroPython to see the prompt after the serial login.

Using screen (C-a k to exit)

$ screen /dev/ttyUSB0 115200

[Press C-d]

MicroPython v1.8.7-7-gb5a1a20a3 on 2017-01-09; ESP module with ESP8266
Type "help()" for more information.
>>>

Using picocom (C-a C-x to exit)

$ picocom -b115200 /dev/ttyUSB0
picocom v1.7

port is        : /dev/ttyUSB0
flowcontrol    : none
baudrate is    : 115200
parity is      : none
databits are   : 8
escape is      : C-p
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,

Removing stale lock: /var/lock/LCK..ttyUSB0
Terminal ready

[Press C-d]

MicroPython v1.8.7-7-gb5a1a20a3 on 2017-01-09; ESP module with ESP8266
Type "help()" for more information.
>>>

Testing

>>> print("hello world")
hello world

WebREPL - REPL prompt over WiFi

WebREPL allows you to use the Python prompt over WiFi, connecting through a browser. The latest versions of Firefox and Chrome are supported.

For your convenience, WebREPL client is hosted at http://micropython.org/webrepl. Alternatively, you can install it locally from the the GitHub repository https://github.com/micropython/webrepl.

Before connecting to WebREPL, you should set a password and enable it via a normal serial connection.

>>> import webrepl_setup

Follow the on-screen instructions and prompts. To make any changes active, you will need to reboot your device. After reboot you will see something like this

WebREPL daemon started on ws://192.168.4.1:8266
Started webrepl in normal mode
could not open file 'main.py' for reading

MicroPython v1.8.7-7-gb5a1a20a3 on 2017-01-09; ESP module with ESP8266
Type "help()" for more information.
>>> 

To use WebREPL connect your computer to the ESP8266’s access point (ssid: MicroPython-xxxxxx, pass: micropythoN).

Using the REPL

Once you have a prompt you can start experimenting! Anything you type at the prompt will be executed after you press the Enter key. MicroPython will run the code that you enter and print the result (if there is one). If there is an error with the text that you enter then an error message is printed.

>>> print('hello esp8266!')
hello esp8266!

If your board has an LED attached to GPIO2 (the ESP-12 modules do) then you can turn it on and off using the following code:

>>> import machine, time
>>> pin = machine.Pin(2, machine.Pin.OUT)
>>> pin.high()
>>> pin.low()

Line editing

  • Left and Right keys to move the cursor
  • Delete and Backspace keys
  • Input history with Up and Down keys
  • Tab completion
  • Line continuation and auto-indent
  • Paste mode (C-e)
  • Soft reset (C-d)