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)
At this point, I only had to :
- start another terminal on RPI in which the server will run using:
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â
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.