serial-port - 将 XBee 数据读入 Processing

标签 serial-port processing xbee

我最近构建了一个 Tweet A Watt ( http://www.ladyada.net/make/tweetawatt/ ) radio 源监视器,它使用 XBee 进行数据传输。我正在尝试将 Tweet A Watt 数据放入 Processing 以用于创建一些视觉能量反馈原型(prototype)。使用用于处理的 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 个不同的样本。这是我试图在 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)论坛中,但经过数十次查看后我还没有任何回复,所以我想我应该把网撒得更广一些。

最佳答案

在我的书中,我专注于使用更完整的 XBee-API libraries for Java由安德鲁拉普创建。它们涵盖系列 1 和系列 2 radio ,提供全套 API 交互。 Building Wireless Sensor Networks page 上提供了使用这些库的代码示例.

关于serial-port - 将 XBee 数据读入 Processing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3017546/

相关文章:

c# - 用 C# 向串口发送写入命令?

Android - View.OnLayoutChangeListener 和 ViewTreeObserver.OnGlobalLayoutListener 的区别

javascript - JavaScript 或 Processing 中的矢量路径混合

linux - 使用 Exagear 桌面将 XCTU 安装到 Raspbian Jessie

zigbee - Ti CC2530 和 Digi ConnectPortX4 之间的通信

javascript - 具有多个设备的 Windows 上的 Node 串行端口挂起

jar - 将 Java JAR 文件移动到新机器

javascript - 有没有办法让代码循环的一部分而不是另一部分? (第5页)

Arduino 的 iOS 虚拟按钮

c# - 如何在关闭前等待串口清空