General

DFRobot GPS/GPRS/GSM Module V3.0 working example

userHead N4rf 2013-08-29 11:32:48 52677 Views39 Replies

Ok, Ive been working on a simple code to stream GPS data to a webserver and then be able to locate it with Google Maps API.

here is a WORKING example of what Ive done so far. Before you begin, Im open to any suggestions and improvements. Again, 
ITS A SIMPLE CODE, It uses delays (because I know what the ATcommand answer will be) instead of interpreting the module answer.

The only time it reads data, is when decoding GPS stream into Lat/Long strings.

Im using GPS/GPRS/GSM Module V3.0 and Arduino UNO.


ANyways... here is the arduino code:
 

Code: Select all char aux_str[30]; char aux; char latitude[15]; char longitude[15]; char inChar;  //used to decode gps int index; char inData[200]; void setup() {   //Init the driver pins for GSM function    pinMode(3,OUTPUT);    pinMode(4,OUTPUT);    pinMode(5,OUTPUT);   //Output GSM Timing    digitalWrite(5,HIGH);    delay(1500);    digitalWrite(5,LOW);        Serial.begin(9600);        // Use these commands instead of the hardware switch 'UART select' in order to enable each mode    // If you want to use both GMS and GPS. enable the required one in your code and disable the other one for each access.    digitalWrite(3,LOW);//enable GSM TX?RX    digitalWrite(4,HIGH);//disable GPS TX?RX        delay(20000); //wait 20secs for module to be ready        start_GSM(); //initializes GSM/GPRS configs        delay(5000);        start_GPS();  //initializates GPS       } void loop()     {           read_GPS();  //acquire actualLat/Long   delay(2000);   send_GPRS(); //send actual Lat/Long to desired url   delay(30000); } void start_GSM(){     //Configuracion GPRS Claro Argentina    Serial.println("AT");    delay(2000);    Serial.println("AT CREG?");    delay(2000);    Serial.println("AT SAPBR=3,1,"APN","igprs.claro.com.ar"");  //replace with you mobile network APN    delay(2000);    Serial.println("AT SAPBR=3,1,"USER","clarogprs""); //replace with your  mobile network USER    delay(2000);    Serial.println("AT SAPBR=3,1,"PWD","clarogprs999""); //replace with your mobile password    delay(2000);    Serial.println("AT SAPBR=3,1,"Contype","GPRS"");  //config connection as GPRS    delay(2000);    Serial.println("AT SAPBR=1,1");  //set it    delay(10000);    Serial.println("AT HTTPINIT"); //initiate HTTP    delay(2000);    Serial.println("AT HTTPPARA="CID",1"); //mobile network CID    delay(2000); } void send_GPRS(){  //here we send the actual Lat/long          /*I adapted a PHP template to receive the lat/long data             if there is latitude=XXXXX&longitude=XXXX on header, PHP will write info into a TXT file             If not, then, it will read TXT file and place a mark on a google map API          **/          Serial.print("AT HTTPPARA="URL","YOU.URL.COM/geo/geo2_0.php?latitude=");          Serial.print(latitude);          Serial.print("&longitude=");          Serial.print(longitude);          Serial.println(""");          delay(2000);          Serial.println("AT HTTPACTION=0"); //now GET action          delay(2000);                     } void start_GPS(){     //Configuracion en Inicializacion GPS    Serial.print("AT");    delay(1000);    Serial.println("AT CGPSIPR=9600");// (set the baud rate)    delay(1000);    Serial.println("AT CGPSPWR=1"); // ?turn on GPS power supply?    delay(1000);    Serial.println("AT CGPSRST=1"); //?reset GPS in autonomy mode?    delay(120000); //delay long enough to assure GPS gets date, otherwise, location will be 0/0 } void read_GPS(){            Serial.println("AT CGPSINF=0");  //ask SIM908 GPS info        read_String();  //read and store serial answer        strtok(inData, ","); //decode string into comma separated values    strcpy(longitude,strtok(NULL, ",")); // Gets longitude    strcpy(latitude,strtok(NULL, ",")); // Gets latitude            convert2Degrees(latitude);  //fix value    convert2Degrees(longitude); //fix value         } void read_String() {      index=0;            while(Serial.available() > 0) // Don't read unless                                                  // there you know there is data   {       if(index < 199) // One less than the size of the array       {           inChar = Serial.read(); // Read a character           inData[index] = inChar; // Store it           index  ; // Increment where to write next           inData[index] = ''; // Null terminate the string       }   } } int8_t convert2Degrees(char* input){    float deg;    float minutes;    boolean neg = false;        //auxiliar variable    char aux[10];    if (input[0] == '-')    {        neg = true;        strcpy(aux, strtok(input 1, "."));    }    else    {        strcpy(aux, strtok(input, "."));    }    // convert string to integer and add it to final float variable    deg = atof(aux);    strcpy(aux, strtok(NULL, ''));    minutes=atof(aux);    minutes/=1000000;    if (deg < 100)    {        minutes  = deg;        deg = 0;    }    else    {        minutes  = int(deg) % 100;        deg = int(deg) / 100;        }    // add minutes to degrees    deg=deg minutes/60;    if (neg == true)    {        deg*=-1.0;    }    neg = false;    if( deg < 0 ){        neg = true;        deg*=-1;    }        float numeroFloat=deg;    int parteEntera[10];    int cifra;    long numero=(long)numeroFloat;      int size=0;        while(1){        size=size 1;        cifra=numero;        numero=numero/10;        parteEntera[size-1]=cifra;        if (numero==0){            break;        }    }      int indice=0;    if( neg ){        indice  ;        input[0]='-';    }    for (int i=size-1; i >= 0; i--)    {        input[indice]=parteEntera[i] '0';        indice  ;    }    input[indice]='.';    indice  ;    numeroFloat=(numeroFloat-(int)numeroFloat);    for (int i=1; i<=6 ; i  )    {        numeroFloat=numeroFloat*10;        cifra= (long)numeroFloat;                  numeroFloat=numeroFloat-cifra;        input[indice]=char(cifra) 48;        indice  ;    }    input[indice]=''; }


Ok, so far, that's the Arduino Code.

And here is the PHP that receives and shows the information, Its arranged by Date, because eventually i want to be able to select a date and see where Ive been. NOTE: you need jquery-1.10.1.min.js within PHP file for it to properly work.

Here is the .js file:
http://www.mediafire.com/?hfeieb260bg89vj

DFrobot wont allow me to attach this file. 


 

Code: Select all<!--   This Script is to store GPS strings over header as Latitude & Longitude store them into a TXT file under the name of the day. If no Lat&Long information on the header, reads the TXT file according to the date, and after that post them into a Google Maps V3 Api map. Author: N4rf - --> <?php    if (!empty($_GET['latitude']) && !empty($_GET['longitude'])) {        function getParameter($par, $default = null){            if (isset($_GET[$par]) && strlen($_GET[$par])) return $_GET[$par];            elseif (isset($_POST[$par]) && strlen($_POST[$par]))                return $_POST[$par];            else return $default;        }                 $day=date("d");   $month=date("m"); $year=date("y"); $fecha=$day.$month."20".$year; $filename=$fecha.'.txt';        $file = $filename;        $lat = getParameter("latitude");        $lon = getParameter("longitude");        $person = $lat.",".$lon."n";                echo "            DATA:n            Latitude: ".$lat."n            Longitude: ".$lon;        if (!file_put_contents($file, $person, FILE_APPEND | LOCK_EX))            echo "nt Error saving Datan";        else echo "nt Data Saven";    }    else { ?> <!DOCTYPE html> <html>     <head>    <!-- Load Jquery -->    <script language="JavaScript" type="text/javascript" src="jquery-1.10.1.min.js"></script>    <!-- Load Google Maps Api -->    <!-- IMPORTANT: change the API v3 key -->    <script src="http://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&sensor=false"></script>    <!-- Initialize Map and markers -->    <script type="text/javascript">        var myCenter=new google.maps.LatLng(-31.40306,-64.19827);        var marker;        var map;        var mapProp;        function initialize()        {            mapProp = {              center:myCenter,              zoom:15,              mapTypeId:google.maps.MapTypeId.ROADMAP              };            setInterval('mark()',30000);        }        function mark()        {                 //adquirimos las fechas para generar el archivo del dia         var f = new Date();                         if(f.getDate() < 10 && (f.getMonth() 1)<10){         var fecha =("0" f.getDate() "0" (f.getMonth()  1)  f.getFullYear()); }else if(f.getDate() > 10 && (f.getMonth() 1)<10){         var fecha =(f.getDate() "0" (f.getMonth()  1)  f.getFullYear()); }else if(f.getDate() < 10 && (f.getMonth() 1)>10){         var fecha =("0" f.getDate() (f.getMonth()  1)  f.getFullYear()); }else if(f.getDate() > 10 && (f.getMonth() 1)>10){         var fecha =(f.getDate() (f.getMonth()  1)  f.getFullYear()); }            map=new google.maps.Map(document.getElementById("googleMap"),mapProp);            var file = fecha ".txt";            $.get(file, function(txt) {                var lines = txt.split("n");                for (var i=0;i<lines.length;i  ){                    console.log(lines[i]);                    var words=lines[i].split(",");                    if ((words[0]!="")&&(words[1]!=""))                    {                        marker=new google.maps.Marker({                              position:new google.maps.LatLng(words[0],words[1]),                              });                        marker.setMap(map);                        map.setCenter(new google.maps.LatLng(words[0],words[1]));                    }                }                marker.setAnimation(google.maps.Animation.BOUNCE);            });        }        google.maps.event.addDomListener(window, 'load', initialize);    </script> </head> <body> <body background="background.png">    <?php        echo '            <!-- Draw information table and Google Maps div -->        <div>            <center><br />                <b> Posicion Actual</b><br /><br />                <br /><br />                <div id="googleMap" style="width:800px;height:700px;"></div>            </center>        </div>';    ?> </body> </html> <?php } ?>


Im setting a dummy for you to try the php code with a Google APi Key of mine:

all you have to do is copy paste this into your browser adress and change XXX for numbres...

http://gpsdummy.net23.net/geo/geo.php?l ... itude=XXXX

example:

http://gpsdummy.net23.net/geo/geo.php?l ... =34.214234

and if you want to see the last location point, just go to

http://http://gpsdummy.net23.net/geo/geo.php


Again, its a very simple, yet working example, suggestions and improvements are welcome.


EDITED: Since quoting code is messed up (there are quite a few "/" missing that is missunderstood by forum plataform) Im attaching files.

Please, in order to work, remember to edit your mobile network APN, USER; and PWD.

and on PHP file, copy/pase your Google API key.

2015-04-22 05:51:21 you should use this link for the working code: https://github.com/DFRobot/GPS-GPRS-GSM ... geoLocator userHeadPic MaartenVandereet
2015-04-22 05:51:21 you should use this link for the working code: https://github.com/DFRobot/GPS-GPRS-GSM ... geoLocator userHeadPic MaartenVandereet
2015-04-22 05:51:21 you should use this link for the working code: https://github.com/DFRobot/GPS-GPRS-GSM ... geoLocator userHeadPic MaartenVandereet
2015-04-22 05:51:21 you should use this link for the working code: https://github.com/DFRobot/GPS-GPRS-GSM ... geoLocator userHeadPic MaartenVandereet
2015-04-21 22:07:55 hi to all, im working with a GPS/GPRS/GSM v 3.0, i test this sketch but cant compilate cause have to many error, someone can help me, sorry for mi bad english

Saludos
userHeadPic gustavocastellanos
2015-04-21 22:07:55 hi to all, im working with a GPS/GPRS/GSM v 3.0, i test this sketch but cant compilate cause have to many error, someone can help me, sorry for mi bad english

Saludos
userHeadPic gustavocastellanos
2015-04-21 22:07:55 hi to all, im working with a GPS/GPRS/GSM v 3.0, i test this sketch but cant compilate cause have to many error, someone can help me, sorry for mi bad english

Saludos
userHeadPic gustavocastellanos
2015-04-21 22:07:55 hi to all, im working with a GPS/GPRS/GSM v 3.0, i test this sketch but cant compilate cause have to many error, someone can help me, sorry for mi bad english

Saludos
userHeadPic gustavocastellanos
2015-04-03 06:38:13 Hi, i am trying to make this but it doesn't work. I've put the arduino code into my arduino and i'm getting gps data. But when i'm opening the php file it only shows an empty google maps. it doens't show any location. It only works when i put the gps data into the link of the php server en then it greates a new file. But it doens't do it automaticaly

Is my gprs not working or what?
please help me.

Thanks!
userHeadPic MaartenVandereet
2015-04-03 06:38:13 Hi, i am trying to make this but it doesn't work. I've put the arduino code into my arduino and i'm getting gps data. But when i'm opening the php file it only shows an empty google maps. it doens't show any location. It only works when i put the gps data into the link of the php server en then it greates a new file. But it doens't do it automaticaly

Is my gprs not working or what?
please help me.

Thanks!
userHeadPic MaartenVandereet
2015-04-03 06:38:13 Hi, i am trying to make this but it doesn't work. I've put the arduino code into my arduino and i'm getting gps data. But when i'm opening the php file it only shows an empty google maps. it doens't show any location. It only works when i put the gps data into the link of the php server en then it greates a new file. But it doens't do it automaticaly

Is my gprs not working or what?
please help me.

Thanks!
userHeadPic MaartenVandereet
2015-04-03 06:38:13 Hi, i am trying to make this but it doesn't work. I've put the arduino code into my arduino and i'm getting gps data. But when i'm opening the php file it only shows an empty google maps. it doens't show any location. It only works when i put the gps data into the link of the php server en then it greates a new file. But it doens't do it automaticaly

Is my gprs not working or what?
please help me.

Thanks!
userHeadPic MaartenVandereet
2015-01-12 07:54:14 Hi, Thanks for this wonderful work. Everything is working just fine on my PC bur if i move the server to raspberry PI, the http post it not working i get error saving data. Can anybody help me with that?
Thanks, Georgian.
userHeadPic cry4brk
2015-01-12 07:54:14 Hi, Thanks for this wonderful work. Everything is working just fine on my PC bur if i move the server to raspberry PI, the http post it not working i get error saving data. Can anybody help me with that?
Thanks, Georgian.
userHeadPic cry4brk
2015-01-12 07:54:14 Hi, Thanks for this wonderful work. Everything is working just fine on my PC bur if i move the server to raspberry PI, the http post it not working i get error saving data. Can anybody help me with that?
Thanks, Georgian.
userHeadPic cry4brk
2015-01-12 07:54:14 Hi, Thanks for this wonderful work. Everything is working just fine on my PC bur if i move the server to raspberry PI, the http post it not working i get error saving data. Can anybody help me with that?
Thanks, Georgian.
userHeadPic cry4brk
2014-11-22 04:45:47 Hi N4rf,

Thank you for the code,  is it possible to upload gps and sensor data to a data base like Thingspeak . com ??

i am trying to use a sensor with the gps and send the information to thingspeak website but i am having trouble if you know how to set up the shield to send data to a website like thingspeak it would be great.

Thank you again for your awesome work !!

userHeadPic samvivi7
2014-11-22 04:45:47 Hi N4rf,

Thank you for the code,  is it possible to upload gps and sensor data to a data base like Thingspeak . com ??

i am trying to use a sensor with the gps and send the information to thingspeak website but i am having trouble if you know how to set up the shield to send data to a website like thingspeak it would be great.

Thank you again for your awesome work !!

userHeadPic samvivi7
2014-11-22 04:45:47 Hi N4rf,

Thank you for the code,  is it possible to upload gps and sensor data to a data base like Thingspeak . com ??

i am trying to use a sensor with the gps and send the information to thingspeak website but i am having trouble if you know how to set up the shield to send data to a website like thingspeak it would be great.

Thank you again for your awesome work !!

userHeadPic samvivi7
2014-11-22 04:45:47 Hi N4rf,

Thank you for the code,  is it possible to upload gps and sensor data to a data base like Thingspeak . com ??

i am trying to use a sensor with the gps and send the information to thingspeak website but i am having trouble if you know how to set up the shield to send data to a website like thingspeak it would be great.

Thank you again for your awesome work !!

userHeadPic samvivi7