Here is my entire code:
Transmitter:
#include <XBee.h>
XBee xbee = XBee();
unsigned long start = millis();
uint8_t payload[] = { 0, 0, 0, 0, 0, 0, 0 };
uint8_t onoff = 0;
// with Series 1 you can use either 16-bit or 64-bit addressing
// 16-bit addressing: Enter address of remote XBee, typically the coordinator
Tx16Request tx = Tx16Request(0xFFFF, payload, sizeof(payload));
TxStatusResponse txStatus = TxStatusResponse();
int statusLed = 6;
int errorLed = 13;
unsigned long time = 0;
unsigned long currenttime = 0;
unsigned long outtime = 0;
int toggle = 7;
const int red = 13; // the pin that the LED is attached to
int incomingByte; // a variable to read incoming serial data into
float test;
void flashLed(int pin, int times, int wait) {
for (int i = 0; i < times; i++) {
digitalWrite(pin, HIGH);
delay(wait);
digitalWrite(pin, LOW);
if (i + 1 < times) {
delay(wait);
}
}
}
void setup() {
pinMode(statusLed, OUTPUT);
pinMode(errorLed, OUTPUT);
pinMode(toggle, INPUT);
Serial.begin(9600);
xbee.setSerial(Serial);
}
void loop() {
// start transmitting after a startup delay. Note: this will rollover to 0 eventually so not best way to handle
if (millis() - start > 15000) {
// break down 10-bit reading into two bytes and place in payload
onoff = digitalRead(toggle);
time = millis();
outtime = 445565;
payload[0] = onoff;
payload[1] = (outtime >> 24) & 0x000000ff;
payload[2] = (outtime >> 16) & 0x000000ff;
payload[3] = (outtime >> 8) & 0x000000ff;
payload[4] = (outtime ) & 0x000000ff;
payload[5] = 0;
payload[6] = 0;
xbee.send(tx);
// flash TX indicator
flashLed(statusLed, 1, 100);
}
// after sending a tx request, we expect a status response
// wait up to 5 seconds for the status response
if (xbee.readPacket(5000)) {
// got a response!
// should be a znet tx status
if (xbee.getResponse().getApiId() == TX_STATUS_RESPONSE) {
xbee.getResponse().getZBTxStatusResponse(txStatus);
// get the delivery status, the fifth byte
if (txStatus.getStatus() == SUCCESS) {
// success. time to celebrate
flashLed(statusLed, 10, 50);
} else {
// the remote XBee did not receive our packet. is it powered on?
flashLed(errorLed, 3, 500);
}
}
} else if (xbee.getResponse().isError()) {
//nss.print("Error reading packet. Error code: ");
//nss.println(xbee.getResponse().getErrorCode());
// or flash error led
} else {
// local XBee did not provide a timely TX Status Response. Radio is not configured properly or connected
flashLed(errorLed, 2, 50);
delay(500);
flashLed(errorLed, 2, 50);
}
delay(1000);
}
Receiver code:
/*
This code is for the HUB box. It simply takes a switch input to determine
if it should send an "ON" signal or an "OFF" signal
OFF:
int onoff=0
float time=0.0
ON:
int onoff=1
float time=millis()/1000.;
*/
#include <SD.h>
#include <XBee.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,10,9,8,7);
XBee xbee = XBee();
XBeeResponse response = XBeeResponse();
Rx16Response rx16 = Rx16Response();
//uint8_t data[6];
const int chipSelect = 4;
uint8_t onoff = 0;
unsigned long time = 0;
unsigned long big[7];
int toggle = 7;
const int red = 13; // the pin that the LED is attached to
const int yellow = 6;
int incomingByte; // a variable to read incoming serial data into
float test;
void setup() {
// initialize serial communication:
Serial.begin(9600);
xbee.setSerial(Serial);
// initialize the LED pin as an output:
pinMode(red, OUTPUT);
pinMode(yellow, OUTPUT);
pinMode(toggle, INPUT);
File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile) {
dataFile.println("Time Voltage Current Power Xaccel Yaccel Zaccel Exceed?");
dataFile.close();
}
lcd.begin(16, 2);
lcd.print("Wireless test");
delay(1000);
lcd.clear();
}
void loop() {
xbee.readPacket(5000);// waits for a packet for up to 5000 ms
if (xbee.getResponse().isAvailable()) {
// got something
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Got Something");
delay(500);
if (xbee.getResponse().getApiId() == RX_16_RESPONSE) {
xbee.getResponse().getRx16Response(rx16);
onoff = rx16.getData(0);
big[1] = rx16.getData(1);
big[2] = rx16.getData(2);
big[3] = rx16.getData(3);
big[4] = rx16.getData(4);
time = big[4] + (big[3]<<8) + (big[2]<<16) + (big[1]<<24);
flashLed(yellow, 1, 50);
flashLed(red, 1, 50);
flashLed(yellow, 1, 50);
flashLed(red, 1, 50);
} else if (xbee.getResponse().isError()){
flashLed(yellow, 2, 100);
}
} else {
flashLed(red, 2, 100);
}
lcd.clear();
delay(500);
lcd.print(onoff);
delay(500);
lcd.clear();
delay(500);
lcd.print(rx16.getData(1));
delay(500);
lcd.clear();
delay(500);
lcd.print(rx16.getData(2));
delay(500);
lcd.clear();
delay(500);
lcd.print(rx16.getData(3));
delay(500);
lcd.clear();
delay(500);
lcd.print(rx16.getData(4));
delay(500);
lcd.clear();
delay(500);
lcd.print(time);
delay(500);
//Write data to SD card
File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile) {
dataFile.print(onoff, 1);
dataFile.print("\t");
dataFile.print(time, 6);
dataFile.println("\t");
dataFile.close();
}
delay(3000);
}
void flashLed(int pin, int times, int wait) {
for (int i = 0; i < times; i++) {
digitalWrite(pin, HIGH);
delay(wait);
digitalWrite(pin, LOW);
if (i + 1 < times) {
delay(wait);
}
}
}