Having some trouble with the syntax / format to make an if else work the way I am wanting. It is a portable GPS tracker - where the current behavior in the case of not getting a GPS lock, it uploads 0.000000 for latitude and longitude to a dashboard.
I am trying to get it to only upload battery level when latitude is 0. In the code below, I have the if/else portion in the loop commented out. When I uncomment, it only runs the if portion of commands (and also fails to HTTP post to the dashboard).
Other than this, the tracker does what I need, wakes up from a mercury switch interrupt, connects, obtains GPS (or not), uploads to a dashboard and goes to sleep - would just prefer the resultant dashboard map not include 0 latitude / 0 longitude way points is all.
Thanks for any assistance.
#include "Adafruit_FONA.h"
#include <SoftwareSerial.h>
#include <avr/sleep.h>
#include <avr/power.h>
#include <avr/interrupt.h>
#define DEBUG // serial debug prints on-off
#ifdef DEBUG
#define DEBUG_PRINT(x) Serial.print(x)
#define DEBUG_PRINTLN(x) Serial.println(x)
#else
#define DEBUG_PRINT(x)
#define DEBUG_PRINTLN(x)
#endif
#define FONA_PWRKEY 16
#define FONA_TX 14
#define FONA_RX 15
SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX);
SoftwareSerial *fonaSerial = &fonaSS;
Adafruit_FONA_LTE fona = Adafruit_FONA_LTE();
uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout = 0);
char imei[16] = {0};
uint8_t type;
uint16_t battLevel = 0;
float latitude, longitude, speed_kph, heading, altitude;
uint8_t counter = 0;
char URL[100];
char body[100];
char latBuff[12], longBuff[12], locBuff[50], speedBuff[12],
headBuff[12], altBuff[12], battBuff[12];
void setup() {
}
void loop() {
pinMode(2, INPUT_PULLUP);
#ifdef DEBUG
Serial.begin(115200);
#endif
latBuff[12]=0;
longBuff[12]=0;
locBuff[50]=0;
speedBuff[12]=0;
headBuff[12]=0;
altBuff[12]=0;
battBuff[12]=0;
DEBUG_PRINTLN(F("*** GPS Tracker ***"));
pinMode(FONA_PWRKEY, OUTPUT);
powerOn(true);
moduleSetup();
fona.setNetworkSettings(F("hologram"));
fona.enableGPRS(false);
counter = 0;
while (counter < 3 &&!fona.enableGPRS(true)) {
DEBUG_PRINTLN(F("Failed to turn on LTE, retrying..."));
counter++;
delay(2000);
}
DEBUG_PRINTLN(F("Turned on LTE!"));
counter = 0;
while (counter < 5 &&!netStatus()) {
DEBUG_PRINTLN(F("Failed to connect to cell network, retrying..."));
counter++;
delay(2000);
}
DEBUG_PRINTLN(F("Connected to cell network!"));
battLevel = readVcc();
delay(500);
counter = 0;
while (counter < 3 &&!fona.enableGPS(true)) {
DEBUG_PRINTLN(F("Failed to turn on GPS, retrying..."));
counter++;
delay(2000);
}
DEBUG_PRINTLN(F("Turned on GPS!"));
counter = 0;
while (counter < 20 &&!fona.getGPS(&latitude, &longitude, &speed_kph, &heading, &altitude)) {
DEBUG_PRINTLN(F("Failed to get GPS location, retrying..."));
counter++;
delay(10000);
}
DEBUG_PRINTLN(F("GPS Location"));
DEBUG_PRINTLN(F("---------------------"));
DEBUG_PRINT(F("Latitude: ")); DEBUG_PRINTLN(latitude);
DEBUG_PRINT(F("Longitude: ")); DEBUG_PRINTLN(longitude);
DEBUG_PRINT(F("Speed: ")); DEBUG_PRINTLN(speed_kph);
DEBUG_PRINT(F("Heading: ")); DEBUG_PRINTLN(heading);
DEBUG_PRINT(F("Altitude: ")); DEBUG_PRINTLN(altitude);
DEBUG_PRINTLN(F("---------------------"));
dtostrf(latitude, 1, 6, latBuff);
dtostrf(longitude, 1, 6, longBuff);
dtostrf(speed_kph, 1, 0, speedBuff);
dtostrf(heading, 1, 0, headBuff);
dtostrf(altitude, 1, 1, altBuff);
dtostrf(battLevel, 1, 0, battBuff);
sprintf(locBuff, "%s,%s,%s,%s", latBuff, longBuff, altBuff, battBuff);
//int latcheck;
//latcheck = atoi(latBuff);
//DEBUG_PRINT(F("latcheck = ")); DEBUG_PRINT(latcheck);
//if (latcheck = '0') {
//counter = 0;
//const char * token = "hashvalue";
//sprintf(URL, "http://demo.thingsboard.io/api/v1/%s/telemetry", token);
//sprintf(body, "{\"batt\":%s}", battBuff);
//while (counter < 3 && !fona.postData("POST", URL, body)) {
//DEBUG_PRINTLN(F("Failed to complete HTTP POST..."));
//counter++;
//delay(1000); }
//}
//else {
counter = 0;
const char * token = "hashvalue";
sprintf(URL, "http://demo.thingsboard.io/api/v1/%s/telemetry", token);
sprintf(body, "{\"lat\":%s,\"long\":%s,\"alt\":%s,\"batt\":%s}", latBuff, longBuff, altBuff, battBuff);
while (counter < 3 && !fona.postData("POST", URL, body)) {
DEBUG_PRINTLN(F("Failed to complete HTTP POST..."));
counter++;
delay(1000); }
//}
moduleOff();
}
void wakeUpNow() {
sleep_disable ();
detachInterrupt(0);
}
void powerOn(bool onoff) {
digitalWrite(FONA_PWRKEY, LOW);
if (onoff) delay(100);
else delay(1400);
digitalWrite(FONA_PWRKEY, HIGH);
}
void moduleSetup() {
fonaSS.begin(115200);
DEBUG_PRINTLN(F("Configuring to 9600 baud"));
delay(5000);
fonaSS.println("AT+IPR=9600");
fonaSS.begin(9600);
if (! fona.begin(fonaSS)) {
DEBUG_PRINTLN(F("Couldn't find FONA"));
while (1);
}
type = fona.type();
DEBUG_PRINTLN(F("FONA is OK"));
DEBUG_PRINT(F("Found "));
switch (type) {
case SIM7000A:
DEBUG_PRINTLN(F("SIM7000A (American)")); break;
case SIM7000C:
DEBUG_PRINTLN(F("SIM7000C (Chinese)")); break;
case SIM7000E:
DEBUG_PRINTLN(F("SIM7000E (European)")); break;
case SIM7000G:
DEBUG_PRINTLN(F("SIM7000G (Global)")); break;
default:
DEBUG_PRINTLN(F("???")); break;
}
uint8_t imeiLen = fona.getIMEI(imei);
if (imeiLen > 0) {
DEBUG_PRINT("Module IMEI: "); DEBUG_PRINTLN(imei);
}
}
float readVcc() {
if (!fona.getBattVoltage(&battLevel)) DEBUG_PRINTLN(F("Failed to read batt"));
else DEBUG_PRINT(F("battery = ")); DEBUG_PRINT(battLevel); DEBUG_PRINTLN(F(" mV"));
return battLevel;
}
bool netStatus() {
int n = fona.getNetworkStatus();
DEBUG_PRINT(F("Network status ")); DEBUG_PRINT(n); DEBUG_PRINT(F(": "));
if (n == 0) DEBUG_PRINTLN(F("Not registered"));
if (n == 1) DEBUG_PRINTLN(F("Registered (home)"));
if (n == 2) DEBUG_PRINTLN(F("Not registered (searching)"));
if (n == 3) DEBUG_PRINTLN(F("Denied"));
if (n == 4) DEBUG_PRINTLN(F("Unknown"));
if (n == 5) DEBUG_PRINTLN(F("Registered roaming"));
if (!(n == 1 || n == 5)) return false;
else return true;
}
void moduleOff() {
DEBUG_PRINTLN(F("Shutting down GPS, GPRS"));
delay(5);
fona.enableGPRS(false);
fona.enableGPS(false);
fona.powerDown();
//powerOn(false);
MCU_powerDown();
}
void MCU_powerDown() {
DEBUG_PRINTLN(F("Shutting down MCU"));
delay(5);
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
noInterrupts ();
sleep_enable();
attachInterrupt(0,wakeUpNow, LOW);
interrupts ();
sleep_cpu ();
}