Ipad Pro, Raspberry pi 4 and micro:bit (Lions, Tigers and Bears) approach to coding micropython with REPL on the iPad Pro.
Following on from using an iPad Pro for development, this week I thought rather than write about a specific project that I would share the pains (not really pain more of tinkerers dream) on completing my iPad Pro setup.
In previous articles I have shared the work around necessary to set up your iPad as a coding platform, however there was one part of what I personally like to do via development that I hadn’t quite yet resolved.
I can freely code my micro:bits with the iPad but ultimately I wanted to use MicroPython rather than staying in the realm of MakeCode. I decided to document the journey mainly as a memoir to myself but also for any other would be coders who decide that they would like to do embedded style coding on an iPad Pro. Note that since the micro:bit does not mount when connected to an iPad you cannot use a more simplistic approach like you can with a laptop.
Firstly we need the iPad Pro to have Linux and not just a shell we need a physical device connected, running a remote droplet does not help me plug in a micro:bit and access a REPL or flash MicroPython code.
Connecting a raspberry pi 4 to an iPad Pro
After watching a video by tech craft of YouTube I learnt that it is possible to connect the raspberry pi 4 to an iPad Pro via USB C. My raspberry pi 4 doubles as a desktop computer or at least it did mainly to fill the gap for my MicroPython needs.
I will not double up here, the stages to connect up your raspberry pi4 to an iPad Pro are very well documented but I suggest you at least use another SD card image if you use your raspberry pi 4 as a desktop as in my case my nodered installation does not work post set up.
If you follow that guide you will now have a raspberry pi 4 sidecar that you can shell into from your iPad with a fixed IP Address. Whilst a little pointless, if at home you already have a desktop setup you can use, for me the task ahead was for two reasons; firstly I love the iPad Pro and will go through work arounds to make it my sole device until apple catch up with my needs. Secondly pre-COVID I travelled regular and it would be nice to have access to my hobby coding whilst stuck in hotels.
So if you follow the guide you can now plug in your raspberry pi 4 to the iPad Pro via USB C and because its USB C you can replicate an Ethernet connection. The new, albeit very expensive, magic keyboard also comes with the luxury of an additional USB C port for charging which means you can run your pi and still charge up when required (note the battery drain from the pi being plugged in is not bad at all).
Shelly
On the iPad I use an app called Shelly (I paid for this as it is an App I use a lot). Now just as before I can SSH into my raspberry pi4, the only difference now is that it is physically connected and not reliant on being on WiFi. So for the small cost of a raspberry pi 4 (using the 4gb model) and the massive cost of an iPad Pro I now have the setup that most can achieve with a small laptop 😁
Joking aside I love the IPad Pro but I would like to point out it is my love of other applications and the way it is used that has steered me this way rather than opting to stick with the normal setup, I really believe the future of computing lies some of the way towards where iPad is going but if you are serious about doing a lot of coding it is probably not the easiest choice over a traditional laptop.
So shelling into a raspberry pi 4 does not complete the story as you know I am a big fan of the micro:bit and whilst makecode is a fantastic tool and also natively available for the iPad I really enjoy using the mu editor on the raspberry pi and more specifically micropython.
Even shelling into the raspberry pi isn’t going to give me back mu and I had thought of using VNC however normally I do not like this experience and it behaves a little odd on an iPad. I do however like working on the command line, it gives you a sense of ultimate power accomplishing things on the command line and well I thought some bright spark must have a way for me to interact with the micro:bit over the command line.
Cool Linux tools uflash & picocom
At this point two previously unknown applications come to the floor, uflash and picocom. Uflash allows us to flash micropython via the command line and also send python scripts to the microbit. Picocom is a terminal tool that we can use over USB to talk to the micro:bit and interact with the REPL (a feature I love from mu editor, note all this pain would have been unnecessary if mu code was ported to the iPad Pro but hey we are always learning).
Let’s the get the applications installed
First up picocom
sudo apt install picocom
Next up for uflash we need to use pip to install it although I do believe its installed already if you have python3 and mu editor on the raspberry pi but for completion:
pip3 install uflash
(note the u is for mu rather than a u as in the symbol for micro as in micropython).
Linux to the rescue!
So in theory we are all set to to talk to our micro:bit but first we need some Linux wizardry to find out what USB port we are using for the micro:bit
Note running uflash on my raspberry pi did not find the micro:bit as it would if you were in the GUI, this is because the micro:bit does not auto mount in headless mode. To get around this we need to mount the micro:bit
Running sudo blkid shows us connected devices
This reveals that we currently have our “micro:bit” attached at SDA.
Use sudo mkdir /mnt/MICROBIT to create a folder for our mount point
sudo mount /dev/sda /mnt/MICROBIT mounts the microbit (when we are done coding we need to unmount it with sudo unmount /mnt/MICROBIT)
Now we can run sudo uflash and MicroPython will flash to the micro:bit
Connecting to the micro:bit with picocom
If you run dmseg in your shell you should get an output like below I have highlighted the part we need to make a note of.
Note the ttyAM0C that’s a zero not an O this is the device name we can use picocom to connect to the REPL of the micro:bit
picocom /dev/ttyAM0C -b 115200
And press return should show the following prompt:
>>>
REPL
We can now play with the REPL, which is oddly a strange experience as we are using an iPad to shell to a raspberry pi which is then shelled to the micro:bit via a terminal in picocom. Micropython code executed here is run directly on the micro:bit.
So there it is, a heck of a work around but shows that with intent and a little skill with Linux, we can now code in micropython direct to a micro:bit via an iPad Pro with a Raspberry pi sidecar. All this so I can tinker offline whilst away.
Oh to break out of picocom use Ctrl+a+q
Can I flash scripts of micropython to the micro:bit?
You certainly can you can build your code in an editor of your choice, I use nano (insert link) save your file then use uflash to push it to the micro:bit
sudo uflash my_script.py
Full documentation on uflash here I would not advise googling uflash
https://uflash.readthedocs.io/en/latest/
You are receiving this write up the same day I have managed to get it all working so there is probably a lot of room for improvement here such as getting the micro:bit to auto mount etc, as always I’d love to hear any improvements or better solutions if you have any in the comments.
Of course you do not have to be a genius to work out that there is many more benefits of adding a raspberry pi sidecar to your iPad Pro, in essence as long as you stick to command line you can now code in many languages previously not accessible on the iPad. As I go further down this rabbit hole I will of course report any other gems discovered.
Thanks for blogging in. Full article with links on my website www.bloggedin.co.uk