What code will send serial data smoothly over xbees?

Hi,
I have an analog sensor (connected to A0) on an Arduino Fio with xbee that sends data to another xbee connected to an explorer usb that plugs into my macbook pro laptop, and then I read the serial data from the sensor in Max/Msp/Jitter software. (My Xbee's are "XBee Pro S1"'s)

I've been having trouble getting the smooth stream of numbers to show up reliably in Max/Msp/Jitter, sometimes a stream of numbers appears and sometimes it doesn't. Through much testing, I have determined that the issue is my fio code. I tested out some different fio codes and now it's not working at all.

I thought I was originally using this code, but now when I connect and read the serial monitor in arduino (IDE), all it reads is "kkkkkkkkkkkkkkkkkkkkkkkkk....".
Here's what the serial monitor says: (It looks like it boots up then sends some data then reboots and then sends a little less data then reboots???)

k
+STWMOD=0
k
+STWMOD=0
k
+STWMOD=0
k
+STWMOD=0
kkk
+STWMOD=0
kkkkk
+STWMOD=0
kkkkk
+STWMOD=0

+STNA=modem

+STAUTO=0

+STOAUT=1

+STPIN=0000

+INQ=1
571 590
470 420
468 420
468 447
469 462
469 465
469 466
471 466
472 466
475 467
475 467
475 466
475 466
475 465
475 467
469 467
475 470
474 471
476 472
474 471
476 468
472 468
477 467
474 466
474 466
466 461
466 461
469 461
474 469
481 475
490 483
492 486
492 486
492 485
481 477
479 472
479 472
479 476
479 476
470 471
469 469
464 463
461 459
461 456
461 455
460 452
458 449
458 449
457 449
454 448
454 448
457 451
457 454
457 454
455 452
455 448
457 448
459 448
461 450
462 454
462 455
462 455
464 462
465 463
465 463
475 467
475 467
471 464
468 459
468 458
468 459
477 464
477 469
478 472
477 475
477 475
462 464
425 436
425 436
423 425
422 420
422 420
434 420
436 434
465 445
450 452
465 452
477 466
477 466
478 467
478 467
480 469
480 470
480 470
468 463
468 463
474 466
474 468
471 468
471 468
474 468
474 468
470 466
469 461
469 461
471 463
474 463
474 465
475 468
475 472
475 473
477 473
475 472
477 469
477 469
484 473
488 476
488 478
485 478
480 476
480 476
481 479
483 479
483 479
483 480
490 481
492 483
492 483
492 483
492 484
492 484
491 489
491 489
491 489
495 491
499 494
499 494
489 486
484 478
484 477
486 477
486 479
485 479
480 478
480 478
487 482
490 484
492 486
493 487
494 487
494 485
495 484
495 484
495 487
493 487
493 488
496 493
498 493
498 493
499 494
499 494
496 488
495 485
495 485
495 484
493 486
493 486
493 490
498 492
502 495
504 496
504 497
505 497
506 497
506 497
509 499
509 500
508 500
505 500
504 500
504 500
502 500
505 500
505 499
507 499
508 499
508 499
508 500
508 500
508 500
508 501
508 504
508 505
510 505
510 504
510 504
509 501
515 504
510 503
512 504
512 505
514 507
514 511
519 516
522 517
522 517
521 516
521 514
521 513
520 509
k
+STWMOD=0

+STNA=modem

+STAUTO=0

+STOAUT=1

+STPIN=0000

+INQ=1
508 496
516 469
530 469
530 502
530 510
529 521
529 521
521 520
521 517
521 517
522 517
525 517
526 517
526 516
517 510
517 510
522 511
527 519
527 522
527 522
519 518
518 515
k
+STWMOD=0

+STNA=modem

+STAUTO=0

+STOAUT=1

+STPIN=0000

+INQ=1
524 478
530 475
531 475
k
+STWMOD=0

+STNA=modem

+STAUTO=0
k
+STWMOD=0
k
+STWMOD=0
k
+STWMOD=0
kk
+STWMOD=0
k
+STWMOD=0
k
+STWMOD=0
k
+STWMOD=0
kkkkk
+STWMOD=0
kkkkkkkkkkkkkkkkkkkkk

Am I missing something? Is this code totally useless? (I can't read code very much) This is the fio code:

/* Upload this sketch into Seeeduino and press reset*/
 int median_of_3( int a, int b, int c ){
    int the_max = max( max( a, b ), c );
    int the_min = min( min( a, b ), c );
    // unnecessarily clever code
    int the_median = the_max ^ the_min ^ a ^ b ^ c;
    return( the_median );
 }   
  
int newest = 0;
int recent = 0;
int oldest = 0;

int newest2 = 0;
int recent2 = 0;
int oldest2 = 0;

void setup() 
{ 
  oldest = analogRead(0);
  oldest2 = analogRead(1);
    recent = oldest;
    newest = recent;
    recent2 = oldest2;
    newest2 = recent2;
    //Serial.begin(9600);          //Serial port for debugging, Comment this line if not required  
   // pinMode(RxD, INPUT);
    //pinMode(TxD, OUTPUT);
    setupBlueToothConnection();
 
} 
 
void loop() 
{ 
  // drop oldest value and shift in latest value
    oldest = recent;
    recent = newest;
    newest = analogRead(0);
    oldest2 = recent2;
    recent2 = newest2;
    newest2 = analogRead(1);

    int median = median_of_3( oldest, recent, newest );
int median2 = median_of_3( oldest2, recent2, newest2 );
  
delay(100);
    Serial.print(median);
   Serial.print(" ");
   Serial.println(median2);
   
    
    
   
} 
 
void setupBlueToothConnection()
{
    Serial.print("Setting up Bluetooth link");       //For debugging, Comment this line if not required    
    Serial.begin(9600); //Set BluetoothBee BaudRate to default baud rate 38400 (9600 for xbee)
    delay(2000);
    sendBlueToothCommand("\r\n+STWMOD=0\r\n");
    delay(200);
    sendBlueToothCommand("\r\n+STNA=modem\r\n");
    delay(200);
    sendBlueToothCommand("\r\n+STAUTO=0\r\n");
    delay(200);
    sendBlueToothCommand("\r\n+STOAUT=1\r\n");
    delay(200);
    sendBlueToothCommand("\r\n+STPIN=0000\r\n");
    delay(3000); // This delay is required.
    Serial.print("\r\n+INQ=1\r\n");
    delay(3000); // This delay is required.
    //Serial.print("Setup complete");
 
}
 
void sendBlueToothCommand(char command[])
{
    char a;
    //blueToothSerial.print(command);
    Serial.print(command);                          //For debugging, Comment this line if not required    
    delay(3000);
 
   // while(blueToothSerial.available())              //For debugging, Comment this line if not required  
   // {                                               //For debugging, Comment this line if not required   
   //    Serial.print(char(blueToothSerial.read()));  //For debugging, Comment this line if not required  
}                                               //For debugging, Comment this line if not required

I then tried just the simple "AnalogReadSerial" arduino code, and that sends the numbers, but in chunks, (I think the arduino stores up the data and then sends it in spurts to the computer??)

What code do I need to put on my fio to send in a smooth stream of serial numbers from my analog sensor???

(SECOND ISSUE) Also, this may be a separate issue, but after I connect/disconnect the xbee explorer a few times from my Macbook pro laptop, it stops recognizing the port, or the port name switches even though I'm plugging it into the same usb slot directly into my lapttop.... I've looked through forums and it seems like this may just be a Mac bug?? I restart my computer, and then it can recognize the port again... just curious if there is something I can do to prevent this issue, as it's annoying to have to keep restarting my computer.

The short answer is data communications is never a smooth stream of characters. Slow async may appear that way because of the slowness, but any of the other ways is ALWAYS in packets of data.

Paul

So there is no way to get the fio to send serial data consistently over the xbee's (like every few milliseconds)? It will only send data in chunks like every 3 seconds? Maybe "smooth" isn't the correct term I should be using...

What do you make of my serial monitor example where it does send the data quickly (the two columns of numbers) but then seems to suddenly stop and reset (+STWMOD=0 etc.) after like 10 seconds of sending numbers consistently?

isaralynn:
So there is no way to get the fio to send serial data consistently over the xbee's (like every few milliseconds)? It will only send data in chunks like every 3 seconds? Maybe "smooth" isn't the correct term I should be using...

What do you make of my serial monitor example where it does send the data quickly (the two columns of numbers) but then seems to suddenly stop and reset (+STWMOD=0 etc.) after like 10 seconds of sending numbers consistently?

Asynchronous means you cannot depend on data being sent at a specific time. The ONLY time dependent part is the start bit ensures the following bits will be each be sent at specific time intervals, the baud rate. The time between each byte is not defined. May be 0 or minutes in length.

The other question, I guess, means you have some debugging to do.

Paul

Hm I still don't think I'm understanding. In the fio code can't you tell it when to send data to the other xbee?

My code works fine when I use an Uno and directly connect it to my computer, so my issue has to do with the xbee sending data quickly enough when not connected directly to the computer.

So there is no way to get the fio to send serial data consistently over the xbee's

You aren't using XBees, so the short answer is no.

You are using bluetooth devices that happen to have a XBee form factor. Stop claiming you are using XBees.

PaulS:
You aren't using XBees, so the short answer is no.

You are using bluetooth devices that happen to have a XBee form factor. Stop claiming you are using XBees.

This is a link to the product I'm using, I have two, one connected to the Arduino Fio and one connected to the Explorer USB: XBee Pro Module - Series 1 - 60mW with Wire Antenna [XBP24-AWI-001] : ID 964 : $37.95 : Adafruit Industries, Unique & fun DIY electronics and kits

If these aren't xbee's then what is an XBee??

The items in that link ARE XBees.

    setupBlueToothConnection();

If you are setting up an XBee connection, that is a pretty dumb name.

void setupBlueToothConnection()
{
    Serial.print("Setting up Bluetooth link");       //For debugging, Comment this line if not required    
    Serial.begin(9600); //Set BluetoothBee BaudRate to default baud rate 38400 (9600 for xbee)
    delay(2000);
    sendBlueToothCommand("\r\n+STWMOD=0\r\n");
    delay(200);
    sendBlueToothCommand("\r\n+STNA=modem\r\n");
    delay(200);
    sendBlueToothCommand("\r\n+STAUTO=0\r\n");
    delay(200);
    sendBlueToothCommand("\r\n+STOAUT=1\r\n");

Sure as hell looks like code to set up bluetooth hardware, not XBees.

Ah, sorry I see the confusion now. Yes I think I originally was using a Bluetooth bee, and then a few years ago switched to Xbees and a friend adapted the code, but it has been awhile so I forget where this code came from exactly. I thought I remembered this code working with the Xbees just fine, even though it says bluetooth in the comments.

For anyone having the same issue as me trying to get xbees to send analog sensor data to a serial port, a friend helped me out and I have a working code now! here ya go:

#define SENSOR_PIN 0 // where the breath sensor connects to the arduino

#define NUM_SAMPLES 700 // the size of the output buffer

#define TX_TIME 50 // how often in ms the value is sent over the xbee

int output_buffer[NUM_SAMPLES];
int output_index = 0;

void setup() { 
  // init the output_buffer so it is all zeros
  for (int i = 0; i < NUM_SAMPLES; i++) {
    output_buffer[i] = 0;
  }
  Serial.begin(9600);
} 
 
void loop() {
  int current_value = analogRead(0);

  output_buffer[output_index] = current_value;

  // increment the index, wrap around if needed
  output_index += 1;
  if (output_index >= NUM_SAMPLES) {
    output_index = 0;
  }

  long buffer_sum = 0;

  for (int i = 0; i < NUM_SAMPLES; i++) {
    buffer_sum += output_buffer[i];
  }

  int output_value = buffer_sum / NUM_SAMPLES;

  if (millis() % TX_TIME == 0) {
    Serial.println(output_value);    
  }

}