Skip to content

Instantly share code, notes, and snippets.

@mhahl
Created May 20, 2016 12:43
Show Gist options
  • Save mhahl/952146733e2d469f58492b60f5c5b66d to your computer and use it in GitHub Desktop.
Save mhahl/952146733e2d469f58492b60f5c5b66d to your computer and use it in GitHub Desktop.
// This #include statement was automatically added by the Particle IDE.
#include "SparkJson/SparkJson.h"
#include "TinyGPS/TinyGPS.h"
#include "HttpClient/HttpClient.h"
http_header_t headers[] = {
{ "Host", "api.nsabackdoor.net" },
{ "Content-Type", "application/json" },
{ "Accept", "application/json" },
{ NULL, NULL }
};
HttpClient http;
TinyGPS gps;
FuelGauge fuel;
// Store GPS data
char szHttpResult[64];
/*****************************************/
int m_sleep = 1 * 60 * 1000;
bool m_enabled = true;
int m_device_id = 1;
/*****************************************/
void updateSettings() {
Serial.println("Panther#updateSettings: [ENTRY]");
http_request_t request;
http_response_t response;
// api.nsabackdoor.net
IPAddress remote_addr(221, 121, 139, 231);
// Rails is listening on port 3000
request.port = 3000;
// Endpoint to create a new position
char url[64];
sprintf(url, "/devices/%i.json", m_device_id);
request.path = url;
// Remote address
request.ip = remote_addr;
http.get(request, response, headers);
Serial.println("----- updateSettings Debug -----");
Serial.println(response.status);
Serial.println(response.body);
Serial.println("-----------------------------");
if(response.status == 200) {
Serial.println("Panther#updateSettings: response.status=200");
StaticJsonBuffer<512> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject((char*)response.body.c_str());
if (!root.success()) {
Serial.println("Panther#updateSettings: parseObject() failed");
return;
}
int poll_freq = root["poll_freq"];
bool enabled = root["enabled"];
m_enabled = enabled;
m_sleep = poll_freq * 60 * 1000;
root.prettyPrintTo(Serial);
}
Serial.println("Panther#updateSettings: [EXIT]");
}
void postRequest(char json[]) {
Serial.println("Panther#postRequest: [ENTRY]");
http_request_t request;
http_response_t response;
// api.nsabackdoor.net
IPAddress remote_addr(221, 121, 139, 231);
// Rails is listening on port 3000
request.port = 3000;
// Endpoint to create a new position
request.path = "/positions";
// Remote address
request.ip = remote_addr;
// Set the request body
request.body = json;
http.post(request, response, headers);
Serial.println(json);
Serial.println("----- postRequest Debug -----");
Serial.println(response.status);
Serial.println(response.body);
Serial.println("-----------------------------");
if(response.status == 200) {
Serial.println("Panther#postRequest: SUCCESS: QuietBasket reports success");
} else {
Serial.println("Panther#postRequest: ERROR: QuietBasket responds with error");
}
Serial.println("Panther#postRequest: [EXIT]");
}
void submitPosition(float latitude, float longitude) {
Serial.println("Panther#submitPosition: [ENTRY]");
char lat[64];
char lon[64];
sprintf(lat, "%.6f", latitude);
sprintf(lon, "%.6f", longitude);
StaticJsonBuffer<512> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
JsonObject& positionInfo = root.createNestedObject("position");
positionInfo["latitude"] = lat;
positionInfo["longitude"] = lon;
positionInfo["device_id"] = 1; // Hard code device_id
positionInfo["fuel_charge"] = fuel.getSoC(); // Update battery level
// This print:s
// {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]}
char buff[1024];
root.prettyPrintTo(buff, sizeof(buff));
Serial.println(buff);
postRequest(buff);
Serial.println("Panther#submitPosition: [EXIT]");
}
// Setup the serial device.
void setup(){
updateSettings();
Serial.println("Panther#setup: Serial1.begin()");;
Serial1.begin(9600);
}
void loop(){
bool isValidGPS = false;
for (unsigned long start = millis(); millis() - start < 1000;){
// Check GPS data is available
while (Serial1.available()){
char c = Serial1.read();
// parse GPS data
if (gps.encode(c)) {
Serial.println("Panther#loop: isValidGPS = true");
isValidGPS = true;
}
}
}
// If we have a valid GPS location then publis hit
if (isValidGPS){
float lat, lon;
unsigned long age;
gps.f_get_position(&lat, &lon, &age);
if(lat != TinyGPS::GPS_INVALID_F_ANGLE || lon != TinyGPS::GPS_INVALID_F_ANGLE) {
if(m_enabled) {
submitPosition(lat, lon);
}
} else {
Serial.println("Panther#loop: Invalid lat,long angle");
}
} else {
Serial.println("Panther#loop: invalid GPS position");
}
// Sleep for some time
updateSettings();
// If the device poll time is less than
// 5 we go to sleep to save popwer.
//
if (m_sleep < (5 * 60 * 1000 )) {
Serial.println("Panther#loop: Waiting ...");
delay(m_sleep);
} else {
Serial.println("Panther#loop: Sleeping ...");
System.sleep(D2, RISING, (m_sleep / 1000));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment