My Cart

How to Make a Weather Calendar with Temperature and Light Sensors Using Waspmote Starter Kit

Difficulty Level: Intermediate -

1. Introduction

This tutorial allows the user to create a file with different measures, temperature, light, date and time and store them in the Waspmote SD, creating a log. It also sends all the read data via XBee to a PC with a XBee gateway.

Ingredients:

  • 1 x Waspmote Starter Kit:
    • 1x Waspmote 802.15.4 uFL
    • 1x Waspmote Gateway 802.15.4 uFL
    • 1x 2300 mAh LiPo Battery
    • 1x miniUSB Cable
  • 1 x Starter Kit:
    • 1x LDR
    • 1x 220Ω resistors
    • 1x Temperature sensor (MCP9700A)
    • 1x Breadboard
    • 1x Programming cable
    • Jumper Wires

Preparation Time: 30 minutes

Buy now

For further information about Waspmote, consult the main tutorial.

Step 1: Connection

Connect the LEDs with the resistors and the buzzer in the breadboard as you can see in the next diagram.

Connect two wires, red and black, to the two long rows on the side of the breadboard to provide access to 3.3V in and ground.

Place the sensor with the flat part looking up like the diagram. Connect the left leg of the sensor to GND and the right to 3.3V. Connect the central leg to the Analog 6.

Connect digital pins 1 to 6 to the longest leg of each LED. Place a resistor in the other leg of the LED and the remaining leg of the resistor to GND.

Finally connect the XBee in the correct socket as you can see in the diagram and plug the XBee gateway in your PC.

Step 2: The Code

Waspmote:

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

#include <WaspXBee802.h>

////////////////////////////////////////////////
// XBee Parameters
////////////////////////////////////////////////

// Destination MAC address
char* MAC_ADDRESS="0013A200400A3451";

packetXBee* packet; 
char pac[100];



////////////////////////////////////////////////
// Calendar Log Parameters
////////////////////////////////////////////////

// define folder and file to store data
char* path="/calendar";
char* filename="/calendar/log.csv";

// buffer to write into Sd File
char toWrite[200];

// define variables to read stored frames 
uint16_t lengthSD;
int32_t numLines;
uint8_t frameSD[200];

int light = 0;
float temperature = 0.0;
char temperaturestring[20];

// variables to define the file lines to be read
int startLine;
int endLine;


void setup()
{
  ////////////////////////////////////////////////
  // 0. Initialization
  ////////////////////////////////////////////////
  
  // 0.1 USB port for debugging
  USB.ON();
  USB.println(F("Weather Calendar"));
  delay(100);
  
  // 0.2 Sending init XBee message
  snprintf(pac,sizeof(pac),"Weather Calendar\n");
  sendXbee(pac);
  
  ////////////////////////////////////////////////
  // 1. SD Configuration
  ////////////////////////////////////////////////

  // 1.1 Set SD ON
  SD.ON();

  // 1.2 create path
  SD.mkdir(path);

  // 1.3 Create file for Waspmote Frames
  SD.create(filename);
  
  // 1.4 Write log header
  writeHeader();
  
  // 1.5 Set SD OFF
  SD.OFF();
  
  ////////////////////////////////////////////////
  // 2. Pin configuration
  ////////////////////////////////////////////////
  
  pinMode(ANALOG1,INPUT);  //LDR
  
  ////////////////////////////////////////////////
  // 3. RTC configuration
  ////////////////////////////////////////////////
   
  RTC.ON(); 
  // Setting time [yy:mm:dd:dow:hh:mm:ss]
  // dow = day of the week
  // Sunday = 01, Monday = 02, Tuesday = 03 ...
  RTC.setTime("15:05:04:02:09:00:00");
      
}

void loop()
{    
  ////////////////////////////////////////////////
  // 4. Logging with RTC
  ////////////////////////////////////////////////
  
  // 4.1 Get RTC time
  RTC.getTime();
  
  // 4.2 Check time, enter once every hour
  if (RTC.minute == 00 && RTC.second == 00){
	  
	// 4.3 Read sensors
    getSensors();
    
    // 4.4 Write log to SD
    writeToSD();
    
    // 4.5 Wait a second
    delay(1000);
  }
  
}


////////////////////////////////////////////////
// Get Sensor function
////////////////////////////////////////////////
  
void getSensors(){
	
  // Turn on sensor power
  PWR.setSensorPower(SENS_3V3,SENS_ON);
  
  delay(500);
  
  // Reading sensors
  light = analogRead(ANALOG1);
  temperature = Utils.readTemperature();
  
  // Turn off sensor power
  PWR.setSensorPower(SENS_3V3,SENS_OFF);	
}

////////////////////////////////////////////////
// Write to SD function
////////////////////////////////////////////////
  
void writeToSD(){
  
  // Turn on SD
  SD.ON();
  
  memset(toWrite, 0x00, sizeof(toWrite) );
  
  // Create string for storing data
  Utils.float2String(temperature,temperaturestring,2);
  sprintf(toWrite, "%s,%d,%s", RTC.getTime(), light, temperaturestring);
  
  // USB debugging
  USB.print("toWrite: ");
  USB.println(toWrite);

  // Write data to SD
  if(SD.appendln(filename, toWrite)) 
  {
    USB.println(F("append ok"));
    
  }
  else 
  {
    USB.println(F("append failed"));
    
  }
  
  // Sending data via XBee
  snprintf(pac,sizeof(pac),"Light: %i\n",light);
  sendXbee(pac);
  snprintf(pac,sizeof(pac),"Temperature: %s\n",temperaturestring);
  sendXbee(pac);
  
  // Turn on SD
  SD.OFF();

}


////////////////////////////////////////////////
// Write header function
////////////////////////////////////////////////
  
void writeHeader(){
	
  // Turn on SD
  SD.ON();
  
  memset(toWrite, 0x00, sizeof(toWrite) );
  
  // Create header string
  sprintf(toWrite, "DOW,Date,Hour,Light,Temperature");
  
  // USB debugging
  USB.print("Header: ");
  USB.println(toWrite);
  
  // Write header to SD
  if(SD.appendln(filename, toWrite)) 
  {
    USB.println(F("append ok"));
  }
  else 
  {
    USB.println(F("append failed"));
  }
  
  // Turn off SD
  SD.OFF();
}


////////////////////////////////////////////////
// Send XBee function
////////////////////////////////////////////////

void sendXbee(char* str){
	
  // Memory allocation
  packet=(packetXBee*) calloc(1,sizeof(packetXBee));
  
  // Choose transmission mode: UNICAST or BROADCAST
  packet->mode=BROADCAST; 
  
  // set destination XBee parameters to packet
  xbee802.setDestinationParams( packet, MAC_ADDRESS,str, MAC_TYPE);
  
  // Turn off XBee
  xbee802.ON();
  delay(100);
  
  // send XBee packet
  xbee802.sendXBee(packet);
  
  // free variables
  free(packet);
  packet=NULL;
  
  // Turn off XBee
  xbee802.OFF();
  
}