Hardware Reference
In-Depth Information
Who's Out There? Broadcast Messages
The first advantage to sessionless protocols like UDP and 802.15.4 is that they allow for
broadcasting messages to everyone on the network at once. Although you don't want
to do this all the time—because you'd flood the network with messages that not every
device needs—it's a handy ability to have when you want to find out who else is on your
network. You simply send out a broadcast message asking “Who's there?” and wait for
replies.
Querying for Other Devices Using
UDP
Arduino's Ethernet library includes the ability to send and
receive UDP packets, so you can use it to write a simple
sketch that listens for broadcast messages and responds
to them. It's useful when you want to make a large number
of networked devices all respond at once. You can use Pro-
cessing to send your broadcast query.
by Stephane Cousot available from the Processing.
org's libraries page at http://processing.org/reference/
libraries/#data_protocols . To use it, download it and make
a new directory called udp in the libraries subdirectory of
your Processing sketch directory. Then unzip the contents
of the download and drop them in the directory you
created. After that, restart Processing and you're ready to
use the UDP library.
Any Arduino with an Ethernet shield connected to your
network will do for this exercise. You don't need any extra
hardware other than the shield.
There's no way to send UDP messages using the Process-
ing Network library, but there's a good free UDP library
This Processing sketch
sends out a broadcast
UDP message on port 43770. This is an
arbitrary number that's high enough
that it's probably not used by other
common applications.
Try It
/*
UDP broadcast query sender/receiver
Context: Processing
*/
// import the UDP library:
import hypermedia.net.*;
It doesn't matter what the IP address
or router of the machine running
this sketch is, because it uses the
special subnet broadcast address:
255.255.255.255.
UDP udp; // initialize the UDP object
void setup() {
udp = new UDP( this, 43770 ); // open a UDP port
udp.listen( true ); // listen for incoming messages
}
Unlike session-based messaging,
where you wrote and read bytes from a
stream like serial communication, UDP
messages are all sent with one discrete
message, udp.send() , which includes
the address and port for sending.
void draw()
{
}
void keyPressed() {
String ip = "255.255.255.255"; // the remote IP address
int port = 43770; // the destination port
udp.send("Hello!\n", ip, port ); // the message to send
}
»
 
Search WWH ::




Custom Search