My Cart

How to Measure CO2 and Temperature Using Waspmote Gas Sensors Kit

Difficulty Level: Intermediate -

1. Introduction

This tutorial allows the user to know the CO2 and temperature levels in a particular area thanks to the Waspmote Gas 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 now

For further information about Waspmote, consult the main tutorial.

Step 1: Connection

First connect the XBee 802.15.4 module in the Wasmpote XBee socket, then connect the Waspmote Gases Board.

Connect the CO2 sensor in the S1A socket and the temperature sensor in the ST socket of Waspmote Gases Board as you can see in the diagram.

Finally plug the XBee gateway in your PC.

Step 2: The Code

Waspmote:

Code:
/*  
 *  Waspmote Gas 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 & Marcos Martinez
 */

#include <WaspSensorGas_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
////////////////////////////////////////////////

//GAIN of the CO2 sensor stage
#define GAIN_CO2  7

// Variable to store the read CO2 value
float co2Val;

//Variable to store the read temperature value
float temperatureVal;  

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.1 Memory allocation
  packet = (packetXBee*) calloc(1,sizeof(packetXBee));

  // 3.2 Choose transmission mode: UNICAST or BROADCAST
  packet -> mode = UNICAST;

  // 3.3 Set destination XBee parameters to packet
  xbee802.setDestinationParams(packet, MAC_ADDRESS, frame.buffer, frame.length); 

  // 3.4 Initial message transmission
  xbee802.sendXBee(packet);

  // 3.5 Check TX flag
  if( xbee802.error_TX == 0 ) 
  {
    USB.println("ok");
  }
  else 
  {
    USB.println("error");
  }

  // 3.6 Free memory
  free(packet);
  packet = NULL;

  // 3.7 Communication module to OFF
  xbee802.OFF();
  delay(100);
}

void loop()
{ 
  ////////////////////////////////////////////////
  // 4. Turn on and read sensors
  ////////////////////////////////////////////////

  // 4.1 Turn on the sensor board
  SensorGasv20.ON();

  // 4.2 Turn on the RTC
  RTC.ON();

  // 4.3 Configure the sensor socket
  SensorGasv20.configureSensor(SENS_CO2, GAIN_CO2);
  
  // 4.4 Turn on the CO2 sensor and wait for stabilization and
  // sensor response time
  SensorGasv20.setSensorMode(SENS_ON, SENS_CO2);

  // The sensor needs time to warm up and get a response from gas
  // To reduce the battery consumption, use deepSleep instead delay
  // After 1 minute, Waspmote wakes up thanks to the RTC Alarm
  PWR.deepSleep("00:00:01:00", RTC_OFFSET, RTC_ALM1_MODE1, ALL_ON);

  // 4.5 Read the sensors 
  co2Val = SensorGasv20.readValue(SENS_CO2);
  temperatureVal = SensorGasv20.readValue(SENS_TEMPERATURE);

  // 4.6 Print the result through the USB
  USB.print(F("CO2: "));
  USB.print(co2Val);
  USB.println(F("V"));

  USB.print(F("Temperature: "));
  USB.print(temperatureVal);
  USB.println(F("ÂşC"));

  USB.println();

  // 4.7 Turn off the sensor board
  SensorGasv20.OFF();

  // 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_CO2, co2Val);
  frame.addSensor(SENSOR_TCA, temperatureVal);

  ////////////////////////////////////////////////
  // 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);
}