This tutorial allows the user to know the PIR and light levels thanks to the Waspmote Events sensor kit. With this code Waspmote wakes up every three minutes from the deep sleep mode making use of the RTC, read the sensors and create a data frame for sending it via XBee to a PC with a XBee gateway.
Ingredients:
Preparation Time: 30 minutes
Buy nowFor further information about Waspmote, consult the main tutorial.
First connect the XBee 802.15.4 module in the Wasmpote XBee socket, then connect the Waspmote Events Board.
Connect the PIR sensor in the S7 socket and the light sensor in the S2 socket of Waspmote Events Board as you can see in the diagram.
Finally plug the XBee gateway in your PC.
Waspmote:
/* * Waspmote Events Sensor Kit * * Copyright (C) Libelium Comunicaciones Distribuidas S.L. * http://www.libelium.com * * This program 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. * * 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 this program. If not, see http://www.gnu.org/licenses/. * * Version: 1.0 * Design: David Gascón * Implementation: Alejando Gallego, Marcos Martinez & Victor Boria */ #include <WaspSensorEvent_v20.h> #include <WaspFrame.h> #include <WaspXBee802.h> //////////////////////////////////////////////// // XBee Parameters //////////////////////////////////////////////// // PAN (Personal Area Network) Identifier uint8_t PANID[2]={ 0x12,0x34}; //XBee channel uint8_t XBee_channel= 0x0F; // Destination MAC address char* MAC_ADDRESS="0013A200400A3451"; //Pointer to an XBee packet structure packetXBee* packet; //////////////////////////////////////////////// // Sensors Parameters //////////////////////////////////////////////// //TYPE of the water flow sensor stage #define TYPE_FLOW SENS_SMART_FLOW_5V // Variable to store the read PIR value float pirVal; //Variable to store the read light value float lightVal; //THRESHOLD for interruption from the light sensor const float THRESHOLD = 1.0; void setup() { //////////////////////////////////////////////// // 0. Init USB port for debugging //////////////////////////////////////////////// USB.ON(); USB.println(F("start")); delay(100); //////////////////////////////////////////////// // 1. Initial message composition //////////////////////////////////////////////// // 1.1 Set mote Identifier (16-Byte max) frame.setID("WASPMOTE"); // 1.2 Create new frame frame.createFrame(ASCII); // 1.3 Set frame fields (String - char*) frame.addSensor(SENSOR_STR, (char*) "Start frame"); // 1.4 Print frame frame.showFrame(); //////////////////////////////////////////////// // 2. Configure XBee parameters //////////////////////////////////////////////// // 2.1 Switch on the XBee module xbee802.ON(); // wait for a second delay(1000); // 2.2 set channel xbee802.setChannel(XBee_channel); // check at commmand execution flag if( xbee802.error_AT == 0 ) { USB.println(F("Channel set OK")); } else { USB.println(F("Error setting channel")); } // 2.3 set PANID xbee802.setPAN(PANID); // check the AT commmand execution flag if( xbee802.error_AT == 0 ) { USB.println(F("PANID set OK")); } else { USB.println(F("Error setting PANID")); } // 2.4 set encryption mode (1:enable; 0:disable) xbee802.setEncryptionMode(0); // check the AT commmand execution flag if( xbee802.error_AT == 0 ) { USB.println(F("encryption set OK")); } else { USB.println(F("Error setting security")); } // 2.5 write values to XBee module memory xbee802.writeValues(); // check the AT commmand execution flag if( xbee802.error_AT == 0 ) { USB.println(F("write values OK")); } else { USB.println(F("Error writing values")); } //////////////////////////////////////////////// // 3. Send initial message //////////////////////////////////////////////// // 3.2 Memory allocation packet = (packetXBee*) calloc(1,sizeof(packetXBee)); // 3.3 Choose transmission mode: UNICAST or BROADCAST packet -> mode = UNICAST; // 3.4 Set destination XBee parameters to packet xbee802.setDestinationParams(packet, MAC_ADDRESS, frame.buffer, frame.length); // 3.5 Initial message transmission xbee802.sendXBee(packet); // 3.6 Check TX flag if( xbee802.error_TX == 0 ) { USB.println("ok"); } else { USB.println("error"); } // 3.7 Free memory free(packet); packet = NULL; // 3.8 Communication module to OFF xbee802.OFF(); delay(100); } void loop() { //////////////////////////////////////////////// // 4. Turn on and read sensors //////////////////////////////////////////////// // 4.1 Turn on the sensor board SensorEventv20.ON(); // 4.2 Turn on the RTC RTC.ON(); // 4.3 Firstly, wait for signal stabilization while( digitalRead(DIGITAL5) == 1 ) { USB.println(F("...wait for stabilization")); delay(1000); } // 4.4 Configure the socket 2 threshold SensorEventv20.setThreshold(SENS_SOCKET2, THRESHOLD); // 4.5 Enable interruptions from the board SensorEventv20.attachInt(); // 4.6 Read the sensors pirVal = SensorEventv20.readValue(SENS_SOCKET7); lightVal = SensorEventv20.readValue(SENS_SOCKET2, SENS_RESISTIVE); // 4.7 Print the result through the USB USB.print(F("Sensor output: ")); USB.print(pirVal); USB.println(F(" Volts")); USB.print(F("Resistance: ")); USB.print(lightVal); USB.println(F("kohms\n")); USB.println(); delay(10); //////////////////////////////////////////////// // 5. Sensor message composition //////////////////////////////////////////////// // 5.1 Create new frame frame.createFrame(ASCII); // 5.2 Set frame fields frame.addSensor(SENSOR_PIR, pirVal); frame.addSensor(SENSOR_LUM, lightVal); //////////////////////////////////////////////// // 6. Send sensor message //////////////////////////////////////////////// // 6.1 Switch on the XBee module xbee802.ON(); // 6.2 Memory allocation packet = (packetXBee*) calloc(1,sizeof(packetXBee)); // 6.3 Choose transmission mode: UNICAST or BROADCAST packet -> mode = UNICAST; // 6.4 Set destination XBee parameters to packet xbee802.setDestinationParams(packet, MAC_ADDRESS, frame.buffer, frame.length); // 6.5 Initial message transmission xbee802.sendXBee(packet); // 6.6 Check TX flag if( xbee802.error_TX == 0 ) { USB.println("ok"); } else { USB.println("error"); } // 6.7 Free memory free(packet); packet = NULL; // 6.8 Communication module to OFF xbee802.OFF(); /////////////////////////////////////////// // 7. Sleep /////////////////////////////////////////// // Go to deepsleep // After 3 minutes, Waspmote wakes up thanks to the RTC Alarm PWR.deepSleep("00:00:03:00", RTC_OFFSET, RTC_ALM1_MODE1, ALL_OFF); USB.ON(); USB.println(F("wake up\n")); /////////////////////////////////////// // 8. Check Interruption Flags /////////////////////////////////////// // 8.1. Check interruption from Sensor Board if(intFlag & SENS_INT) { interrupt_function(); } // 8.2. Check interruption from RTC alarm if( intFlag & RTC_INT ) { USB.println(F("-----------------------------")); USB.println(F("RTC INT captured")); USB.println(F("-----------------------------")); // clear flag intFlag &= ~(RTC_INT); } } /********************************************** * * interrupt_function() * * Local function to treat the sensor interruption * * ***********************************************/ void interrupt_function() { // Disable interruptions from the board SensorEventv20.detachInt(); // Load the interruption flag SensorEventv20.loadInt(); // Read the sensor lightVal = SensorEventv20.readValue(SENS_SOCKET2, SENS_RESISTIVE); // In case the interruption came from socket 7 if( SensorEventv20.intFlag & SENS_SOCKET7) { USB.println(F("-----------------------------")); USB.println(F("Interruption from socket 7")); USB.println(F("-----------------------------")); // Printing and enabling interruptions USB.println(F("Presence detected\n")); // User should implement some warning // In this example, now wait for signal // stabilization to generate a new interruption while( digitalRead(DIGITAL5) == 1 ) { USB.println(F("...wait for stabilization")); delay(1000); } } // In case the interruption came from socket 2 else if( SensorEventv20.intFlag & SENS_SOCKET2) { USB.println(F("-----------------------------")); USB.println(F("Interruption from socket 2")); USB.println(F("-----------------------------")); // Printing and enabling interruptions // Print the resistance of the sensor USB.print(F("Resistance: ")); USB.print(lightVal); USB.println(F("kohms\n")); } // Clean the interruption flag intFlag &= ~(SENS_INT); // Enable interruptions from the board SensorEventv20.attachInt(); }
If you are interested in Internet of Things (IoT) or M2M projects check our open source sensor platform Waspmote which counts with more than 100 sensors available to use 'off the shelf', a complete API with hundreds of ready to use codes and a low consumption mode of just 0.7µA to ensure years of battery life.
Know more at:
Get the Starter Kits at: