PeerTalk, BeagleBone, and Raspberry Pi

Computers & Mobile Other Boards Raspberry Pi

The Monday Jolt

This post was originally published in two parts [1,2] on the The Daily ACKย Aug. 25, 20012. It is reposted here on the MAKE site with permission.

I came across an excellent bit of wizardry byย Rasmus Anderssonย calledย PeerTalk. It’s a Objective-C library allowing you to communicate between your iPhone and your Mac over the USB dock cable using TCP sockets.

My immediate thought was that if this really only depended on having USB host mode capability at the far end, the same mechanism should be able to be used to talk to something like theย BeagleBone, or theย Raspberry Pi, not just your Mac. This would allow you connect your phone directly to the micro controller board and to drive hardware directly, a lot like theย Redpark cableย but bypassing Apple’sย External Accessoryย framework.

PeerTalk

So I started digging around inside the source code to see if it depended on anything that was going to be specific to OS X, it became apparent thatย PeerTalkย was mostly some really nice socket code sitting on top of theย USB Multiplex Daemon (usbmuxd). This bit of software is in charge of talking to your iPhone over USB and coordinating access to its services by other applications. Effectively this is what iTunes and Xcode use to talk to your phone when you plug it into your Mac’s USB port.

So any device that wants to talk to the iPhone using this method needsย usbmuxd. Fortunately ย for me there are a number of people who have worked outย how to talk to the iPhone from Linux, and there is a workingย usbmuxdย for Linux.

BeagleBone

There are also As a few other dependencies which aren’t present on the stockย ร…ngstrรถm Linuxย distribution on my BeagleBone, or even packages viaย opkg, buildingย usbmuxdย on my BeagleBone requiresย libusbย andย cmake. So before buildingย usbmuxd, I had to buildย cmake, which meant resolving some problems with the stock compression libraries that shipped with ร…ngstrรถm.

However several hours later after enough waiting around for software to build to convince me that before doing any serious development on the BeagleBone I really had to build an ARMv7 toolchain on my Mac to cross-compile things instead of building them directly on the board….

iPhone talking directly to my BeagleBone using PeerTalk

…I managed to get a simpleย “hello”ย from my iPhone to the BeagleBone and then viaย screenย to my Mac using port forwarding and that old stand by,ย telnet.

While I was hacking away on getting this working, I wasn’t alone.ย David Houseย was looking down some of the same back alleyways to get PeerTalk talking to his Raspberry Pi, and we batted the problem back and forth on Twitter while waiting for code to compile well into the night…

While I was working on one end, David was working on the other, putting together a client on the Raspberry Pi sitting on top of usbmuxd that’d talk natively to the PeerTalk on iOS which he called peertalk-python. Since we had the source code of both ends, it wasn’t that hard. Just hard enough.

If you want to replicate this on the BeagleBone, or the Raspberry Pi, you should first download and build theย PeerTalkย library, and then build and deploy the iOS and OSX example applications andย get that up and running.

Then connect up and boot your BeagleBone. You’ll need to power the board using a mains adapter as when you’re compiling things. It’s possible you’ll be drawing enough amperage that your computer will turn off the USB port to protect itself and as a result power down your BeagleBone. I had this happen to me a couple of times before I finally dug a mains adapter out of my office drawer. However since you’re powering the board from the mains you’ll also have to connect an Ethernet cable so that you canย sshย root@beaglebone.localย and log into the board over the network.

1.ย Go ahead and login toย your BeagleBoneย as root.

2.ย Download, build and installย libusb. Version 1.0.9 builds, links and installs okay.

3.ย Download, build and installย cmake,ย which you’ll need to buildusbmuxdย later.ย You’ll need to grab the latest Git nightly checkout as older release versions don’t build, having problems with the stockย libbz2compression on the BeagleBone.

4.ย We also needย libplist,ย however thisย is available as part of the package management system on ร…ngstrรถm Linux, so all you need to do to install this is typeย opkg install libplist-devย at the prompt.

5.ย Download, build and installย usbmuxd. Version 1.0.8 builds, links and installs okay, although you may beed to useย ccmakeย and configure by hand, rather than usingย cmake,ย as it can’t seem to find theย libusbย include files that got installed intoย /usr/local.

6.ย Create aย usbmuxย user

groupadd -r usbmux -g 114
useradd -r -g usbmux -d / -s /sbin/nologin -c “usbmux user” -u 114 usbmux

7.ย As the BeagleBoardย doesn’t have syslog turned on by default, and you’ll need it for debugging, turn onย syslogdย from the relevant script inย /etc/init.d.

8.ย Run up theย usbmuxย deamon, by typingย usbmuxd -v -vย at the prompt.

9.ย Plug your iPhone into the (host side) USB on your BeagleBoard, you should see some debug scrolling by inย /var/log/messages.

10.ย Downloadย David House’sย peertalk-pythonย andย its dependances.

11.ย On your iPhone start theย PeerTalk client for iOS.

12.ย Start the pythonย client on the BeagleBone by typingย python ./peertalk.pyย at the prompt.

Type in a message at the prompt, and you should see something like this…

Bi-directional communication between the iPhone and the BeagleBone via USB

From there it’s pretty trivial to build aย “Hello World”ย example, just by hacking around with David’s code and toggling the heartbeat LED when the BeagleBone receives any messages.

ย  ย ย defย run(self):
ย  ย  ย  ย  framestructure = struct.Struct("! I I I I")
ย  ย  ย  ย  ledOnย ='echo 1 > /sys/class/leds/beaglebone::usr0/brightness'
ย  ย  ย  ย  ledOffย ='echo 0 > /sys/class/leds/beaglebone::usr0/brightness'
ย  ย  ย  ย ย i =ย 0
ย  ย  ย  ย ย whileย self._running:
ย  ย  ย  ย  ย  ย ย try:
ย  ย  ย  ย  ย  ย  ย  ย  msg =ย self._psock.recv(16)
ย  ย  ย  ย  ย  ย  ย  ย ย ifย len(msg) >ย 0:
ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  frame = framestructure.unpack(msg)
ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  size = frame[3]
ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  msgdata =ย self._psock.recv(size)
ย  ย  ย  ย  ย  ย  ย  ย  ย  ย ย printย "Received: %s"ย % msgdata
ย  ย  ย  ย  ย  ย  ย  ย  ย  ย ย ifย i ==ย 0:
ย ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  os.system(ledOn)
ย ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  i =ย 1
ย  ย  ย  ย  ย  ย  ย  ย  ย  ย ย else:
ย ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  os.system(ledOff)
ย ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  i =ย 0
ย  ย  ย  ย  ย  ย ย except:
ย  ย  ย  ย  ย  ย  ย  ย ย pass
Which gets you to this point…

Which is pretty much where I’ve reached right now. Next steps is a proper application on the iOS end of things with more generic control of the BeagleBone’s header pins, and a more flexible Python backend on the BeagleBone itself.ย David Houseย also managed to get everything up and working on the Raspberry Pi.

The only changes from the BeagleBone set up procedure is that you should grabย libplistย usingย apt-getย rather thanย opkg, and since you won’t be logged in as root you should remember toย sudoย usbmuxd -v -vย when you start the USB daemon. Apart from that, you should be good to go…

Tagged

Alasdair Allan is a scientist, author, hacker and tinkerer, who is spending a lot of his time thinking about the Internet of Things. In the past he has mesh networked the Moscone Center, caused a U.S. Senate hearing, and contributed to the detection of what wasโ€”at the timeโ€”the most distant object yet discovered.

View more articles by Alasdair Allan
Discuss this article with the rest of the community on our Discord server!

ADVERTISEMENT

FEEDBACK