Opening a Mac App with an Arduino

Hello, I've been working on a project that would use an Arduino to control my entire home audio system automatically, depending on certain factors. I would like to communicate with iTunes via separate apps made with AppleScript. One app, for example would be "play.app", and would play the music. Others would be conditional, and might play music depending on whether something's already playing, or if iTunes is open, etc.

The thing is, I want the arduino to send a character, for example "p" or a value, such as 1, and I need some other program to receive these serial messages and open the corresponding application. Does anyone know of a program I can use to accomplish this? I would prefer something simple and easy to use, as I don't have much time to learn a complex language, and also something that could export the program so that I can make it run at startup.

Any help would be much appreciated! :)

Thanks, -Dylan

Looks like the question has come up a few times in the last few years. I did a quick google of "applescript read serial input" and the first link was this: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1201066343.

You could also consider trying python.

You can talk from the arduino directly into Apple script with this extension:- Apple script SerialPort X http://mysite.verizon.net/vzenuoqe/MacSoft.html Use this on the arduino side:-

// Appple script tester using Serial Command X
// Push buttons on pins 11 & 12 to ground

void setup(){
  Serial.begin(9600);
  pinMode(12,INPUT);
  pinMode(11,INPUT);
  // enable internal pull ups
  digitalWrite(11, HIGH); 
  digitalWrite(12, HIGH);

}

void loop(){
  if(digitalRead(11) == LOW) {Serial.write(49); delay(1000); } // send a number and debounce delay
  if(digitalRead(12) == LOW) {Serial.write(50); delay(1000); }
}

and this on the apple script side

set arduinoPort to "/dev/cu.usbmodem1d11"
-- this opens the port and resets the arduino
set arduino to serialport open arduinoPort bps rate 9600 data bits 8 parity 0 stop bits 1 handshake 0
if arduino is equal to -1 then
    display dialog " could not open port "
else
    repeat -- repeat forever
        -- wait until something is sent
        set bytesIn to 0
        repeat until bytesIn is not equal to 0
            set bytesIn to serialport bytes available arduino
        end repeat
        set cmd to serialport read arduino for 1
        set cmds to cmd as string
        -- display dialog "Command recieved = " & cmds

        if cmds is equal to "1" then
            display dialog "Command 1 recieved"
        end if
        if cmds is equal to "2" then
            display dialog "Command to quit recieved"
            exit repeat
        end if


    end repeat -- of the forever repeat
    -- note this next bit will reset the arduino
    serialport close arduino
end if

Okay, I downloaded and installed SerialportX, and I tried out the code with applescript. When I try to run it, I get this message: "/dev/cu.usbmodemfa131" doesn’t understand the serialport open message.

Does it work for you? What could I be doing wrong?

Thanks, -Dylan

My arduino has a com port name of /dev/cu.usbmodem1d11 You should replace this with the name of your arduino. This is the name you see in the tools menu when you select the serial port for your system.

Grumpy_Mike: My arduino has a com port name of /dev/cu.usbmodem1d11 You should replace this with the name of your arduino. This is the name you see in the tools menu when you select the serial port for your system.

I forgot to mention, I did change it to /dev/cu.usbmodemfa131, which I got from the arduino software. I made no other changes to the code. I also tried it with another USB port, still didn't work.

EDIT: Let me add that I'm running OSX Lion: 10.7.3

Ah, only on the one down from that here, 10.6.8

:. Looks like I'll have to open up separate apps, then. Anybody know how I can open these apps based on signals from the arduino? Maybe Python? And how would I accomplish this?

Thanks for your help, Mike. :)

-Dylan

Ok, before you give up try some of the other examples you get with serialPortX.

I don’t think I’ll be able to get this working. I’ve done research, and I can’t find any fixes for Lion, and I don’t want to downgrade just for this project. I think the best way to go at this point is by simply opening apps. I’ve been looking around on processing and python forums, and I haven’t found any useful info. Anybody know how to do this, and with what software?

Thanks again,
-Dylan

I'm running Lion 10.7.3, and Mike's test code works ok for me. My Arduino shows up as /dev/cu.usbmodem621. All I did was download SerialPortX and copy the SerialPort X.osax file into /Library/ScriptingAdditions.

If you know Applescript, try downloading http://www.satimage.fr/software/en/downloads/index.html . This is "Smile" software and has a built in RS232 driver. It is a nicer package then Applescript, lots of examples, and it is free.

I just got it working! I did a little more research, turns out I clicked the enticing "download version 1" link on the page. The correct version 2.1 is not directly on that site. Anyways, upon downloading and installing the 2.1 version, it works perfectly. I'm going to have a lot of fun with my arduino this weekend. :P

Anyways, I have incorporated the code into my existing code and it works beautifully. Only one thing.How do I get the applescript to repeat indefinitely?

Thanks! -Dylan

How do I get the applescript to repeat indefinitely?

That sample code does that:-

repeat -- repeat forever

the only thing is I put an exit trap door in it

if cmds is equal to "2" then
            display dialog "Command to quit recieved"
            exit repeat

So miss that bit of code out or don't send the command to quit.

Hi Thanks! It works perfectly! One more thing, though. How do you write to the arduino? I have code on the arduino to flash a light when it recieves "1". I've tried it on the terminal and it works, but I can't get it to work in applescript. Here's the part of my arduino code that does this:

  if (Serial.available() > 0) {
                // read the incoming byte:
                incomingByte = Serial.read();

                // say what you got:
                Serial.print("I received: ");
                Serial.println(incomingByte);
  }
  if (incomingByte == 49) {
    while (1 == 1) {
      digitalWrite(relaypin3, HIGH);
      digitalWrite(pushswitchlight, HIGH);
      delay(1000);
      digitalWrite(relaypin3, LOW);
      digitalWrite(pushswitchlight, LOW);
      delay(1000);
    }
  }

And the code for the applescript:

set arduinoPort to "/dev/cu.usbmodemfd131"
set arduino to serialport open arduinoPort bps rate 9600 data bits 8 parity 0 stop bits 1 handshake 0
if arduino is equal to -1 then
    display dialog " could not open port "
else
    delay 1
    serialport write "1"
end if

serialport close arduino

Thanks, -Dylan

Look at the CP290Test sample application, that shows you how to write to the serial port.