How to connect xbee with processing ?

Hello, I'm a student and i make a project. In this project we have many sensor like tilt sensor, Co2 sensor, camera and we need to connect this component who are in a robot, to a computer for see the information send by the robot. We have two XBee pro, we try to send information but we don't understand how to see the information on Processing, Actually we see the information in XCTU.

Thanks for your help.

Tagged:

Answers

  • Hi, Sokky,

    You will need to create a serial connection between Processing and one of the XBee modules. Here is an example that uses an XBee 802.15.4 module connected to a PC running Processing, to communicate with another XBee module connected to an Arduino controller. There is a potentiometer connected to analog pin 0 on the Arduino, and the Processing sketch prints the value of the pin as transmitted via the XBee radios.

    [By default the Arduino sends character data, so e.g. if the potVal is 10, the Arduino will transmit char(49), char(48), char(13), char(10), i.e. '1', '0', carriage return, line feed. So, the Processing sketch uses the readStringUntil method to read up to the line feed (10).]

    /** Processing sketch */
    
    import processing.serial.*;
    
    Serial xbee;
    
    String potVal;
    
    void setup() {
      size(300, 300);
      println(Serial.list()); // print list of available serial ports
      // you might need to change the Serial.list()[VALUE]
      xbee = new Serial(this, Serial.list()[1], 9600);
    }
    
    void draw() {
      // you could map the incoming data to a graphical or 
      // text display
    }
    
    void serialEvent(Serial xbee) {
      // See Tom Igoe's serial examples for more info
      potVal = xbee.readStringUntil(10); // read incoming data until line feed
      if (potVal != null) {
        println("Incoming data: " + potVal);
      }
    }
    

    Corresponding Arduino code (just for illustration):

    /** Arduino sketch */
    
    #include <SoftwareSerial.h>
    
    #define xbeeRx 6 // XBee DOUT
    #define xbeeTx 7 // XBee DIN
    
    int potPin = 0; // pot connected to analog pin 0
    int potVal = 0; // variable for pot value
    
    SoftwareSerial xbee = SoftwareSerial(xbeeRx, xbeeTx);
    
    void setup() {
      pinMode(xbeeRx, INPUT);
      pinMode(xbeeTx, OUTPUT);
      xbee.begin(9600);
    }
    
    void loop() {
      potVal = analogRead(potPin); // read the value of analog pin 0
      xbee.println(potVal); // send the value via XBee
    }
    

    If you have multiple sensors, then you will also need to determine a scheme for parsing the data. I suggest looking online for Tom Igoe's examples or check out the book Making Things Talk.

    Hope this helps. Good luck!

Sign In or Register to comment.