This tutorial allows the user to know the atmospheric pressure, temperature and humidity levels thanks to the Waspmote Agriculture sensors 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 Agriculture Board.
Connect the atmospheric pressure sensor and the digital temperature & humidity sensor in the correct socket of Waspmote Agriculture Board as you can see in the diagram.
Finally plug the XBee gateway in your PC.
Waspmote:
/* * Waspmote Agriculture Sensors 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 & Victor Boria */ #include <WaspSensorAgr_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 //////////////////////////////////////////////// // Variable to store the read soil moisture value float pressureValue; //Variable to store the read temperature value float temperatureValue; //Variable to store the read humidity value float humidityValue; 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 SensorAgrv20.ON(); // 4.2 Turn on the RTC RTC.ON(); // 4.3 Turn on the atmospheric pressure sensor and wait for stabilization and // sensor response time SensorAgrv20.setSensorMode(SENS_ON, SENS_AGR_PRESSURE); delay(10); // 4.4 Turn on the temperature and humidity sensor and wait for stabilization and // sensor response time SensorAgrv20.setSensorMode(SENS_ON, SENS_AGR_SENSIRION); // 4.5 Read the sensors pressureValue = SensorAgrv20.readValue(SENS_AGR_PRESSURE); temperatureValue = SensorAgrv20.readValue(SENS_AGR_SENSIRION, SENSIRION_TEMP); humidityValue = SensorAgrv20.readValue(SENS_AGR_SENSIRION, SENSIRION_HUM); // 4.6 Print the result through the USB USB.print(F("Pressure: ")); USB.print(pressureValue); USB.println(F("kPa")); USB.print(F("Temperature: ")); USB.print(temperatureValue); USB.println(F("ºC")); USB.print(F("Humidity: ")); USB.print(humidityValue); USB.println(F("%RH")); USB.println(); // 4.7 Turn off the sensors SensorAgrv20.setSensorMode(SENS_OFF, SENS_AGR_PRESSURE); SensorAgrv20.setSensorMode(SENS_OFF, SENS_AGR_SENSIRION); // 4.8 Turn off the RTC RTC.OFF(); delay(10); //////////////////////////////////////////////// // 5. Sensor message composition //////////////////////////////////////////////// // 5.1 Create new frame frame.createFrame(ASCII); // 5.2 Set frame fields frame.addSensor(SENSOR_PA, pressureValue); frame.addSensor(SENSOR_TCB, temperatureValue); frame.addSensor(SENSOR_HUMB, humidityValue); //////////////////////////////////////////////// // 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); }
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: