Options
All
  • Public
  • Public/Protected
  • All
Menu

Main class for a MicroPython device connection.

See also https://github.com/metachris/micropython-ctl

const micropython = new MicroPythonDevice()

// Connect to micropython device over network
await micropython.connectNetwork('DEVICE_IP', 'WEBREPL_PASSWORD')

// Or connect to micropython device over serial interface
await micropython.connectSerial('/dev/ttyUSB0')

// Run a Python script and capture the output
const output = await micropython.runScript('import os; print(os.listdir())')

// List all files in the root
const files = await micropython.listFiles()

Hierarchy

  • MicroPythonDevice

Index

Constructors

constructor

Properties

onTerminalData

onTerminalData: (data: string) => void

Callback to receive terminal (REPL) data

micropython.onTerminalData = (data) => process.stdout.write(data)

Type declaration

    • (data: string): void
    • Parameters

      • data: string

      Returns void

onclose

onclose: () => void

Callback that is triggered when the connection is lost or closed.

micropython.onclose = () => console.log('connection closed')

Type declaration

    • (): void
    • Returns void

Methods

connectNetwork

  • connectNetwork(host: string, password: string, timeoutSec?: number): Promise<string>
  • Connect to a device over the network (requires enabled WebREPL)

    throws

    {CouldNotConnect} Connection failed

    Parameters

    • host: string

      IP address or hostname

    • password: string

      webrepl password

    • timeoutSec: number = 5

      Connection timeout (default: 5 sec). To disable, set to 0

    Returns Promise<string>

connectSerial

  • connectSerial(path: string): Promise<string>
  • Connect to a device over the serial interface

    throws

    {CouldNotConnect} Connection failed

    Parameters

    • path: string

      Serial interface (eg. /dev/ttyUSB0, /dev/tty.SLAB_USBtoUART, ...)

    Returns Promise<string>

disconnect

  • disconnect(): Promise<void>

gcCollect

  • gcCollect(): Promise<void>

getBoardInfo

  • Get information about the board.

    const boardInfo = await micropython.getBoardInfo()
    console.log(boardInfo)
    
    {
      sysname: 'esp32',
      nodename: 'esp32',
      release: '1.13.0',
      version: 'v1.13 on 2020-09-02',
      machine: 'ESP32 module with ESP32',
      uniqueId: 'c44f3312f529',
      memFree: 108736,
      fsBlockSize: 4096,
      fsBlocksTotal: 512,
      fsBlocksFree: 438
    }
    

    Returns Promise<BoardInfo>

getFile

  • getFile(filename: string): Promise<Buffer>
  • Get the contents of a file.

    const data = await micropython.getFile('boot.py')
    
    throws

    {ScriptExecutionError} if not found: "OSError: [Errno 2] ENOENT"

    Parameters

    • filename: string

      filename of file to download

    Returns Promise<Buffer>

    contents of the file in a Buffer

getFileHash

  • getFileHash(filename: string): Promise<string>
  • Get SHA256 hash of a file contents.

    const data = await micropython.getFileHash('boot.py')
    
    throws

    {ScriptExecutionError} if not found: "OSError: [Errno 2] ENOENT"

    Parameters

    • filename: string

      filename of target file

    Returns Promise<string>

    sha256 hash, hexlified

getState

getVer

  • getVer(): Promise<string>
  • GET_VER webrepl command. Returns the micropython version. Only works with network connections, not with serial.

    Returns Promise<string>

isConnected

  • isConnected(): boolean
  • Whether currently connected to a device.

    Returns boolean

isFileTheSame

  • isFileTheSame(filename: string, data: Buffer): Promise<boolean>
  • Check whether a file is the same as provided, within a single runScript execution. Does not work in browser.

    • If filesize is different, then file is different
    • If filesize equal then compare sha256 hash

    This is a helper for bulk uploading directories, but only if they have changed.

    throws

    {ScriptExecutionError} if not found: "OSError: [Errno 2] ENOENT"

    Parameters

    • filename: string
    • data: Buffer

    Returns Promise<boolean>

isProxyConnection

  • isProxyConnection(): boolean

isSerialDevice

  • isSerialDevice(): boolean

isTerminalMode

  • isTerminalMode(): boolean

listFiles

mkdir

  • mkdir(name: string): Promise<boolean>
  • throws

    {ScriptExecutionError}

    Parameters

    • name: string

    Returns Promise<boolean>

putFile

  • putFile(targetFilename: string, data: Buffer, options?: PutFileOptions): Promise<boolean>

remove

  • remove(path: string, recursive?: boolean): Promise<void>
  • Remove a file or directory. Optional recursively

    throws

    {ScriptExecutionError} if not found: "OSError: [Errno 2] ENOENT"

    throws

    {ScriptExecutionError} if directory not empty: "OSError: 39"

    Parameters

    • path: string
    • recursive: boolean = false

      default: false

    Returns Promise<void>

rename

  • rename(oldPath: string, newPath: string): Promise<void>
  • Rename a file or directory (uos.rename)

    throws

    {ScriptExecutionError} if not found: "OSError: [Errno 2] ENOENT"

    throws

    {ScriptExecutionError} if directory not empty: "OSError: 39"

    Parameters

    • oldPath: string
    • newPath: string

    Returns Promise<void>

reset

runScript

  • Execute a Python script on the device, wait and return the output.

    throws

    {ScriptExecutionError} on Python code execution error. Includes the Python traceback.

    Parameters

    Returns Promise<string>

sendData

  • sendData(data: string | Buffer | ArrayBuffer): void
  • Parameters

    • data: string | Buffer | ArrayBuffer

    Returns void

startInternalWebserver

  • startInternalWebserver(): Promise<void>
  • The internal webserver is used to proxy runScript commands over an existing connection

    Returns Promise<void>

statPath

  • statPath(path: string): Promise<{ exists: boolean; isDir: boolean; size: number }>
  • Parameters

    • path: string

    Returns Promise<{ exists: boolean; isDir: boolean; size: number }>

Generated using TypeDoc