Arduino Code for XBeez

You need 2 Arduinos, 2 Xbees (series 1), XBee Shields,
patience and two USB cables.
This code is for 16 Analog Inputs.

HOW TO GET STARTED:

1. UPGRADING THE FIRMWARE:
Upgrade the XBee Firmware by putting the XBee on their shield
and the shield on an Arduino.
Make sure to take off the ATMega chip first. You can take the ATmega chip off yourself,
it's not soldered to the board, but be careful not to bend the legs.
Connect the XBee Shield with Arduino like described here:

http://www.libelium.com/squidbee/index.php?title=Uploading_XBee_firmware

Then download this software to upgrade the firmware using X-CTU
(download from: http://www.digi.com/support/supporttype.jsp?sfid=0&pgid=12&fr=Y&tp=0)

2. CONFIGURING THE XBEE:
Disconnect the pins you connected for upgrading again.
Put the XBee on the normal shield. Put the shield on an Arduino board ( the ATMEGA chip should still be off the Arduino board as described above).
Put the headers from "XBee" to "USB".

3. Open a serial connection to the XBee by connecting the board with USB to your computer.
Open a Serial Port to send AT commands to the Arduino.
Write +++
Be careful to have the correct bauld rate (9600 or 19200)

4. For the sending XBEE write:

ATRE you delete all settings and go to the default ones
ATMY1234/r you give it the name 1234
ATDLFFFF destination is all XBees around (broadcasting)
ATDH0/r high word of destination address
ATID1111/r you set a common PANID for all XBees
WR/r you safe all settings

5. For receiving XBee write:

ATRE you delete all settings
ATMY5678/r you give it the name 5678
ATDLFFFF destination is all XBees around (broadcasting)
ATDH0/r high word of destination address
ATID1111/r you set a common PANID for all XBees
WR/r you safe all settings

6. Take the shield off the Arduino board and
put back the ATMEGA chip in the right position.
7. Put the XBee shield back on the Arduino.
8. Now you have to program the Arduinos.

First the SENDER arduino:

Put the header on "USB", select the serial port and the right board in the ARduino environment settings

Then add this code and compile it before uploading it to the board:

// serial out is on port 1
// serial in is on port 0

// a analog input
//int switchPin = 0;

/*
Analog input reader for connected domes project - 16 inputs
** pin 6 and 7 are empty!!! code to read these pins are commented out.
*/

int analogValue0 = 0; // variable to hold the analog value
int analogValue1 = 0; // variable to hold the analog value
int analogValue2 = 0; // variable to hold the analog value
int analogValue3 = 0; // variable to hold the analog value
int analogValue4 = 0; // variable to hold the analog value
int analogValue5 = 0; // variable to hold the analog value
//int analogValue6 = 0; // variable to hold the analog value
//int analogValue7 = 0; // variable to hold the analog value
int analogValue8 = 0; // variable to hold the analog value
int analogValue9 = 0; // variable to hold the analog value
int analogValue10 = 0; // variable to hold the analog value
int analogValue11= 0; // variable to hold the analog value
int analogValue12 = 0; // variable to hold the analog value
int analogValue13 = 0; // variable to hold the analog value
int analogValue14 = 0; // variable to hold the analog value
int analogValue15 = 0; // variable to hold the analog value

// a status light is on port 13
int ledPin = 13;

// a byte to send out data:
char thisByte = 0;

void setup () {
// set pins to input and output appropriately
pinMode(ledPin, OUTPUT);
pinMode(analogValue0, INPUT);
pinMode(analogValue1, INPUT);
pinMode(analogValue2, INPUT);
pinMode(analogValue3, INPUT);
pinMode(analogValue4, INPUT);
pinMode(analogValue5, INPUT);
// pinMode(analogValue6, INPUT);
// pinMode(analogValue7, INPUT);
pinMode(analogValue8, INPUT);
pinMode(analogValue9, INPUT);
pinMode(analogValue10, INPUT);
pinMode(analogValue11, INPUT);
pinMode(analogValue12, INPUT);
pinMode(analogValue13, INPUT);
pinMode(analogValue14, INPUT);
pinMode(analogValue15, INPUT);

// start up the serial connection with 9600-8-n-1-true (non-inverted):
Serial.begin(9600);

// blink the status LED
blinkLED(ledPin, 3);

// for some reason it seems to help to send an arbitrary character first
//then pause for the guard time before requesting command mode
Serial.print("X");
delay(1100);
// put the XBee in command mode
Serial.print("+++");
delay(1100);
// wait for a response from the XBee for 2000 ms, or start
// over with setup if no valid response comes

if (returnedOK() == 'T') {
// if an OK was received then continue
}
else {
setup(); // otherwise go back and try setup again
}

// set the PAN (personal area network) ID number
// this example uses 0x3330, but you'll want to choose your own
// unique hexadecimal number between 0x0 and 0xFFFE
// (note the comma at the end of the command which indicates that another command will follow)
Serial.print("ATMY5678,");
Serial.print("ID1111,");
// set the Destination High to 0x0
// to select 16 bit addressing mode. These addresses can
// be assigned and changed by sending commands from a microcontroller
Serial.print("DH0,");
// set the Destination Low (16 bit address)
// this example uses 0x0 for send and 0x1 for receive but you'll
// want to choose your own hexadecimal numbers between 0x0 and 0xFFFE
Serial.print("DLFFFF,");
// exit command mode (note that we use Serial.printLN here to issue a linefeed that completes the command sequence)
Serial.println("CN");

// the preceeding commands can also be sent on a single line like this, using a single AT command with commas:
// Serial.println("ATID3330,DH0,DL1,CN");

// the preceeding command line could also be sent as separate commands, by reissuing the AT command:
// Serial.println("ATID3330");
// Serial.println("ATDH0");
// Serial.println("ATDL1");
// Serial.println("ATCN");

// wait for a response from the XBee for 2000 ms, or start
// over with setup if no valid response comes

if (returnedOK() == 'T') {
// if an OK was received then continue
}
else {
setup(); // otherwise go back and try setup again
}

}

void loop () {
// read the switch:
//thisByte = analogRead(switchPin);
// convert it to a readable ASCII value, send it out the serial port:
//Serial.println(thisByte, DEC);

// read the analog input on pin 0~15:
analogValue0 = analogRead(0);
analogValue1 = analogRead(1);
analogValue2 = analogRead(2);
analogValue3 = analogRead(3);
analogValue4 = analogRead(4);
analogValue5 = analogRead(5);
//analogValue6 = analogRead(6);
//analogValue7 = analogRead(7);
analogValue8 = analogRead(8);
analogValue9 = analogRead(9);
analogValue10 = analogRead(10);
analogValue11 = analogRead(11);
analogValue12 = analogRead(12);
analogValue13 = analogRead(13);
analogValue14 = analogRead(14);
analogValue15 = analogRead(15);

// print it out in many formats:
Serial.println(analogValue0,DEC); // print as an ASCII-encoded decimal
Serial.print("A\t");
Serial.println(analogValue1,DEC);
Serial.print("B\t");
Serial.println(analogValue2,DEC);
Serial.print("C\t");
Serial.println(analogValue3,DEC);
Serial.print("D\t");
Serial.println(analogValue4,DEC);
Serial.print("E\t");
Serial.println(analogValue5,DEC);
Serial.print("F\t");
//Serial.print(analogValue6);
//Serial.print("\t");
//Serial.print(analogValue7);
//Serial.print("\t");
Serial.println(analogValue8,DEC);
Serial.print("G\t");
Serial.println(analogValue9,DEC);
Serial.print("H\t");
Serial.println(analogValue10,DEC);
Serial.print("I\t");
Serial.println(analogValue11,DEC);
Serial.print("J\t");
Serial.println(analogValue12,DEC);
Serial.print("K\t");
Serial.println(analogValue13,DEC);
Serial.print("L\t");
Serial.println(analogValue14,DEC);
Serial.print("M\t");
Serial.println(analogValue15,DEC);
Serial.println("N");
// delay 150 milliseconds before the next reading:
delay(150);

}

void blinkLED(int targetPin, int numBlinks) {
// this function blinks the status LED light as many times as requested
for (int i=0; i 1) {
// read three incoming bytes which should be "O", "K", and a linefeed:
for (int i=0; i<3; i++) {
incomingChar[i] = Serial.read();
}
if ( strstr(incomingChar, okString) != NULL ) { // check to see if the respose is "OK"
// if (incomingChar[0] == 'O' && incomingChar[1] == 'K') { // check to see if the first two characters are "OK"
result = 'T'; // return T if "OK" was the response
}
else {
result = 'F'; // otherwise return F
}
}
}
return result;
}

Change the headers to XBee and open the serial port to check if the sender is sending anything.

Now program the receiver Arduino (put headers on "USB")

CODE FOR ARDUINO WITH RECEIVING XBEE:

// serial out is on port 1
// serial in is on port 0

// an output light is on port 11
int outputPin = 11;

// a status light is on port 13
int ledPin = 13;

// a byte to receive data:
char inByte = 0;

void setup () {
// set pins to input and output appropriately
pinMode(ledPin, OUTPUT);
pinMode(outputPin, OUTPUT);

// start up the serial connection with 9600-8-n-1-true (non-inverted):
Serial.begin(9600);

// blink the status LED
blinkLED(ledPin, 3);

// for some reason it seems to help to send an arbitrary character first
//then pause for the guard time before requesting command mode
Serial.print("X");
delay(1100);
// put the XBee in command mode
Serial.print("+++");
delay(1100);
// wait for a response from the XBee for 2000 ms, or start
// over with setup if no valid response comes

if (returnedOK() == 'T') {
// if an OK was received then continue
}
else {
setup(); // otherwise go back and try setup again
}

Serial.print("ATID1111,");
Serial.print("MY1234,");
Serial.print("ATDLFFFF,");
Serial.print("ATDH0,");

// exit command mode (note that we use Serial.printLN here to issue alinefeed that completes the command sequence)
Serial.println("CN");

if (returnedOK() == 'T') {
// if an OK was received then continue
}
else {
setup(); // otherwise go back and try setup again
}

}

void loop () {
// get any incoming data:
if (Serial.available() > 1) {
// read a byte
inByte = Serial.read();
// light the LED if a 1 has been received
}
if (inByte == '1') {
digitalWrite(outputPin, HIGH);
}
// douse the LED if anything else was received
else {
digitalWrite(outputPin, LOW);
}
}

void blinkLED(int targetPin, int numBlinks) {
// this function blinks the status LED light as many times as requested
for (int i=0; i 0) {
// read three incoming bytes which should be "O", "K", and a linefeed:
for (int i=0; i<3; i++) {
incomingChar[i] = Serial.read();
Serial.println(inByte);
}
if ( strstr(incomingChar, okString) != NULL ) { // check to see ifthe respose is "OK"
// if (incomingChar[0] == 'O' && incomingChar[1] == 'K') { // checkto see if the first two characters are "OK"
result = 'T'; // return T if "OK" was the response
}
else {
result = 'F'; // otherwise return F
}
}
}
return result;
}

Now put both Arduinos headers on "Xbee" and take the data from Receiving XBee via USB cable to the internet or whereever you want it. For testing if you receive data, put an LED on pin 11 of the receiving Arduino