Pages: [1]   Go Down
Author Topic: Fio 2 way communication xbee  (Read 493 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm trying to do 2 way communication with 2 Fio's.  Using Series 1 Xbees.  Calling Serial.print("coded string") and trying to get an LED to respond to local pulse sensor AS WELL AS the other pulse sensor.  IOW, 2 pulse sensors each on a Fio.  Each Fio has an LED for local pulse, plus an LED for remote (other persons) pulse.

I set them up like this:
Code:
XBee 1
PAN ID: 1111
DH: 0
DL: 12
MY: 11 (radio 17)

XBee 2
PAN ID: 1111
DH: 0
DL: 11
MY: 12 (radio 18)

If I look at the serial monitor does that affect the delivery of the serial print data.  I've been wanting to track what's up by adding serial print statements but I fear they'll affect my serial read functions.

Would appreciate any references on how to set up cross communication between 2 xbee based fio boards.

Thanks in advance,
Chris.

The code I'm using for serial reader function:
Code:
// read serial port
char* serialReader(){
  int makeSerialStringPosition;
  int inByte;
  char serialReadString[50] = "";
  const int terminatingChar = 13; //Terminate lines with CR

  inByte = Serial.read();
  makeSerialStringPosition=0;

  if (inByte > 0 && inByte != terminatingChar) { //If we see data (inByte > 0) and that data isn't a carriage return
    delay(100); //Allow serial data time to collect (I think. All I know is it doesn't work without this.)

    while (inByte != terminatingChar && Serial.available() > 0){ // As long as EOL not found and there's more to read, keep reading
      serialReadString[makeSerialStringPosition] = inByte; // Save the data in a character array
      makeSerialStringPosition++; //Increment position in array
      //if (inByte > 0) Serial.println(inByte); // Debug line that prints the charcodes one per line for everything recieved over serial
      inByte = Serial.read(); // Read next byte
    }

    if (inByte == terminatingChar) //If we terminated properly
    {
      serialReadString[makeSerialStringPosition] = 0; //Null terminate the serialReadString (Overwrites last position char (terminating char) with 0
      // Serial.print("from method: ");
      // Serial.println(serialReadString);
      // if (strcmp(serialReadString, "LEDOn") == 0) digitalWrite(13, HIGH);
      // if (strcmp(serialReadString, "LEDOff") == 0) digitalWrite(13, LOW);

      return serialReadString;
    }
  }
  return "x"; // return this when nothing is recieved...
}

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
char* serialReader(){
  int makeSerialStringPosition;
  int inByte;
  char serialReadString[50] = "";
<snip>
      return serialReadString;
    }
  }
  return "x"; // return this when nothing is recieved...
}
Returning a pointer to a local variable, which is destroyed as soon as the function ends, is really not a good idea.

Quote
If I look at the serial monitor does that affect the delivery of the serial print data.
Do you think the Serial Monitor can really tell when you are looking at it?

Quote
I've been wanting to track what's up by adding serial print statements but I fear they'll affect my serial read functions.
Serial.print() won't "affect" the Serial.read() functions. They will affect what is available to be read. The serial port can't distinguish between data that the XBee is to transmit and data that is to be shown on the Serial Monitor. Ideally, you'd use two different serial ports for this - one for the PC and one for the XBee. I don't know whether that is possible with the Fio.

You could have each message prefixed with "XB:" is the (receiving) XBee is supposed to care and "PC:" if not.

Code:
      serialReadString[makeSerialStringPosition] = 0; //Null terminate the serialReadString (Overwrites last position char (terminating char) with 0
Most people use shorter names, like index, for the array index variable.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for your help, PaulS.

"Do you think the Serial Monitor can really tell when you are looking at it?"  haha, pretty funny.  Like a Schrödinger's Fio serial port.  I guess I've always used serial to send command to an arduino (and to view its receipt with the monitor) so I was concerned that viewing from the Arduino serial port might be eating the output.

Now it seems I've got it almost working, but what I am observing is that the 2 devices are constantly resetting each other.  I suppose it has something to do with the way the serial is used for loading new software.

Right now I have to remove the xbee in order to load updated code to each Fio.

The AREF pin is jumped to 3.3V because the PulseSensor I'm using needs 3.3V.

I'm using adafruit's FTDI Friend and I lifted CTS pin because that's where the AREF (3.3V) pin is on the FTDI Friend.  Perhaps I should connect the AREF directly to CTS?

Anyone have any tips on subduing the resets on the Fio?

Thanks in advance,
Chris.

Here's my simple 2 way code (buttons instead of pulse sensors)

Code:

// twowaytest.ino
// testing 2-way communications with the Fio

int buttonPin = 2; // button with 10k to gnd
int localHbLed = 13; //led to gnd
int remoteHbLed = 12; // led to gnd

int buttonState = 0;
int lastState = 0;
const int terminatingChar = 13; //Terminate lines with CR

void setup(){

  pinMode(localHbLed,OUTPUT);   
  pinMode(remoteHbLed,OUTPUT);
  pinMode(buttonPin, INPUT); 

  Serial.begin(57600);
  Serial.print(255);
  delay(2000);
 
  analogReference(EXTERNAL);
  blink(3); // show we're on...
}


void loop(){

  buttonState = digitalRead(buttonPin);

  if (lastState != buttonState) {
    if (buttonState == HIGH) {     
      // turn LED on:   
      digitalWrite(localHbLed, HIGH);
      sendDataToOther("pulseUp");
      lastState = HIGH;
    }
    else {
      // turn LED off:
      digitalWrite(localHbLed, LOW);
      sendDataToOther("pulseDn"); 
      lastState = LOW;
    }
  }

  getDataFromOther();
  delay(20);                          //  take a break
}

void sendDataToOther(String pulseVal){
  // send info to the other xbee
  if (pulseVal == "pulseUp"){
    Serial.print("pulseUp\r");
  }
  else {
    Serial.print("pulseDn\r");
  }
}

void getDataFromOther() {

  char* theOtherPulse = serialReader();
  // Serial.print("Other is: ");
  // Serial.println(theOtherPulse);

  // if (theOtherPulse == theOtherTest[0]){
  if (strstr(theOtherPulse, "PulseUp") != 0) {
    digitalWrite(remoteHbLed,HIGH);
    // Serial.print("recd... HIGH ");
    // Serial.println(theOtherPulse);
  }
  else {
    digitalWrite(remoteHbLed,LOW);
    // Serial.print("recd... LOW ");
    // Serial.println(theOtherPulse);
  }
}

// read serial port
char* serialReader(){
  int makeSerialStringPosition;
  int inByte;
  char serialReadString[50] = "";
 

  inByte = Serial.read();
  makeSerialStringPosition=0;

  if (inByte > 0 && inByte != terminatingChar) { //If we see data (inByte > 0) and that data isn't a carriage return
    delay(100); //Allow serial data time to collect (I think. All I know is it doesn't work without this.)

    while (inByte != terminatingChar && Serial.available() > 0){ // As long as EOL not found and there's more to read, keep reading
      serialReadString[makeSerialStringPosition] = inByte; // Save the data in a character array
      makeSerialStringPosition++; //Increment position in array
      //if (inByte > 0) Serial.println(inByte); // Debug line that prints the charcodes one per line for everything recieved over serial
      inByte = Serial.read(); // Read next byte
    }

    if (inByte == terminatingChar) //If we terminated properly
    {
      serialReadString[makeSerialStringPosition] = 0; //Null terminate the serialReadString (Overwrites last position char (terminating char) with 0
      // Serial.print("from method: ");
      // Serial.println(serialReadString);
      // if (strcmp(serialReadString, "LEDOn") == 0) digitalWrite(13, HIGH);
      // if (strcmp(serialReadString, "LEDOff") == 0) digitalWrite(13, LOW);

      return serialReadString;
    }
  }
  return "x"; // return this when nothing is recieved...
}

int stringToInt(String thisString) {
  int i, value, length;
  length = thisString.length();
  char blah[(length+1)];
  for(i=0; i<length; i++) {
    blah[i] = thisString.charAt(i);
  }
  blah[i]=0;
  value = atoi(blah);
  return value;
}

// blink n times
void blink(int howManyTimes){
  digitalWrite(localHbLed, LOW);
  digitalWrite(remoteHbLed, LOW);
  delay(200);
  for (int i=0; i< howManyTimes; i++){
    digitalWrite(localHbLed, HIGH);
    digitalWrite(remoteHbLed, LOW);
    delay(200);
    digitalWrite(localHbLed, LOW);
    digitalWrite(remoteHbLed, HIGH);
    delay(200);
  }
  digitalWrite(remoteHbLed, LOW);
}


Logged

Pages: [1]   Go Up
Jump to: