Category: SensitiveBuilding
API Mode Zigbee Network + sensor
Xbee API Mode Network
/*
* *********ROMANTIC LIGHTING SENSOR ********
* detects whether your lighting is
* setting the right mood
* USES PREVIOUSLY PAIRED XBEE ZB RADIOS
* by Rob Faludi http://faludi.com */
/*
*** CONFIGURATION ***
SENDER: (REMOTE SENSOR RADIO)
ATID3456 (PAN ID)
ATDH -> set to SH of partner radio
ATDL -> set to SL of partner radio
ATJV1 -> rejoin with coordinator on startup ATD02 pin 0 in analog in mode
ATIR64 sample rate 100 millisecs (hex 64)
* THE LOCAL RADIO _MUST_ BE IN API MODE *
RECEIVER: (LOCAL RADIO)
ATID3456 (PAN ID)
ATDH -> set to SH of partner radio ATDL -> set to SL of partner radio
*/
#define VERSION "1.02"
int LED = 11;
int debugLED = 13; int analogValue = 0;
void setup() {
pinMode(LED,OUTPUT); pinMode(debugLED,OUTPUT); Serial.begin(9600);
}
void loop() {
// make sure everything we need is in the buffer
if (Serial.available() >= 21) {
// look for the start byte if (Serial.read() == 0x7E) {
//blink debug LED to indicate when data is received digitalWrite(debugLED, HIGH);
delay(10);
digitalWrite(debugLED, LOW);
// read the variables that we're not using out of the buffer for (int i = 0; i<18; i++) {
byte discard = Serial.read(); }
int analogHigh = Serial.read();
int analogLow = Serial.read();
analogValue = analogLow + (analogHigh * 256);
} }
/*
* The values in this section will probably
* need to be adjusted according to your
* photoresistor, ambient lighting, and tastes.
* For example, if you find that the darkness
* threshold is too dim, change the 350 value * to a larger number.
*/
// darkness is too creepy for romance
if (analogValue > 0 && analogValue <= 350) {
digitalWrite(LED, LOW); }

// medium light is the perfect mood for romance
if (analogValue > 350 && analogValue <= 750) { digitalWrite(LED, HIGH);
}
// bright light kills the romantic mood
if (analogValue > 750 && analogValue <= 1023) { digitalWrite(LED, LOW);
} }
/*
* *********ROMANTIC LIGHTING SENSOR WITH FEEDBACK********
* detects whether your lighting is
* setting the right mood and shows
* you the results on the sensor module
* USES PREVIOUSLY PAIRED XBEE ZB RADIOS
* by Rob Faludi http://faludi.com */
/*
*** CONFIGURATION ***
SENDER: (REMOTE SENSOR RADIO)
ATID3456 (PAN ID)
ATDH -> set to SH of partner radio
ATDL -> set to SL of partner radio
ATJV1 -> rejoin with coordinator on startup
ATD02 pin 0 in analog in mode with a photo resistor
106 | Chapter 4: Ins and Outs
(don't forget the voltage divider circuit--resistor
to ground is good)
ATD14 pin 1 in digital output (default low) mode with an
LED from that pin to ground
ATIR64 sample rate 100 millisecs (hex 64)
* THE LOCAL RADIO _MUST_ BE IN API MODE *
RECEIVER: (LOCAL RADIO) ATID3456 (PAN ID)
ATDH -> set to SH of partner radio ATDL -> set to SL of partner radio
*/
#define VERSION "1.02"
int LED = 11;
int debugLED = 13;
int analogValue = 0;
int remoteIndicator = false; // keeps track of the desired remote
// on/off state
int lastRemoteIndicator = false; // record of prior remote state
unsigned long lastSent = 0; // records last time the remote was // reset to keep it in sync
void setup() { pinMode(LED,OUTPUT); pinMode(debugLED,OUTPUT); Serial.begin(9600);
}
void loop() {
// make sure everything we need is in the buffer
if (Serial.available() >= 23) {
// look for the start byte
if (Serial.read() == 0x7E) {
//blink debug LED to indicate when data is received digitalWrite(debugLED, HIGH);
delay(10);
digitalWrite(debugLED, LOW);
// read the variables that we're not using out of the buffer // (includes two more for the digital pin report)
for (int i = 0; i<20; i++) {
byte discard = Serial.read(); }
int analogHigh = Serial.read();
int analogLow = Serial.read();
analogValue = analogLow + (analogHigh * 256);
} }
/*
* The values in this section will probably
* need to be adjusted according to your
* photoresistor, ambient lighting, and tastes.
* For example, if you find that the darkness
* threshold is too dim, change the 350 value
* to a larger number. */
// darkness is too creepy for romance
if (analogValue > 0 && analogValue <= 350) {
digitalWrite(LED, LOW);
remoteIndicator = false; }
// medium light is the perfect mood for romance
if (analogValue > 350 && analogValue <= 750) { digitalWrite(LED, HIGH);
remoteIndicator = true; }
// bright light kills the romantic mood
if (analogValue > 750 && analogValue <= 1023) {
digitalWrite(LED, LOW);
remoteIndicator = false; }
// set the indicator immediately when there's a state change
if (remoteIndicator != lastRemoteIndicator) {
if (remoteIndicator==false) setRemoteState(0x4); if (remoteIndicator==true) setRemoteState(0x5); lastRemoteIndicator = remoteIndicator;
}
// reset the indicator occasionally in case it's out of sync if (millis() - lastSent > 10000 ) {
if (remoteIndicator==false) setRemoteState(0x4); if (remoteIndicator==true) setRemoteState(0x5); lastSent = millis();
}
}
void setRemoteState(int value) { // pass either a 0x4 or 0x5 to turn the pin on/off
Serial.print(0x7E, BYTE);
Serial.print(0x0, BYTE); Serial.print(0x10, BYTE);
// start byte
// high part of length (always zero)
// low part of length (the number of bytes // that follow, not including checksum)
// 0x17 is a remote AT command
Serial.print(0x17, BYTE);
Serial.print(0x0, BYTE); // frame id set to zero for no reply // ID of recipient, or use 0xFFFF for broadcast Serial.print(00, BYTE);
Serial.print(00, BYTE);
Serial.print(00, BYTE);
Serial.print(00, BYTE);
Serial.print(00, BYTE);
Serial.print(00, BYTE); Serial.print(0xFF, BYTE); Serial.print(0xFF, BYTE);
// 16 bit of recipient or Serial.print(0xFF, BYTE); Serial.print(0xFE, BYTE); Serial.print(0x02, BYTE);
// command name in ASCII characters Serial.print('D', BYTE); Serial.print('1', BYTE);
// command data in as many bytes as needed
Serial.print(value, BYTE);
// checksum is all bytes after length bytes
long sum = 0x17 + 0xFF + 0xFF + 0xFF + 0xFE + 0x02 + 'D' + '1' + value; Serial.print( 0xFF - ( sum & 0xFF) , BYTE ); // calculate the proper checksum delay(10); // safety pause to avoid overwhelming the
// serial port (if this function is not implemented properly)
}
Xbee Zigbee Network . Doorbell test
Using 2 Xbee Series 2 to run a simple network.
Network 1
Code — Sender:
/*
* ********* Doorbell Basic BUTTON ********
* requires pre-paired XBee Radios
* and the BELL program on the receiving end
* by Rob Faludi http://faludi.com
*/
#define VERSION "1.00a0"
int BUTTON = 2;
void setup() {
pinMode(BUTTON, INPUT);
Serial.begin(9600);
}
void loop() {
// send a capital D over the serial port if the button is pressed
if (digitalRead(BUTTON) == HIGH) {
Serial.print('D');
delay(10); // prevents overwhelming the serial port
}
}
Code — Receiver:
/*
* ********* Doorbell Basic BELL ********
* requires pre-paired XBee Radios
* and the BUTTON program on the receiving end
* by Rob Faludi http://faludi.com
*/
#define VERSION "1.00a0"
int BELL = 5; //WAS 4
void setup() {
pinMode(BELL, OUTPUT);
Serial.begin(9600);
}
void loop() {
// look for a capital D over the serial port and ring the bell if found
if (Serial.available() > 0) {
if (Serial.read() == 'D'){
//ring the bell briefly
digitalWrite(BELL, HIGH);
delay(10);
digitalWrite(BELL, LOW);
}
}
}
Network 2 with feedback loop
Basic Doorbell with Feedback
The setup remains the same, but now we are using the LED that is in pin 11 on the Sender (button). The code is modified in that the receiving Arduino not only rings the bell, but also sends a ‘K’ back to the sender, at which point it lights the LED.
Code — Sender:
/*
* ********* Doorbell Feedback BUTTON ********
* requires pre-paired XBee Radios
* and the BELL program on the receiving end
* by Rob Faludi http://faludi.com
*/
#define VERSION “1.00a0″
int BUTTON = 2;
int LED = 11;
void setup() {
pinMode(BUTTON, INPUT);
pinMode(LED, OUTPUT);
Serial.begin(9600);
}
void loop() {
// send a capital D over the serial port if the button is pressed
if (digitalRead(BUTTON) == HIGH) {
Serial.print(‘D’);
delay(10); // prevents overwhelming the serial port
}
// if a capital K is received back, light the feedback LED
if (Serial.available() > 0 ) {
if (Serial.read() == ‘K’) {
digitalWrite(LED, HIGH);
}
}
// when the button is released, turn off the LED
if (digitalRead(BUTTON) == LOW) {
digitalWrite(LED, LOW);
}
}
Code — Receiver:
/*
* ********* Doorbell Feedback BELL ********
* requires pre-paired XBee Radios
* and the BUTTON program on the receiving end
* by Rob Faludi http://faludi.com
*/
#define VERSION "1.00a0"
int BELL = 4;
void setup() {
pinMode(BELL, OUTPUT);
Serial.begin(9600);
}
void loop() {
// look for a capital D over the serial port and ring the bell if found
if (Serial.available() > 0) {
if (Serial.read() == 'D'){
//send feedback that the message was received
Serial.print('K');
//ring the bell briefly
digitalWrite(BELL, HIGH);
delay(10);
digitalWrite(BELL, LOW);
}
}
}













