Controlling a Raspberry Pi with your iPhone

So you have a Raspberry Pi (Pi), and you have an iPhone.. Want to use the iPhone to make the Raspberry Pi do something?

In this article I’m going to use my iPhone to do something simple with my Raspberry Pi. In this case light up an LED.

What you will need:
Raspberry Pi ( obviously) mine is a Model B Version 2
– iPhone (connected to WiFi)
– Network Connection
LED light
Resistor (I used 200 ohm)
– Wire to connect it all
Bread board or clips to hold it all together.

All of these items can be gotten at

http://www.newark.com/wcsstore/ExtendedSitesCatalogAssetStore/cms/asset/images/common/header-footer-sprite.png

Assumptions:
– You have already set up your Pi for the first time. In other words you plugged it in and ran it once.
– You have connected to the internet.
– You used the default install of Raspbian (not sure this is required though)

Lets get started!

The first thing you will need to do is get your Pi configured. We need to put a few things on your Pi
SIDE NOTE: (running headless)
If you are like me and you don’t have your Pi connected to a monitor and keyboard then you will need to SSH in the device.
To do this you will need to open a terminal window (mac or pc) and type

ssh pi@192.168.0.117

(replace with your Pi’s IP of course)
(username: pi password: raspberry )

Now you will need to type into your terminal window

sudo apt-get install rpi.gpio

this will install the GPIO Library.

Lets write some code
Now we need to write some python script that will allow us to communicate to the GPIO (general purpose input/output) pins. To do this you can open a terminal (if its not open), and type.

NOTE: you need to this part via the UI can’t do it off a ssh connection. If you don’t have one you can do a wget and download this script. (optional)

wget http://mypocket-technologies.com/iphoneScript
sudo idle

This will open up the python script editor
next type in the new python window

import socket
import RPi.GPIO as GPIO
import os 

GPIO.setmode(GPIO.BOARD) ## Use board pin numbering
GPIO.setup(7, GPIO.OUT) ## Setup GPIO Pin 7 to OUT

#Inputs
#-----------------------------------------------------------------------------
RxSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
TxSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

#Defaults
IP = '0.0.0.0'
InPort = 7777
OutPort = 7777


RxSock.bind((IP,InPort))

while True:
	data = RxSock.recvfrom(512)
	message = data[0]
	senderinfo = data[1]
	senderIP = senderinfo[0]
	print "message received: ", message


	#PIN 7
	if (message == 'P7H'):
		print "Pin 7 is now High"
		GPIO.output(7, True)
		TxSock.sendto('Pin 7 is now High',(senderIP,OutPort))

	elif (message == 'P7L'):
 		print "Pin 7 is now Low"
		GPIO.output(7, False)
		TxSock.sendto('Pin 7 is now Low',(senderIP,OutPort))

Click File > Save when you are done (Ctrl + S).
Lastly run to start the script

sudo nice -n 10 python iphoneScript

* Note the “nice -n 10” sets process priority. If operations are very important, use a lower number than 10)

Now the hardware
Lastly you will need to set up the led and do all the connections.
I have a diagram showing what needs to go where. You need to have a wire from ground and a wire from pin 4
RaspberryPi

If you are having trouble getting the led to light up, double-check your wiring, and make sure you have installed the GPIO Python library according to my instructions. You can download the completed script here

Run the Ardumote App on iPhone/iPad. 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.

Learn to create the iPhone app piece of this project.

Check out canada.newark.com for more products and help.

Loading Facebook Comments ...