Poetic Operations: Online Companion

Local Autonomy Networks / Autonets - Xbee Code Examples

These two open source Xbee example files were used in the development and testing of the Autonets garments. They are included here for reference for future development. 
 

xbee_button.ino

#include <softwareserial.h>
int localLedPin = 13; // on board LED is connected to digital pin 13
int ledPin = 9; // LED is connected to digital pin 9
int switchPin = 10; // switch connected to digital pin 10
int switchValue; // a variable to keep track of when switch is pressed
int sentNo;
// create a software serial port for the XBee
//rx, tx
SoftwareSerial mySerial(5, 6);
void setup()
{
pinMode(ledPin, OUTPUT); // sets the ledPin to be an output
pinMode(localLedPin, OUTPUT); // sets the ledPin to be an output
pinMode(switchPin, INPUT); // sets the switchPin to be an input
digitalWrite(switchPin, HIGH); // sets the default (unpressed) state of switchPin to HIGH
mySerial.begin(9600);
sentNo = 0;
}
void loop() // run over and over again
{
String text;
unsigned long startTime = millis();
// remove anything weird from the buffer
mySerial.flush();
switchValue = digitalRead(switchPin); // check to see if the switch is pressed
// check xbee to see if the switch is pressed
// parse the incoming characters into a local String variable
char newChar;
int timeout = 4000;
while (millis()-startTime < timeout) {
if (mySerial.available()) {
newChar = (char)mySerial.read();
if (newChar == '\r' || newChar == '\n') {
break;
}
else {
text.concat(newChar);
}
}
}
if(text == "Y"){
digitalWrite(ledPin, HIGH); // turn the LED on
}
else {
digitalWrite(ledPin, LOW); // turn the LED off
}
if (switchValue == LOW) { // if the switch is pressed then,
mySerial.write("Y\n");
digitalWrite(localLedPin, HIGH); // turn the local LED on
//sentNo = 0;
}
else { // otherwise,
//if(sentNo == 0){
mySerial.write("N\n");
// sentNo = 1;
digitalWrite(localLedPin, LOW); // turn the local LED off
// }
}
}


Series2_Rx.ino

/**
* Copyright (c) 2009 Andrew Rapp. All rights reserved.
*
* This file is part of XBee-Arduino.
*
* XBee-Arduino is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* XBee-Arduino is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBee-Arduino. If not, see
http://www.gnu.org/licenses/

.
*/
#include <xbee.h>
#include <softwareserial.h>
/*
This example is for Series 2 XBee
Receives a ZB RX packet and sets a PWM value based on packet data.
Error led is flashed if an unexpected packet is received
*/
XBee xbee = XBee();
XBeeResponse response = XBeeResponse();
// create reusable response objects for responses we expect to handle
ZBRxResponse rx = ZBRxResponse();
ModemStatusResponse msr = ModemStatusResponse();
int statusLed = 13;
int errorLed = 13;
int dataLed = 13;
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(dataLed, OUTPUT);
// start serial
xbee.begin(9600);
flashLed(statusLed, 3, 50);
}
// continuously reads packets, looking for ZB Receive or Modem Status
void loop() {
xbee.readPacket();
if (xbee.getResponse().isAvailable()) {
// got something
if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE) {
// got a zb rx packet
// now fill our zb rx class
xbee.getResponse().getZBRxResponse(rx);
if (rx.getOption() == ZB_PACKET_ACKNOWLEDGED) {
// the sender got an ACK
flashLed(statusLed, 10, 10);
} else {
// we got it (obviously) but sender didn't get an ACK
flashLed(errorLed, 2, 20);
}
// set dataLed PWM to value of the first byte in the data
analogWrite(dataLed, rx.getData(0));
} else if (xbee.getResponse().getApiId() == MODEM_STATUS_RESPONSE) {
xbee.getResponse().getModemStatusResponse(msr);
// the local XBee sends this response on certain events, like association/dissociation
if (msr.getStatus() == ASSOCIATED) {
// yay this is great. flash led
flashLed(statusLed, 10, 10);
} else if (msr.getStatus() == DISASSOCIATED) {
// this is awful.. flash led to show our discontent
flashLed(errorLed, 10, 10);
} else {
// another status
flashLed(statusLed, 5, 10);
}
} else {
// not something we were expecting
flashLed(errorLed, 1, 25);
}
} else if (xbee.getResponse().isError()) {
//nss.print("Error reading packet. Error code: ");
//nss.println(xbee.getResponse().getErrorCode());
}
}