Wiki

Reso-nance numérique | Arts et cultures libres

Outils du site


Panneau latéral

materiel:xbee:star:processing-codes

Receive XBee packets in API mode (basics with serial)

/*
 * Receive XBee packets in API mode (basics with serial)
 * from http://www.pobot.org/XBee-API-pour-pilotage-a-distance.html
 */
import processing.serial.*;
import xbee.*;
Serial port;
 
void setup()
{
  println(Serial.list());
  port = new Serial(this, Serial.list()[0], 9600);
  port.write("+++");
  delay(1100);
  port.write("ATRE,ID1111,MY1234,DH0,DL0,CN\r\n");
}
 
void draw()
{
 while (port.available() != 0) {
  String val = hex(port.readChar());
  if (val.equals("007E")) {
    println();
  }
  print(val);
  print(" ");
 } 
}

Receive XBee packets in API mode with xbee-api

/*
 * Receive XBee packets in API mode with xbee-api
 * XBee#2 send values from an analog sensor to XBee#1
 *
 * From http://code.google.com/p/xbee-api/wiki/Processing
 *
 * Configurations : 
 * XBEE#1 : ATID1234, ATAP2
 * XBEE#2 : ATID5678, ATDL1234, ATAP2
 */
 
import processing.serial.*;
import java.util.concurrent.*;
import java.util.Queue; //  avoid Queue error
 
XBee xbee;
Queue<XBeeResponse> queue = new ConcurrentLinkedQueue<XBeeResponse>();
boolean message;
XBeeResponse response;
 
void setup() {
  try { 
    PropertyConfigurator.configure(dataPath("")+"log4j.properties"); // optional.set up logging
    xbee = new XBee();
    xbee.open("/dev/ttyUSB0", 9600); // replace with your COM port
    xbee.addPacketListener(new PacketListener() {
      public void processResponse(XBeeResponse response) {
        queue.offer(response);
      }
    }
    );
  } 
  catch (Exception e) {
    System.out.println("XBee failed to initialize");
    e.printStackTrace();
    System.exit(1);
  }
}
 
void draw() {
  try {
    readPackets();
  } 
  catch (Exception e) {
    e.printStackTrace();
  }
}
 
void readPackets() throws Exception {
  while ((response = queue.poll()) != null) {
    // we got something!
    try {
      RxResponseIoSample ioSample = (RxResponseIoSample) response;
      println("We received a sample from " + ioSample.getSourceAddress());
      if (ioSample.containsAnalog()) {
        println("Sensor port 20 (10 bits) : " +
          ioSample.getSamples()[0].getAnalog0());
      }
    } 
    catch (ClassCastException e) {
      // not an IO Sample
    }
  }
}
/home/resonancg/www/wiki/data/pages/materiel/xbee/star/processing-codes.txt · Dernière modification: 2014/09/18 00:43 de resonance