{"id":209,"date":"2014-08-04T21:39:40","date_gmt":"2014-08-04T21:39:40","guid":{"rendered":"http:\/\/bitcows.com\/?p=209"},"modified":"2014-08-04T21:39:40","modified_gmt":"2014-08-04T21:39:40","slug":"controlling-a-raspberry-pi-with-your-iphone","status":"publish","type":"post","link":"https:\/\/bitcows.com\/?p=209","title":{"rendered":"Controlling a Raspberry Pi with your iPhone"},"content":{"rendered":"<p>So you have a Raspberry Pi (Pi), and you have an iPhone.. Want to use the iPhone to make the Raspberry Pi do something?<\/p>\n<p>In this article I&#8217;m going to use my iPhone to do something simple with my Raspberry Pi. In this case light up an LED.<\/p>\n<p><strong>What you will need:<\/strong><br \/>\n&#8211; <a href=\"http:\/\/canada.newark.com\/raspberry-pi-accessories\" title=\"newark.com\" target=\"_blank\" rel=\"noopener\">Raspberry Pi<\/a> ( obviously) mine is a Model B Version 2<br \/>\n&#8211; iPhone (connected to WiFi)<br \/>\n&#8211; Network Connection<br \/>\n&#8211; <a href=\"http:\/\/canada.newark.com\/lumex\/ssl-lx5093gd\/led-green-t-1-3-4-5mm-30mcd-565nm\/dp\/59K0273?MER=PPSO_N_C_Optoelectronics_Level0Bottom_CA_None\" title=\"LED LIGHT\" target=\"_blank\" rel=\"noopener\">LED light <\/a><br \/>\n&#8211; <a href=\"http:\/\/canada.newark.com\/multicomp\/mcf-0-25w-220r\/resistor-carbon-film-220-ohm-250mw\/dp\/38K0351?MER=PPSO_N_P_EverywhereElse_None\" title=\"Resistor\" target=\"_blank\" rel=\"noopener\">Resistor<\/a> (I used 200 ohm)<br \/>\n&#8211; Wire to connect it all<br \/>\n&#8211; <a href=\"http:\/\/canada.newark.com\/twin-industries\/tw-e40-1020\/breadboard-solderless-830-tie\/dp\/56T0250?MER=TSTSO_S_C_Breadboard_None\" title=\"Bread Board\" target=\"_blank\" rel=\"noopener\">Bread board<\/a> or clips to hold it all together.<\/p>\n<p>All of these items can be gotten at <\/p>\n<div style=\"overflow:hidden;width:261px;height:40px;\"><a href=\"http:\/\/canada.newark.com\/\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"http:\/\/www.newark.com\/wcsstore\/ExtendedSitesCatalogAssetStore\/cms\/asset\/images\/common\/header-footer-sprite.png\" alt=\"http:\/\/www.newark.com\/wcsstore\/ExtendedSitesCatalogAssetStore\/cms\/asset\/images\/common\/header-footer-sprite.png\" style=\"width:611px;height:216px;max-width:inherit\"><\/a><\/div>\n<p><strong>Assumptions:<\/strong><br \/>\n&#8211; You have already set up your Pi for the first time. In other words you plugged it in and ran it once.<br \/>\n&#8211; You have connected to the internet.<br \/>\n&#8211; You used the default install of Raspbian (not sure this is required though)<\/p>\n<p>Lets get started!<\/p>\n<p>The first thing you will need to do is get your Pi configured. We need to put a few things on your Pi<br \/>\n<em>SIDE NOTE:<\/em> (<a href=\"http:\/\/www.raspberrypi.org\/documentation\/remote-access\/vnc\/\" title=\"raspberry pi headless\" target=\"_blank\" rel=\"noopener\">running headless<\/a>)<br \/>\nIf you are like me and you don&#8217;t have your Pi connected to a monitor and keyboard then you will need to SSH in the device.<br \/>\nTo do this you will need to open a terminal window (mac or pc) and type<\/p>\n<pre class=\"prettyprint\">ssh pi@192.168.0.117<\/pre>\n<p>(replace with your Pi\u2019s IP of course)<br \/>\n(<em>username:<\/em> <strong>pi<\/strong> <em>password:<\/em> <strong>raspberry<\/strong> )<\/p>\n<p>Now you will need to type into your terminal window<\/p>\n<pre class=\"prettyprint\">sudo apt-get install rpi.gpio<\/pre>\n<p>this will install the GPIO Library. <\/p>\n<p><strong>Lets write some code<\/strong><br \/>\nNow 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.<\/p>\n<p><em>NOTE:<\/em> you need to this part via the UI can&#8217;t do it off a ssh connection. If you don&#8217;t have one you can do a wget and download this script. <strong>(optional)<\/strong> <\/p>\n<pre class=\"prettyprint\">wget http:\/\/mypocket-technologies.com\/iphoneScript<\/pre>\n<pre class=\"prettyprint\">sudo idle<\/pre>\n<p>This will open up the python script editor<br \/>\nnext type in the new python window<\/p>\n<pre class=\"prettyprint\">\nimport socket\nimport RPi.GPIO as GPIO\nimport os \n\nGPIO.setmode(GPIO.BOARD) ## Use board pin numbering\nGPIO.setup(7, GPIO.OUT) ## Setup GPIO Pin 7 to OUT\n\n#Inputs\n#-----------------------------------------------------------------------------\nRxSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)\nTxSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)\n\n#Defaults\nIP = '0.0.0.0'\nInPort = 7777\nOutPort = 7777\n\n\nRxSock.bind((IP,InPort))\n\nwhile True:\n\tdata = RxSock.recvfrom(512)\n\tmessage = data[0]\n\tsenderinfo = data[1]\n\tsenderIP = senderinfo[0]\n\tprint \"message received: \", message\n\n\n\t#PIN 7\n\tif (message == 'P7H'):\n\t\tprint \"Pin 7 is now High\"\n\t\tGPIO.output(7, True)\n\t\tTxSock.sendto('Pin 7 is now High',(senderIP,OutPort))\n\n\telif (message == 'P7L'):\n \t\tprint \"Pin 7 is now Low\"\n\t\tGPIO.output(7, False)\n\t\tTxSock.sendto('Pin 7 is now Low',(senderIP,OutPort))\n<\/pre>\n<p>Click File > Save when you are done (Ctrl + S).<br \/>\nLastly run to start the script<\/p>\n<pre class=\"prettyprint\">sudo nice -n 10 python iphoneScript<\/pre>\n<p>* Note the &#8220;nice -n 10&#8221; sets process priority.  If operations are very important, use a lower number than 10)<\/p>\n<p><strong>Now the hardware<\/strong><br \/>\nLastly you will need to set up the led and do all the connections.<br \/>\nI have a diagram showing what needs to go where. You need to have a wire from ground and a wire from pin 4<br \/>\n<img decoding=\"async\" src=\"http:\/\/bitcows.com\/wp-content\/uploads\/2014\/08\/RaspberryPi.png\" alt=\"RaspberryPi\" width=\"375\"  class=\"alignnone size-full wp-image-216\" \/><\/p>\n<p>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 <a href=\"http:\/\/mypocket-technologies.com\/iphoneScript\" target=\"_blank\" rel=\"noopener\">here<\/a><\/p>\n<p>Run the <a href=\"https:\/\/itunes.apple.com\/us\/app\/ardumote\/id449100779?mt=8\" target=\"_blank\" rel=\"noopener\">Ardumote App<\/a> on iPhone\/iPad. Create a Toggle Button with your Pi\u2019s IP, port 7777, and set the outgoing message as \u201cP7\u2033 so that \u201cP7H\/L\u201d will be sent when you toggle the switch.<\/p>\n<p><a href=\"http:\/\/bitcows.com\/?p=249\">Learn to create the iPhone app piece of this project.<\/a><\/p>\n<p>Check out <a href=\"http:\/\/canada.newark.com\/\" target=\"_blank\" rel=\"noopener\">canada.newark.com<\/a> for more products and help.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;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: &#8211; Raspberry Pi&hellip;<\/p>\n<p class=\"more-link\"><a href=\"https:\/\/bitcows.com\/?p=209\" class=\"themebutton\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":277,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,15,16],"tags":[49,59],"class_list":["post-209","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-electronics","category-ios","category-code-examples","tag-ios-2","tag-raspberry-pi"],"_links":{"self":[{"href":"https:\/\/bitcows.com\/index.php?rest_route=\/wp\/v2\/posts\/209","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bitcows.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bitcows.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bitcows.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bitcows.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=209"}],"version-history":[{"count":0,"href":"https:\/\/bitcows.com\/index.php?rest_route=\/wp\/v2\/posts\/209\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/bitcows.com\/index.php?rest_route=\/"}],"wp:attachment":[{"href":"https:\/\/bitcows.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bitcows.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bitcows.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}