CAR_GPS_TRACKER/src/core/NetMgr.cpp

61 lines
1.6 KiB
C++

#include "NetMgr.hpp"
namespace {
String at(const String& cmd, uint32_t timeoutMs) {
Serial.print(">> "); Serial.println(cmd);
while (Serial2.available()) (void)Serial2.read();
Serial2.println(cmd);
String resp; uint32_t t0 = millis();
while (millis() - t0 < timeoutMs) {
while (Serial2.available()) {
char c = (char)Serial2.read();
resp += c; Serial.print(c);
t0 = millis();
}
}
resp.trim();
Serial.println("\n[Resp]\n" + resp);
return resp;
}
}
namespace NetMgr {
String sendAT(const String& cmd, uint32_t timeoutMs) { return at(cmd, timeoutMs); }
void powerOnSIM7080(uint8_t pwrkeyPin) {
pinMode(pwrkeyPin, OUTPUT);
digitalWrite(pwrkeyPin, LOW); delay(2000);
digitalWrite(pwrkeyPin, HIGH); delay(1000);
digitalWrite(pwrkeyPin, LOW); delay(30000);
}
bool waitLTE(uint32_t ms) {
uint32_t t0 = millis();
while (millis() - t0 < ms) {
String r = at("AT+CEREG?", 4000);
if (r.indexOf("+CEREG: 0,1") != -1 || r.indexOf("+CEREG: 0,5") != -1) return true;
delay(3000);
}
return false;
}
void attachAndPdp(const char* apn) {
at("AT", 2000);
at("AT+CMEE=1", 2000);
at("AT+CPIN?", 2000);
at("AT+CSQ", 2000);
at("AT+CNMP=38", 5000);
at("AT+CMNB=1", 5000);
at("AT+CGDCONT=1,\"IP\",\"" + String(apn) + "\"", 5000);
at("AT+CGATT=1", 10000);
if (!waitLTE()) { Serial.println("No LTE reg"); while (true) delay(1000); }
at("AT+CNCFG=0,1,\"" + String(apn) + "\"", 5000);
at("AT+CNACT=0,1", 45000); // may 767 if already active
at("AT+CNACT?", 3000);
at("AT+CDNSCFG=\"8.8.8.8\",\"1.1.1.1\"", 3000);
}
} // namespace NetMgr