CAR_GPS_TRACKER/CAR_GPS_TRACKER.ino
2025-08-05 12:28:55 +10:00

123 lines
4.9 KiB
C++

#include <HardwareSerial.h> // Using HardwareSerial2 for better performance
// --- SIM7080G Pin Definitions ---
// Make sure these match your board's actual connections.
// If using the Waveshare example's pins 10,11 this would need SoftwareSerial.
// We are using HardwareSerial2 here (pins 16, 17) for efficiency if available.
#define LTE_RX_PIN 16 // ESP32 GPIO16 connected to SIM7080G TX
#define LTE_TX_PIN 17 // ESP32 GPIO17 connected to SIM7080G RX
#define PWRKEY_PIN 4 // ESP32 GPIO4 connected to SIM7080G PWRKEY (as before)
// --- Function Prototypes ---
// REVISED: powerUp function based on Waveshare example timing
void powerUp();
// REVISED: sendCommandAndPrintAllResponse - will simply print all modem output
String sendCommandAndPrintAllResponse(String command, unsigned long timeoutMs = 3000);
void setup() {
Serial.begin(115200); // For debugging output to PC
while (!Serial) {
delay(10); // Wait for Serial Monitor to connect
}
Serial.println("--- Starting SIM7080G Basic Communication Test ---");
Serial.println("Configuring Serial2 for LTE module at 115200 baud.");
// Initialize Serial2 for communication with the SIM7080G
Serial2.begin(115200, SERIAL_8N1, LTE_RX_PIN, LTE_TX_PIN);
// Set PWRKEY pin mode and ensure it's LOW initially as per Waveshare setup.
pinMode(PWRKEY_PIN, OUTPUT);
digitalWrite(PWRKEY_PIN, LOW); // Important to ensure a known initial state
delay(1000); // Small delay to stabilize
Serial.println("Attempting Modem Power Up sequence...");
powerUp(); // Call the revised powerUp sequence
Serial.println("Power Up sequence initiated. Waiting for modem to become responsive (up to 120 seconds)...");
unsigned long modemReadyStartTime = millis();
const unsigned long MODEM_READY_TIMEOUT = 120 * 1000; // Total 120 seconds timeout
// Loop to repeatedly send "AT" and check for any response
Serial.println("Repeatedly sending 'AT' to check for responsiveness...");
while (millis() - modemReadyStartTime < MODEM_READY_TIMEOUT) {
String response = sendCommandAndPrintAllResponse("AT", 1500); // Send AT, wait 1.5s for any response
if (response.indexOf("OK") != -1) {
Serial.println("Modem is responsive! Continuing initial checks.");
break;
}
Serial.print("Modem not yet responsive (");
Serial.print((millis() - modemReadyStartTime) / 1000);
Serial.println("s elapsed). Retrying...");
delay(2000); // Wait 2 seconds before retrying AT command
}
if (millis() - modemReadyStartTime >= MODEM_READY_TIMEOUT) {
Serial.println("Modem did not become responsive within timeout. Check wiring, module power, and PWRKEY sequence.");
while(true); // Halt if modem fails to become responsive
}
Serial.println("\n--- Basic AT Command Tests ---");
delay(1000); // Small delay before next command
sendCommandAndPrintAllResponse("ATI", 3000); // Get product ID
delay(1000);
sendCommandAndPrintAllResponse("AT+CMEE=1", 1000); // Enable verbose errors
delay(1000);
sendCommandAndPrintAllResponse("AT+CPIN?", 3000); // Check SIM status
delay(1000);
Serial.println("\n--- Setup complete. Monitor loop output for continuous checks. ---");
}
void loop() {
// Continuously check basic modem status to see if it remains responsive
Serial.println("\n--- Loop: Continuous Basic Checks ---");
sendCommandAndPrintAllResponse("AT", 1000);
delay(5000); // Check every 5 seconds
}
// REVISED: powerUp function based on Waveshare example timing
void powerUp(){
Serial.println("Applying power pulse to PWRKEY_PIN (GPIO4)...");
digitalWrite(PWRKEY_PIN, HIGH); // Ensure HIGH first (idle state)
delay(100); // Small stabilization delay
digitalWrite(PWRKEY_PIN, LOW); // Pull PWRKEY low
delay(2000); // Hold low for 2 seconds (as per Waveshare example)
digitalWrite(PWRKEY_PIN, HIGH); // Release PWRKEY (set HIGH)
// The long boot delay is handled in setup() to monitor responsiveness
Serial.println("PWRKEY pulse complete. Module should now be booting.");
}
// REVISED: sendCommandAndPrintAllResponse function for raw modem output debugging
String sendCommandAndPrintAllResponse(String command, unsigned long timeoutMs) {
Serial.print("Sending: ");
Serial.println(command);
// Clear Serial2 receive buffer before sending command
while (Serial2.available()) {
Serial2.read();
}
Serial2.println(command); // Send command to the modem
String rawResponse = "";
unsigned long startTime = millis();
while (millis() - startTime < timeoutMs) {
if (Serial2.available()) {
char c = (char)Serial2.read();
rawResponse += c;
startTime = millis(); // Reset timeout as long as data is being received
}
delay(1); // Small delay to prevent busy-waiting
}
Serial.println("--- Raw Modem Response Received ---");
Serial.println(rawResponse); // Print everything received (raw, untrimmed)
Serial.println("-----------------------------------");
return rawResponse; // Return the raw response for checking in setup/loop
}