Commit 8669e7bc authored by O'Reilly Media, Inc.'s avatar O'Reilly Media, Inc.
Browse files

Initial commit

parents
## Example files for the title:
# Building Wireless Sensor Networks, by Robert Faludi
[![Building Wireless Sensor Networks, by Robert Faludi](http://akamaicovers.oreilly.com/images/9780596807733/cat.gif)](https://www.safaribooksonline.com/library/view/title/780596807757//)
The following applies to example files from material published by O’Reilly Media, Inc. Content from other publishers may include different rules of usage. Please refer to any additional usage rights explained in the actual example files or refer to the publisher’s website.
O'Reilly books are here to help you get your job done. In general, you may use the code in O'Reilly books in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from our books does not require permission. Answering a question by citing our books and quoting example code does not require permission. On the other hand, selling or distributing a CD-ROM of examples from O'Reilly books does require permission. Incorporating a significant amount of example code from our books into your product's documentation does require permission.
We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN.
If you think your use of code examples falls outside fair use or the permission given here, feel free to contact us at <permissions@oreilly.com>.
Please note that the examples are not production code and have not been carefully testing. They are provided "as-is" and come with no warranty of any kind.
/*
* *********XBee Internet Gateway Light Example********
* by Rob Faludi http://faludi.com
* rewritten for St. Ann's http://saintannsny.org
*/
#define NAME "XIG Light Example"
#define VERSION "1.10"
#define LED_PIN 13
int outputLight = 12;
int lightState;
void setup() {
pinMode(LED_PIN,OUTPUT);
pinMode(outputLight,OUTPUT);
blinkLED(LED_PIN,2,100);
blinkLED(outputLight,2,100);
Serial.begin(115200); // faster is better for XIG
delay(2000);
}
void loop() {
if (millis() % 1000 == 0) { // wait a second before sending the next request
// request the current light state, set with zig_light_form_example.php
Serial.println("<YOUR-SERVER-HERE>/lightSetting.txt");
}
if (Serial.available() > 0) { // if there's a byte waiting
lightState = Serial.read(); // read the ASCII numeral byte
// turn on the light if the response is 1, or off if the response is zero
if (lightState == '0' || lightState=='1') {
lightState=lightState-48; // transform ASCII into an integer
digitalWrite(outputLight, lightState);
}
}
}
////////////////// UTILITIES //////////////////
// this function blinks the an LED light as many times as requested, at the requested blinking rate
void blinkLED(byte targetPin, int numBlinks, int blinkRate) {
for (int i=0; i<numBlinks; i++) {
digitalWrite(targetPin, HIGH); // sets the LED on
delay(blinkRate); // waits for blinkRate milliseconds
digitalWrite(targetPin, LOW); // sets the LED off
delay(blinkRate);
}
}
/*
* *********XBee Internet Gateway Upload Example********
* by Rob Faludi http://faludi.com
*/
#define NAME "XIG Upload Example"
#define VERSION "1.00"
#define LED_PIN 13
int inputPin = 0;
void setup() {
pinMode(LED_PIN,OUTPUT);
blinkLED(LED_PIN,2,100);
Serial.begin(115200); // faster is better for XIG
delay(2000);
}
void loop() {
// read the analog pin
int value = analogRead(inputPin);
// upload the current value to the server, it will be stored in a dataFile.txt
Serial.print("<YOUR-SERVER-HERE>/xig_upload_example.php?value=");
Serial.println(value, DEC);
// wait a second between uploads
delay(1000);
blinkLED(LED_PIN,1,100);
}
////////////////// UTILITIES //////////////////
// this function blinks the an LED light as many times as requested, at the requested blinking rate
void blinkLED(byte targetPin, int numBlinks, int blinkRate) {
for (int i=0; i<numBlinks; i++) {
digitalWrite(targetPin, HIGH); // sets the LED on
delay(blinkRate); // waits for blinkRate milliseconds
digitalWrite(targetPin, LOW); // sets the LED off
delay(blinkRate);
}
}
File added
/*
* ********* 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;
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);
}
}
}
/*
* ********* 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
}
}
/*
* ********* 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 = 5;
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);
}
}
}
/*
* ********* 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);
}
}
/*
* *********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 (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 re-set 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;
}
// re-set 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 and 0x5 to turn the pin on or off
Serial.print(0x7E, BYTE); // start byte
Serial.print(0x0, BYTE); // high part of length (always zero)
Serial.print(0x10, BYTE); // low part of length (the number of bytes that follow, not including checksum)
Serial.print(0x17, BYTE); // 0x17 is a remote AT command
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); // 0xFF for broadcast
Serial.print(0xFF, BYTE); // 0xFF for broadcast
// 16 bit of recipient or 0xFFFE if unknown
Serial.print(0xFF, BYTE);
Serial.print(0xFE, BYTE);
Serial.print(0x02, BYTE); // 0x02 to apply changes immediately on remote
// 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)
}
/*
* *********ROMANTICLIGHTING 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);
}
}
/*
* Draws a set of switches for managing XBee Actuators
* by Rob Faludi http://faludi.com
*/
// used for communication via xbee api
import processing.serial.*;
// xbee api libraries available at http://code.google.com/p/xbee-api/
// Download the zip file, extract it, and copy the xbee-api jar file
// and the log4j.jar file (located in the lib folder) inside a "code"
// folder under this Processing sketch’s folder (save this sketch, then
// click the Sketch menu and choose Show Sketch Folder).
import com.rapplogic.xbee.api.XBee;
import com.rapplogic.xbee.api.XBeeAddress64;
import com.rapplogic.xbee.api.XBeeException;
import com.rapplogic.xbee.api.XBeeTimeoutException;
import com.rapplogic.xbee.api.zigbee.ZNetRemoteAtRequest;
import com.rapplogic.xbee.api.zigbee.ZNetRemoteAtResponse;
import com.rapplogic.xbee.api.ApiId;
import com.rapplogic.xbee.api.AtCommand;
import com.rapplogic.xbee.api.AtCommandResponse;
import com.rapplogic.xbee.api.XBeeResponse;
import com.rapplogic.xbee.api.zigbee.NodeDiscover;
String version = "1.02";
// *** REPLACE WITH THE SERIAL PORT (COM PORT) FOR YOUR LOCAL XBEE ***
String mySerialPort = "/dev/tty.usbserial-A1000iMG";
// create and initialize a new xbee object
XBee xbee = new XBee();
int error=0;
// make an array list of thermometer objects for display
ArrayList switches = new ArrayList();
ArrayList nodes = new ArrayList();
// create a font for display
PFont font;
float lastNodeDiscovery;
void setup() {
size(800, 230); // screen size
smooth(); // anti-aliasing for graphic display
// You’ll need to generate a font before you can run this sketch.
// Click the Tools menu and choose Create Font. Click Sans Serif,
// choose a size of 10, and click OK.
font = loadFont("SansSerif-10.vlw");
textFont(font);
// The log4j.properties file is required by the xbee api library, and
// needs to be in your data folder. You can find this file in the xbee
// api library you downloaded earlier
PropertyConfigurator.configure(dataPath("")+"log4j.properties");
// Print a list in case the selected serial port doesn't work out
println("Available serial ports:");
println(Serial.list());
try {
// opens your serial port defined above, at 9600 baud
xbee.open(mySerialPort, 9600);
}
catch (XBeeException e) {
println("");
println(" ** Error opening XBee port: " + e + " **");
println("");
println("Is your XBee plugged in to your computer?");
println("Did you set your COM port in the code near line 30?");
error=1;
}
// run a node discovery to find all the radios currently on the network
// (this assumes that all the network radios are Actuator nodes)
nodeDiscovery();
lastNodeDiscovery = millis(); // note the time when the discovery was made
}
// draw loop executes continuously
void draw() {
background(255); // draw a white background
// report any serial port problems in the main window
if (error == 1) {
fill(0);
text("** Error opening XBee port: **\n"+
"Is your XBee plugged in to your computer?\n" +
"Did you set your COM port in the code near line 27?",
width/3, height/2);
}
// create a switch object for each node that doesn't have one yet
// ...and get current state of every new node
for (int j=0; j < nodes.size(); j++) {
XBeeAddress64 address64 = ((NodeDiscover) nodes.get(j)).getNodeAddress64();
int i = 0;
boolean foundIt = false;
for (i=0; i < switches.size(); i++) {
if ( ((Switch) switches.get(i)).addr64.equals(address64) ) {
foundIt = true;
break;
}
}
// if the switch does not yet exist, create a new one
// stop if there's more than can fit on the screen
if (foundIt == false && switches.size() < 5) {
switches.add(new Switch(address64, (switches.size())));
((Switch) switches.get(i)).getState();
}
}