Hardware Reference
In-Depth Information
The Arduino sketch to
read and send directed
UDP datagrams is pretty simple.
First, you need to include the relevant
libraries and set up some global
variables as usual. Notice that you're
setting up a specific remote IP address
and port—it should be the computer's
address on which the Processing
sketch will run.
Send It
/*
XBee to UDP
Context: Arduino
*/
#include <SPI.h>
#include <Ethernet.h>
#include <Udp.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
IPAddress myIp( 192,168,1,20 );
IPAddress yourIp( 192,168,1,21 );
8 You'll need to change
these numbers.
unsigned int myPort = 43770; // local port to listen on
unsigned int yourPort = 43770; // remote port to send to
// A UDP instance to send and receive packets over UDP
UDP udp;
setup() starts the Ethernet con-
nection, initializes serial communi-
cations, opens a UDP port, and starts
an initial UDP packet for sending.
8
void setup() {
// start the serial library:
Serial.begin(9600);
// start the Ethernet connection:
Ethernet.begin(mac, myIp);
// start UDP:
udp.begin(myPort);
// give the Ethernet shield a second to initialize:
delay(1000);
// set up a packet to send:
udp.beginPacket(yourIp, yourPort);
}
loop() just listens for serial input When
it receives a byte of value 0x7E—
indicating the beginning of a new
XBee message—it sends the previous
datagram using endPacket() , and
begins a new packet. Finally, it adds
any new byte to the current packet.
That's it!
void loop() {
if (Serial.available()) {
int serialByte = Serial.read();
// if you get a 0x7E,
// send the packet and begin a new one:
if (serialByte == 0x7E) {
udp.endPacket();
// set up a packet to send:
udp.beginPacket(yourIp, yourPort);
}
// send the byte:
udp.write(serialByte);
}
}
 
Search WWH ::




Custom Search