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.

      Comments

      author
      peckerar (author)2017-05-16

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

      Any ideas why?

      author
      Tecwyn Twmffat (author)2016-02-22

      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!

      author
      kinasmith (author)Tecwyn Twmffat2016-02-22

      GOOD LUCK

      author
      Tecwyn Twmffat (author)kinasmith2016-02-24

      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 ....

      author

      I´m wondering if you can give me the code? I´m struggling a bit.

      author

      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.

      author
      KieranD7 (author)kinasmith2016-04-07

      Hi Kinasmith,

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

      This is my first Arduino project...

      author
      Tecwyn Twmffat (author)KieranD72016-04-07

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

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

      author
      KieranD7 (author)Tecwyn Twmffat2016-04-07

      Did you have any luck getting the code to work?

      author
      Tecwyn Twmffat (author)KieranD72016-04-08

      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.

      author
      KieranD7 (author)Tecwyn Twmffat2016-05-13

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

      author
      Tecwyn Twmffat (author)KieranD72016-05-13

      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.

      author
      KieranD7 (author)Tecwyn Twmffat2016-04-07

      Would it work with Sparkfun web data server?

      author
      FirdausS7 (author)2016-10-04

      hi kina,

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

      author
      pierrot1010 (author)2016-08-23

      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)?

      author
      GillH1 made it! (author)2016-04-25

      Okay,

      To make this work for my FONA 808 Shield I had to modify the original sketch quite a bit. The PIN Out had to be changed a little and there is not need to turn the FONA on and off with the shield so I got rid of a lot of that. Added a few commands in the GPRS setup. I added a few delays to get the serial output to be clean. Also I had to add a 10sec delay to the getLocation function otherwise it would return an error. Runs like a champ for me now.

      One thing to note. In the getLocation sub routine. The data is broken down into substrings but char position. I had to change this for my area. The Lat and Lon results will vary depending on location. It could start with 3 digits, or a - sign.

      for example Kina's location was:

      +CIPGSMLOC: 0,-73.974037,40.646976,2015/02/16,21:05:11

      mine was

      +CIPGSMLOC: 0,34.403347,-103.33228,2016/04/26,03:45:54

      so our longitudes started at different locations in the string. Kina's Lat has 10 chars, mine has 10. Kina's Lon has 9 Chars mine has 10.

      I think a better way to do this would be to break the string down where the commas are. I'm new to this and will try to figure it out.

      Also, the cell towers in my current location are few and far between. I'm going to see if I can get a more accurate fix using the GPS location instead.

      Thanks for the nice piece of code Kina!

      099f6ad3-11bd-4a37-a7c3-eeeff8309e61.jpg
      author
      kinasmith (author)GillH12016-04-25

      Nice work! I'm glad you could get it to work with different hardware and happy my code helped a little in that process.

      author
      kinasmith (author)kinasmith2016-05-12

      I use Black Wireless. They have pretty cheap prepaid plans and I use them for my phone plan too, so it makes it easy.

      author
      AaronH92 (author)GillH12016-05-12

      Awesome work! Also thank you Kina!

      What data plan are you using? And who is your provided? I tried adding delays like you mentioned and I also followed all the changes you mentioned but I am getting server time out. +CIPGSMLOC: 408

      I feel like it might be due to only using the $3 dollar a month talk and text via T-mobile. That plan doesn't include data.

      author
      kinasmith (author)AaronH922016-05-12

      I was using a prepaid plan which included data. You will need data access to the network. Talk/Text only plans won't work.

      author
      AaronH92 (author)kinasmith2016-05-12

      Hi Kina, who did you go through? Just T-Mobile or did you find a cheaper provider?

      author
      AaronH92 (author)kinasmith2016-05-12

      Hi Kina, who did you go through? Just T-Mobile or did you find a cheaper provider?

      author
      AaronH92 (author)kinasmith2016-05-12

      Hi Kina, who did you go through? Just T-Mobile or did you find a cheaper provider?

      author
      AaronH92 (author)kinasmith2016-05-12

      Hi Kina, who did you go through? Just T-Mobile or did you find a cheaper provider?

      author
      GillH1 (author)GillH12016-04-25

      The edited sketch

      FONO_808_Location_Tracker_edit3.ino
      author
      AaronH92 (author)2016-05-11

      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?

      author
      AaronH92 (author)2016-05-11

      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?

      author
      abrahamameh123 (author)2016-05-02

      Getting Location...: Get Location ERROR:

      author
      abrahamameh123 (author)2016-05-02

      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

      author
      GillH1 made it! (author)2016-04-27

      I got this to work with the GPS. I added a sub routine to turn on the GPS and changed the getLocation sub routine to retrieve location data from the GPS. Its very accurate. It can take about 30mins for the GPS to get a good lock. Mine usually locks in less than 5. I also edited the getLocation to break the content string down by commas. Now if the tag travels from say Canada to the tip of South America, the lat and lon fields will always be correct even with the shifting length of those strings. Sketch is included below.

      Thanks again Kina! Never would have done this without your original code.

      mat is

      FONO_808_Location_Tracker_GPS_2.2.ino
      author
      GillH1 (author)2016-04-24

      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

      author
      Arbot (author)2016-04-11

      I tried each and every step and it worked but the last and the final step gave me the following reports. My sim is unlocked too, so is there anything that I should do to get this working.

      ........Started setup

      Setup DONE!

      FONA Already On, Did Nothing

      Initializing: please wait 10 sec...

      Disable echo:

      Set to TEXT Mode: OK

      Attach GPRS:

      Set Connection Type To GPRS: OK

      Set APN:

      GPRS Already on

      ERROR:

      FONA Already On, Did Nothing

      Initializing: please wait 10 sec...

      Disable echo: OKOKERRORERROR

      Set to TEXT Mode:

      Attach GPRS: OK

      Set Connection Type To GPRS:

      Set APN: OK.

      GPRS Already on

      ERROR: OK

      FONA Already On, Did Nothing

      Initializing: please wait 10 s

      author
      Tecwyn Twmffat (author)2016-04-07

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

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

      author
      Fezder (author)2016-02-16

      Thanks mate, this helped, at first I had putted coordinates as in reverse hehe...

      author
      NorahH1 (author)2016-02-12

      Hi kinasmith

      first of all thanks for sharing this with us =)

      i went through each step and they worked fine with no errors, except for the final step where the following output appears in the Serial Monitor :

      .............Started setup

      Setup DONE!

      FONA Already On, Did Nothing

      Initializing: please wait 10 sec...

      Disable echo:

      Set to TEXT Mode: OK

      Attach GPRS:

      Set Connection Type To GPRS: OK

      Set APN:

      GPRS Already on

      ERROR:

      FONA Already On, Did Nothing

      Initializing: please wait 10 sec...

      Disable echo: OKOKOK

      Set to TEXT Mode:

      Attach GPRS: OK

      Set Connection Type To GPRS:

      Set APN: OK.

      GPRS Already on

      ERROR: OK

      FONA Already On, Did Nothing

      Initializing: please wait 10 sec...

      Disable echo: OKOKERROR

      Set to TEXT Mode:

      Attach GPRS: OK

      Set Connection Type To GPRS:

      Set APN: OK

      GPRS Already on

      ERROR: OK

      author
      kinasmith (author)NorahH12016-02-12

      I can't tell too much from the Serial print output. Did you try inputing each command in Cool-Term like I described above? Does each one work? Make sure your APN is correct. And make sure your service provider isn't blocking your device from connecting to the network, some carriers don't like unapproved devices on their networks.

      author
      NorahH1 (author)kinasmith2016-02-13

      Yes i did input each command in cool-term and no errors has appeared to me except the last step. and the carrier is not blocking my device. so what do you think the problem is ?

      author
      kinasmith (author)NorahH12016-02-13

      Which step is the "last step"? Which command is giving you an error? what is the error? Have you looked it up in the Application Notes? What cellular service are you using? Are you using the FONA board? What revision of it? What country are you in? Have you tried other commands or capabilities of the FONA? Can you send/receive SMS? Can you make a GET request? What is your signal strength?

      author
      NorahH1 (author)kinasmith2016-02-13

      What i mean by the last step is the fifth step of your instructable .. All the commands in the first , second and third step worked fine with me i got all the responses Ok and the GPS long and lat. I am from Kuwait and using Ooredoo GPRS service (APN is action.ooredoo.com) with FONA 808 board and have tried making a phone call and send/receive sms messages and it worked like a charm i also tried establishing a UDP connection using the tcp applications command sheet and it also worked ! Well i had a problem when making a GET request after intitating the http and entering the command at+httppara"cid",1 it used to respond with at+httpara=b instead of OK i tried setting the url first ! then the http parameters and i think it worked since at+httppara"cid",1 responded with OK after that. Then i had and error in the serial monitor after uploading Fona_location.ino file to the Arduino. What should i do to make it work and send the location to the sparkfun data stream that I created ? I really appreciate that you're trying to help me Thank you so much !

      author
      kinasmith (author)NorahH12016-02-13

      That all seems right. Can you make the post to the sparkfun database using cool-term? If yes, try checking the code to make sure there are no typos or anything and make sure all the commands are right. Make sure all the commands work in Cool-Term, then double check that each command works with the FONA in code. Beyond that, I'm not sure how much I can help. Cellular stuff is a pain to work with. Happy debugging!

      author
      NorahH1 (author)NorahH12016-02-12

      please advise and thank you

      author
      RenzoS2 (author)2015-11-01

      Hi I get "+CME ERROR: operation not allowed" after I key in AT+SAPBR=1,1. What do I do?

      author
      AzreenA1 (author)2015-10-29

      Hi. How to make a vehicle tracker system by using lonet sin808 and arduino uno? Can u help me?

      author
      AzreenA1 (author)AzreenA12015-10-29

      Sorry. Lonet sim808.

      author
      MichaelK75 (author)2015-09-28

      What’s the accuracy for the location? I’ve read something about ±200m using cell phone estimation only.

      author
      kinasmith (author)MichaelK752015-09-28

      If accuracy was important, I would use a real GPS.

      author
      MichaelK75 (author)kinasmith2015-09-28

      Yes of course, but GPS doesn’t work in buildings and requires additional hard- and software. So I’m wondering how the SIM800 estimates position and how accurate it is. What are your experiences? Are we talking about a few hundred meters or kilometers?

      author
      kinasmith (author)MichaelK752015-09-28

      My impression is that this command I'm using above gives you the GPS location of the cell tower you are connected to, not your "actual" location. Which would make accuracy contingent on the density of cell towers in the area. I have not done extensive testing on this.

      author
      maspinall (author)2015-09-25

      got a signal and i got internet access tuned out it was the mobile provider they don't allow data over anything apart from phones. anyway after a lot of complaining. i managed to get them to allow data on my arduino device. im nearly there but i cant get the getLocation() to work. it just returns an Error 0. and i know it has a GPS location as i tried the (sendATCommand("AT+CIPGSMLOC=1,1")).... is there any way of capturing the string returned from that command straight from the sendATcommand as your way dont seem to work on the LoNet

      author
      kinasmith (author)maspinall2015-09-28

      See Step #3. You should be doing all of this through a Serial Terminal to make sure everything works, read the errors, etc. before you try to implement it in code. In the Serial Terminal, send the command, you'll see the response. Again, reference step #3.

      About This Instructable

      76,588views

      284favorites

      License:

      More by kinasmith:How to Mill a circuit board with a Stepcraft Mill and EagleCADMake a Tool Length sensor for Stepcraft 2 CNCCompost Sensor
      Add instructable to: