I'm working on a project using an A9G module with an ESP32 to send GPS location data to a remote HTTPS server. However, I'm encountering several issues:
- Incorrect Responses: The server response logs show incorrect or unexpected data.
- Missing SIM Info: There's no output showing SIM information.
- No Requests Visible on Server: The server doesn't seem to receive any requests.
Code:
#include <HardwareSerial.h>
#include <WiFi.h>
#include <time.h>
// Define the serial interfaces for ESP32 and A9G
#define MODEM_TX 17 // TX pin for A9G
#define MODEM_RX 16 // RX pin for A9G
// Configure the modem
#define SerialMon Serial
HardwareSerial SerialAT(1); // Use hardware serial 1
// Variables to store GPS data
String latitude = "";
String longitude = "";
String lastUpdated = "";
// Server URL to send GPS data
const char* serverUrl = "https://test.dev/api/test";
void setup() {
// Set console baud rate
SerialMon.begin(115200);
delay(10);
// Set GSM module baud rate
SerialAT.begin(115200, SERIAL_8N1, MODEM_RX, MODEM_TX);
delay(3000);
// Initialize GPS and GPRS
if (!initializeGPRS()) {
// GPRS setup failed
SerialMon.println("GPRS not set up correctly.");
}
initializeGPS();
// Initialize time
configTime(0, 0, "pool.ntp.org");
}
void loop() {
// Get GPS data from A9G
getGPSData();
// Send GPS data to the server if GPS data is valid
if (!latitude.isEmpty() && !longitude.isEmpty()) {
sendGPSDataToServer();
}
// Wait for 5 seconds before updating the next data
delay(5000);
}
void initializeGPS() {
SerialMon.println("Initializing GPS...");
SerialAT.println("AT+GPS=1"); // Turn on GPS
delay(1000);
SerialAT.println("AT+GPSRD=1"); // Start continuous GPS read
delay(1000);
SerialMon.println("GPS Initialized.");
}
bool initializeGPRS() {
SerialMon.println("Initializing GPRS...");
// Attach to GPRS
SerialAT.println("AT+CGATT=1");
delay(1000);
// Set APN
SerialAT.println("AT+CGDCONT=1,\"IP\",\"www\"");
delay(1000);
// Activate PDP context
SerialAT.println("AT+CGACT=1,1");
delay(3000);
// Print GPRS connection status
printGPRSStatus();
// Open GPRS context
SerialAT.println("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"");
delay(1000);
SerialAT.println("AT+SAPBR=3,1,\"APN\",\"www\"");
delay(1000);
SerialAT.println("AT+SAPBR=1,1");
delay(3000);
// Ping to test GPRS
SerialMon.println("Pinging google.com...");
SerialAT.println("AT+HTTPGET=\"https://google.com\"");
clearSerialBuffer();
delay(10000); // Wait for ping response
while (SerialAT.available()) {
String response = SerialAT.readStringUntil('\n');
SerialMon.println("Ping Response: " + response); // Print ping response
if (response.indexOf("+") != -1) {
// Ping successful
break;
} else if (response.indexOf("ERROR") != -1) {
// Ping failed
SerialMon.println("GPRS not set up correctly.");
return false;
}
}
SerialMon.println("GPRS Initialized.");
return true;
}
void getGPSData() {
// Send command to read GPS data
SerialAT.println("AT+GPSRD?");
delay(2000); // Wait for response
// Read the response
while (SerialAT.available()) {
String line = SerialAT.readStringUntil('\n');
if (line.indexOf("$GNGGA") != -1) {
// Parse the GPS data
int startIndex = line.indexOf("$GNGGA") + 7;
int endIndex = line.indexOf(",", startIndex);
String time = line.substring(startIndex, endIndex);
// Latitude
startIndex = endIndex + 1;
endIndex = line.indexOf(",", startIndex);
latitude = convertToDecimal(line.substring(startIndex, endIndex), true);
// Skip N/S indicator
startIndex = endIndex + 3;
endIndex = line.indexOf(",", startIndex);
longitude = convertToDecimal(line.substring(startIndex, endIndex), false);
// Print GPS data
SerialMon.println("Latitude: " + latitude);
SerialMon.println("Longitude: " + longitude);
// Update last updated time
updateLastUpdated();
break; // Exit the loop after reading valid GPS data
}
}
}
void printGPRSStatus() {
SerialAT.println("AT+SAPBR=2,1");
delay(1000);
while (SerialAT.available()) {
String response = SerialAT.readStringUntil('\n');
if (response.indexOf("+SAPBR:") != -1) {
SerialMon.println("GPRS Status: " + response);
break;
}
}
}
String convertToDecimal(String coordinate, bool isLatitude) {
if (isLatitude) {
int degrees = coordinate.substring(0, 2).toInt();
float minutes = coordinate.substring(2).toFloat();
float decimal = degrees + (minutes / 60.0);
return String(decimal, 6);
} else {
int degrees = coordinate.substring(0, 3).toInt();
float minutes = coordinate.substring(3).toFloat();
float decimal = degrees + (minutes / 60.0);
return String(decimal, 6);
}
}
void updateLastUpdated() {
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
lastUpdated = "Failed to obtain time";
return;
}
char buffer[32];
strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", &timeinfo);
lastUpdated = String(buffer);
}
void sendGPSDataToServer() {
String postData = "latitude=" + latitude + "&longitude=" + longitude + "&lastUpdated=" + lastUpdated;
SerialAT.println("AT+HTTPPARA=\"CID\",1"); // Set PDP context ID
delay(1000);
SerialAT.println("AT+HTTPPARA=\"CONTENT\",\"application/x-www-form-urlencoded\""); // Set content type
delay(1000);
SerialAT.println("AT+HTTPPARA=\"URL\",\"" + String(serverUrl) + "\""); // Set URL
delay(1000);
SerialAT.println("AT+HTTPDATA=" + String(postData.length()) + ",10000"); // Set data length and timeout
delay(1000);
SerialAT.println(postData); // Send POST data
delay(1000);
clearSerialBuffer();
SerialAT.println("AT+HTTPPOST"); // Perform POST action
delay(1000);
// Wait for response
while (SerialAT.available()) {
String response = SerialAT.readStringUntil('\n');
SerialMon.println("Server Response: " + response);
}
// End HTTP service
SerialAT.println("AT+HTTPTERM");
// Log when a request is sent
SerialMon.println("Sent GPS data to server.");
delay(5000); // Wait for next data update
}
void clearSerialBuffer() {
while (SerialAT.available()) {
SerialAT.read(); // Clear the buffer
}
}
Logs:
Initializing GPRS...
Pinging google.com...
Ping Response: AT+HTTPGET="https://google.com"
GPRS Initialized.
Initializing GPS...
GPS Initialized.
Latitude: **********
Longitude: **********
Server Response: +GPSRD:$GNGGA, ............................................................
Server Response: $GPGSA,A,1,,,,,,,,,,,,,,,*1E
Server Response: $BDGSA,A,1,,,,,,,,,,,,,,,*0F
Server Response: $GPGSV,1,1,01,07,,,22*7F
Server Response: $BDGSV,1,1,00*68
Server Response: $GNRMC,............................................................
Server Response: $GNVTG,0.00,T,,M,0.000,N,0.000,K,N*2C
Server Response: +GPSRD:$GNGGA,............................................................
Server Response: $GPGSA,A,1,,,,,,,,,,,,,,,*1E
Server Response: $BDGSA,A,1,,,,,,,,,,,,,,,*0F
Server Response: $GPGSV,1,1,01,07,,,22*7F
Server Response: $BDGSV,1,1,00*68
Server Response: $GNRMC,19............................................................
Server Response: $GNVTG,............................................................
Sent GPS data to server.
Could anyone help diagnose and resolve the issues with incorrect server responses, missing SIM information display, and the absence of visible requests on the server? Any insights or suggestions would be greatly appreciated.