Skip to content

Instantly share code, notes, and snippets.

@ushiboy
Created June 20, 2020 01:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ushiboy/8a216c12d5f3763e3301d046bb59558a to your computer and use it in GitHub Desktop.
Save ushiboy/8a216c12d5f3763e3301d046bb59558a to your computer and use it in GitHub Desktop.
picmd example

picmd example

Host (Raspberry PI) Side

Install

Deploy app.py and requirements.txt to Raspbian.

Login to Raspbian and set up the Python environment (You need python3-venv to use venv).

$ python3 -m venv venv
$ source venv/bin/activate
$ pip install -r requirements.txt

Enable serial communication in raspi-config.

Launch

Run the app.py script under the venv environment.

(venv) $ python app.py

Client (PC) Side

Install

Deploy app.js and package.json to PC.

Install node-picmd using npm.

$ npm install

Launch

Run the app.js.

$ node app.js
const { PiCmd } = require('@ushiboy/picmd');
(async function() {
const pic = PiCmd.connect('/dev/ttyUSB0')
const r1 = await pic.request(0x01, Buffer.from('world'));
console.log(r1.value.toString('utf-8'));
const r2 = await pic.request(0x02);
console.log(r2.value.readDoubleLE(0))
}())
import re
import subprocess
from picmd import PiCmd
app = PiCmd.create('/dev/serial0')
@app.handler(0x01)
def greeting(data: bytes) -> str:
return 'hello %s' % data.decode('utf-8')
@app.handler(0x02)
def measure_temp():
r = subprocess.run(['vcgencmd', 'measure_temp'], capture_output=True)
d = r.stdout.decode('utf-8')
m = re.match(r"^temp=([\d\.]*)'C", d)
return float(m.group(1))
app.run()
{
"name": "picmd-example",
"version": "0.1.0",
"description": "picmd client example",
"main": "app.js",
"author": "ushiboy",
"license": "MIT",
"dependencies": {
"@ushiboy/picmd": "^0.1.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment