Arduino projects

GPS tracker using A9G GPRS/GPS module

GPS tracker using A9G GPRS/GPS module

In this article, we will make a GPS tracker using the A9G GPRS/GPS module and Arduino development board. The A9G module GPS tracker based on Maduino is a product based on IoT (Internet of Things) solutions that integrates the microcontroller ATSAMD21G18 with the best power management and storage functions, and the GRRS/GSM+GPS module A9G.

A GPS Tracker is a navigation device, usually carried by a moving vehicle or person, that uses the Global Positioning System (GPS) to track the device’s movement and determine its location. Recorded location data can be stored within the tracking unit or transmitted to an internet-connected device using a cellular (GPRS or SMS) unit. This allows the location to be displayed on a map in real time, or while the track is being analyzed using GPS tracking software.

AT command to obtain GPS data

GPS tracker using A9G GPRS/GPS module

To turn GPS on/off you can send the following commands.

  1. AT+GPS=1 //Turn ON GPS
  2. AT+GPS=0 //Turn OFF GPS

Copy code

The following AT command is used to read NEMA information every 10 seconds.

  1. AT+GPSRD=10

Copy code

To stop retrieving NEMA information every 10 seconds, you can send the following command using.

  1. AT+GPSRD=0

Copy code

To get the GPS information of latitude and longitude you can send the following command using.

  1. AT+GPSRD=0

Copy code

How to obtain GPS location information?

Copy the following code and upload it to the Maduino A9G development board using the Arduino IDE. After uploading the code, the A9G GPS tracker is ready. Complete code:

#include <stdio.h>
#include <string.h>

#define DEBUG true
int PWR_KEY = 9;
int RST_KEY = 6;
int LOW_PWR_KEY = 5;

bool ModuleState=false;
unsigned long timeCount;

void setup()
{
pinMode(PWR_KEY, OUTPUT);
pinMode(RST_KEY, OUTPUT);
pinMode(LOW_PWR_KEY, OUTPUT);
digitalWrite(RST_KEY, LOW);
digitalWrite(LOW_PWR_KEY, HIGH);
digitalWrite(PWR_KEY, HIGH);

SerialUSB.begin(115200);
while (!SerialUSB)
{
; // wait for serial port to connect
}
Serial1.begin(115200);
digitalWrite(PWR_KEY, LOW);
delay(3000);
digitalWrite(PWR_KEY, HIGH);
delay(5000);
ModuleState=moduleStateCheck();
if(ModuleState==false)//if it’s off, turn on it.
{
digitalWrite(PWR_KEY, LOW);
delay(3000);
digitalWrite(PWR_KEY, HIGH);
delay(5000);
SerialUSB.println(“Now turnning the A9/A9G on.”);
}

//GPS test
sendData(“AT+GPS=1”, 1000, DEBUG);//1: turn on GPS 0:Turn off GPS
sendData(“AT+GPSRD=10”, 1000, DEBUG);//Read NEMA information every 10 seconds

sendData(“AT+LOCATION=2”, 1000, DEBUG);//AT+LOCATION=X //1:Get base address, 2:get gps address

//sendData(“AT+CCID”, 3000, DEBUG);
//sendData(“AT+CREG?”, 3000, DEBUG);
//sendData(“AT+CGATT=1”, 1000, DEBUG);
//sendData(“AT+CGACT=1,1”, 1000, DEBUG);
//sendData(“AT+CGDCONT=1,\”IP\”,\”CMNET\””, 1000, DEBUG);

//sendData(“AT+CIPSTART=\”TCP\”,\”www.mirocast.com\”,80″, 2000, DEBUG);
timeCount=millis();
SerialUSB.println(“Maduino A9/A9G GPS Test Begin!”);
}

void loop()
{

if(millis()-timeCount>5000)
{
sendData(“AT+LOCATION=2”, 1000, DEBUG);
timeCount=millis();//refresh
}
while (Serial1.available() > 0) {
SerialUSB.write(Serial1.read());
yield();
}
while (SerialUSB.available() > 0) {
Serial1.write(SerialUSB.read());
yield();
}
}

bool moduleStateCheck()
{
int i = 0;
bool state=false;
for (i = 0; i < 10; i++)
{
String msg = String(“”);
msg = sendData(“AT”, 1000, DEBUG);
if (msg.indexOf(“OK”) >= 0)
{
SerialUSB.println(“A9/A9G Module had turned on.”);
state=true;
return state;
}
delay(500);
}
return state;
}

String sendData(String command, const int timeout, boolean debug)
{
String response = “”;
Serial1.println(command);
long int time = millis();
while ((time + timeout) > millis())
{
while (Serial1.available())
{
char c = Serial1.read();
response += c;
}
}
if (debug)
{
SerialUSB.print(response);
}
return response;
}

After uploading the code, open the serial monitor and set the baud rate to 115200. The module takes some time to retrieve data from the satellite. Soon the serial monitor will display NEMA information as well as latitude and longitude coordinates.

GPS tracker using A9G GPRS/GPS module

We can show our specific location in https://www.gps-coordinates.net/ :

GPS tracker using A9G GPRS/GPS module

Make an A9G GPS tracker with OLED display

Now let us add an OLED display to the development board so that we can retrieve GPS information on the OLED display. We need two libraries for OLED displays.

1. Adafruit GFX Library: https://github.com/adafruit/Adafruit-GFX-Library

2. SSD130 OLED library: https://github.com/adafruit/Adafruit_SSD1306

Now copy the following code and upload it to the Maduino A9G development board. Complete code:

#include <stdio.h>
#include <string.h>

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

void(* resetFunc) (void) = 0; //declare reset function at address 0

#define DEBUG true
int PWR_KEY = 9;
int RST_KEY = 6;
int LOW_PWR_KEY = 5;

bool ModuleState=false;
unsigned long timeCount;

void setup()
{
pinMode(PWR_KEY, OUTPUT);
pinMode(RST_KEY, OUTPUT);
pinMode(LOW_PWR_KEY, OUTPUT);
digitalWrite(RST_KEY, LOW);
digitalWrite(LOW_PWR_KEY, HIGH);
digitalWrite(PWR_KEY, HIGH);

SerialUSB.begin(115200);
//while (!SerialUSB)
{
; // wait for serial port to connect
}
Serial1.begin(115200);
digitalWrite(PWR_KEY, LOW);
delay(3000);
digitalWrite(PWR_KEY, HIGH);
delay(5000);
ModuleState=moduleStateCheck();
if(ModuleState==false)//if it’s off, turn on it.
{
digitalWrite(PWR_KEY, LOW);
delay(3000);
digitalWrite(PWR_KEY, HIGH);
delay(5000);
SerialUSB.println(“Now turnning the A9/A9G on.”);
}

//GPS test
sendData(“AT+GPS=1”, 1000, DEBUG);//1: turn on GPS 0:Turn off GPS
sendData(“AT+GPSRD=10”, 1000, DEBUG);//Read NEMA information every 10 seconds

sendData(“AT+LOCATION=2”, 1000, DEBUG);//AT+LOCATION=X //1:Get base address, 2:get gps address

if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128×64
Serial.println(F(“SSD1306 allocation failed”));
for (;;)
{
SerialUSB.println(“Now reset the maduino zero”);
delay(1000);
resetFunc();//restart
delay(20);
}
}
delay(2000);
display.clearDisplay();

display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 10);
//Display static text
display.println(“how2electronics.com!”);
display.println(“Maduino Zero A9/A9G GPS Tracker!”);
display.display();
delay(2000);

timeCount=millis();
SerialUSB.println(“Maduino A9/A9G GPS Test Begin!”);
display.clearDisplay();
}

void loop()
{

if(millis()-timeCount>5000)
{
sendData(“AT+LOCATION=2”, 1000, DEBUG);
timeCount=millis();//refresh
}
while (Serial1.available() > 0)
{
display.clearDisplay();
String cstring = Serial1.readString();
SerialUSB.print(cstring);//SerialUSB.write(Serial1.read());
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println(cstring);
display.display();
yield();
}
while (SerialUSB.available() > 0) {
Serial1.write(SerialUSB.read());
yield();
}
}

bool moduleStateCheck()
{
int i = 0;
bool state=false;
for (i = 0; i < 10; i++)
{
String msg = String(“”);
msg = sendData(“AT”, 1000, DEBUG);
if (msg.indexOf(“OK”) >= 0)
{
SerialUSB.println(“A9/A9G Module had turned on.”);
state=true;
return state;
}
delay(500);
}
return state;
}

String sendData(String command, const int timeout, boolean debug)
{
String response = “”;
Serial1.println(command);
long int time = millis();
while ((time + timeout) > millis())
{
while (Serial1.available())
{
char c = Serial1.read();
response += c;
}
}
if (debug)
{
SerialUSB.print(response);
}
return response;
}

 After the code is uploaded, it will take some time for the A9G GSM/GPRS+GPS module to obtain data. Once the data is received, the information will be displayed on the OLED display.

GPS tracker using A9G GPRS/GPS module

The above is all about making a GPS tracker using the A9G GPRS/GPS module and Arduino development board. If you have any questions, please feel free to reply below this post.

Source: https://www.yiboard.com/thread-1747-1-1.html

Back to top button