Wiki

Reso-nance numérique | Arts et cultures libres

Outils du site


Panneau latéral

materiel:xbee:star:processing-codes

Ceci est une ancienne révision du document !


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/attic/materiel/xbee/star/processing-codes.1410993701.txt.gz · Dernière modification: 2014/09/18 00:41 de resonance