Created
May 3, 2016 20:02
-
-
Save jokamjohn/8919830dab61d5caeb93efdfddfe29c9 to your computer and use it in GitHub Desktop.
Firebase push code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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://prepaid-water.firebaseio.com/data.json"; | |
| epoch = timeInEpoch(); | |
| //The values in the brackets are the variables. | |
| String data = "\"currentUnits\":" + String(currentUnits) + ", \"meterNumber\":" + String(meterNumber) + ", \"time\":" + String(epoch) + ", \"vouncher\":" + String(vouncher); | |
| 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