Class: WebREPL

WebREPL(optsopt)

new WebREPL(optsopt)

Represents a WebREPL connection.
Parameters:
Name Type Attributes Description
opts object <optional>
Initialization option
Properties
Name Type Attributes Description
ip string <optional>
Ip address to connect to.
password string <optional>
Password to authenticate webrepl session.
autoConnect boolean <optional>
Flags if should connect automatically when instantiating WebREPL class.
autoAuth boolean <optional>
Flags if should authenticate the webrepl session automatically.
timeout number <optional>
How long, in milliseconds, should wait for response from webrepl.
Properties:
Name Type Attributes Default Description
ip string <optional>
'192.168.4.1' Ip address to connect to.
password string <optional>
'micropythoN' Password to authenticate webrepl session.
autoConnect boolean <optional>
false Flags if should connect automatically when instantiating WebREPL class.
autoAuth boolean <optional>
false Flags if should authenticate the webrepl session automatically.
timeout number <optional>
5000 How long, in milliseconds, should wait for response from webrepl.
fileBuffer Uint8Array Buffer for files being requested by `loadFile`.
ws WebSocket WebSocket connection with board.
Source:
Example
let repl = new WebREPL({
    ip: '192.168.1.4',
    password: 'micropythoN',
    autoConnect: true,
    autoAuth: true,
    timeout: 5000
})

Methods

connect()

Creates a websocket connection following the WebREPL standards on the ip set while instantiating `WebREPL` and binds events for its opening and for parsing incoming messages.
Source:
Example
let repl = new WebREPL()
repl.connect()

disconnect()

Close the current websocket connection.
Source:
Example
let repl = new WebREPL()
repl.disconnect()

enterRawRepl() → {Promise}

Sends character to enter RAW Repl mode (CTRL-A).
Source:
Returns:
- Resolves when board enters in raw repl mode, rejects if timeout.
Type
Promise
Example
let repl = new WebREPL({ autoConnect: true })
repl.on('authenticated', function() {
    repl.enterRawRepl()
        .then(() => {
            // RAW REPL
        })
    })
})

eval(command)

Send command to websocket connection.
Parameters:
Name Type Description
command string Command to be sent.
Source:
Example
let repl = new WebREPL({ autoConnect: true })
repl.on('authenticated', function() {
    this.eval('print("hello world!")\r')
})

exec(command)

Evaluate command to the board followed by a line break (\r).
Parameters:
Name Type Description
command string Command to be executed by WebREPL.
Source:
Example
let repl = new WebREPL({ autoConnect: true })
repl.on('authenticated', function() {
    repl.exec('print("hello world!")')
})

execFromString(code, interval) → {Promise}

Execute a string containing lines of code separated by `\n` in raw repl mode. It will send a keyboard interrupt before entering RAW REPL mode.
Parameters:
Name Type Description
code string String containing lines of code separated by `\n`.
interval number Interval in milliseconds between the lines of code.
Source:
Returns:
Resolves when exits raw repl mode.
Type
Promise
Example
let repl = new WebREPL({ autoConnect: true })
let code = `for i in range(0, 10):\n    print(i)`
repl.on('authenticated', function() {
    this.execFromString(code)
        .then(() => console.log('code executed'))

})

execRaw(code, interval) → {Promise}

Evaluate the code on raw repl line by line and resolve promise when it gets executed (prints "OK"). If `interval` is passed, wait that amount of time before evaluating next line (important for ESP32 WebREPL).
Parameters:
Name Type Description
code string String containing lines of code separated by `\n`.
interval number Interval in milliseconds between the lines of code.
Source:
Returns:
Resolves when code is pasted and executed (got `OK` from repl) on raw repl mode.
Type
Promise
Example
let repl = new WebREPL({ autoConnect: true })
let code = `for i in range(0, 10):\n    print(i)`
repl.on('authenticated', function() {
    this.enterRawRepl()
        .then(() => this.execRaw(code, 30))
        .then(() => this.exitRawRepl(code))
})

exitRawRepl() → {Promise}

Sends character to enter RAW Repl mode (CTRL-D + CTRL-B).
Source:
Returns:
Resolves when exits raw repl mode (Gets MicroPython booting message).
Type
Promise
Example
let repl = new WebREPL({ autoConnect: true })
repl.on('authenticated', function() {
    repl.enterRawRepl()
        .then(() => repl.execRaw('print("hello world!")'))
        .then(() => repl.exitRawRepl())
})

loadFile(filename) → {Promise}

Load file from MicroPython's filesystem.
Parameters:
Name Type Description
filename string File name
Source:
Returns:
Resolves with `Uint8Array` containing the data from requested file.
Type
Promise
Example
let repl = new WebREPL({ autoConnect: true })
let filename = 'foo.py'
repl.saveAs = (blob) => {
    console.log('File content', blob)
}
repl.loadFile(filename)

sendFile(filename, putFileData) → {Promise}

Save file to MicroPython's filesystem. Will use the filename from the `file` argument object as the filesystem path.
Parameters:
Name Type Description
filename string Name of file to be sent
putFileData Uint8Array Typed array buffer with content of file to be sent.
Source:
Returns:
Resolves when file is sent.
Type
Promise
Example
let repl = new WebREPL({ autoConnect: true })
let filename = 'foo.py'
let buffer = new TextEncoder("utf-8").encode('print("hello world!")');
repl.on('authenticated', function() {
    repl.sendFile(filename, buffer)
})

sendStop()

Sends a keyboard interrupt character (CTRL-C).
Source:
Example
let repl = new WebREPL({ autoConnect: true })
let stopButton = document.querySelector('#stop-code')
repl.on('authenticated', () => {
    stopButton.addEventListener('click', repl.sendStop.bind(repl))
})

softReset()

Sends a keyboard interruption (sendStop) and then the character to perform a software reset on the board (CTRL-D).
Source:
Example
let repl = new WebREPL({ autoConnect: true })
let resetButton = document.querySelector('#reset-button')
repl.on('authenticated', function() {
    resetButton.addEventListener('click', (e) => repl.softReset())
})

Events

authenticated

WebREPL authenticated session with success.
Source:

connected

WebSocket connection is opened.
Source:

data

`ArrayBuffer` coming from the webrepl.
Type:
  • ArrayBuffer
Source:

disconnected

WebSocket connection is closed.
Source:

error

Incoming WebSocket connection error or internal WebREPL error.
Type:
  • ErrorEvent
Source:

output

Every message coming from the webrepl to be printed on the terminal.
Type:
  • string
Source: