My Cart

How to Measure Liquid Flow and Current Using Waspmote Smart Metering Sensors Kit

Difficulty Level: Intermediate -

1. Introduction

This tutorial allows the user to know the liquid flow and current levels thanks to the Waspmote Smart Metering 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 Smart Metering Board.

Connect the liquid flow sensor in the S4 socket and the current sensor in the S1 socket of Waspmote Smart Metering Board as you can see in the diagram.

Finally plug the XBee gateway in your PC.

Step 2: The Code

Waspmote:

Code:
/*  
 *  Waspmote Smart Metering 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 <WaspSensorSmart_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 liquid flow value
float flowVal;
//Variable to store the read current value
float currentVal;  


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
  SensorSmartv20.ON();

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

  // 4.3 Turn on the liquid flow sensor and wait for stabilization and
  // sensor response time
  SensorSmartv20.setSensorMode(SENS_ON, SENS_SMART_FLOW_5V);
  delay(10);

  // 4.4 Turn on the 03 sensor and wait for stabilization and
  // sensor response time
  SensorSmartv20.setSensorMode(SENS_ON, SENS_SMART_CURRENT);

  // 4.5 Read the sensors 
  flowVal = SensorSmartv20.readValue(SENS_SMART_FLOW_5V, SENS_FLOW_YFG1);
  currentVal = SensorSmartv20.readValue(SENS_SMART_CURRENT);

  // 4.6 Print the result through the USB
  USB.print(F("Flow: "));
  USB.print(flowVal);
  USB.println(F("l/min"));

  USB.print(F("Current: "));
  USB.print(currentVal);
  USB.println(F("A"));

  USB.println();

  // 4.7 Turn off the sensors
  SensorSmartv20.setSensorMode(SENS_OFF, SENS_SMART_FLOW_5V);
  SensorSmartv20.setSensorMode(SENS_OFF, SENS_SMART_CURRENT);

  // 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_WF, flowVal);
  frame.addSensor(SENSOR_CU, currentVal);

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

        

Links and Documentation

Related Tutorials

Datasheets and Manuals



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: