Build a Mobile App and trigger Subprocess on your Raspberry Pi

This tutorial will run you through how to easily build a very very simple Mobile App using the Python library Kivy from scratch, as well as how to use the app for triggering a subprocess on the Raspberry Pi. At this point I must also warn you that I also tried to create a iOS package using Xcode in order to deploy the app on my iPhone. I was unable to do so, since Apple seems to impose loads of limitations on apps and so it does not allow you to run subprocesses... However, since it was also a great learning experience, I felt like sharing the bitter part of my journey too!!

Paswordless SSH access to the Pi

Before getting started, let’s take a brief detour to make sure we can SSH into the Pi without a password, since this is required in order to make the code below work. This can be done by using SSH keys and it’s a very very straightforward process. This official tutorial is very clear and easy to follow.

ssh pi@<your_IP_address> 

Kivy

Kivy is this open source python library for app development. Marco and I explored the webpage and it looked promising: in theory you could write some python code and the library would package it as a mobile app that you could later simply deploy on your iPhone or Android. The tutorials on it looked fast and easy. “Too good to be true?” — we thought. Now we know for sure: too good to be true.

-m pip install kivy

Building our LED App

  • Grid Layout and Buttons
from kivy.app import App
from kivy.uix.gridlayout import GridLayout


class MyGrid(GridLayout)::
pass


class MyApp(App):
def build(self):
return MyGrid()
if __name__ == "__main__":
MyApp().run()
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
class MyGrid(GridLayout):
def __init__(self, **kwargs):
super(MyGrid, self).__init__(**kwargs)
self.cols = 2
self.hello = Button(text="hello", font_size=40) #button 1
self.world = Button(text="world", font_size=40) #button 2

#Adding the buttons as widgets. self.add_widget(self.hello)
self.add_widget(self.world)


class MyApp(App):
def build(self):
return MyGrid()
if __name__ == "__main__":
MyApp().run()
class MyGrid(GridLayout):
def __init__(self, **kwargs):
super(MyGrid, self).__init__(**kwargs)
self.cols = 2
self.hello = Button(text="hello", font_size=40) #button1
self.world = Button(text="world", font_size=40) #button2
self.button = Button(text="button 3", font_size=40) #button3

#Adding the buttons as widgets.
self.add_widget(self.hello)
self.add_widget(self.world)
self.add_widget(self.button)
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
class MyGrid(GridLayout):
def __init__(self, **kwargs):
super(MyGrid, self).__init__(**kwargs)
self.cols = 1
self.led = Button(text="Turn on LED", font_size=40)
self.add_widget(self.led)


class MyApp(App):
def build(self):
return MyGrid()
if __name__ == "__main__":
MyApp().run()
class MyGrid(GridLayout):

def __init__(self, **kwargs):
super(MyGrid, self).__init__(**kwargs)
self.cols = 1
self.led = Button(text="Turn on LED", font_size=40)
self.add_widget(self.led) self.led.bind(on_press=self.pressed) def pressed(self, instance):
print("Button has been pressed")
import subprocess
  1. The path to the script you want to run. → We created a while ago a script for flashing an LED, and we have this file already stored inside the Raspberry Pi. Here there is a simple example on how to create such a file!
ip = "your.ip.address"
filepath = "Documents/Python-Projects/your_file.py"
class MyGrid(GridLayout):

def __init__(self, **kwargs):
super(MyGrid, self).__init__(**kwargs)
self.cols = 1
self.led = Button(text="Turn on LED", font_size=40)
self.add_widget(self.led) self.led.bind(on_press=self.pressed) def pressed(self, instance):
subprocess.run("ssh pi@{} python {}".format(ip, file_path), shell=True, check=True)
ssh pi@your.ip.address python Documents/Python-Projects/your_file.py

Creating an iOS Package

At the beginning of this article I promised you failures… and here they are! It was all going smoothly, but the problems started when we tried to deploy the App on our phones. Firstly, I can’t put into words how long it took us to wrap our heads around the process to do this for iOS. There were hours and hours of swearing and banging our heads against the wall (Xcode is a nightmare). And the worse thing was, once everything was done and ready, we discovered that… Apple does not support the subprocess module!!!!! 🤬🤬🤬 We only found out about this at the very end, because the app kept crashing when we clicked on the button for no reason…! This, this, this and this posts all agree: Apple does not allow you to run subprocesses. We were so disappointed. 😭 (if there is a way around this I’m unaware of please leave a comment and you will make me very happy!!!)

brew install autoconf automake libtool pkg-config
brew link libtool
sudo easy_install pip
# pip method if available (sudo might be needed.)
sudo pip install Cython==0.29.10
pip3 install -r requirements.txt
git clone git://github.com/kivy/kivy-ios
cd kivy-ios
ls
toolchain <command> [<args>]

Available commands:
build Build a recipe (compile a library for the required target architecture)
clean Clean the build of the specified recipe
distclean Clean the build and the result
recipes List all the available recipes
status List all the recipes and their build status

Xcode:
create Create a new xcode project
update Update an existing xcode project (frameworks, libraries..)
launchimage Create Launch images for your xcode project
icon Create Icons for your xcode project
pip Install a pip dependency into the distribution
pip3 Install a pip dependency into the python 3 distribution
cd kivy-ios
python3 toolchain.py build python3
python3 toolchain.py build sdl2
python3 toolchain.py build python3 kivy
python3 toolchain.py recipes
python3 toolchain.py create <title> <app_directory>
python3 toolchain.py create exampleapp ~/Desktop/kivy/example_app
open exampleapp-ios/exampleapp.xcodeproj

Sources:

https://github.com/kivy/kivy-ios

ML Engineer @ SpoonGuru | From Madrid | Always learning ✌🏻

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store