Communication between XBee and Processing

hi... First of all, i'm newbie in this field and trying to explore it deeply. i'm currently doing a simple project about pulse sensor. so far, i'm succeed to display the reading of pulse sensor at processing using usb that has connected to pc.but now, i want to make it wirelessly transfered using xbee s1. i already try with the below sketch but the reading could not display.it just display the interface. for your info i'm using arduino uno r3 in this project...

-sorry for my broken english-

import processing.serial.*;
import xbee.*;
import xbee.XBeeDataFrame;
import xbee.XBeeReader;
PFont font;
Scrollbar scaleBar;

Serial port;     
XBeeReader xbee;

int rssi = 0;
int address = 0;
int samples = 0;
int[] analog;

int Sensor;      
int HRV;         
int BPM;         
int[] RawY;      
int[] ScaledY;   
int[] rate;      
float zoom;      
float offset;    
color eggshell = color(255, 253, 248);
int heart = 0;   
//  THESE VARIABLES DETERMINE THE SIZE OF THE DATA WINDOWS
int PulseWindowWidth = 490;
int PulseWindowHeight = 512; 
int BPMWindowWidth = 180;
int BPMWindowHeight = 340;

boolean beat = false;    // set when a heart beat is detected, then cleared when the BPM graph is advanced

void setup() {
  size(700, 600);  // Stage size
  frameRate(100);  
  font = loadFont("Arial-BoldMT-24.vlw");
  textFont(font);
  textAlign(CENTER);
  rectMode(CENTER);
// Scrollbar inputs: x,y,width,height,minVal,maxVal
  scaleBar = new Scrollbar (310, 575, 180, 8, 0.5, 1.0);  // set parameters for the scale bar
  RawY = new int[PulseWindowWidth];          // initialize raw pulse waveform array
  ScaledY = new int[PulseWindowWidth];       // initialize scaled pulse waveform array
  rate = new int [BPMWindowWidth];           // initialize BPM waveform array
  zoom = 0.75;                               // initialize scale of heartbeat window

// set the visualizer lines to 0
 for (int i=0; i<rate.length; i++){
    rate[i] = 555;      // Place BPM graph line at bottom of BPM Window 
   }
 for (int i=0; i<RawY.length; i++){
    RawY[i] = height/2; // initialize the pulse window data line to V/2
 }

// GO FIND THE ARDUINO
  println(Serial.list());    // print a list of available serial ports
  // choose the number between the [] that is connected to the Arduino
  port = new Serial(this, Serial.list()[2], 9600);  // make sure Arduino is talking serial at this baud rate
  xbee = new XBeeReader(this, port);
  xbee.startXBee(); 
   println("XBee Library version " + xbee.getVersion());


  port.clear();            // flush buffer
  port.bufferUntil('\n');  // set buffer full flag on receipt of carriage return
}

void draw() {
  background(0);
  noStroke();
// DRAW OUT THE PULSE WINDOW AND BPM WINDOW RECTANGLES  
  fill(eggshell);  // color for the window background
  rect(255,height/2,PulseWindowWidth,PulseWindowHeight);
  rect(600,385,BPMWindowWidth,BPMWindowHeight);

// DRAW THE PULSE WAVEFORM
  // prepare Pulse Window Screen
  stroke(0,175,200);  // set up to draw a line at +V/2 (512 on the ADC)
  line(10, height/2, PulseWindowWidth+10, height/2); // draw a line at +V/2
  // prepare pulse data points    
  RawY[RawY.length-1] = (1023 - Sensor) - 212;   // place the new raw datapoint at the end of the array
  zoom = scaleBar.getPos();                      // get current waveform scale value
  offset = map(zoom,0.5,1,150,0);                // calculate the offset needed at this scale
  for (int i = 0; i < RawY.length-1; i++) {      // move the pulse waveform by
    RawY[i] = RawY[i+1];                         // shifting all raw datapoints one pixel left
    float dummy = RawY[i] * zoom + offset;       // adjust the raw data to the selected scale
    ScaledY[i] = constrain(int(dummy),44,556);   // transfer the raw data array to the scaled array
  }
  stroke(250,0,0);                               // red is a good color for the pulse waveform
  noFill();
  beginShape();                                  // using beginShape() renders fast
  for (int x = 1; x < ScaledY.length-1; x++) {    
    vertex(x+10, ScaledY[x]);                    //draw a line connecting the data points
  }
  endShape();

// DRAW THE BPM WAVE FORM
// first, shift the BPM waveform over to fit then next data point only when a beat is found
 if (beat == true){   // move the heart rate line over one pixel every time the heart beats 
   beat = false;      // clear beat flag (beat flag waset in serialEvent tab)
   for (int i=0; i<rate.length-1; i++){
     rate[i] = rate[i+1];                  // shift the bpm Y coordinates over one pixel to the left
   }
// then limit and scale the BPM value
   BPM = min(BPM,200);                     // limit the highest BPM value to 200
   float dummy = map(BPM,0,200,555,215);   // map it to the heart rate window Y
   rate[rate.length-1] = int(dummy);       // set the rightmost pixel to the new data point value
 } 
 // GRAPH THE HEART RATE WAVEFORM
 stroke(250,0,0);                          // color of heart rate graph
 strokeWeight(2);                          // thicker line is easier to read
 noFill();
 beginShape();
 for (int i=0; i < rate.length-1; i++){    // variable 'i' will take the place of pixel x position   
   vertex(i+510, rate[i]);                 // display history of heart rate datapoints
 }
 endShape();

// DRAW THE HEART AND MAYBE MAKE IT BEAT
  fill(250,0,0);
  stroke(250,0,0);
  // the 'heart' variable is set in serialEvent when arduino sees a beat happen
  heart--;                    // heart is used to time how long the heart graphic swells when your heart beats
  heart = max(heart,0);       // don't let the heart variable go into negative numbers
  if (heart > 0){             // if a beat happened recently, 
    strokeWeight(8);          // make the heart big
  }
  smooth();   // draw the heart with two bezier curves
  bezier(width-100,50, width-20,-20, width,140, width-100,150);
  bezier(width-100,50, width-190,-20, width-200,140, width-100,150);
  strokeWeight(1);          // reset the strokeWeight for next time


// PRINT THE DATA AND VARIABLE VALUES
  fill(eggshell);                                       // get ready to print text
  text("Pulse Sensor Amped Visualizer 1.0",245,30);     // tell them what you are
  text("HRV " + HRV + "mS",600,585);                    // print the time between heartbeats in mS
  text(BPM + " BPM",600,200);                           // print the Beats Per Minute
  text("Pulse Window Scale " + nf(zoom,1,2), 150, 585); // show the current scale of Pulse Window

//  DO THE SCROLLBAR THINGS
  scaleBar.update (mouseX, mouseY);
  scaleBar.display();

//   
}  //end of draw loop

// called every time an XBee event is received: every 2s in the case of the Tweet A Watt
public void xBeeEvent(XBeeReader xbee) {    
   // Grab a frame of data
   XBeeDataFrame data = xbee.getXBeeReading();   

  println("");
  println("LOOP " + hour() + ":" + minute() + ":" + second());

    // Get the transmitter address
    address = data.getAddress16();
    println("API ID: " + address);    

    // Get the RSSI
    rssi = data.getRSSI();
  println("RSSI: " + rssi);      

  // Get total number of samples
  samples = data.getTotalSamples();   
  println("Total Samples: " + samples);    

  // Output the Analog readings for each sample     
  // ONLY GETS FIRST SAMPLE - How do I access all samples?
  for (int i=0; i < samples; i++) {
   analog = data.getAnalog(i);
   print("[");
   for (int j=0; j < analog.length; j++) {
    print(analog[j]);
    if (j < analog.length - 1) { print(", "); }
   }
   print("]");
   if (i < samples - 1) { print(", "); }
   else { println(""); }
  }
}

Answers

  • Have you programmed your XBees using X-CTU? You will need one coordinator XBee and one Router or End Device XBee, both with the same PAN ID.

    I'm not familiar with the XBee Classes, but are they compatible with the old XBee S1 devices? You might look into upgrading to the S2 devices. The S1s are being phased out and might not be supported.

Sign In or Register to comment.