Help sending a char to Arduino from an iPhone via hardware serial

Hello, so I am trying to make an LED (Flow_A) blink on a kegboard-mini Arduino shield, by sending a char from my iPhone, and I feel I am getting pretty close but for whatever reason the LED is not blinking, I think the problem lies in my Objective-C code :-/ but I am 100% sure that is the problem. Anyone here have any suggestions, on how I could troubleshoot this?

The Arduino sketch looks like the following, http://pastie.org/7704129

I am using a Jailbroken Serial class to send commands to the Arduino, from this project on github.

The documentation for the class can be found here.

And the method for sending the char looks like the following,

- (IBAction)blinkFlow_A_LED:(id)sender {
    
    // method to blink the Flow_A LED on the kegboard-mini Arduino sheild.
    
    NSLog(@"blink Flow_A btn pressed");
    
    NSLog(@"hello kyle");
    
    // open serial port / interface
    
    [serial open:B2400];
    if(serial.isOpened)
    {
        NSLog(@"Serial Port Opened");
    }
    else NSLog(@"Serial Port Closed");
    
    // print serial debugging messages
    serial.debug = true;
    
    
    // send serial data (tx)
    
    
//    char buffer [7];
//    
//    buffer[0] = '{';
//    buffer[1] = 'b';
//    buffer[2] = 'l';
//    buffer[3] = 'i';
//    buffer[4] = 'n';
//    buffer[5] = 'k';
//    buffer[6] = '}';
    
    
//    char character;
//    
//    character = 'b';
    
    char blink;
    blink = 'b';
    
    [serial write:&blink length:1];
    
    // print message sent
    NSLog(@"the command sent was:%c",blink);
}

How is your phone connected to your Arduino? What code is running on the Arduino?

PaulS: How is your phone connected to your Arduino? What code is running on the Arduino?

The iPhone is connected to the Arduino via serial port on the 30 pin dock connector, and the code running on the Arduino, is the pastie link posted in the first message.

Try delaying two seconds after opening the serial port before sending data to the arduino so you aren't talking to the bootloader when you send the data.

-br

billroy:
Try delaying two seconds after opening the serial port before sending data to the arduino so you aren’t talking to the bootloader when you send the data.

-br

How exactly would I do that? The Arduino is plugged in to the USB port on my computer, and is usually on, so I don’t think I am talking to the bootloader, as I can send commands via the serial monitor fine. When I send “b” to blink the LED it blinks, and when I send “s” it stops blinking the LED. So I think the Arduino code is good, I think there is a problem with the Objective-C code somewhere :frowning:

If you mean open the serial port wait 2 seconds then send the “b” command via Objective-C, I am all ears on how to do something like that.

When you first open the serial port, the Arduino resets and enters the boot loader to see if you want to upload a new program. It waits a couple seconds to see if the IDE is trying to upload, and if not it then times out and launches your Arduino C program.

During the time the bootloader is active, the first couple seconds after the serial port is opened, the bootloader receives any serial input you send. Not your program.

This ends up looking like "the arduino isn't hearing what I send to it", which is, I believe, the symptom you arrived here complaining about.

Try waiting a couple seconds in the iPhone code right after you open the serial port. There must be a way in Objective-C...

-br

Well, I added the code below to the method posted above (tongue twister) and the LED is still not blinking even with a delay of three (3) seconds.

// delay sending serial data (tx) for a given time period
    
    NSLog(@"start delay");
    
    [NSThread sleepForTimeInterval:3.0];
    
    NSLog(@"delay of 3 seconds ended");

Could you post the whole Obj-C code? Your snippet does not provide confidence that the delay is in the right place.

Thanks,

-br

billroy: Could you post the whole Obj-C code? Your snippet does not provide confidence that the delay is in the right place.

Thanks,

-br

- (IBAction)blinkFlow_A_LED:(id)sender {
    
    // method to blink the Flow_A LED on the kegboard-mini Arduino sheild.
    
    NSLog(@"blink Flow_A btn pressed");
    
    NSLog(@"hello kyle");
    
    // open serial port / interface
    
    [serial open:B2400];
    if(serial.isOpened)
    {
        NSLog(@"Serial Port Opened");
    }
    else NSLog(@"Serial Port Closed");
    
    // print serial debugging messages
    serial.debug = true;
    
    
    // send serial data (tx)
    
    
//    char buffer [7];
//    
//    buffer[0] = '{';
//    buffer[1] = 'b';
//    buffer[2] = 'l';
//    buffer[3] = 'i';
//    buffer[4] = 'n';
//    buffer[5] = 'k';
//    buffer[6] = '}';
    
    
//    char character;
//    
//    character = 'b';
    
    // delay sending serial data (tx) for a given time period
    
    NSLog(@"start delay");
    
    [NSThread sleepForTimeInterval:3.0];
    
    NSLog(@"delay of 3 seconds ended");
    
    char blink;
    blink = 'b';
    
    [serial write:&blink length:1];
    
    // print message sent
    NSLog(@"the command sent was:%c",blink);
}

It all looks good. Sorry, out of answers.

-br

billroy: It all looks good. Sorry, out of answers.

-br

I'm not an expert in iPhones or objective-C but can you do this just to confirm you have serial connection?

Connect Arduino to your computer. Open serial monitor at the speed you set iPhone serial port. Connect iPhone TX to Arduino TX Connect iPhone RX to Arduino RX Remove Arduino's ATMEGA320 chip if you want Write a simple program to send a few characters every second from iPhone. See if these characters make it to the computer serial monitor.

liudr: Connect iPhone TX to Arduino TX Connect iPhone RX to Arduino RX

Isn't the iPhone TX suppose to be connected to the Arduino RX :fearful: ?

Double-check that 2400 baud rate, too - it's rare enough to be worth questioning / confirming.

-br

billroy: Double-check that 2400 baud rate, too - it's rare enough to be worth questioning / confirming.

-br

Yeah their both set 2400 :-/

ipatch:

liudr:
Connect iPhone TX to Arduino TX
Connect iPhone RX to Arduino RX

Isn’t the iPhone TX suppose to be connected to the Arduino RX :fearful: ?

No, you are going to use the Arduino as a USB TTL converter. So TX to TX and RX to RX. Your iPhone replaces the arduino in sending/receiving information.

liudr:

ipatch:

liudr: Connect iPhone TX to Arduino TX Connect iPhone RX to Arduino RX

Isn't the iPhone TX suppose to be connected to the Arduino RX :fearful: ?

No, you are going to use the Arduino as a USB TTL converter. So TX to TX and RX to RX. Your iPhone replaces the arduino in sending/receiving information.

hunh?

I think your thinking about something else. I just did a serial write, and printed the message on my iPhone screen, so I think you may be thinking about something else.

I am heavily referencing THIS

You might want to see this specifically

ipatch:

liudr:

ipatch:

liudr: Connect iPhone TX to Arduino TX Connect iPhone RX to Arduino RX

Isn't the iPhone TX suppose to be connected to the Arduino RX :fearful: ?

No, you are going to use the Arduino as a USB TTL converter. So TX to TX and RX to RX. Your iPhone replaces the arduino in sending/receiving information.

hunh?

I think your thinking about something else. I just did a serial write, and printed the message on my iPhone screen, so I think you may be thinking about something else.

I am heavily referencing THIS

You might want to see this specifically

I repeat, test your iPhone serial connection with a pc before you connect it to Arduino. Do whatever suits you cause you apparently didn't understand what I said was "testing iPhone serial with a PC".

well i kind of have a proof of concept working, and I hooked the TX from the iPhone to the RX of the Arduino, you can see in this video.

http://www.youtube.com/watch?v=w06StMBDB44

OK cool. Then my step of checking connection is unnecessary. What did you change between non-working and working? Was it objective C or Arduino?