Raspberry Pi + iPhone: Control a RPi with an iPhone in 2 Minutes

The Raspberry Pi is without a doubt one of the coolest little gadgets of the year.   At a price of $35, its also a cheap little computer that anyone can tinker around with.   The first thing I wanted to do with my Pi was control things.  Naturally, I wanted to control it with my iPhone and my Ardumote app, and it turned out to be a lot easier than I expected.   Read on to see how after the break…

At $35, the Raspberry Pi is a steal for what it is capable of.   In comparison, an Arduino Uno is about $20-25, and it doesn’t come close to the specs on the Pi.  They are built for different purposes, of course, but its hard not to compare the two, especially when the Pi comes fully equipped with General Purpose Inputs and Outputs (GPIO) that are user accessible.   The Pi also comes with built in 10/100 Ethernet, video/HDMI out, USB, and the ability to boot Linux.   This is what makes it so enticing.

When you have Ethernet and Linux, networking is not going to be very hard to do.  Essentially, the Raspberry Pi can be used as an Arduino + Ethernet Shield (more on why you might NOT want to do this in the “details” section below…).   Just looking at costs, an ethernet shield alone is above $35, and you need an Arduino ($20) with it.   With a Pi, you get both for less money AND a lot of other possibilities.

So, back to the topic, how can we use the Pi as an Arduino and control it with an iPhone?  Here are instructions:

1)  You should already have SSH working on Debian Wheezy or Occidentalis.  If not, there are tons of articles on it… just google it.
2) Log into your Pi using SSH from a terminal (only type the bold text):

  • bash-3.2$  ssh pi@192.168.1.18  (replace with your Pi’s IP)
  • enter password when prompted (its “raspberry” by default)

3)  If you are running Occidentalis (which you should!), you already have this installed, so skip to step 4.   Otherwise, get & install the GPIO Library:

  • pi@raspberrypi ~ $  sudo apt-get install rpi.gpio

4) Next, download a Python script that will receive/handle messages from the Ardumote App..  You don’t need to change anything, but you can mess with it later if you want.

  • pi@raspberrypi ~ $  wget http://samratamin.com/RaspPi/Ardumote

5)  Run the Ardumote Python script:

  • pi@raspberrypi ~ $  sudo nice -n 10 python Ardumote
  • *optional* – Add an ampersand (&) at the end to run the script in the background instead

6)  Run the Ardumote App on iPhone/iPad (Below are some screenshots).  Create a Toggle Button with your Pi’s IP, port 7777, and set the outgoing message as “P7″ so that “P7H/L” will be sent when you toggle the switch.   This will let you toggle Pin 7 (see details below) on your Pi’s GPIO.  You can also create buttons (not toggles) with messages “Shutdown” and “Reboot” to do both on your pi (see contents of the Ardumote python script on the Pi). Pins 3,5,7,8,10,11,12,13,15 are enabled for I/O by default on the Ardumote python script, so you can use messages “P3″, “P5″, etc on switches in the Ardumote App.  You can always add more yourself!

    

 

Thats it!   You should see the messages show up on the Pi.  If you can find a LED or buzzer in your stash, hook it up on Pin 7 (GPIO 4), you can see it in action (be careful to use a series resistor with the LED so you don’t pull too much current!).

Now, the details…

Using the Pi as a powerful and cheap Arduino/Ethernet Shield combo has its pros and cons:

  •  The Pros:  Everything is in one small package on the Pi.  Access to a real OS gives you a lot of power and flexibility.  For example, you can control the GPIO pins with Python, shell scripts or even C code.   You can even use apps like Ardumote to control things like key presses in the OS itself.  You can also program/re-program the GPIO controls on the Pi over the air.


  • The Cons:  One of the biggest pros may also be a con.  Because the Pi runs a Linux OS, it may not be the best choice for time sensitive operations.  There are lots of processes running in the background for the OS to function (like garbage collection), and processor operations are thus scheduled or divided by the OS based on priority.   Hence, if you have a low priority script running on your OS, its timing and operation can be very different depending on the load on the processor.  To a certain level, you can control the priority with its “nice” level.  Notice in step 5, we write a command with “nice -n 10″ at the beginning.  The lower the nice number is, the higher the priority the process is given.  For example, you can run the Ardumote script with “sudo nice -n 1 python Ardumote” for very high priority.  If you don’t have any other processes running, 10 works just fine.  That said, its still not perfect for timing applications.   Bottom Line:  If you really care about timing and predictable behavior, the Pi is not the right tool for the job – Get an Arduino or some other micro-controller instead.

 

GPIO / Script Details:  GPIO Pin assignments are a little confusing on the Pi.  Luckily, there are diagrams out there for this.   If you open up the Ardumote script, you will notice that we are controlling pin 7 on the board, which is GPIO 4 on both the Rev 1 and Rev 2 boards.  Be careful in selecting the right pins and looking up the correct numbers when using the GPIO.  Most of the pins on the Pi are unbuffered and can be damaged easily!   In the python script, you can now copy and paste the message handler blocks to handle any message thrown at the pi and thus control or automate anything.

Let us know if you do something cool with the Pi in the comments!

10 thoughts on “Raspberry Pi + iPhone: Control a RPi with an iPhone in 2 Minutes

  1. Pingback: Controlling and Viewing the Raspberry Pi GPIO via PC Browser | Jeff's Skinner Box

  2. Pingback: Debbra Filpo

  3. Pingback: 30+ Cool Ideas for your Raspberry PI Project | PingBin

  4. Very nice! A little off the rails here: would you recon It’d be possible to edit some of the code in the scripts/ app to work over Bluetooth?

  5. Pingback: Raspberry Pi – Et c’est parti ! « PHP Visions

  6. Pingback: xbox 360

  7. Pingback: ipad 3 review

  8. Great system does anybody know how to start the script on boot? I’d like to make an home automatin system out of it

  9. Pingback: Raspberry Pi – Et c’est parti !