My Cart

How to Measure Ultrasound and Light Using Waspmote Smart Cities Sensors Kit

Difficulty Level: Intermediate -

1. Introduction

This tutorial allows the user to know the light level and the distance from an object thanks to the Waspmote Smart Cities 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 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 Cities Board.

Connect the light sensor and the ultrasound sensor in the correct socket of Waspmote Smart Cities Board as you can see in the diagram.

Finally plug the XBee gateway in your PC.

Step 2: The Code

Waspmote:

Code:
/*  
 *  Waspmote Smart Cities 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 & Victor Boria
 */

#include <WaspSensorCities.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 ultrasound value
float ultraSoundValue;
//Variable to store the read light value
float lightValue;  


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

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

  // 4.3 Turn on the ultrasound sensor and wait for stabilization and
  // sensor response time
  SensorCities.setSensorMode(SENS_ON, SENS_CITIES_ULTRASOUND_3V3);
  delay(10);

  // 4.4 Turn on the light sensor and wait for stabilization and
  // sensor response time
  SensorCities.setSensorMode(SENS_ON, SENS_CITIES_LDR);

  // 4.5 Read the sensors 
  ultraSoundValue = SensorCities.readValue(SENS_CITIES_ULTRASOUND_3V3, SENS_US_WRA1);
  lightValue = SensorCities.readValue(SENS_CITIES_LDR);

  // 4.6 Print the result through the USB
  USB.print(F("Distance: "));
  USB.print(ultraSoundValue);
  USB.println(F("cm"));

  USB.print(F("Luminosity: "));
  USB.print(lightValue);
  USB.println(F("%"));

  USB.println();

  // 4.7 Turn off the sensors
  SensorCities.setSensorMode(SENS_OFF, SENS_CITIES_ULTRASOUND_3V3);
  SensorCities.setSensorMode(SENS_OFF, SENS_CITIES_LDR);

  // 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_US_3V3, ultraSoundValue);
  frame.addSensor(SENSOR_LUM, lightValue);

  ////////////////////////////////////////////////
  // 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: