Python magic on Raspberry Pi from anywhere
In the context of making robots, and adding python to the mix, I realized that we were about to make something magic.
Like with a magic wand, you want to wave here , and have a miracle happen there, at a distance.
I had implemented at work with colleague’s help a first stab of Service Oriented Architecture (SOA). And here I stumbled upon it, all ready for use.
The secret sauce is called rpyc ! It allows to run single instructions or complete scripts on a remote ‘server’. Let’s say we had a RPi in a robot, we could pilot it trhough the whole house via python, with fast closed loop on the robot, and maybe more involved trajectory planning done on a PC/Android somewhere accessible via wifi. How cool is that?
One use that caught my eye also, was the possibility to build a (mostly) single script based multi platform (anything running python that can take native modules?) infrastructure for orchestrating tests for distributed systems, which I worked on for some years. But here I am contemplating doing more than I dreamt of, and for home projects.
Anyway, let’s get a bunch of random platforms connected!
For that, we need rpyc both on the client and the server.
Note: make sure that they all use the same version of Python, in what follows it is python2.7.
Installation on the server (RPi)
Steps I found here, my comments for applying it on rpi image 2015-05-05-raspbian-wheezy on July 3, 2015:
First, the package installer we will need to add modules to python
> sudo apt-get install python-pip (...) > pip --versionpip 1.1 from /usr/lib/python2.7/dist-packages (python 2.7)
Note: python v2.6 gets installed as well, but ignored later. Maybe an alternative way worth a try.
Next the actual modules, rpyc and its dependency plumbum:
>sudo pip install rpyc (...) Downloading rpyc-3.3.0.tar.gz (53Kb): 53Kb downloaded (...) Downloading plumbum-1.4.2.tar.gz (52Kb): 52Kb downloaded (...)
Installation on client 1 (PC)
Start with installing a python distribution, I chose Anaconda 2.7
then open a fresh command line window and use pip, which should now be available on the path (meaning: the installer made the anaconda utilities reachable regardless of your current directory):
c:\User\XXX> pip install rpyc (...) Downloading rpyc-3.3.0.tar.gz (53kB) (...) Downloading plumbum-1.4.2.tar.gz (52kB)
The first interaction
At this point, I only had to :
- start another terminal on RPI in which the server will run using:
> rpyc_classic.py
Yes, as simple as that on that side.
Then on the PC I was then able to start Spyder, the editor that comes in Anaconda, and try out the following in the immediate window:
import rpyc c = rpyc.classic.connect("192.168.1.10") import sys c.modules.sys.stdout = sys.stdout c.execute("print 'hi here'") # this should come back to the client
And it did show on the client, as expected. Great!
Installation on client 2 (Android phone)
When I realized that Raspbian could do it, so maybe Android could, I got very excited. Enough to carry me through laboriously finding a fix on the way. So here is my proud contribution to future users:
First install Qpython (I chose the default one with python 2.7)
Then run one of the pre-loaded scripts with the GUI: -click the logo/button - select ‘run local script’ , then chose ‘pip_console.py’
in the console:
>pip install rpyc
On this LG3 phone, I ran into some issues, as it seems that the installation via ‘pip’ was imperfect. After sticking with it for a while, the following was uncovered:
- that the path in case of the app’s ‘console’ window was able to ‘import rpyc’ without failure
- but a script running from ‘run local script’ or ‘program’ icon selection, would not be able to ‘import rpyc’ , and rather left that window with a console in a different context at the above
- I compared the $PATH in both the ‘console’ and ‘run local script approach’, and saw major differences in the working case:
- I recognized an explicit path to rpyc and its dependency plumbum, beware its ugly:
/data/data/com.hipipal.qpyplus/files/lib/python2.7/site-packages/rpyc-3.3.0-py2.7.egg /data/data/com.hipipal.qpyplus/files/lib/python2.7/site-packages/plumbum-1.4.2-py2.7.egg
- when I browsed in both places, there was a subfolder with simpler name (as in ‘rpyc’ for one, and ‘plymbum’ for each respectively), with their module signature file: ‘__init__.pyo’
- when I browsed around in the parent directory, I found out that the pygame library/module had, under ‘/.../site-packages’ , both a file finishing in .egg (not directory as for rpyc) and a concise ‘pygame’ folder with its first file: ‘__init__.pyo’
So after first confirming that adding the path helped, it was time to try and fixed the install by bringing the hidden folders one level up. [EDIT> Note that what follows must be run in the shell window that runs in qpython in order to have sufficient privilegies. To do that you can create a bad script and run it, you will end up in the shell !! Tested again on September 22, 2015] :
>cd data/data/com.hipipal.qpyplus/files/lib/python2.7/site-packages/ >cp -R rpyc-3.3.0-py2.7.egg/rpyc . >cp -R plumbum-1.4.2-py2.7.egg/plumbum .
And TADAAA ... filnally the following program, typed on qpython’s editor, worked!
import rpyc c = rpyc.classic.connect("192.168.1.10") import sys c.modules.sys.stdout = sys.stdout c.execute("print 'hi here'")
Manouvering qpython and consoles with a good keyboard
Now to type on a phone scripts or program , you need a good exhaustive keyboard. I chose to go with : Hacker’s keyboard
that s it for now. What a glorious tool.







