Hardware Reference
In-Depth Information
Graphing the Results
Now that all the hardware is ready, it's time to write a
Processing sketch to graph the data. This sketch will
receive UDP packets from the Arduino, parse the XBee
packets contained therein, and graph the results. You'll
notice that the parsing routine looks very similar to the
parsing routine you wrote for the Arduino in the previous
project. It's useful to compare them to see how the same
algorithm is implemented slightly differently in the two
programming languages.
X
First, you need to import the UDP
library, initialize it, and write a method
to listen for incoming datagrams.
8
/* XBee Packet Reader and Graphing Program
Reads a packet from an XBee radio via UDP and parses it.
Graphs the results over time.
Context: Processing
*/
This program will print out strings of
numbers that look a lot like the initial
ones from the Arduino sketch in the
gas sensor project. That's because the
datagrams the program is receiving are
the same protocol—the XBee protocol
for sending analog readings.
import hypermedia.net.*;
import processing.serial.*;
UDP udp; // define the UDP object
int queryPort = 43770; // the port number for the device query
void setup() {
// create a new connection to listen for
// UDP datagrams on query port:
udp = new UDP(this, queryPort);
// listen for incoming packets:
udp.listen( true );
}
void draw() {
// nothing happens here.
}
/*
listen for UDP responses
*/
void receive( byte[] data, String ip, int port ) {
int[] inString = int(data); // incoming data converted to string
print(inString);
println();
}
 
Search WWH ::




Custom Search