From 26e8175d6bf862dd32b406ee10d7d4c632a09a04 Mon Sep 17 00:00:00 2001 From: noerw Date: Wed, 31 Aug 2016 20:20:55 +0200 Subject: [PATCH] hacky fix for getISODate() allocated far to much memory, to allow printing the char* ? --- gps.h | 7 ++++--- mobile-sensebox.ino | 46 +++++++++++++++++++++++++-------------------- storage.h | 24 +++++------------------ 3 files changed, 35 insertions(+), 42 deletions(-) diff --git a/gps.h b/gps.h index 76fc5af..e5ddff9 100644 --- a/gps.h +++ b/gps.h @@ -44,9 +44,10 @@ bool updateTime() { /* UTILS */ -String getISODate() { - char result[16] = { 0 }; +static char* getISODate() { + // TODO: check why we need to allocate that much storage for a 20 character string for Serial printing?? + char result[100] = { 0 }; sprintf(result, "%04d-%02d-%02d-T%02d:%02d:%02dZ", year(), month(), day(), hour(), minute(), second()); - return (String)result; + return result; } diff --git a/mobile-sensebox.ino b/mobile-sensebox.ino index 3e33129..a882e92 100644 --- a/mobile-sensebox.ino +++ b/mobile-sensebox.ino @@ -35,9 +35,9 @@ void setup() { DEBUG_OUT.begin(115200); WiFi.mode(WIFI_STA); - connectWifi(WIFI_SSID, WIFI_PASS); + //connectWifi(WIFI_SSID, WIFI_PASS); - delay(5000); + //delay(5000); // DEBUG: oportunity to connect to network logger // wait until we got a first fix from GPS, and thus an initial time /*DEBUG_OUT.print("Getting GPS fix.."); @@ -54,10 +54,9 @@ void setup() { DEBUG_OUT.print("SPIFF bytes free: "); DEBUG_OUT.println(storage.begin()); - digitalWrite(D9, HIGH); + digitalWrite(D9, HIGH); // DEBUG: integrated led? doesnt work } -bool firstLoop = true; void loop() { //pollGPS(); //DEBUG_OUT.pollClients(); @@ -75,21 +74,28 @@ void loop() { printState(wifi); - //delay(20000); - if (firstLoop) { - Measurement testMeasure; - testMeasure.lat = 51.2; - testMeasure.lng = 7.89; - testMeasure.value = 66.6; - strcpy(testMeasure.timeStamp, "2016-08-21T09:07:22Z"); - strcpy(testMeasure.sensorID, "123457812345678123456781234567"); - - if (storage.add(testMeasure)) { - DEBUG_OUT.println("measurement stored! storage size: "); - } else { - DEBUG_OUT.println("measurement store failed! storage size: "); - } - DEBUG_OUT.println(storage.size()); - firstLoop = false; + // recall all previous measurements + while (storage.size()) { + String measure = storage.pop(); + DEBUG_OUT.println("popped a measurement: "); + DEBUG_OUT.println(measure.substring(0, 31)); // size of sensorID + DEBUG_OUT.println(measure.substring(32)); // skip the newline char } + + // store new measurement + Measurement testMeasure; + testMeasure.lat = 51.2; + testMeasure.lng = 7.89; + testMeasure.value = wifi.numNetworks; + strcpy(testMeasure.timeStamp, getISODate()); + strcpy(testMeasure.sensorID, "123457812345678123456781234567"); + + if (storage.add(testMeasure)) { + DEBUG_OUT.print("measurement stored! storage size: "); + } else { + DEBUG_OUT.print("measurement store failed! storage size: "); + } + DEBUG_OUT.println(storage.size()); + + delay(2000); } diff --git a/storage.h b/storage.h index aef3bdb..32ac695 100644 --- a/storage.h +++ b/storage.h @@ -8,14 +8,13 @@ #define MEASUREMENT_JSON_SIZE (JSON_OBJECT_SIZE(5)) struct Measurement { - char timeStamp[21]; + char timeStamp[20]; float lat; float lng; float value; char sensorID[32]; }; -// TODO: TEST THIS THING!! class Storage { protected: // not needed, as we post the data as json string? @@ -32,16 +31,15 @@ class Storage { }*/ bool serializeMeasurement(Measurement& m, Print& f) { + f.println(m.sensorID); StaticJsonBuffer jsonBuffer; JsonObject& root = jsonBuffer.createObject(); - // TODO: fix data model - root["date"] = m.timeStamp; + // TODO: replace temporary data model + root["createdAt"] = m.timeStamp; root["lat"] = m.lat; root["lng"] = m.lng; root["value"] = m.value; - root["sensorId"] = m.sensorID; root.printTo(f); - f.print("\n"); return root.success(); } @@ -75,24 +73,12 @@ class Storage { if (!dir.next()) return measurement; // abort if storage is empty String fileName = dir.fileName(); File f = dir.openFile("r"); - measurement = f.readStringUntil('\n'); // assumes that the data does not contain any \n! + measurement = f.readString(); f.close(); SPIFFS.remove(fileName); return measurement; } - /*const Measurement pop(const char* directory = "/measurements/") { - Dir dir = SPIFFS.openDir(directory); - Measurement m; - if (!dir.next()) return m; // abort if storage is empty - String fileName = dir.fileName(); - File f = dir.openFile("r"); - m = deserializeMeasurement(f); - f.close(); - SPIFFS.remove(fileName); - return m; - }*/ - uint16_t size(const char* directory = "/measurements/") { Dir dir = SPIFFS.openDir(directory); uint16_t i = 0;