将XBee数据读入Processing

时间:2010-06-10 19:00:27

标签: serial-port processing electronics xbee

我最近建立了一个Tweet A Watt(http://www.ladyada.net/make/tweetawatt/)无线电源监控器,它使用XBee进行数据传输。我正在尝试将Tweet A Watt数据导入Processing,以用于创建一些视觉能量反馈原型。使用XBee API库进行处理(http://www.faludi.com/code/xbee-api-library-for-processing/),我取得了一些进展,但遇到了一个障碍,我将不胜感激。

我的处理草图如下所示:

/*
XBee Communication Prototype
XBee API Library by Daniel Shiffman and Rob Faludi: http://www.faludi.com/code/xbee-api-library-for-processing/
Sample XBee communication code adapted from Tom Igoe: http://www.tigoe.net/pcomp/code/category/Processing/148
*/

//import the xbee and serial libraries:
import xbee.*;
import processing.serial.*;

// set up Xbee parameters:
Serial port;
XBeeReader xbee;
int rssi = 0;     // received signal strength
int address = 0;     // sender's address
int samples = 0;     // total number of samples
int[] analog;     // values from the analog I/O pins

void setup() {
  // set up xbee
  port = new Serial(this, Serial.list()[0], 9600);
  xbee = new XBeeReader(this, port);
  xbee.startXBee();  
}

void draw() {}    

// 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(""); }
  }
}

这一切都按预期工作。每隔2s调用一次xBeeEvent,并输出API ID,RSSI和Total Samples的正确值(19)。但是,当输出模拟读数的内容时,我似乎将第一个样本重复19次。请参阅此示例输出:

LOOP 10:37:57
API ID: 1
RSSI: -61
Total Samples: 19
[512, -1, -1, -1, 688, -1], [512, -1, -1, -1, 688, -1], [512, -1, -1, -1, 688, -1], [512, -1, -1, -1, 688, -1], [512, -1, -1, -1, 688, -1], [512, -1, -1, -1, 688, -1], [512, -1, -1, -1, 688, -1], [512, -1, -1, -1, 688, -1], [512, -1, -1, -1, 688, -1], [512, -1, -1, -1, 688, -1], [512, -1, -1, -1, 688, -1], [512, -1, -1, -1, 688, -1], [512, -1, -1, -1, 688, -1], [512, -1, -1, -1, 688, -1], [512, -1, -1, -1, 688, -1], [512, -1, -1, -1, 688, -1], [512, -1, -1, -1, 688, -1], [512, -1, -1, -1, 688, -1], [512, -1, -1, -1, 688, -1]

LOOP 10:38:59
API ID: 1
RSSI: -61
Total Samples: 19
[503, -1, -1, -1, 561, -1], [503, -1, -1, -1, 561, -1], [503, -1, -1, -1, 561, -1], [503, -1, -1, -1, 561, -1], [503, -1, -1, -1, 561, -1], [503, -1, -1, -1, 561, -1], [503, -1, -1, -1, 561, -1], [503, -1, -1, -1, 561, -1], [503, -1, -1, -1, 561, -1], [503, -1, -1, -1, 561, -1], [503, -1, -1, -1, 561, -1], [503, -1, -1, -1, 561, -1], [503, -1, -1, -1, 561, -1], [503, -1, -1, -1, 561, -1], [503, -1, -1, -1, 561, -1], [503, -1, -1, -1, 561, -1], [503, -1, -1, -1, 561, -1], [503, -1, -1, -1, 561, -1], [503, -1, -1, -1, 561, -1]

如您所见,第一个样本重复19次。从Tweet A Watt软件( wattcher.py )运行原始Python脚本输出类似的XBee数据包读数,但有19个不同的样本。这是我正在尝试进入Processing的状态。

在XBee API库中,getAnalog()和getAnalog(n)函数定义如下:

getAnalog() – returns an array of integers that represents the current state of each analog channel with -1 indicating that the channel is not configured for analog. Use this when there is only one sample per frame.

getAnalog(int n) – returns the nth sample of analog data as an array of integers with -1 indicating that the channel is not configured for analog.

我在 for 循环中使用 getAnalog(int n)。在调用 XBeeDataFrame data = xbee.getXBeeReading(); 时,问题是我只得到一个“数据帧”吗?

我也试过直接读取串行数据包而不使用XBee API库(参考(http://www.tigoe.net/pcomp/code/category/Processing/8),(http://processing.org/reference/libraries/serial/Serial.html)和(http://ssdl.stanford.edu/ssdl/images/stories/AA236/0708A/Lab/Rover/Parts/xbeeproproductmanual.pdf),但是我的在这方面缺乏经验使得这有点不起作用。

如果有人熟悉XBee数据包,XBee API库或阅读处理中的串行数据可以提供帮助,我们将不胜感激。我希望数据存在,我只是没有正确访问它。我意识到这是一个非常具体的问题,我已将它发布在Adafruit(Tweet A Watt工具包的制造商 - http://forums.adafruit.com/viewtopic.php?f=40&t=16067&sid=4e34727fa59b7c7d589564d2d6b85e46)和Processing(http://processing.org/discourse/yabb2/YaBB.pl?num=1276111549)论坛上,但经过几十次观察后我没有任何回复,所以我想我会把网络扩大一点。

如果我遗漏了任何有用的信息,请告诉我。在此先感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

在我的书中,我专注于使用Andrew Rapp创建的更完整的XBee-API libraries for Java。它们涵盖了1系列和2系列无线电,提供了一整套API交互。 Building Wireless Sensor Networks page上提供了使用这些库的代码示例。