Introduction: DIY IPhone Controller for Raspberry-Pi (OSC)
To start off this project I would like to explain how all this will work. We will be using a form of communication called OSC (Open Sound Control) and an app called "TouchOSC" which is available on the Google Play and Apple app store, along with pyOSC. What we will do is the following:
- Create a suitable layout for the Touch OSC app to control the Raspberry Pi (will explain further as we go)
- Upload the layout to the app
- Install pyOSC on your Raspberry Pi (I assume you already have your Pi setup and connected to your Wifi / Ethernet)
- Write a simple script in Python to handle messages from the TouchOSC app, and output the values it recieves
REASONS WHY I CREATED THIS LITTLE PROJECT:
- To show the world how easy it is control your Pi without a big hassle
- To establish an easy and legitimate tutorial (I figured this out from bits and pieces here an there, so I don't want other people to struggle with creating a simple OSC project like I had to)
I hope you enjoy this project!
BIG, BIG THANKS TO:
www.playwithmyled.com for the main image and inspiration for this tutorial!
Step 1: Gather Your Materials / Create OSC Layout
In this step we will basically have you get yourself prepared for the project by gathering all of our resources that we will require:
1. Download the TouchOSC app:
The app (last I remember) costs $5. Really, you can get it for free if you pirate it (I suggest not to pirate the app, even though it will still work).
2. Download the TouchOSC layout creator (to create your own GUI for your phone):
Go to this link here , scroll down to the bottom of the page and download the TouchOSC editor that suites you
3. To start building your own layout open the program you downloaded in step 2 above:
- You want to begin selecting the proper layout options for your device / phone/ tablet. iPhone screen already come with pre-configurations, but for Android devices you must select the CUSTOM layout options and put in your screen resolution. In my case I am using a Samsung Galaxy Tab 3 which is 600x1024.
- Next, you want to add in all sorts of widgets into your UI by "right-clicking" on the black screen. Once you select one, look at it's setting on the left hand side and set it up to what you want to do.
SUPER, SUPER IMPORTANT INFORMATION:
When you are creating your UI, please make sure to jot down the name of your widgets and their value ranges, you will need this for future reference, also SAVE your layout file!!!
- After you create a UI that you like, turn on your smartphone / tablet and open the TouchOSC app. (Make sure you device is connected to the same Wifi network as your computer)
- On your computer: click on "Sync"
- In the app, click on the "Layout" button and your computer should appear. Click on your computers name and it will ask you if you want to download the UI file you created. Hit "Yes", or whatever comes up.
- Then, in the app, select your layout you just made and go back to the home screen
- Press "Done" to then view your layout that you just created to test it out and see how it works
THE IMAGES WILL GUIDE YOU THROUGH ALL THE STEPS!
Step 2: Boot Up Your Raspberry Pi and Install PyOSC
Now that we have our UI (User Interface) setup, we next need to install pyOSC for python so that our R-Pi can easily communicate with our device using the code we write.
In this step I assume that:
- You already have an SSH connection to your Pi
- You have an established internet connection the your Pi
- And, that you know a little bit of python (I'm more of a Java guy, so python is a little different for me, so you should be fine...(BTW: You actually don't need any good programming abilities to be able to do this))
- WE WILL BE USING THE FOLLOWING PROGRAMS IN THIS STEP AND THE NEXT:
- Putty -- or any other SSH client
- FileZilla, so that you can copy the files over to your Pi
1. Open up Putty and connect to your R-Pi via SSH (you will need it's IP address and connect through port 22)
- To get the IP address, simply download FRING -- from the Google Play and Apple app store, it will list all the devices on your network. So from there you can get the IP of your Pi.
2. After you have established a connection, login with the Username: 'pi' and password: 'raspberry' (do this if you haven't already changed the Username and Password)
3. Once you are logged in, begin by typing the commands below to install pyOSC
sudo apt-get install git
Next, type in this:
cd /tmp -----------------This will change the dierectory
git clone git://gitorious.org/pyosc/devel.git --------------------This will download the pyOSC package
cd devel ----------------This will change the directory
sudo ./setup.py install ----------------This will run the install script for pyOSC
Now Were Done! :D
IN THE IMAGES YOU CAN SEE THE ENTIRE PROCESS
Step 3: Making the Python Script (more Like Transferring It)
THE SCRIPT IS ATTACHED TO THIS STEP!!!!!
In this last step we will be editing a script that I wrote, to suit your needs with your UI that you created in Step 1. I have attached a simple version of the script so that it outputs the data it receives from your Android / Apple device.
- Open the file (preferably on your PC)
- Look at the IP address section and edit the IP to the IP of your Pi, along with the port you will use
- Scroll down to the bottom
- Edit the names of the widgets you used in the UI editor, leave the attached methods alone
- Create your own functions/ methods, and assign them to handle the values from each widget
- You can basically do almost anything you want with your Pi, now that you have complete wireless control
Putting the script onto your Pi:
1. Open up Putty
2. Type in:
3. Next, open up FileZilla
4. Connect to your Raspberry Pi, as if SSH-ing into it
5. Go to the Desktop directory of your Pi in FileZilla
6. Find the directory of the python script you edited on your PC and drag the file over into the Desktop folder of the Pi
7. Go back to Putty and type:
ls .////This will list all the files in the directory
8. If you see the file name that you just transferred in the list of files then you are OK to go!
Step 4: Running the Script and Connecting Your Device
This step is the most fun, and it is the coolest! :D
1. Open up Putty
2. Navigate to the Desktop directory (you should still be there from the last step)
3. Now, type in the following command to run your script:
sudo python YourFileName.py
4. From there you should see the script start by saying: "I'm Idle!" meaning that it has not received any data
5. Leave the script running and flip over to your device that has TouchOSC installed on it
6. Click on the OSC button and setup your connection
7. The HOST: is your R-Pi's IP address
8. Port: Is the port you set in the script (default 8000)
9. Once you have input the proper information, hit the DONE button
10. You should see your custom UI layout that you made
11. Now,with Putty open, you should see values that get displayed, either producing an error message, or giving you the actual values. If you get an error message it might be that your widget names are not set properly in the script.
If you don't want to have to go through the trouble of re-programming it and putting the file back on your Pi, just type in this command to open up the text editor in your Pi:
sudo nano YourFileName.py
After you scroll down and edit your file, hit "Ctrl+X" hit "Y", then press "Enter"
It should save your changes and you should try running your script again.
If you run into any more problems please contact me. Thanks!
I hope that his project can really help people out. Here are a list of the possibilities that can arise once you set this whole operation up:
- Wireless control of your Pi and GPIO pins
- Use this to create a custom remote control for a Pi radio
- Control a webcam attached to your Pi
- Possible robotics applications
Step 5: Taking It a Step Further (Battery Powered + Wireless)
Some people might think that this project is great, but what would make this project really awesome is making it completely wireless. This means that our Pi wouldn't need to connect to any wall outlet, and have a USB Wi-Fi adapter to be controlled from anywhere. This part is really not that difficult. Here are the items you will need:
- A 5V battery back-up phone charger (a portable phone charger will last the Pi about 3-6 hours depending on the capacity)
- A USB Wi-Fi adapter (this will enable us to connect the Pi to any network and connect our device with TouchOSC installed, to keep constant and wireless control)
You can connect the battery to the Pi to power it, and use the WiFi adapter to keep yourself connected. Look at the image in this Step to see the break down/ theory.
With complete wireless control your projects can be portable and light, no need to carry around a bunch of cables, just plug your Pi in and your ready for outdoor away from home demonstrations. Enjoy!