The purpose of my project is to have remote control of an electrical outlet. I am using an Arduino 101, several Digi Xbee Zigbee radios, and SparkFun Beefcake relays. The design is a modified version of a project from "Building Wireless Sensor Networks". Please see attached schematic for rough circuit details. The schematic shows one router and relay, my goal is to have central control over several router/relay units.
If I send a remote AT command using the broadcast settings (FFFF & FFFE), I am able to turn on and off the digital pin connected to the relay every time I press the on or off buttons. If I change the program and try to use the specific addresses of the router radio (64-bit and 16-bit) I am able to change the state of the digital pin (and the relay) maybe 1 out of every 50 tries.
I think the router is assigned a new 16-bit network address each time it is powered up and joins the network (JV is set to 1 so router attempts to join network on startup) which I think could be part of the problem.
In an attempt to troubleshoot I wrote a quick program to request network addresses (AT command ND) but this does not work with any reliability either. The router sends a response to the coordinator sporadically, maybe once every 20 attempts.
Any ideas what I could do differently?
NETWORK ADDRESS PROGRAM
void setup() {
Serial.begin(9600);
Serial1.begin(9600);
}
void loop() {
if (Serial1.available()>=13){
Serial.println("");
while (Serial1.available()>0){
byte next_byte = Serial1.read();
Serial.print(next_byte,HEX);
}
Serial.println("");
}
Node_Discovery();
delay(10000);
}
void Node_Discovery(){
Serial.println("Node Discovery Initiated");
Serial1.write(0x7E); // Start byte
Serial1.write((byte)0x0); // High part of length
Serial1.write(0x10); // Low part of length
Serial1.write(0x17); //API for remote AT command
Serial1.write(0x01); // Frame ID set to 1 for reply
//ID of recepient, or 000000000000ffff for broadcast
Serial1.write((byte)0x0);
Serial1.write((byte)0x0);
Serial1.write((byte)0x0);
Serial1.write((byte)0x0);
Serial1.write((byte)0x0);
Serial1.write((byte)0x0);
Serial1.write(0xFF);
Serial1.write(0xFF);
//16-bit recepient, or FFFE for broadcast
Serial1.write(0xFF);
Serial1.write(0xFE);
Serial1.write(0x02);
Serial1.write('N');
Serial1.write('D');
Serial1.write((byte)0x0);
long sum1 = 0x17 + 0x01 + 0xFF + 0xFF + 0xFF + 0xFE + 0x02 + 'N' + 'D';
Serial1.write(0xFF-(sum1 & 0xFF)); //checksum
}
BROADCAST LIGHT SWITCH PROGRAM
int BUTTON = 2;
int OFFBUTTON = 3;
int LED = 13;
int SwitchState = 0;
int OnButtonState = 0;
int OffButtonState = 0;
void setup() {
pinMode(BUTTON,INPUT_PULLUP);
pinMode(OFFBUTTON,INPUT_PULLUP);
pinMode(LED,OUTPUT);
Serial.begin(9600);
Serial1.begin(9600);
}
void loop() {
//Serial.println("System On");
OnButtonState = digitalRead(BUTTON);
OffButtonState = digitalRead(OFFBUTTON);
if (OnButtonState==0 && SwitchState==0)
{
//if (SwitchState==0){
Serial.println("ON");
setRemoteState(0x5);
digitalWrite(LED,HIGH);
SwitchState=1;
delay(1000);
//}
}
if (OffButtonState==0 && SwitchState==1)
{
Serial.println("OFF");
setRemoteState(0x4);
digitalWrite(LED,LOW);
SwitchState=0;
delay(1000);
}
delay(50);
//Serial.print("On Button ");
//Serial.println(OnButtonState);
//Serial.print("Off Button ");
//Serial.println(OffButtonState);
}
void setRemoteState(char value){
Serial1.write(0x7E); // Start byte
Serial1.write((byte)0x0); // High part of length
Serial1.write(0x10); // Low part of length
Serial1.write(0x17); //API for remote AT command
Serial1.write((byte)0x0); // Frame ID set to zero for no reply
//ID of recepient, or 000000000000ffff for broadcast
Serial1.write((byte)0x0);
Serial1.write((byte)0x0);
Serial1.write((byte)0x0);
Serial1.write((byte)0x0);
Serial1.write((byte)0x0);
Serial1.write((byte)0x0);
Serial1.write(0xFF);
Serial1.write(0xFF);
//16-bit recepient, or FFFE for broadcast
Serial1.write(0x80);
Serial1.write(0x0B);
Serial1.write(0x02);
//Command name in ASCII characters
Serial1.write('D');
Serial1.write('3');
Serial1.write(value);
long sum = 0x17 + 0xFF + 0xFF + 0x80 + + 0x0B + 0x02 + 'D' + '3' + value;
Serial1.write(0xFF-(sum & 0xFF)); //checksum
//
}
my_schematic.pdf (315 KB)