In this article we are going to see an example of how to connect a new sensor to SquidBee. In this case we are going to use the vibration sensor from the movement state sensor pack. We can use this "new mote" for example for detecting if the mote is being stolen, something knocks the device, the mote falls or even an earthquake happens.
As you can see on the photo, the vibration sensor fits inside the probe and the wires to the mote are protected using the Heat-shrinkable sleeve. This sensor is very simple, it actually is a resistor witch value is changing with its vibrating movement. We use this variation for detecting a vibration on the sensor. The circuit we need to integrate the sensor into SquidBee is also very simple, just a pull-up resistor (1k).
In this case, we atach the sensor to digital input 2, that matches with one of the hardware interrupt pin in SquidBee, now the main time SquidBee is sleeping but when a vibration is detected, the interrupt is activated and SquidBee wakes up and send the message we've programmed.
Other important thing we use in this example is the sleeping feature in SquidBee, if you see the code, you'll see arduino is most of the time sleeping (saving power). For waking up SquidBee we use one of the interrupts sources in Arduino, the one on pin 2 (where we connect the sensor). When a vibration is detected the voltage value on pin 2 goes to 0 V and the interrupt is triggered, once the interrupts happens arduino is waked up, it wakes up the XBee, sends the message and later all the system goes back to sleep.
/*
* Copyright (C) 2008 Libelium Comunicaciones Distribuidas S.L.
*
* This file is part of SquidBee code examples
* squidbee_vibration.pde 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 Foobar. If not, see <http://www.gnu.org/licenses/>.
*/
/* Name: squidbee_vibration.pde
* Version 0.0.1
* Desc: A vibration sensor is attached to digital input 2, an interrupt is set in this pin
* in SquidBee. When the sensor detects vibration, the interrupt is activated and Squidbee
* wakes up and send a message
* Author: Marcos Yarza <m.yarza"at/removethis"libelium.com>
*/#include <avr/sleep.h>
int wakePin = 2; // pin used for waking up
int XBee_wake_pin = 7; // pin used for waking up XBee (connected to pin 9 in XBee)
void wakeUpNow() // here the interrupt is handled after wakeup
{
// do nothing
}
void setup()
{
pinMode(wakePin, INPUT);
pinMode(XBee_wake_pin, INPUT);
digitalWrite(XBee_wake_pin, HIGH);
Serial.begin(19200);
attachInterrupt(0, wakeUpNow, LOW); // use interrupt 0 (pin 2) and run function
// wakeUpNow when pin 2 gets LOW
}
void sleepNow() // here we put the arduino to sleep
{
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here
sleep_enable(); // enables the sleep bit in the mcucr register
// so sleep is possible. just a safety pin
attachInterrupt(0,wakeUpNow, LOW); // use interrupt 0 (pin 2) and run function
// wakeUpNow when pin 2 gets LOW
sleep_mode(); // here the device is actually put to sleep!!
// THE PROGRAM CONTINUES FROM HERE AFTER WAKING UP
sleep_disable(); // first thing after waking from sleep:
// disable sleep...
detachInterrupt(0); // disables interrupt 0 on pin 2 so the
// wakeUpNow code will not be executed
// during normal running time.
}
void loop()
{
digitalWrite(XBee_wake_pin, LOW); // after wakes up the micro, wakes up XBee
delay(10);
Serial.println("Vibration alarm!"); // sends the alarm messge
digitalWrite(XBee_wake_pin, HIGH); // sleep XBee again
delay(100);
sleepNow(); // sleep function called here
}