libelium
  • rss
  • flickr
  • twitter
  • facebook
  • youtube

Tutorial: Arduino Deluxe Pack: How to use XBee and GPS with Arduino UNO

Posted on February 17, 2012 by Cooking Hacks There have been 0 comments

Introduction

In  this tutorial we are going to introduce the use of ARDUINO with XBEE and GPS. We will use the Arduino Deluxe Pack (802.15.4 Version).This pack includes almost all the ingredients neccesary for this proyect. You will need to add some small wires and an Internal GPS Antenna not included in the pack.  We are going to read the data from the GPS module and transmit it through the Xbee 802.15.4 . This small example will let you to know the possition of any arduino board and send this data through the xbee device.

Ingredients:

Difficulty: Medium -medium
Preparation Time: 30 minutes

Steps Index

Step 1: Connecting the GPS module to Arduino (USB gateway mode):

First of all , we need to connect the GPS device to arduino as you could see in the picture.

After this we will connect the GPS device to the Arduino board.

As you can see in the picture GPS module is connected using pins 8,9 ...,13,GND,AREF , it doesn't receive any power from these pins so we need power the GPS module using some small wires , red (for positive) and black (for negative).
We connect the wires as you can see in the picture , the red one from +5 V pin of Arduino to +5 V pin of the GPS module (the two pins of the right side), and the black one from GND pin of Arduino to GND of the GPS module (the two pins of the lef side).

Once we have the GPS module connected , we will connect the communication shield to Arduino and the Xbee board to it.

Be aware that the power pins of the Arduino board will be covered by the Communication shield so we should bend the wires to be placed under the Communication shield .
Repeat the process and place the other Xbee over the other Communication shield and this over the other arduino.

At the end of the connection process we should obtain two Arduino boards like the ones of the photo.

Step 2 : Programming arduino.

VERY IMPORTANT : To upload a sketch to an Arduino board with a Xbee shield, you'll need to put both jumpers on the shield to the "USB" setting (i.e. place them on the two pins closest to the edge of the board) or remove them completely (but be sure not to lose them!). Then, you can upload a sketch normally from the Arduino environment. Once you have programmed Arduino replace the jumper in their Xbee possition.

We will connect the emitter (the Arduino with the GPS module, communication shield and Xbee module ) and we will upload the gps_xbee_emitter code. After that , we will connect the receiver (the Arduino with communication shield and Xbee module) and we will upload the gps_xbee_receiver code .

Step 3 : The code

There are two codes:
Code for emitter

/*
*  Copyright (C) 2010 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 0.1
*  This code have been based on the following tutorials, if you want more information
*   pleae visit it:
*
*    http://www.cooking-hacks.com/index.php/documentation/tutorials/arduino-gps
*    http://www.cooking-hacks.com/index.php/documentation/tutorials/arduino-xbee-shield
*
*  Author: Diego Corredera
*/

//----- GPS configuration ---------

// include the SoftwareSerial library
#include <SoftwareSerial.h>

// Constants
#define rxPin 9      //rx pin in gps connection
#define txPin 8      //tx pin in gps connection

// set up the serial port

SoftwareSerial gps = SoftwareSerial(rxPin, txPin);

// variables
byte byteGPS = 0;
int i = 0;
int h = 0;

// Buffers for data input
char inBuffer[300] = "";
char GPS_RMC[100]="";
char GPS_GGA[100]="";

// ----- End GPS configuration --------

void setup() {

Serial.begin(9600);                                                        // Arduino serial port configuration
delay(1000);

Serial.println("\r\n-------- Config device --------");
Serial.flush();                                                            // Clear serial buffer

// ------ Xbee module configuration -------------

command_mode();
reset();
read_parameter();
config_parameter();
read_parameter();

// ------ End Xbee module configuration ---------

// ------ Gps serial port configuration ---------

pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
gps.begin(4800);

// ------ End gps serial port configuration -----

Serial.println("\r\n-------- Sending gps data with Xbee --------");
}

void loop() {

// ------- Gps data parser ----------

// Read the RMC sentence from GPS
byteGPS = 0;
byteGPS = gps.read();
while(byteGPS != 'R'){
byteGPS = gps.read();
}
GPS_RMC[0]='$';
GPS_RMC[1]='G';
GPS_RMC[2]='P';
GPS_RMC[3]='R';

i = 4;
while(byteGPS != '*'){
byteGPS = gps.read();
inBuffer[i]=byteGPS;
GPS_RMC[i]=byteGPS;
i++;
}

// Read GGA sentence from GPS
byteGPS = 0;
byteGPS = gps.read();
while(byteGPS != 'A'){
byteGPS = gps.read();
}
GPS_GGA[0]='$';
GPS_GGA[1]='G';
GPS_GGA[2]='P';
GPS_GGA[3]='G';
GPS_GGA[4]='G';
GPS_GGA[5]='A';

i = 6;
while(byteGPS != '*'){
byteGPS = gps.read();
inBuffer[i]=byteGPS;
GPS_GGA[i]=byteGPS;
i++;
}

// print the GGA sentence to USB
Serial.print("GGA sentence: ");
h = 0;
while(GPS_GGA[h] != 42){
Serial.print(GPS_GGA[h],BYTE);
h++;
}
Serial.println();

// print the RMC sentence to USB
Serial.print("RMC sentence: ");
h = 0;
while(GPS_RMC[h] != 42){
Serial.print(GPS_RMC[h],BYTE);
h++;
}
Serial.println();
delay(1000);

// --- End gps data parser --------

}

//---------------------------------------------------------------------------------------------------------

// XBEE control commands : These functions manage the commands to enter and out from configuration mode,reset and save data
//---------------------------------------------------------------------------------------------------------
void command_mode(){                                                        // Enter in configuration mode
while(Serial.available()==0){
delay(1000);
Serial.print("+++");
delay(1000);
}
read_data();
}

void exit_cn(){                                                             // Exit command mode
Serial.println("\r\nATCN");
read_data();
Serial.println("\r\n***** Exit command mode *****");
delay(1000);
}

void reset(){                                                              // Reset Xbee
Serial.println("\r\nATRE");
read_data();
Serial.println("\r\n***** Device reset *****");
delay(1000);
}

void write_config(){                                                     // Writes the configuration in the xbee
Serial.println("\r\nATWR");
read_data();
Serial.println("\r\n***** Writting configuration in Xbee module *****");
delay(1000);
}

//-------------------------------------------------------------------------------------------------------------

// XBEE visualization commands : These functions read and write data from or to xbee or serial buffer
//-------------------------------------------------------------------------------------------------------------
void read_data(){                 // Reading data response from serial xbee
byte data;
while(Serial.available()==0){}
while(Serial.available()>0){
data=Serial.read();
delay(10);                  // Waiting to receive new data
write_data(data);
}
Serial.flush();                    // Clear serial buffer
}

void write_data(byte data){    // Write data from xbee to serial
Serial.print(char(data));
}

void read_parameter(){        // Reading configuration parameter
Serial.println("\r\n-------- Reading device parameter --------");

Serial.println("\r\nATID");  // Network ID
read_data();

Serial.println("\r\nATCH");  // Channel ID
read_data();

Serial.println("\r\nATMY");  // Address of the module
read_data();

Serial.println("\r\nATSH");  // Serial number of XBee  high (32 bits)
read_data();

Serial.println("\r\nATSL"); // Serial number of XBee low (32 bits)
read_data();

Serial.println("\r\nATDH"); // Destination address high (32 bits)
read_data();

Serial.println("\r\nATDL"); // Destination address low (32 bits)
read_data();

Serial.println("\r\nATBD"); // Baud rate
read_data();
}

void config_parameter(){        // Reading configuration parameter
Serial.println("\r\n-------- Config device parameter --------");

Serial.println("\r\nATID3329"); // Network ID
read_data();

Serial.println("\r\nATCHD");    // Channel ID
read_data();

//Serial.println("\r\nATDH");    // Destination address high (32 bits)
//read_data();

//Serial.println("\r\nATDL");    // Destination address low (32 bits)
//read_data();

}

//-------------------------------------------------------------------------------------------------------------

Code for receiver

/*
*  Copyright (C) 2010 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 0.1
*  This code have been based on the following tutorials, if you want more information
*   pleae visit it:
*
*    http://www.cooking-hacks.com/index.php/documentation/tutorials/arduino-gps
*    http://www.cooking-hacks.com/index.php/documentation/tutorials/arduino-xbee-shield
*
*  Author: Diego Corredera
*/

void setup() {

Serial.begin(9600);
delay(1000);
Serial.println("\r\n-------- Config device --------");
Serial.flush();                                                            // Clear serial buffer

// ------ Xbee module configuration -------------

command_mode();
reset();
read_parameter();
config_parameter();
read_parameter();

// ------ End Xbee module configuration ---------

Serial.println(" \r\n ------ Receiving data from GPS -------- ");
}

void loop() {

read_data();                                                         // Reads data sended throw Xbee and writes to the serial port

}

// XBEE control commands : These functions manage the commands to enter and out from configuration mode,reset and save data
//---------------------------------------------------------------------------------------------------------
void command_mode(){                                                        // Enter in configuration mode
while(Serial.available()==0){
delay(1000);
Serial.print("+++");
delay(1000);
}
read_data();
}

void exit_cn(){                                                             // Exit command mode
Serial.println("\r\nATCN");
read_data();
Serial.println("\r\n***** Exit command mode *****");
delay(1000);
}

void reset(){                                                              // Reset Xbee (If you have not use write_config this functions reset the device to his previous values)
Serial.println("\r\nATRE");
read_data();
Serial.println("\r\n***** Device reset *****");
delay(1000);
}

void write_config(){                                                     // Writes the configuration in the Xbee module
Serial.println("\r\nATWR");
read_data();
Serial.println("\r\n***** Writting configuration in Xbee module *****");
delay(1000);
}

//-------------------------------------------------------------------------------------------------------------

// XBEE visualization commands : These functions read and write data from or to xbee or serial buffer
//-------------------------------------------------------------------------------------------------------------
void read_data(){               // Reading data response from serial xbee
byte data;
while(Serial.available()==0){}
while(Serial.available()>0){
data=Serial.read();
delay(10);                  // Waiting to receive new data
write_data(data);
}
Serial.flush();             // Clear serial buffer
}

void write_data(byte data){     // Write data from xbee to serial
Serial.print(char(data));
}

void read_parameter(){        // Reading configuration parameter
Serial.println("\r\n-------- Reading device parameter --------");

Serial.println("\r\nATID");  // Network ID
read_data();

Serial.println("\r\nATCH");  // Channel ID
read_data();

Serial.println("\r\nATMY");  // Address of the module
read_data();

Serial.println("\r\nATSH");  // Serial number of XBee  high (32 bits)
read_data();

Serial.println("\r\nATSL"); // Serial number of XBee low (32 bits)
read_data();

Serial.println("\r\nATDH"); // Destination address high (32 bits)
read_data();

Serial.println("\r\nATDL"); // Destination address low (32 bits)
read_data();

Serial.println("\r\nATBD"); // Baud rate
read_data();
}

void config_parameter(){        // Reading configuration parameter
Serial.println("\r\n-------- Config device parameter --------");

Serial.println("\r\nATID3329"); // Network ID
read_data();

Serial.println("\r\nATCHD");    // Channel ID
read_data();

//Serial.println("\r\nATDH");    // Destination address high (32 bits)
//read_data();

//Serial.println("\r\nATDL");    // Destination address low (32 bits)
//read_data();

}

//-------------------------------------------------------------------------------------------------------------

Step 4 : Final arrangements

Once we have the code uploaded , we will disconnect the emitter and we will carry it somewhere where we will have GPS connection. Remember that the connection of the GPS will be only possible outdoors.Don't forget pòwering the Arduino , if you don't power the Arduino the system won't work.
We will open a serial monitor with the receiver conected to the computer and we will start receiving gps data after some minutes.
It is possible that the GPS will take several minutes to connect to the satelite , if this happens we will receive the empty NMEA sentences (the senteces
of the GPS wihtout data of the position). We only need to wait some minutes to leave the GPS to connect to the satelite.
Note for advanced users: If you want "play" with Xbee , in the code added with this tutorial you have all the basic commands used to configure the communication between two Xbee modules. For more information about this please consult the link provided about arduino xbee shield.

For more information about it please revise :
* Tutorial GPS
* Tutorial XBee

If you are searching for Wireless Sensor Networks devices (motes) you may be interested in our ready to market sensor platform: Waspmote. You can discover the differences between both platforms in the document: Waspmote vs SquidBee.


This post was posted in Arduino, General, News and Events, Tutorials

Comments