Skip to content

Instantly share code, notes, and snippets.

@jokamjohn
Created May 3, 2016 20:03
Show Gist options
  • Select an option

  • Save jokamjohn/9e7d41539b859f0342fa1f5f60fb9382 to your computer and use it in GitHub Desktop.

Select an option

Save jokamjohn/9e7d41539b859f0342fa1f5f60fb9382 to your computer and use it in GitHub Desktop.
firebase pull code
#include <Process.h>
//Call the set clock method in the setup method. i.e setClock()
void loop() {
//Add this code where the data is to be sent from.
String url = "https://trans-former.firebaseio.com/data.json";
epoch = timeInEpoch();
//The values in the brackets are the variables.
String data = "\"humidity\":" + String(humidity) + ", \"temperature\":" + String(temperature) + ", \"time\":" + String(epoch);
String jsonData = "{" + data + "}";
Serial.println(jsonData);
Process p;
p.runShellCommand("curl -k -X POST " + url + " -d '" + jsonData + "'");
while(p.running());
}
// Synchronize clock using NTP
void setClock() {
Process p;
Serial.println("Setting clock.");
// Sync clock with NTP
p.runShellCommand("ntpd -nqp 0.openwrt.pool.ntp.org");
// Block until clock sync is completed - 1433666585 is unix time for 7.6.2015 08:43:05 GMT
while(timeInEpoch() < 1433666585);
}
//Returns a UNIX timestamp
unsigned long timeInEpoch() {
Process time;
char epochCharArray[12] = "";
// Get UNIX timestamp
time.begin("date");
time.addParameter("+%s");
time.run();
// When execution is completed, store in charArray
while (time.available() > 0) {
time.readString().toCharArray(epochCharArray, 12);
}
// Return long with timestamp
return atol(epochCharArray);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment