Page 1 of 1

RPC with MicroPython

Posted: Thu May 07, 2020 1:27 pm
by stijn
I have 2 separate MicroPython processes communicating over sockets. A is a standard MicroPython process, B has MicroPython embedded and executes a file and then enters a loop where it waits for strings on the socket, executes them using execute_from_lexer() and sends the result back. This allows A to execute code on B i.e. 'remote procedure call'. I always did this manually by typing strings so in A I'd write

Code: Select all

a = [1, 2]
rpc.Exec('a = {}'.format(repr(a)))
rpc.Exec('del a[0]')
which has an effect similar of calling exec('a=[1, 2]'); eval('foo(a)'); exec('del a[0]') on B. That's a bit tedious, error prone and not super nice to use in a text editor since linting etc don't work on the strings.

So I checked if this could not be automated (like Pyro5 and similar) and was pretty happy to find out MicroPython supports enough special methods (at least on the unix port) to make a pretty nice implementation, even supporting iteration over remote objects. So now the equivalent of the code above on A is that I'm just writing actual code and it gets executed on B:

Code: Select all

rpc.a = [1, 2]
del rpc.a[0]
This might be useful for others so here's the code, including tests so it's clear what is supported: ... 49cc5059c9 (doesn't include the actual socket code since that's written in C++)