Skip to main content

Python client for an Arduino RPC bridge using MsgPack over serial.

Project description

pyrpc

A tiny RPC bridge between Arduino and Python using MsgPack over serial.

It lets you expose C++ functions on an Arduino board and call them directly from Python, with automatic (de)serialization of basic types, containers, and structs.

Arduino side

Install pyrpc from the Arduino Library Manager.

#include <pyrpc.h>

double sum(int i, float d) {
	return i + d;
}

void setup() {
	Serial.begin(115200);

	pyrpc::begin();  

	// register an onConnect procedure (called when Python connects)
	pyrpc::registerOnConnect([]() {
		// do whatever you need here
	});

	pyrpc::registerRpc(
		"sum",
		sum,
		F("@brief sum two numbers @return double @arg i integer @arg d float")
	);
}

void loop() {
	pyrpc::process();  // handle incoming RPC calls
}

Upload this sketch to your Arduino (baudrate must match the Python side).

Python side

Install the Python package:

pip install ardupyrpc

Call your Arduino procedures from Python:

from ardupyrpc import Rpc

# on_connect=True sends an onConnect message to the Arduino after connecting
rpc = Rpc("/dev/ttyACM0", baudrate=115200, on_connect=True)

print(rpc.help)  # shows all available remote procedures

result = rpc.call("sum", 3, 1.5)
print("sum result:", result)

rpc.close()

A more complete example is available in the examples/ folder.

Current limitations

When STL containers are not supported, there is a limation of 128 bytes for the serialized data. This means that complex nested structures may not work as expected and the help command may fail to retrieve the full documentation.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ardupyrpc-1.0.0.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ardupyrpc-1.0.0-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file ardupyrpc-1.0.0.tar.gz.

File metadata

  • Download URL: ardupyrpc-1.0.0.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for ardupyrpc-1.0.0.tar.gz
Algorithm Hash digest
SHA256 f68554385eac321e13d5349abfb0b7aa336dfed333aa4de0da1a767ff6814da0
MD5 eafe16a41627675515b705d26b1a1e6b
BLAKE2b-256 a0f80e123a4774eac12caf78188ab2c773ec4d750124f617f798f530e0796dca

See more details on using hashes here.

File details

Details for the file ardupyrpc-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: ardupyrpc-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for ardupyrpc-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c35ce362a4828ca1ed806e95df471e223307aa89c9ca7bc08db8bdf2b3f9036b
MD5 ad130bb212221fbe21c4a7bf7903b90b
BLAKE2b-256 d1be72d62e5d158f73416d0a19a6c29ff8f5ad9ec42780adbe66fe016c1d7f3b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page