Talk to MicroPython devices from websites/webapps, Node.js programs, Electron applications, VS Code extensions, the terminal, and more.
mctl
command line utilitycli/
await
with any command).main.ts
, cli/index.ts
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:
yarn ts-node examples/basic.ts
)yarn ts-node examples/terminal.ts
)yarn mctl
)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:
window.DEBUG = 1
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()
})()
Find more examples in /examples/
. You can run them like this: yarn ts-node examples/basic.ts
yarn ts-node examples/basic.ts
)yarn ts-node examples/terminal.ts
)yarn mctl
)$ 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! 🚀
I'm happy about feedback, please reach out:
Code library:
getFile
via serial improvement - currently it fills the device RAM and probably works badly with large filemctl
:
mctl
and pymakrget
with wildcards: get '*.py'
Tests:
getFileHash
, isFileTheSame
, listFiles
with and without hashmctl
commands: get -r .
, put -r .
, ..Various:
Maybe (not sure it's needed, don't rush into implementing):
yarn test
yarn build && yarn pack
tests/test-package-installation.sh
E:/tests/test-package-installation.bat
# 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 'latest'
yarn publish
# or publish 'beta'
yarn publish --tag beta
# push to git
git push && git push --tags
Perhaps update the mctl
npm package too (using cli/package.json
).
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:
Generated using TypeDoc