How to Make a GSM Location Tracker With the AdaFruit FONA and Arduino

85,305

287

50

Introduction: How to Make a GSM Location Tracker With the AdaFruit FONA and Arduino

Introduction

In this tutorial we are going to make a web connected location logger using the Adafruit FONA Board, an Arduino, and the Sparkfun Data Service. It will get its location using triangulation and post it to an online database with a GET request over a cellular (GPRS) connection.

We will be using CoolTerm to communicate with the FONA, so please install it here. There is a link to a Sparkfun Tutorial on how to use it in Resources.

We will also be using the Sparkfun Data Service for the online database portion of this tutorial. Any "internet of things" service should work, however, with some modification to the code.

Resources

Parts List

Step 1: Hookup the Fona

You will need your Lipo Battery, the Arduino, the FONA + Antennae, a breadboard, your SIM card, and some hookup wire. I like to attach my breadboards and Arduino’s together like in the picture, it makes for a more reliable connection and less troubles trying to keep the two in close proximity while prototyping. I just used a piece of blue corrugated plastic and some hot glue, but a piece of cardboard will work just as well.

  • Insert your SIM Card into the SIM Slot on the back of the Board
  • Attach the Antenna

Make the following Connections

Arduino to Breadboard

  • Arduino GND -> breadboard Ground
  • Arduino 5v -> breadboard Power

FONA to Breadboard

  • Solder on the header pins if you haven’t already
  • Plug the FONA like in the picture, with the Bat PIN in ROW 1
  • FONA Vio -> Power Bus (This is VERY important. Communication won’t work without this)
  • FONA GND -> Ground Bus

FONA to Arduino

  • FONA RX -> PIN 3
  • FONA TX -> PIN 4
  • FONA PS -> PIN 6 (‘Power State’ pin. It is HIGH when the FONA is on, and LOW with the FONA is off)
  • FONA KEY -> PIN 7 (Pull this pin low for 2 seconds and the FONA will turn off/on)

Your setup should look somewhat like it does in the picture.

  • Double check all of the connections
  • Plug the Battery into the Connector on the FONA

The FONA is powered from the battery it is attached to. It will not work without that battery there. The USB connector on the FONA charges the Battery, that is all. The FONA will not work with just the USB plugged in, as the power comes from the Battery, NOT the USB connection. If you want to charge the Battery from an external 5v source, there is a hole next to the USB port labeled ‘5v’. I have a green wire soldered into it, so I can charge the battery from the Arduino. This is useful for embedding but not needed for prototyping.

Step 2: Test the FONA

Upload the following code to test the FONA - Arduino Connection:

#include <SoftwareSerial.h><softwareserial.h><br></softwareserial.h>
#define FONA_RX 3 //comms
#define FONA_TX 4 //comms
#define FONA_KEY 6 //powers board down
#define FONA_PS 7 //status pin. Is the board on or not?
SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX); //initialize software serial
char inChar = 0;
void setup() {
    pinMode(FONA_PS, INPUT);
    pinMode(FONA_KEY,OUTPUT);   
    digitalWrite(FONA_KEY, HIGH);
    Serial.begin(9600);
    Serial.println("Serial Ready");
    fonaSS.begin(9600);
    Serial.println("Software Serial Ready");
}
void loop() {
    if (fonaSS.available()){
        inChar = fonaSS.read();
        Serial.write(inChar);
        delay(20);
    }
    if (Serial.available()>0){
        fonaSS.write(Serial.read());
    }
}

Turn on FONA

  • to turn on the the FONA press and hold the button by the Battery Connector for 2 seconds and release.
  • The blue power light should turn on and if the SIM connects, a red light will start to blink slowly.
  • To turn off the board press and hold the same button

Connect Coolterm

Once the code is uploaded, connect to the Arduino Serial Port using Coolterm.

  • Select the correct Serial Port
  • Baud Rate is set to 9600
  • Under Terminal on the left make sure Line Mode is selected
  • Click OK and then Connect
  • Then in the command bar type: AT and press Enter. It is not case sensitive.
  • If the FONA module is active and listening it will respond with OK

If everything has gone OK up to here, Congrats!

Step 3: The AT Commands

Working with these GSM modules is basically just sending them commands over the Serial Port and parsing the responses. These modules have quite a bit of smarts built into them, which makes this process easier.

To send a command, type it into the command line in Coolterm and press Enter.

Command Syntax

    • Test Command: AT+=?
      • returns a list of parameters or value ranges that you can set with the command
    • Read Command: AT+?
      • Returns the current set value of the parameters of that command
    • Write Command: AT+=<....>
      • This command sets user definable parameter values
    • Execution Command: AT+
      • Executes a command without user definable parameter values
    • For a short list of useful commands.
      • Try out all of these commands. It’s pretty cool! Try making a phone call or sending a text!
    • For a compendium of all the commands the datasheet is here.

    Get your Location!

    Type in the following commands to get your (approximate) location

    • AT+CMGF=1
      • response: OK
    • AT+CGATT=1
      • response: OK
    • AT+SAPBR=3,1,"CONTYPE","GPRS"
      • response: OK
    • AT+SAPBR=3,1,"APN","your apn here"
      • response: OK
    • AT+SAPBR=1,1 -> note: the red light should start blinking faster
      • response: OK
    • AT+CIPGSMLOC=1,1
      • response: +CIPGSMLOC:
      • followed by: OK

    Make a GET Request!

    To make a GET request, it’s a similar process at the beginning. You should type all of these out and make sure your SIM card connects and does a GET request manually before you continue. It is actually very important in these complicated systems to check functionality at each step so if it stops working you know exactly at which point it stopped working. It makes the debugging process much easier.

    • First we setup the GPRS:
      • AT+CMGF=1
      • AT+CGATT=1
      • AT+SAPBR=3,1,"CONTYPE","GPRS"
      • AT+SAPBR=3,1,"APN","your apn here"
      • AT+SAPBR=1,1
    • The we setup HTTP and make the request
      • AT+HTTPINIT
      • AT+HTTPPARA=”CID”,1
      • AT+HTTPPARA=”URL”,”your url here”
      • AT+HTTPACTION=0
      • AT+HTTPREAD
    • Then we close the HTTP and the GPRS
      • AT+HTTPTERM
      • AT+SAPBR=0,1

    Step 4: Set Up a Sparkfun Data Stream

      The next step is to create a place for us to send the location data we just received. We are going to use a service that Sparkfun provides for free: data.sparkfun.com. There is a nice set of tutorials that they have put together for it, so I won't be redundant.

      For the included code to work right there needs to be 4 columns labeled:

      • latitude
      • longitude
      • time
      • date

      Once that is done, make sure you have the Private and Public keys and move on to the next step.

      Step 5: Putting It Into Code

      We have learned what commands we need to get our location, to send a get request, and we have created an online database to send our data to. The next step is creating code to automate the process of getting our location and sending up to the database.

      • Download the attached: FONA_Location.ino
      • Copy your APN, Public, and Private keys into the correct spots at the top of the code
      • Upload
      • Connect with CoolTerm and watch the Serial Monitor for any Errors. It should look like the attached image.
      • Make sure the HTTPACTION responds with a 200
      • Check your Sparkfun Datastream to see if the data appears.
      • I tried to make the code as legible as possible, there are lots of comments.

      At this point you should have the GSM module autonomously getting location and posting it to Sparkfun every 5 minutes, which means we are successful. You can now power your arduino with a battery and take it for a walk and see if it works!

      Step 6: Epilogue

      The other aspect we haven't talked about here is power consumption. It would be safe to assume that if you are building a cellular tracker, you will want to put it on something that moves, which will mean it will need to stray from the power outlet/usb ports of our world. This means battery power, which means measuring power consumption and calculating battery life, etc.

      Cellular stuff has a reputation for being pretty power hungry. Adafruit says this module can draw up to 2 Amps (!!) in short bursts. However, the way we are using it, it spends most of its time power off, consuming very little.

      I measured the power consumption from the Battery during normal use.

      • Powered on: ~ 25mA
      • Get Request: Peaks at 150-200mA for short durations
      • One full cycle of requesting location and sending a GET request consumes ~ 7.47 joules
        • avg of 53.4 mA for 35 Seconds
      • Powered off: ~150µA quiescent power consumption

      If we were using a battery with a capacity of 1000mAH, we could get send locations for ~ 10 days at 15 minute intervals before the battery gave out. (in theory, and not considering the power consumption of the Arduino)

      Note:

      I did notice some strange behavior when the battery was connected to the FONA and the Arduino was unplugged. There was between 20-100mA flowing out of the battery for no reason. I'm not sure why this is, but I would be cautious about leaving batteries plugged in to the FONA without it being controlled by a microcontroller.

      2 People Made This Project!

      Recommendations

      • Creative Misuse Contest

        Creative Misuse Contest
      • Fix It! Contest

        Fix It! Contest
      • Tiny Home Contest

        Tiny Home Contest

      50 Discussions

      I have assembled this project with a 2G sim card. It keeps hanging up on Disabled echo:

      Any ideas why?

      I'm going to have a go at this project but instead of using the sparkfun web data server I'm going to use my own website which conveniently runs PHP and MYSQL. Please wish me luck!

      11 replies

      Thanks! I have just got the FONA 3G working properly with Coolterm - had to give it 3ms delay between the characters to get it to ping google etc.

      Now to get your code working. I have every faith in your work ....

      The 3G fona is very tricky/dysfunctional. I went to 2G with great success. Let me know if you need more info on 2G solutions.

      Hi Kinasmith,

      Will this work with the 3G version of the Fona?

      This is my first Arduino project...

      According to Adafruit http on the 3G fona is "not natively supported":

      https://forums.adafruit.com/viewtopic.php?f=25&t=90792

      I ended up using the ideas in this code to create my own code. I did try this code but unfortunately I think that the GSM part of my FONA 800L is broken.

      I checked my board with cool term and no luck!

      I never actually really wanted to make a GSM tracker anyway - I wanted to build a weather station and the HTTP GET principle works very well on the 2G board. I did not use the sparkfun database as I needed much more in the way of data sorting facilities so used a database on my own website.

      Can you please send me the code to get this project working with 3g? I'm kinda struggling at the moment

      Kieran - you will struggle with the Fona 3G, but it does work. I think one other person got it to work over at the adafruit discussion forum. Go to the forum and search 'FONA 3G'. Also, try petitioning adafruit to develop the FONA library for the 3G version. They seem to think the 3G is not able to handle HTTP commands, but I think they are wrong - There's a big mistake/omission in the SIM6320 manual which leaves out all the HTTP commands.

      Let me know how you get on as I'd like to use it myself one day - I'm too lazy to actually write the code myself.

      hi kina,

      can we replaced adafruit FONA board with using GSM shield sim 800?...are coding will working with different board?...tq

      Dear Sir, thank for that code.

      I would like to understand why you used the command

      AT+CMGF=1

      That command is to set SMS system to text mode.
      Why is it useful to use that command in that context (Activating brearer profile to get gsm location)?

      I am get 408 as a response anyone seen that? I believe it means server busy, is anyone else trying just the talk and text plan via T-Mobile?

      I am get 408 as a response anyone seen that? I believe it means server busy, is anyone else trying just the talk and text plan via T-Mobile?

      Getting Location...: Get Location ERROR:

      HELLO, THANKS SO MUCH FOR YOUR WORK..

      i tried to implement this using sim808 lonet module.. although i removed all things gprs.. since i only want the gps detaild printed out only,, but i keep getting this error on the serial monitor

      0
      None
      GillH1

      2 years ago

      In step three I had to use a few more commands to set up the GPRS.

      AT+CIPSHUT //deactivates GPRS PDP content

      SHUT OK

      AT+CGATT=1

      OK

      AT+SAPBR=3,1,"CONTYPE","GPRS"

      OK

      AT+SAPBR=3,1,"APN","FONAnet"

      OK

      AT+CSTT="FONAnet" //starts task and sets APN

      OK

      AT+SAPBR=1,1

      OK

      AT+CIICR //brings up wireless connection with GPRS

      OK

      Moving on but this may require editing the original sketch. I'm guessing these new protocals were implemented after the original sketch was written but I'm not sure