Skip to content

Instantly share code, notes, and snippets.

@niklas88
Created November 5, 2015 10:49
Show Gist options
  • Save niklas88/8d1473e1db9f2eaf1e7d to your computer and use it in GitHub Desktop.
Save niklas88/8d1473e1db9f2eaf1e7d to your computer and use it in GitHub Desktop.
SYSTEM_THREAD(ENABLED);
#include "HttpClient/HttpClient.h"
#include "PowerShield/PowerShield.h"
/**
* Declaring the variables.
*/
HttpClient http;
const long sleep_interval = 60*5;
const long one_day_millis = 60*60*24*1000;
const float battery_threshold = 20.0;
// Headers currently need to be set at init, useful for API keys etc.
http_header_t headers[] = {
{ "Content-Type", "application/json" },
{ "Accept" , "application/json" },
{ NULL, NULL } // NOTE: Always terminate headers will NULL
};
http_request_t request;
http_response_t response;
PowerShield batteryMonitor;
void setup() {
Wire.begin();
batteryMonitor.reset();
batteryMonitor.quickStart();
Time.zone(1.0);
waitUntil(WiFi.ready);
}
void send_update(float temp, float soc) {
time_t time = Time.now();
String timeString = Time.format(time, TIME_FORMAT_ISO8601_FULL);
request.hostname = "raindrop.frickel.club";
request.port = 80;
request.path = "/station/test";
request.body = "{\"Time\" :\""+timeString+"\", \"Temperature\" : "+String(temp)+", \"Battery\" : "+String(soc)+"}";
http.post(request, response, headers);
}
void ensure_time_sync() {
static unsigned long last_time_sync = millis();
unsigned long now = millis();
if(now-last_time_sync > one_day_millis){
Particle.syncTime();
last_time_sync = millis();
}
}
void loop() {
ensure_time_sync();
float stateOfCharge = batteryMonitor.getSoC();
send_update(0.8, stateOfCharge);
if(stateOfCharge < battery_threshold){
float sleep_factor = battery_threshold/stateOfCharge;
long sleep_time = (float) sleep_interval*(sleep_factor*sleep_factor);
System.sleep(SLEEP_MODE_DEEP, sleep_time);
}
delay(20000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment