I’m building a GPS tracker with the Electron and want to save energy by turning off the GPS when the device is not moving for a certain period. However, the accelerometer sometimes stops working (returns a movement of zero) after turning off the GPS. I tried to re-initialize it but I can’t bring it back to a working state.
I encountered this problem with the Asset Tracker library and found a topic about a similar problem where @Dick suggested to try out fancy-asset-tracker. That didn’t work either in my case. Here is a small example firmware (headers and initialization code are copied from fancy-asset-tracker):
#include "Adafruit_LIS3DH.h"
#include "Adafruit_GPS.h"
#include <math.h>
Adafruit_GPS gps(&Serial1);
Adafruit_LIS3DH accel(A2, A5, A4, A3);
void setup() {
// Init accelerometer
accel.begin(LIS3DH_DEFAULT_ADDRESS);
accel.setDataRate(LIS3DH_DATARATE_LOWPOWER_5KHZ);
accel.setRange(LIS3DH_RANGE_4_G);
// Enable GPS
pinMode(D6, OUTPUT);
digitalWrite(D6, LOW);
gps.begin(9600);
Serial1.begin(9600);
gps.sendCommand("$PMTK101*32");
delay(250);
gps.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
delay(250);
gps.sendCommand(PGCMD_NOANTENNA);
delay(250);
// Debug output
Serial.begin(9600);
}
bool gpsOn = 1;
unsigned long lastMoved;
void loop() {
unsigned long now = millis();
// Update GPS
char c = gps.read();
if (gps.newNMEAreceived()) {
gps.parse(gps.lastNMEA());
}
accel.read();
int acc = sqrt(accel.x * accel.x + accel.y * accel.y + accel.z * accel.z);
// Enable GPS when the device is moving, disable after 10 s without movement
if (acc > 9000) {
lastMoved = now;
if (! gpsOn) {
Serial.println("Activating GPS");
gpsOn = 1;
digitalWrite(D6, LOW);
}
}
if (lastMoved != 0 && (now - lastMoved > 10000)) {
if (gpsOn) {
Serial.println("Deactivating GPS");
gpsOn = 0;
digitalWrite(D6, HIGH);
}
}
}
Unfortunately, the problem doesn’t always occur. The program might work for many GPS on/off iterations and then fail. Here is some example output:
<Start, shake device>
<Wait for 10 s>
Deactivating GPS
<Shake device>
Activating GPS
<Wait for 10 s>
Deactivating GPS
<Shake device>
<Nothing happens, GPS not activating, accel.read() => 0>
Am I doing something wrong in the firmware? Do you know a working setup that combines GPS and accelerometer?