Skip to content

Instantly share code, notes, and snippets.

@naikrovek
Created May 18, 2017 13:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naikrovek/330fb772c3c00fb34ef60bea73d120ee to your computer and use it in GitHub Desktop.
Save naikrovek/330fb772c3c00fb34ef60bea73d120ee to your computer and use it in GitHub Desktop.
#include <AssetTracker.h>
#include "Particle.h"
int transmitMode(String command);
int gpsPublish(String command);
int batteryStatus(String command);
int publishInterval(String command);
// Set whether you want the device to publish data to the internet by default here.
// 1 will Particle.publish AND Serial.print, 0 will just Serial.print
int transmittingData = 1;
unsigned int loopCounter = 0;
// Used to keep track of the last time we published data
unsigned long lastPublish = 0;
// How many minutes between publishes? 10+ recommended for long-time continuous publishing!
int delayMinutes = 10;
// Creating an AssetTracker named 't' for us to reference
AssetTracker t = AssetTracker();
// A FuelGauge named 'fuel' for checking on the battery state
FuelGauge fuel;
String previousFix = "";
// setup() and loop() are both required. setup() runs once when the device starts
// and is used for registering functions and variables and initializing things
void setup() {
// increase the default maximum charging voltage from 4.1v to 4.2. this gives a bit of extra lifetime in a low battery situation.
PMIC chargeController;
chargeController.setChargeVoltage(4208); // 4.2v
chargeController.setChargeCurrent(false, false, true, false, false, false); // 1024mA. default is 512mA.
// see: https://github.com/spark/firmware/blob/60f703bd4165a292f89124a7e3755e4eb37b1a03/wiring/src/spark_wiring_power.cpp#L542
// Sets up all the necessary AssetTracker bits
t.begin();
pinMode(D7, OUTPUT); // status LED
// Enable the GPS module. Defaults to off to save power.
// Takes 1.5s or so because of delays.
t.gpsOn();
// Opens up a Serial port so you can listen over USB
Serial.begin(9600);
// These three functions are useful for remote diagnostics. Read more below.
Particle.function("tmode", transmitMode);
Particle.function("batt", batteryStatus);
Particle.function("gps", gpsPublish);
Particle.function("interval", publishInterval);
RGB.control(true);
RGB.color(0, 0, 0);
}
// loop() runs continuously
void loop() {
if (loopCounter % 500 == 0) {
RGB.control(true);
digitalWrite(D7, HIGH);
} else {
digitalWrite(D7, LOW);
}
loopCounter++;
// You'll need to run this every loop to capture the GPS output
t.updateGPS();
// if the current time - the last time we published is greater than your set delay...
if (millis() - lastPublish > delayMinutes * 60 * 1000) {
//String pubAccel = String::format("x:%d, y:%d, z:%d", t.readX(), t.readY(), t.readZ());
//Serial.println("accelerometer: " + pubAccel);
//Particle.publish("A", pubAccel, 60, PRIVATE);
// Dumps the full NMEA sentence to serial in case you're curious
//Serial.println(t.preNMEA());
String currentFix = t.readLatLon();
int fixComparisonResult = -100; // default value of -100 since we don't want a default of 0 for this purpose.
fixComparisonResult = previousFix.compareTo(currentFix);
// GPS requires a "fix" on the satellites to give good data, so we should only publish data if there's a fix
if (t.gpsFix() && (fixComparisonResult != 0)) {
previousFix = currentFix;
// Remember when we published
lastPublish = millis();
// Only publish if we're in transmittingData mode 1;
if (transmittingData) {
// Short publish names save data!
Serial.println("Publishing 'G'");
Particle.publish("G", currentFix, 60, PRIVATE);
} else {
Serial.println("transmittingData is turned off; no publish.");
}
// but always report the data over serial for local development
Serial.println(t.readLatLon());
}
}
}
int transmitMode(String command){
transmittingData = atoi(command);
Serial.println("Setting 'transmitMode' to " + String(transmittingData));
return 1;
}
int publishInterval(String command) {
delayMinutes = atoi(command);
Serial.println("Setting 'publishInterval' to " + String(delayMinutes));
return 1;
}
// Actively ask for a GPS reading if you're impatient. Only publishes if there's a GPS fix, otherwise returns '0'
int gpsPublish(String command) {
// compare last fix to this one. only publish if we've changed.
// GPS will continue to report the previous fix if you lose your
// fix, and this fixes that.
String currentFix = t.readLatLon();
int fixComparisonResult = -100; // default value of -100 since we don't want a default of 0 for this purpose.
fixComparisonResult = previousFix.compareTo(currentFix);
if (t.gpsFix() && fixComparisonResult != 0) {
previousFix = currentFix; // store this fix so we can compare against it next time.
Serial.println("Publishing: G " + currentFix);
Particle.publish("G", currentFix, 60, PRIVATE);
return 1;
} else {
return 0;
}
}
// Lets you remotely check the battery status by calling the function "batt"
// Triggers a publish with the info (so subscribe or watch the dashboard)
// and also returns a '1' if there's >10% battery left and a '0' if below
int batteryStatus(String command) {
Serial.println("Time to publish battery status.");
// Publish the battery voltage and percentage of battery remaining
// if you want to be really efficient, just report one of these
// the String::format("%f.2") part gives us a string to publish,
// but with only 2 decimal points to save space
String batteryStatus = String::format("%.2f",fuel.getVCell()) + ",c:" + String::format("%.2f",fuel.getSoC());
Serial.println("Battery Status is: " + batteryStatus);
Particle.publish("B", "v:" + batteryStatus, 60, PRIVATE);
if (fuel.getSoC() > 10) {
Serial.println("Battery is > 10%");
// if there's more than 10% of the battery left, then return 1
return 1;
} else {
Serial.println("Battery is < 10%.");
// if you're running out of battery, return 0
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment