将 XBee 数据读入处理中
我最近构建了一个 Tweet A Watt (http://www.ladyada.net/make/tweetawatt /)无线电源监控器,使用XBee进行数据传输。我正在尝试将推文 A Watt 数据放入处理中,以用于创建一些视觉能量反馈原型。使用 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(""); }
}
}
这一切都按预期工作。 xBeeEvent 每 2 秒调用一次,并输出 API ID、RSSI 和总样本 (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 个不同的样本。这就是我在处理中试图达到的状态。
在 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)和处理(http://processing.org/discourse/yabb2/YaBB.pl?num=1276111549) 论坛,但看了几十次后我还没有收到任何回复,所以我想我应该投网宽一点。
I've recently built a Tweet A Watt (http://www.ladyada.net/make/tweetawatt/) wireless power monitor, which uses XBee for data transfer. I'm attempting to get the Tweet A Watt data into Processing for use in creating some visual energy feedback prototypes. Using the XBee API Library for Processing (http://www.faludi.com/code/xbee-api-library-for-processing/), I've made some headway, but have come up against an obstacle that I would appreciate any input on.
My Processing sketch looks like this:
/*
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(""); }
}
}
This all works as expected. The xBeeEvent is called every 2s, and outputs the correct values for the API ID, RSSI, and Total Samples (19). However, when outputting the contents of the analog readings, I seem to be getting the first sample repeated 19 times. See this sample output:
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]
As you can see, the first sample is repeated 19 times. Running the original Python script from the Tweet A Watt software (wattcher.py) outputs a similar reading of the XBee packet, but with 19 distinct samples. This is the state I'm trying to get to in Processing.
In the XBee API Library, the getAnalog() and getAnalog(n) functions are defined as follows:
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.
I'm using getAnalog(int n) in the for loop. Is the issue that I am only getting one "frame" of data, in the call to XBeeDataFrame data = xbee.getXBeeReading(); ?
I've also tried reading the Serial packet directly without using the XBee API Library (with reference to (http://www.tigoe.net/pcomp/code/category/Processing/8), (http://processing.org/reference/libraries/serial/Serial.html), and (http://ssdl.stanford.edu/ssdl/images/stories/AA236/0708A/Lab/Rover/Parts/xbeeproproductmanual.pdf), but my lack of experience in this area makes this a bit of a non-starter.
If anyone familiar with the XBee packet, the XBee API Library, or reading Serial data in Processing can assist, it would be greatly appreciated. I expect that the data is there, I'm just not accessing it correctly. I realize this is quite a specific question, and I've posted it in the Adafruit (makers of the Tweet A Watt kit - http://forums.adafruit.com/viewtopic.php?f=40&t=16067&sid=4e34727fa59b7c7d589564d2d6b85e46) and Processing (http://processing.org/discourse/yabb2/YaBB.pl?num=1276111549) forums, but after dozens of views I haven't had any replies, so I figured I'd cast the net a little wider.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在我的书中,我重点介绍了使用更完整的适用于 Java 的 XBee-API 库由安德鲁·拉普创建。它们涵盖系列 1 和系列 2 无线电,提供全套 API 交互。 构建无线传感器网络页面上提供了使用这些库的代码示例。
In my book I’ve focused on using the more complete XBee-API libraries for Java created by Andrew Rapp. They cover both the Series 1 and Series 2 radios, providing a full suite of API interactions. Code examples that use these libraries are available on the Building Wireless Sensor Networks page.