Tuesday, March 31, 2015

Controlling Raspberry Pi Scratch GPIO from an iPad

I mentioned this briefly on Twitter and a few people liked it, so let me write it up properly.

I wanted to get my kids into programming, so I tried setting up Raspberry Pi's Scratch GPIO to be usable from an iPad.

…The short version: it doesn't work. Not even close.
Slow. Terrible to operate. Text is unreadable.

I put the effort in, so I'll at least document how to do it.

What Is Scratch?


Scratch is a learning environment developed at the MIT Media Lab with the goal of teaching programming concepts without having to learn a traditional programming language.

It recently made a small splash when it was announced that you'd be able to develop Romo apps with Scratch.

Coincidentally, my own Romo just arrived.
I'll write a report once the developer app is available.

Although, the release of the iOS developer app has been delayed because Apple hasn't approved it.

It honestly wouldn't surprise me if it never gets approved.
It likely conflicts with Apple's guidelines, and
the Scratch iOS app itself has had its own issues with Apple
and was eventually removed from the App Store.

※ That information was inaccurate — please see the addendum below.

Anyway. Back to the topic.
Scratch makes programming accessible to children, and Scratch GPIO is an extension that lets you control the Raspberry Pi's GPIO pins from it.

Creating a Child Account


Create a user account other than pi.
Replace USER_NAME with whatever you like.
pi@raspberrypi ~ $ sudo useradd -d /home/USER_NAME USER_NAME
pi@raspberrypi ~ $ sudo passwd USER_NAME


Turning Raspberry Pi into a Remote Desktop Server


On Linux, remote desktop access normally means either installing an X Window client on the remote machine or using a VNC server — but both were non-starters for me.

My iPad is a first-generation model, so no X Window apps would run on it.
Newer iPads might work, but I'm not sure.

As for VNC — it connects, but for some reason Scratch refuses to launch inside a VNC session.

The only way I found to connect remotely to the GUI and launch Scratch was via Remote Desktop (RDP).

Install the XRDP server:
pi@raspberrypi ~ $ sudo apt-get install xrdp
pi@raspberrypi ~ $ sudo vi /etc/xrdp/startwm.sh
pi@raspberrypi ~ $ cat /etc/xrdp/startwm.sh
#!/bin/sh

export LANG=ja_JP.UTF-8
. /etc/X11/Xsession


Configure it to start automatically:
pi@raspberrypi ~ $ sudo update-rc.d xrdp defaults
update-rc.d: using dependency based boot sequencing
sudo /etc/init.d/xrdp restart


That's the Raspberry Pi side sorted. Now let's connect from the iPad.

My iPad only runs iOS 5.x.
The app RDP Remote Desktop Connection works on iOS 5.x and is the most usable option I found.

Configure it like this (replace the IP address with your own):
RDP settings screen on iPad

When you connect, it defaults to VNC mode for some reason.
It's painful to navigate, but select "seesman-X11rdp" — using the software keyboard's up/down keys is easier than tapping.

If you make the app prompt for a password every time, you'll have to repeat this selection every single time, making it essentially unusable. Save the password.
Security-conscious people with a home iPad can do what they like.

RDP connection screen

Once connected, it looks like this (Scratch GPIO isn't installed yet at this point):
Raspberry Pi desktop via remote desktop on iPad

Replacing the Font


Install the Sazanami Gothic font for more readable Japanese text:
pi@raspberrypi ~ $ sudo apt-get install ttf-sazanami-gothic
pi@raspberrypi ~ $ sudo vi /usr/share/scratch/locale/ja.po
# Font to use on a Linux system
msgid "Linux-Font"
msgstr "Sazanami Gothic"


Scratch GPIO


Now back to the Raspberry Pi to install Scratch GPIO (visible in the screenshot above).

The current version at the time was ScratchGPIO7.
http://simplesi.net/scratchgpio/scratch-raspberrypi-gpio/

Since we're using a non-pi account, install the samples for that account too:
pi@raspberrypi ~ $ wget http://bit.ly/1wxrqdp -O isgh7.sh
pi@raspberrypi ~ $ sudo bash isgh7.sh
pi@raspberrypi ~ $ sudo bash isgh7.sh USER_NAME


Installation seemed complete — but while it worked under the pi user, Scratch GPIO wouldn't launch under the new account.

Launching Scratch with Elevated Privileges


It turns out Scratch GPIO needs root privileges.
The fix is to let the new user launch Scratch GPIO as root, but only Scratch:

pi@raspberrypi ~ $ sudo vigr
users:x:100:pi,USER_NAME
pi@raspberrypi ~ $ sudo visudo
%users  ALL=(ALL)       NOPASSWD:       /usr/bin/scratch,/scratchgpio7/scratchgpio7plus.sh,/scratchgpio7/scratchgpio7.sh
# Append this line


Update the desktop icons to run via sudo:
pi@raspberrypi ~ $ vi /home/USER_NAME/bin/scratchgpio7plus.sh
pi@raspberrypi ~ $ chmod 755 /home/USER_NAME/bin/scratchgpio7plus.sh
pi@raspberrypi ~ $ cat /home/USER_NAME/bin/scratchgpio7plus.sh
#!/bin/sh

/usr/bin/sudo /scratchgpio7/scratchgpio7plus.sh $@

pi@raspberrypi ~ $ vi /home/USER_NAME/bin/scratchgpio7.sh
pi@raspberrypi ~ $ chmod 755 /home/USER_NAME/bin/scratchgpio7.sh
pi@raspberrypi ~ $ cat /home/USER_NAME/bin/scratchgpio7.sh
#!/bin/sh

/usr/bin/sudo /scratchgpio7/scratchgpio7.sh $@

pi@raspberrypi ~ $ sudo -u USER_NAME vi /home/USER_NAME/Desktop/scratchgpio7plus.desktop
pi@raspberrypi ~ $ cat /home/USER_NAME/Desktop/scratchgpio7plus.desktop
Exec=/home/USER_NAME/bin/scratchgpio7plus.sh

pi@raspberrypi ~ $ sudo -u USER_NAME vi /home/USER_NAME/Desktop/scratchgpio7.desktop
pi@raspberrypi ~ $ cat /home/USER_NAME/Desktop/scratchgpio7.desktop
Exec=/home/USER_NAME/bin/scratchgpio7.sh


Log out of the XRDP session from the iPad, then log back in.
Double-tap the Scratch GPIO icon and Scratch will launch.

Scratch GPIO running on iPad via remote desktop

Double-tap. On an iPad. Of course that's how you open things on an iPad — but you try explaining "double-tap to open" to a child who's never used a computer. Not going to land.

And dragging? On the iPad that means press-and-hold, then drag. Press-and-drag. On a touch screen.

Kids can learn these gestures if you teach them — but the whole point was to spark an interest in programming. Losing their enthusiasm on irrelevant UI friction before they even get started is a dealbreaker.

So: iPad + remote desktop + Scratch is officially off the table.

[Addendum — 2015/03/31]
The iOS version of Scratch has been re-released under the name Pyonkee:
https://itunes.apple.com/jp/app/pyonkee/id905012686?mt=8
Also, the reason Scratch2Romo wasn't approved had nothing to do with Scratch — it was an MFi certification issue on the device side.
My earlier information was out of date and I made some hasty generalisations. Apologies for the confusion.
Thanks to @abee2 for the correction.

No comments:

Post a Comment