Send serial signals wirelessly form iPhone 4 to arduino

I'm currently working "automating" my home to the furthest extent that I can and i would like to control certain appliances in my home via my iPhone 4. I can handle putting the hardware together for the most part but I haven't figured out how to get my iPhone to talk to my arduino unless I use a cable. I haven't done it yet, but I'm OK with jail breaking my iPhone if necessary. Below is the program I've used for testing my relay circuits. As you can probably tell I'm not much of a programmer, but I know most of the basics (in python at least.)

int output2 = 2; int output3 = 3; int output4 = 4; int output5 = 5;

int pushbutton1 = 250; int pushbutton2 = 750;

void setup() {

Serial.begin(38400);

pinMode(output2, OUTPUT); pinMode(output3, OUTPUT); pinMode(output4, OUTPUT); pinMode(output5, OUTPUT);

digitalWrite(output2, LOW); digitalWrite(output3, LOW); digitalWrite(output4, LOW); digitalWrite(output5, LOW);

}

void loop () {

byte val;

if(Serial.available()) { val = Serial.read();

if(val == '2') { Serial.println("Output 2 ON"); digitalWrite(output2, HIGH); delay(pushbutton1); digitalWrite(output2, LOW); Serial.println("Output 2 OFF"); } else if(val == '3') { Serial.println("Output 3 ON"); digitalWrite(output3, HIGH); delay(pushbutton23 ); digitalWrite(output3, LOW); Serial.println("Output 3 OFF"); } else if(val == '4') { Serial.println("Output 4 ON"); digitalWrite(output4, HIGH); delay(pushbutton2); digitalWrite(output4, LOW); Serial.println("Output 4 OFF"); } else if(val == '5') { Serial.println("Output 5 ON"); digitalWrite(output5, HIGH); delay(pushbutton2); digitalWrite(output5, LOW); Serial.println("Output 5 OFF"); } } }

Get a Bluetooth to serial adapter for the Arduino, and open the serial connection from the iPhone via bluetooth. Sparkfun makes one ( rather expensive ) that works well.

Aeturnalus: Get a Bluetooth to serial adapter for the Arduino, and open the serial connection from the iPhone via bluetooth. Sparkfun makes one ( rather expensive ) that works well.

Have you actually opened an SPP connection between an iPhone and one of these Sparkfun modules?

is this what you are talking about?

http://www.sparkfun.com/search/results?term=bluetooth&what=products

how do I go about opening the serial connection on the iPhone?

ahab8783: is this what you are talking about?

http://www.sparkfun.com/search/results?term=bluetooth&what=products

how do I go about opening the serial connection on the iPhone?

If you intend to open a data connection (SPP) to an i-product, the bluetooth module must have an Apple verification coprocessor. If you have a MFI account with Apple, you can purchase a module from a company like connect Blue. Otherwise, the easiest way might be to cannibalize one from a product like a GPS module that is compatible with Apple's bluetooth protocol.

ok, I get the need for the co-processor but how do I go about cannibalizing it from another device?

Hi,

Maybe you can get this, a breakout board for the dock connector

http://www.seeedstudio.com/depot/ipodext-assembled-version-breakout-for-ipod-p-148.html

Then jaibreak the iphone and try to do stuff with it Check this link out, http://hcgilje.wordpress.com/2010/02/15/iphone-serial-communication/

An easier option(more expensive) option could be this,

http://blog.makezine.com/archive/2011/07/59-cable-lets-you-connect-iphone-to-arduino-no-jailbreaking.html

Then register with the apple guys for free and develop an app. U NEED A MAC for this preferably..

For interfacing it wirelessly, u can mayb use the Xbee

I'm using OSC http://hexler.net/software/touchosc to control the hoist on my boat. The Iphone needs to be on the same wifi network as the arduino is. OSC is really easy to setup and the interface can be created with a free program.

ahab8783: ok, I get the need for the co-processor but how do I go about cannibalizing it from another device?

I meant you could buy and disassemble something like this: http://www.amazon.com/gp/product/B004M3BICU pry out the GPS module and just use the Bluetooth module.

I don't know how difficult or feasible that might be, but it might be an option. Otherwise, you can jailbreak your iPhone and use a normal Bluetooth module, I believe. I don't know anything about jail breaking other than what you could find Googling it. Apple have made it difficult to do otherwise.

If you are not stuck on Bluetooth, you could use wifi on the iPhone and a wifi shield on your Arduino and possibly do what you want to do with a web page on the Arduino. There are examples that come with Arduino that you can look at or you could post a new topic here to get help. I'm not very familiar with the web stuff myself.

fredlaatnaarbed: I'm using OSC http://hexler.net/software/touchosc to control the hoist on my boat. The Iphone needs to be on the same wifi network as the arduino is. OSC is really easy to setup and the interface can be created with a free program.

Fred, what wifi shield are you using? do you think something like this would work: http://www.cutedigi.com/product_info.php?products_id=4445 also, do you have to add any hardware yo your iPhone (i.e. breakout board, dock?)

Jane, I hadn't really considered wifi before but after reading through a lot of similar projects online, i definetly think it would be the better wireless median (that and i'm pretty sure dissasembling any electronics will only leave me with dead useless electronics :)

akshaykirt: I like the links you poseted but i don't want to dock my phone if that's at all possible

what do you guys think about this shield?

I would prefere buying this one over the one I posted previously since it comes from sparkfun and I like how much support they offer.

I used this one: http://www.sparkfun.com/products/10536 with an apple airport express. Good thing about the express is that it also can repeat the wifi signal. but the http://www.sparkfun.com/products/9954 you can fix another antenna. decisions.......

fredlaatnaarbed: I used this one: http://www.sparkfun.com/products/10536 with an apple airport express. Good thing about the express is that it also can repeat the wifi signal. but the http://www.sparkfun.com/products/9954 you can fix another antenna. decisions.......

Fred, the typical OSC configuration uses Processing on a host computer that is also on the WiFi network. It looks like you are using the Arduino directly on the WiFi network without those? If so, can you share what your sketch looks like? Are you using any special Libraries?

I like OSC with a PC running Processing THEN Xbee to Uno. This makes iPhone the "remote control".

here is the code:
In the osc interface on your iphone you need to have 7 buttons:
stop, cw, ccw, up, dwn, hsup, hsdwn.
Dont know why but I changed the serverport to 10000 iso 9000.
it works.

// 24 OCT 2011
// ifoon crane interface
// IP address 192.168.30.177 , port 10000

#include <SPI.h>
#include "Ethernet.h"
#include "OSCClass.h"


int IfoonControlPin = 8;
int CraneDwnSwPin = 7;
int CraneUpSwPin = 6;
int CraneCWSwPin = 2;
int CraneCCWSwPin = 3;
int WinchUpSwPin = 4;
int WinchDwnSwPin = 5;
uint8_t ch;
uint8_t val[16];
int counter;
int CraneHeight = 900;
long prevOSCTransmission = millis();

OSCMessage recMes;
OSCMessage sendMes;
OSCClass osc(&recMes);

uint8_t *destIp;
int  destPort = 9000;
char *topAddress="1";
char *subAddress[1]={ "height"  };


byte serverMac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte serverIp[]  = { 
  192, 168, 30, 177 };
int  serverPort  = 10000;
char *page;
char *IfoonCommand;
float value = 0;
int intvalue = 0;
int cw = 0,
ccw = 0,
CraneUp = 0,
CraneDwn = 0,
WinchUp = 0,
WinchDwn = 0,
StopCrane = 0;
long LastPingMillis = millis();

void setup() {
  Serial.begin(19200);
  Ethernet.begin(serverMac ,serverIp);
  osc.begin(serverPort);
  pinMode(IfoonControlPin, OUTPUT);
  pinMode(CraneDwnSwPin, OUTPUT);
  pinMode(CraneUpSwPin, OUTPUT);
  pinMode(CraneCWSwPin, OUTPUT);
  pinMode(CraneCCWSwPin, OUTPUT);
  pinMode(WinchUpSwPin, OUTPUT);
  pinMode(WinchDwnSwPin, OUTPUT);

  digitalWrite(IfoonControlPin, LOW);
  digitalWrite(CraneDwnSwPin, LOW);
  digitalWrite(CraneUpSwPin, LOW);
  digitalWrite(CraneCWSwPin, LOW);
  digitalWrite(CraneCCWSwPin, LOW);
  digitalWrite(WinchUpSwPin, LOW);
  digitalWrite(WinchDwnSwPin, LOW);
  osc.flush();
}

void loop() {
  //osc arrive check
  if ( osc.available() ) {
    Serial.println("received osc");
    logMessage(&recMes);
    page = recMes.getAddress(0);
    IfoonCommand = recMes.getAddress(1);
    value = recMes.getArgFloat(0);
    intvalue = int(value);
    if( !strcmp(IfoonCommand, "ping")){
      Serial.println("received ping");
      LastPingMillis = millis();
    }else if(millis() >(LastPingMillis+65000)){//no ping for 65 sec then everything OFF
      StopCrane = 1;
    }  
    if(!strcmp(page,"1")){
      if(!strcmp(IfoonCommand,"stop")){
        if (intvalue == 1){
          StopCrane = 1;
          cw = 0;
          ccw = 0;
          CraneUp = 0;
          CraneDwn = 0;
          WinchUp = 0;
          WinchDwn = 0;
        }
        else{
          StopCrane = 0;
          LastPingMillis = millis();
        }
      }
      else if(!strcmp(IfoonCommand , "cw")){
        if (intvalue == 1){
          cw = 1; 
          ccw = 0;
        }
        else{
          cw = 0;
        }
      }
      else if(!strcmp(IfoonCommand , "ccw")){
        if (intvalue == 1){
          ccw = 1;
          cw = 0;
        }
        else{
          ccw = 0;
        }
      }
      else if(!strcmp(IfoonCommand , "up")){
        if (intvalue == 1){
          CraneUp = 1;
          CraneDwn = 0;
        }
        else{
          CraneUp = 0;
        }
      }
      else if(!strcmp(IfoonCommand , "dwn")){
        if (intvalue == 1){
          CraneDwn = 1;
          CraneUp = 0;
        }
        else{
          CraneDwn = 0;
        }
      }
      else if(!strcmp(IfoonCommand , "hsup")){
        if (intvalue == 1){
          WinchUp = 1;
          WinchDwn = 0;
        }
        else{
          WinchUp = 0;
        }
      }
      else if(!strcmp(IfoonCommand , "hsdw")){
        if (intvalue == 1){
          WinchDwn = 1;
          WinchUp = 0;
        }
        else{
          WinchDwn = 0;
        }
      }
    }//end page 1
  }//end osc reveive
  if((!strcmp(page,"1"))&&(!StopCrane)){ 
    digitalWrite(IfoonControlPin, HIGH);
  }
  else{
    digitalWrite(IfoonControlPin, LOW);
  } 

  if ((cw)&&(!ccw)&&(!StopCrane)){
    digitalWrite(CraneCWSwPin, HIGH);
    digitalWrite(CraneCCWSwPin, LOW);
  }
  else if ((ccw)&&(!cw)&&(!StopCrane)){
    digitalWrite(CraneCWSwPin, LOW);
    digitalWrite(CraneCCWSwPin, HIGH);
  }
  else{
    digitalWrite(CraneCWSwPin, LOW);
    digitalWrite(CraneCCWSwPin, LOW);
  }

  if ((CraneUp)&&(!CraneDwn)&&(!StopCrane)){
    digitalWrite(CraneUpSwPin, HIGH);
    digitalWrite(CraneDwnSwPin, LOW);
  }
  else if ((CraneDwn)&&(!CraneUp)&&(!StopCrane)){
    digitalWrite(CraneDwnSwPin, HIGH);
    digitalWrite(CraneUpSwPin, LOW);
  }
  else{
    digitalWrite(CraneDwnSwPin, LOW);
    digitalWrite(CraneUpSwPin, LOW);
  }
  
  if ((WinchUp)&&(!WinchDwn)&&(!StopCrane)){
    digitalWrite(WinchUpSwPin, HIGH);
    digitalWrite(WinchDwnSwPin, LOW);
  }
  else if ((WinchDwn)&&(!WinchUp)&&(!StopCrane)){
    digitalWrite(WinchDwnSwPin, HIGH);
    digitalWrite(WinchUpSwPin, LOW);
  }
  else{
    digitalWrite(WinchDwnSwPin, LOW);
    digitalWrite(WinchUpSwPin, LOW);
  }
}

// *********  utility  ***********************************
void logMessage(OSCMessage *mes){
  uint8_t *ip=mes->getIp();
  //disp ip & port
  Serial.print(millis()/1000);
  Serial.print(" sec from IP:");
  Serial.print(ip[0],DEC);
  Serial.print(".");
  Serial.print(ip[1],DEC);
  Serial.print(".");
  Serial.print(ip[2],DEC);
  Serial.print(".");
  Serial.print(ip[3],DEC);
  Serial.print(" port:");
  Serial.print(mes->getPort(),DEC);
  Serial.print("   ");

  //disp adr
  for(int i = 0 ; i < mes->getAddressNum() ; i++){
    Serial.print(mes->getAddress(i));
  }

  //disp type tags
  Serial.print("  ,");
  for(int i = 0 ; i < mes->getArgNum() ; i++){
    Serial.print(mes->getTypeTag(i));
  }
  Serial.print(" ");

  //disp args
  for(int i = 0 ; i < mes->getArgNum() ; i++){
    switch( mes->getTypeTag(i) ){
    case 'i': 
      {
        Serial.print( mes->getArgInt(i) );
      }
      break;
    case 'f':  
      {
        Serial.print( mes->getArgFloat(i) );
      }
      break;
    }
    Serial.print(" ");
  }
  Serial.println(""); 
}