Hardware Reference
In-Depth Information
The getAverageReading() is called
every 10 seconds from the main loop,
as well as every time a client makes
a request. It takes the readings from
a number of XBee messages and
averages them all, as shown at right.
8
float getAverageReading() {
int thisAverage = sensorTotal / readingCount;
// convert to a voltage:
float voltage = 3.3 * (thisAverage / 1024.0);
// reset the reading count and totals for next time:
readingCount = 0;
sensorTotal = 0;
// save the time you took this average:
lastAverageTime = millis();
// return the result:
return voltage;
}
That's the whole sketch. Upload it to
your Arduino, then open the Arduino's
address in a browser, as shown in
Figure 7-10.
Now when you're working in your shop,
your family or friends have two ways
to know whether the air is getting too
foul for you to breathe. They can check
the website or listen for the monkey to
clang his cymbals.
NOTE: This code doesn't actually translate
output voltage into concentration of gases.
To do that calculation, you'll need to read
the datasheet for the sensor you chose.
Figure 7-10
The results of the XBee gas server.
What Happens in the Subnet
Stays in the Subnet
You're probably thinking to yourself, "That's great, but
how come I'm not sending these messages over the
Internet via UDP? Isn't that the message-based protocol
that's parallel to the message-based protocol of the XBee
radios?" Yes, it is. However, it's wise to be careful with
the use of UDP outside your own subnet. If you were to
broadcast beyond your own subnet to the rest of the Net,
you'd flood the whole network with unnecessary traffic.
UDP and message-based protocols are used on the wider
Net but, in general, they are used in a directed manner
rather than as broadcast messages. For example, video
and audio transmissions on the Internet are generally
directed UDP streams. The client contacts the server first
using an HTTP request, or a similar TCP-based request.
The server tells the client to open a port on which to
recieve UDP messages, the client opens the port, and the
server starts a stream of UDP messages. That's a more
polite use of UDP on the wider Internet.
In the next project, you'll use UDP-directed messages from
one device to another, as well as XBee-directed messages.
X
 
Search WWH ::




Custom Search