O'Reilly logo

Building Wireless Sensor Networks by Robert Faludi

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using What You Need

Now that you understand how API mode works and how the structures around it function, we can talk about using it. It’s pretty simple to write your own microcontroller code to work with the API, especially if you use only what you need. While the API is capable of supplying your application with an airtight protocol that covers any possible radio configuration, in most cases you’ll be using only a small subset of that radio’s capabilities. For example, in many sensor network applications your I/O data samples will all be the same length. This means you can get away with never checking the length byte. It also means that your data will show up in the same place in every frame. There’s no need to calculate the digital mask or look for digital samples if you already know that your network never uses digital inputs.

The romantic lighting sensor example in the previous chapter takes advantage of this minimalist strategy. Here’s the code from the loop that reads the I/O Data Sample frame:

// 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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required