5.2 Lab: A Sunset Sensor

In this lab we will move one step forward and we will start to use the XBee in API mode instead of AT mode. This means some extra effort, as we will be building the data packets ourselves. All this work is typically done by libraries, but it is good to do it once to understand the underlying structure of the frames.

The project we will implement is extra cool: a sunset sensor. An XBee router continuously takes analog samples from one of its pins. It sends this information to the coordinator that is connected to an Arduino, that reads the data and lights up an LED. It lights up a yellow LED during the day, when there is a lot of light. A red LED is activated during sunset, when the amount of ambient light is reduced. Finally, a blue led is light up at night, when there is no ambient light.

Enjoy your sunset sensor and built more ambitious projects to share with us!



Code:

 // This code is derived from Robert Faludi's book
 // "Building Wireless Sensor Networks"
 // Check the original for further explanations

 int LED_NIGHT = 12;
 int LED_SUNSET = 11;
 int LED_DAY = 10;
 int debugLED = 13;
 int analogValue = 0;

void setup() {
   pinMode(LED_DAY,OUTPUT);
   pinMode(LED_SUNSET,OUTPUT);
   pinMode(LED_NIGHT,OUTPUT);
   pinMode(debugLED,OUTPUT);
   Serial.begin(9600);
}

void loop() {
   digitalWrite(LED_NIGHT, LOW);
   digitalWrite(LED_SUNSET, LOW);
   digitalWrite(LED_DAY, LOW);

   // 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++) {                         // shouldn't it be 19?
               byte discard = Serial.read();
           }
           int analogHigh = Serial.read();
           int analogLow = Serial.read();
           analogValue = analogLow + (analogHigh * 256);
       }
  }

if (analogValue > 0 && analogValue <= 350) {
    digitalWrite(LED_NIGHT, HIGH);
    delay(10);
    digitalWrite(LED_NIGHT, LOW);
}

if (analogValue > 350 && analogValue <= 750) {
    digitalWrite(LED_SUNSET, HIGH);
    delay(10);
    digitalWrite(LED_SUNSET, LOW);
}

if (analogValue > 750 && analogValue <= 1023) {
    digitalWrite(LED_DAY, HIGH);
    delay(10);
    digitalWrite(LED_DAY, LOW);
}
}

Activity:

After you learn how to read an XBee direct frame, it is time to construct your own. Connect the LEDs to digital output pins in the router XBee and use the arduino to build a message that turns on and off those LEDs using XBee direct.

Modify the project in any way that makes it even more intresting, explain all the details in a blog post and apply for the Sunset Sensor badge.

View Projects 2014 Edition


Comments

comments powered by Disqus