Skip to content

Commit

Permalink
Improved cellular GNSS support
Browse files Browse the repository at this point in the history
  • Loading branch information
stanleyhuangyc committed Jun 27, 2021
1 parent 72ab167 commit 4d5446a
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 28 deletions.
4 changes: 2 additions & 2 deletions firmware_v5/datalogger/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* Data logging
**************************************/
#ifndef HAVE_CONFIG
// enable(1)/disable(0) data streaming
// enable(1)/disable(0) serial data output
#define ENABLE_SERIAL_OUT 0
// specify storage type
#define STORAGE STORAGE_SD
Expand Down Expand Up @@ -40,7 +40,7 @@
#ifndef HAVE_CONFIG
// enable(1)/disable(0) OBD-II reading
#define USE_OBD 1
// GNSS option: disable(0)/standalone(1)/cellular module(2)
// GNSS option: 0:disable 1:external 2:SIM5360/7600 3:SIM7070
#define USE_GNSS 1
// enable(1)/disable(0) MEMS motion sensor
#define USE_MEMS 1
Expand Down
120 changes: 96 additions & 24 deletions firmware_v5/datalogger/datalogger.ino
Original file line number Diff line number Diff line change
Expand Up @@ -234,19 +234,18 @@ public:
{
#if USE_GNSS == 1
if (!checkState(STATE_GPS_FOUND)) {
Serial.print("GPS:");
Serial.print("GNSS:");
if (sys.gpsBegin(GPS_SERIAL_BAUDRATE)) {
setState(STATE_GPS_FOUND);
Serial.println("OK");
//waitGPS();
} else {
sys.gpsEnd();
Serial.println("NO");
}
}
#elif USE_GNSS == 2
#elif USE_GNSS >= 2
if (!checkState(STATE_GPS_FOUND)) {
Serial.print("CELL GPS:");
Serial.print("CELL GNSS:");
if (cellInit()) {
Serial.println("OK");
if (!gd) gd = new GPS_DATA;
Expand All @@ -255,6 +254,7 @@ public:
} else {
Serial.println("NO");
}
sys.gpsBegin();
}
#endif

Expand All @@ -279,6 +279,7 @@ public:
#endif
startTime = millis();
}
#if USE_GNSS
void logLocationData(GPS_DATA* gd)
{
if (lastGPStime == gd->time) return;
Expand Down Expand Up @@ -318,6 +319,7 @@ public:
lastGPStime = gd->time;
setState(STATE_GPS_READY);
}
#endif
#if USE_GNSS == 1
void processGPSData()
{
Expand Down Expand Up @@ -345,7 +347,7 @@ public:
}
}
}
#elif USE_GNSS == 2
#elif USE_GNSS >= 2
void processCellGPS()
{
/*
Expand All @@ -368,7 +370,7 @@ public:
sys.gpsEnd(); // turn off GPS power
Serial.println("OFF");
}
#elif USE_GNSS == 2
#elif USE_GNSS >= 2
Serial.print("GNSS:");
cellUninit();
Serial.println("OFF");
Expand Down Expand Up @@ -415,44 +417,113 @@ public:
sys.reactivateLink();
//ESP.restart();
}
#if USE_GNSS == 2
#if USE_GNSS >= 2
bool cellSendCommand(const char* cmd, char* buf, int bufsize, const char* expected = "\r\nOK", unsigned int timeout = 1000)
{
if (cmd) sys.xbWrite(cmd);
memset(buf, 0, bufsize);
return sys.xbReceive(buf, bufsize, timeout, &expected, 1) != 0;
}
void cellUninit()
{
char buf[32];
cellSendCommand("AT+CPOF\r", buf, sizeof(buf));
}
#if USE_GNSS == 3
bool cellInit()
{
char buf[320];
bool success = false;
for (byte n = 0; n < 2; n++) {
if (!cellSendCommand("AT\r", buf, sizeof(buf))) cellSendCommand(0, buf, sizeof(buf), "START", 5000);
if (cellSendCommand("ATE0\r", buf, sizeof(buf)) && cellSendCommand("ATI\r", buf, sizeof(buf))) {
for (byte n = 0; n < 5; n++) {
if (!cellSendCommand("ATZ\r", buf, sizeof(buf))) {
sys.xbTogglePower();
cellSendCommand("AT\r", buf, sizeof(buf), "+CFUN:", 3000);
}
cellSendCommand("ATE0\r", buf, sizeof(buf));
if (cellSendCommand("ATI\r", buf, sizeof(buf))) {
Serial.println(buf);
//cellSendCommand("AT+SGNSCFG=\"NMEAOUTPORT\",2,115200\r", buf, sizeof(buf));
//Serial.println(buf);
cellSendCommand("AT+CGNSPWR=1\r", buf, sizeof(buf));
Serial.println(buf);
cellSendCommand("AT+CGNSMOD=1,1,0,0,0\r", buf, sizeof(buf));
Serial.println(buf);
cellSendCommand("AT+CVAUXV=61\r", buf, sizeof(buf));
cellSendCommand("AT+CVAUXS=1\r", buf, sizeof(buf));
if (cellSendCommand("AT+CGPS?\r", buf, sizeof(buf), "+CGPS: 1")) {
if (cellSendCommand("AT+CGNSINF\r", buf, sizeof(buf), "+CGNSINF:")) {
Serial.println(buf);
success = true;
break;
}
delay(2000);
if (cellSendCommand("AT+CGPS=1,1\r", buf, sizeof(buf))) {
Serial.println(buf);
}
}
return success;

}
void cellUninit()
{
char buf[32];
cellSendCommand("AT+CGNSPWR=0\r", buf, sizeof(buf));
}
bool cellGetGPSInfo(GPS_DATA* gd)
{
char *p;
char buf[160];
if (cellSendCommand("AT+CGNSINF\r", buf, sizeof(buf), "+CGNSINF:")) do {
Serial.print(buf);
if (!(p = strchr(buf, ':'))) break;
p += 2;
if (strncmp(p, "1,1,", 4)) break;
p += 4;
gd->time = atol(p + 8);
*(p + 8) = 0;
gd->date = atol(p);
if (!(p = strchr(p + 9, ','))) break;
gd->lat = atof(++p);
if (!(p = strchr(p, ','))) break;
gd->lng = atof(++p);
if (!(p = strchr(p, ','))) break;
gd->alt = atof(++p);
if (!(p = strchr(p, ','))) break;
gd->speed = atof(++p) * 1000 / 1852;
if (!(p = strchr(p, ','))) break;
gd->heading = atoi(++p);
Serial.print("UTC:");
Serial.print(gd->date);
Serial.print(' ');
Serial.print(gd->time);
Serial.print(" LAT:");
Serial.print(gd->lat);
Serial.print(" LNG:");
Serial.println(gd->lng);
return true;
} while (0);
return false;
}
#else
bool cellInit()
{
char buf[320];
bool success = false;
for (byte n = 0; n < 3 && !success; n++) {
// try turning on module
sys.xbTogglePower();
// discard any stale data
sys.xbPurge();
delay(3000);
for (byte m = 0; m < 5; m++) {
if (cellSendCommand("AT\r", buf, sizeof(buf)) && cellSendCommand("ATE0\r", buf, sizeof(buf)) && cellSendCommand("ATI\r", buf, sizeof(buf))) {
// retrieve module info
Serial.print(buf);
cellSendCommand("AT+CGPS=1,1\r", buf, sizeof(buf));
success = true;
break;
}
}
sys.xbTogglePower();
}
//cellSendCommand("AT+CGPSNMEARATE=1\r", buf, sizeof(buf));
//cellSendCommand("AT+CGPSINFOCFG=1,31\r", buf, sizeof(buf));
//cellSendCommand("AT+CGPSINFOCFG=10,31\r", buf, sizeof(buf));
return success;
}
void cellUninit()
{
char buf[32];
cellSendCommand("AT+CPOF\r", buf, sizeof(buf));
}
long parseDegree(const char* s)
{
char *p;
Expand Down Expand Up @@ -512,6 +583,7 @@ public:
} while (0);
return false;
}
#endif
#endif
bool checkState(byte flags) { return (m_state & flags) == flags; }
void setState(byte flags) { m_state |= flags; }
Expand Down Expand Up @@ -569,7 +641,7 @@ void setup()
pinMode(PIN_LED, OUTPUT);
pinMode(PIN_LED, HIGH);

if (sys.begin(USE_GNSS == 1, USE_GNSS == 2)) {
if (sys.begin(true, USE_GNSS >= 2)) {
Serial.print("TYPE:");
Serial.println(sys.devType);
}
Expand Down Expand Up @@ -735,7 +807,7 @@ void loop()
if (logger.checkState(STATE_GPS_FOUND)) {
logger.processGPSData();
}
#elif USE_GNSS == 2
#elif USE_GNSS >= 2
if (logger.checkState(STATE_CELL_GPS_FOUND)) {
logger.processCellGPS();
}
Expand Down
2 changes: 0 additions & 2 deletions firmware_v5/datalogger/dataserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,6 @@ int handlerLogFile(UrlHandlerParam* param)
}
param->contentLength = ctx->file.readBytes(param->pucBuffer, param->bufSize);
param->contentType = HTTPFILETYPE_TEXT;
Serial.print(param->contentLength);
Serial.println(" bytes read");
return FLAG_DATA_STREAM;
}

Expand Down

0 comments on commit 4d5446a

Please sign in to comment.