Hardware Reference
In-Depth Information
Now that you can
read the XBee
messages, add code to make a web
server. The rest of this sketch is much
like the RGB server from Chapter 4.
Serve It
// include SPI and Ethernet libraries:
#include <SPI.h>
#include <Ethernet.h>
8 Change these to match your own device
and network.
// initialize a server instance:
Server server(80);
First, include the Ethernet and SPI
libraries, and add a few more global
variables at the top of your sketch to
manage them. New lines are shown in
blue.
// Ethernet MAC address and IP address for server:
byte mac[] = {
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01 };
IPAddress ip(192,168,1,20);
String requestLine = ""; // incoming HTTP request from client
const int packetLength = 22; // XBee data messages will be 22 bytes long
int dataPacket[packetLength]; // array to hold the XBee data
int byteCounter = 0; // counter for bytes received from the XBee
// variables for calculating a sensor average:
const int averageInterval = 10 * 1000; // time between averages, in seconds
long sensorTotal = 0; // used for averaging sensor values
float averageVoltage = 0.0; // the average value, in volts
long lastAverageTime = 0; // when you last took an average
int readingCount = 0; // readings since last average
Add some code to the main loop to
listen for new clients and deal with
them, and to average the values from the
incoming XBee packets every 10 seconds.
New lines are shown in blue.
8
void loop() {
// listen for incoming serial data:
if (Serial.available() > 0) {
listenToSerial();
}
// listen for incoming clients:
Client client = server.available();
if (client) {
listenToClient(client);
}
// calculate an average of readings after <averageInterval> seconds
long now = millis();
if (now - lastAverageTime > averageInterval) {
averageVoltage = getAverageReading();
Serial.println(averageVoltage);
}
}
 
Search WWH ::




Custom Search