Trouble porting Tweet A Watt software to Processing

by johnny.rodgers on Wed Jun 09, 2010 12:44 pm

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, I've made some headway, but have come up against an obstacle that I would appreciate any input on.

My sketch looks like this:
Code: Select all
/*
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:

Code: Select all
LOOP 10:37:59
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:1
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]

LOOP 10:38:3
API ID: 1
RSSI: -62
Total Samples: 19
[495, -1, -1, -1, 411, -1], [495, -1, -1, -1, 411, -1], [495, -1, -1, -1, 411, -1], [495, -1, -1, -1, 411, -1], [495, -1, -1, -1, 411, -1], [495, -1, -1, -1, 411, -1], [495, -1, -1, -1, 411, -1], [495, -1, -1, -1, 411, -1], [495, -1, -1, -1, 411, -1], [495, -1, -1, -1, 411, -1], [495, -1, -1, -1, 411, -1], [495, -1, -1, -1, 411, -1], [495, -1, -1, -1, 411, -1], [495, -1, -1, -1, 411, -1], [495, -1, -1, -1, 411, -1], [495, -1, -1, -1, 411, -1], [495, -1, -1, -1, 411, -1], [495, -1, -1, -1, 411, -1], [495, -1, -1, -1, 411, -1]

LOOP 10:38:5
API ID: 1
RSSI: -62
Total Samples: 19
[493, -1, -1, -1, 271, -1], [493, -1, -1, -1, 271, -1], [493, -1, -1, -1, 271, -1], [493, -1, -1, -1, 271, -1], [493, -1, -1, -1, 271, -1], [493, -1, -1, -1, 271, -1], [493, -1, -1, -1, 271, -1], [493, -1, -1, -1, 271, -1], [493, -1, -1, -1, 271, -1], [493, -1, -1, -1, 271, -1], [493, -1, -1, -1, 271, -1], [493, -1, -1, -1, 271, -1], [493, -1, -1, -1, 271, -1], [493, -1, -1, -1, 271, -1], [493, -1, -1, -1, 271, -1], [493, -1, -1, -1, 271, -1], [493, -1, -1, -1, 271, -1], [493, -1, -1, -1, 271, -1], [493, -1, -1, -1, 271, -1]


As you can see, the first sample is repeated 19 times. Running the wattcher.py script from the Tweet A Watt software 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:
Code: Select all
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.

If I've left out anything that would be helpful, please let me know. Thanks in advance for your help.
johnny.rodgers
 
Posts: 2
Joined: Wed Jun 09, 2010 12:26 pm

Re: Trouble porting Tweet A Watt software to Processing

by Volkemon on Sun Jun 13, 2010 8:15 pm

Hi Johnny!

Stumbled in here with a search for 'XBee library'.

Just found out about it, and it seems to fit what I need.

Let me explore those links you provided on the bottom, and I will post back with results.

EDIT: Check this out- may be of use-

http://code.google.com/p/xbee-arduino/
Livin' , learnin' , lovin' and laffin'. Thanks for the help!
Image
User avatar
Volkemon
 
Posts: 127
Joined: Mon May 10, 2010 7:24 pm
Location: Melbourne FL USA

Re: Trouble porting Tweet A Watt software to Processing

by johnny.rodgers on Mon Jun 14, 2010 7:29 pm

Thanks for taking a look Volkemon. I appreciate it! The Arduino XBee library looks promising. I will look into it and post back if it proves useful.
johnny.rodgers
 
Posts: 2
Joined: Wed Jun 09, 2010 12:26 pm