Hardware Reference
In-Depth Information
Programming an XBee motion detector
You are now going to modify the sketch slightly so that it can transmit the state of the
sensor to a central interface running on your computer. However, you not only want to
transmit the state of the motion sensor, but also an ID identifying the sensor that is detect-
ing the motion. Programming the detector starts by importing the right libraries:
// Libraries
#include <aREST.h>
#include <SPI.h>
The aREST library implements a REST API for Arduino. REST stands for REpresenta-
tional State Transfer, and is widely used in web applications such as Software as a Service
( SaaS ) applications. In our case, we will use this library to standardize the communication
with the central interface that will run on the computer. In this project, the REST com-
mands will be sent over the XBee connection that acts as a serial port from the Arduino
point of view.
After importing the libraries, you need to declare the sensor pin and the ID of the module
as follows:
// Motion sensor pin and ID
int sensor_pin = 8;
String xbee_id = "2";
After this, you can create the instance of the aREST library that will handle the requests
coming from the graphical interface:
// Create aREST instance
aREST rest = aREST();
In the setup() function of the sketch, the first step is to start the serial communication.
Be careful here, as the speed of the serial object has to be the same as the speed of your
XBee modules, which is 9600 bauds by default:
// Start Serial
Serial.begin(9600);
You can also set the ID of the module:
Search WWH ::




Custom Search