My Cart

Tutorial: Arduino Deluxe Pack: How to use XBee and GPS with Arduino UNOFebruary 17, 2012

UPDATE

If you want to use XBee and GPS with Arduino, please check our tutorials by separate.


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.

15 thoughts on “Tutorial: Arduino Deluxe Pack: How to use XBee and GPS with Arduino UNO”

  • Jake

    Can we get an updated version that runs with Arduino 1.0 please?

    Reply
  • 3dotter

    It seems that the only lines which need to be changed in the emitter code are:

    old: Serial.print(GPS_GGA[h], BYTE); into
    new: Serial.write(GPS_GGA[h]);

    and

    old: Serial.print(GPS_RMC[h], BYTE); into
    new: Serial.write(GPS_RMC[h]);

    After this the code compiled in my Arduino 1.0.5

    Good luck.

    Reply
  • Marina

    HELP

    I´ve just done all the tutorial with xbee, arduino uno and gps. I have problems in the final steps:

    The serial window says:

    -----config device----------
    ++++++++++OK
    ATRE
    OK
    *********Device reset*********
    ------Reading device parameter---------
    ATID
    3332
    ATCH
    C
    ATMY
    0
    ATSH
    13a200
    ATSL
    40ACE78A
    ATDH
    0
    ATDL
    0
    ATED
    3
    ------Config device parameter--------------
    ATID3329
    OK
    ATCHD
    OK
    --------Reading device parameter------------
    ATID
    3329
    ATCH
    D
    ATMY
    0
    ATSH
    13a200
    ATSL
    40ACE78A
    ATDH
    0
    ATDL
    0
    ATED
    3
    --------Receiving data from GPS----------
    ERROR

    I will appreciate your advice
    thanks

    Reply
  • Jon

    How do we get this gps data for each node through the coordinator via the API?

    Reply
  • Jorge

    Hola, puedo hacer que los datos se visualicen en una pantalla lcd y ademas de eso que se vea en tiempo real en google earth?

    Reply
  • David Palmer

    Does anything in the code need to be changed if your using XBee 900 hp modules.
    When I hook everything up and I don't have the gps outside it reads real fast but I get a lot of errors. When I put the gps outside it reads slower and goes through the setup with no errors it doesn't give an error when reading the gps but doesn't show any gps co-ordinates it just says - Config device -repeatedly.

    Reply
  • Steve

    ****PLEASE HELP**** Senior Design Project

    What part of the code for both the transmitter and receiver is necessary. Is the xbee configuration parts necessary if I have already configured my xbees? If i simply copy and paste the provided codes to my transmitter and receiver respectively, I receive an error message like Marina. No GPS data is transmitted and shown on the Serial Monitor.

    Thank you very much!

    Reply
    • Cooking Hacks
      Cooking Hacks April 6, 2015 at 7:51 pm

      Hello Steve,

      Please, can you post your question in the forum? This way, our technical team will check your issue, and other users will be up to it.

      Thanks, regards,

      Reply
  • Clevonildio Santos
    Clevonildio Santos March 25, 2015 at 8:31 pm

    when I send compile in one version of the Arduino 1.0.4 a function "Serial.print(GPS_GGA[h],BYTE);" of the error - the ´BYTE´ keyword is no longer supported.
    please, help me...

    Reply
    • Cooking Hacks
      Cooking Hacks April 6, 2015 at 7:43 pm

      Hello Clevonildio,

      Please, can you post your question in the forum? This way, our technical team will check your issue, and other users will be up to it.

      Thanks, regards,

      Reply
  • Umar

    I want to know that what is the update time of sending. I mean after how much time the new coordinates are sent?

    Reply
Leave a Reply