1.13.4 • Published 3 years ago

micropython-ctl v1.13.4

Weekly downloads
89
License
MIT
Repository
github
Last release
3 years ago

MicroPython-Ctl: TypeScript ❤️ MicroPython

Talk to MicroPython devices from websites/webapps, Node.js programs, Electron applications, VS Code extensions, and more.

Build and test status

Usage

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())')
console.log('runScript output:', output)

// List all files in the root
const files = await micropython.listFiles()
console.log('files:', files)

// Get file contents
const fileContents = await micropython.getFile('boot.py')
console.log(fileContents)

// Set a terminal (REPL) data handler, and send data to the REPL
micropython.onTerminalData = (data) => process.stdout.write(data)
micropython.sendData('\x03\x02')  // Ctrl+C and Ctrl+B to enter friendly repl and print version

See also: MicroPythonDevice docs

Note: to connect over the network, you need to enable it on the device first, through the serial REPL: import webrepl_setup (see docs). Also, make sure you can ping the device first.

Code examples:

Browser / Webapps

In websites/webapps, simply include the latest release via CDN (~13kb gzipped):

<script src="https://cdn.jsdelivr.net/npm/micropython-ctl@1.10.0/dist-browser/main.js"></script>

Then you can use it like this:

const micropython = new MicroPythonCtl.MicroPythonDevice()
await micropython.connectNetwork(host, password)

Usage example:

Notes:

  • Browsers don't allow access to USB/serial ports.
  • You can enable debug output by opening the console and entering window.DEBUG = 1
  • You can download the zipped bundle here: main.js.gz

Node.js

Installation:

# If you use yarn
yarn add micropython-ctl

# Alternatively, if you use npm
npm install micropython-ctl

Usage:

// Node.js with TypeScript:
import { MicroPythonDevice } from 'micropython-ctl'

// Node.js without TypeScript:
// const MicroPythonDevice = require('micropython-ctl').MicroPythonDevice

(async () => {
  const micropython = new MicroPythonDevice()

  // Connect to micropython device
  await micropython.connectNetwork('YOUR_IP', 'WEBREPL_PASSWORD')
  // await micropython.connectSerial('/dev/ttyUSB0')

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

  // List all files in the root
  const files = await micropython.listFiles()
  console.log('files:', files)

  // Close
  await micropython.close()
})()

Examples

Find more examples in /examples/. You can run them like this: yarn ts-node examples/basic.ts

Building the code

$ git clone https://github.com/metachris/micropython-ctl.git
$ cd micropython-ctl
$ yarn
$ yarn build
$ yarn lint
$ yarn doc

# Compile and run mctl
$ yarn mctl

# Run a TypeScript example
$ yarn ts-node examples/basic.ts

# Run the test suite (needs a micropython device)
$ yarn test --help

# Experimental: build with esbuild
$ yarn esbuild

Enjoy and do cool things with this code! 🚀


Reach out

I'm happy about feedback, please reach out:

Inspiration & References

Future work

Code library:

  • put/get
    • via network: switch to webrepl protocol instead of manual up- and download
    • getFile via serial improvement - currently it fills the device RAM and probably works badly with large file
  • More examples in the documentation (in main.ts)

mctl:

  • sync: only if files changed (build md5 hashes for all files, and upload only if different)
  • ⚠ Check for issues when alternating mctl and pymakr
  • uploading files that have changed since last upload
  • flash-erase, flash-backup, flash-restore
  • get with wildcards: get '*.py'
  • wifi status, connect, disconnect
  • device aliases (like here)

Tests:

  • getFileHash, isFileTheSame
  • Tests for mctl commands: get -r ., put -r ., ..
  • automated browser testing (selenium [1])
  • Run tests against a local micropython instance in CI (eg by using utelnetserver to connect serial-like (webrepl is not available in host builds))

Various:

  • Webapp examples: drag & drop files to upload
  • Electron example app

Maybe (not sure it's needed, don't rush into implementing):

  • mount related
    • mount: testing
    • mount + repl
    • reuse one instance (eg. in mount mode) to execute other commands
    • mctl mount issues (see here)
  • Vue.js example with attaching the MicroPythonDevice instance to window, so one instance can live across code hot reloads :) (almost done)
  • A slim version for the browser with minimal footprint (only core code, no listfiles etc.)
  • Support new raw-paste mode: https://github.com/micropython/micropython/blob/master/docs/reference/repl.rst#raw-mode-and-raw-paste-mode (only in master, should be part of MicroPython 1.14)
  • Rename ScriptExecutionError to RuntimeError?

Release process

Testing

  • Run the tests with a Device: yarn test
  • Test package installation:
    • Prepare: yarn build && yarn pack
    • macOS & Linux: run tests/test-package-installation.sh
    • Windows: run E:/tests/test-package-installation.bat
  • Test web examples: In the html files, change imports to local and open in Browser

Release

# Update CHANGELOG
code CHANGELOG.md

# Update cli README
yarn mctl help
code cli/README.md

# make sure all is committed in git
git status

# update version number and create a git tag
yarn version

# create the builds for node and browser
./build.sh

# check the final package
yarn pack && tar -xvf micropython-ctl-v* && ll package/
rm -rf package/ micropython-ctl-v*

# publish
yarn publish

# push to git
git push && git push --tags

Update live web examples with code from Github master branch:

ssh nova "cd /server/websites/current.at/micropython-ctl && git pull"

Test the live web examples with Chrome, Firefox, Safari, Edge (on OSX, Linux, Windows and Mobile):

Notes:

1.13.8-beta2

3 years ago

1.13.8-beta1

3 years ago

1.13.4

3 years ago

1.13.4-beta1

3 years ago

1.13.3-beta1

3 years ago

1.13.2

3 years ago

1.13.0

3 years ago

1.14.0-beta1

3 years ago

1.13.2-beta1

3 years ago

1.13.0-beta3

3 years ago

1.13.0-beta2

3 years ago

1.12.2

3 years ago

1.12.1

3 years ago

1.11.2

3 years ago

1.13.0-beta1

3 years ago

1.12.0

3 years ago

1.11.0

3 years ago

1.10.0

3 years ago

1.7.3

3 years ago

1.7.2

3 years ago

1.7.0

3 years ago

1.5.2

3 years ago

1.6.0

3 years ago

1.5.0

3 years ago

1.4.1

3 years ago

1.4.0

3 years ago

1.3.0

3 years ago

1.2.0

3 years ago

1.1.0

3 years ago

1.2.3

3 years ago

1.2.1

3 years ago

1.0.1

3 years ago