hacky fix for getISODate()

allocated far to much memory, to allow printing the char* ?
esp8266-bme280
noerw 8 years ago
parent 50085550dd
commit 26e8175d6b

@ -44,9 +44,10 @@ bool updateTime() {
/* UTILS */ /* UTILS */
String getISODate() { static char* getISODate() {
char result[16] = { 0 }; // 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", sprintf(result, "%04d-%02d-%02d-T%02d:%02d:%02dZ",
year(), month(), day(), hour(), minute(), second()); year(), month(), day(), hour(), minute(), second());
return (String)result; return result;
} }

@ -35,9 +35,9 @@ void setup() {
DEBUG_OUT.begin(115200); DEBUG_OUT.begin(115200);
WiFi.mode(WIFI_STA); 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 // wait until we got a first fix from GPS, and thus an initial time
/*DEBUG_OUT.print("Getting GPS fix.."); /*DEBUG_OUT.print("Getting GPS fix..");
@ -54,10 +54,9 @@ void setup() {
DEBUG_OUT.print("SPIFF bytes free: "); DEBUG_OUT.print("SPIFF bytes free: ");
DEBUG_OUT.println(storage.begin()); DEBUG_OUT.println(storage.begin());
digitalWrite(D9, HIGH); digitalWrite(D9, HIGH); // DEBUG: integrated led? doesnt work
} }
bool firstLoop = true;
void loop() { void loop() {
//pollGPS(); //pollGPS();
//DEBUG_OUT.pollClients(); //DEBUG_OUT.pollClients();
@ -75,21 +74,28 @@ void loop() {
printState(wifi); printState(wifi);
//delay(20000); // recall all previous measurements
if (firstLoop) { while (storage.size()) {
Measurement testMeasure; String measure = storage.pop();
testMeasure.lat = 51.2; DEBUG_OUT.println("popped a measurement: ");
testMeasure.lng = 7.89; DEBUG_OUT.println(measure.substring(0, 31)); // size of sensorID
testMeasure.value = 66.6; DEBUG_OUT.println(measure.substring(32)); // skip the newline char
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;
} }
// 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);
} }

@ -8,14 +8,13 @@
#define MEASUREMENT_JSON_SIZE (JSON_OBJECT_SIZE(5)) #define MEASUREMENT_JSON_SIZE (JSON_OBJECT_SIZE(5))
struct Measurement { struct Measurement {
char timeStamp[21]; char timeStamp[20];
float lat; float lat;
float lng; float lng;
float value; float value;
char sensorID[32]; char sensorID[32];
}; };
// TODO: TEST THIS THING!!
class Storage { class Storage {
protected: protected:
// not needed, as we post the data as json string? // not needed, as we post the data as json string?
@ -32,16 +31,15 @@ class Storage {
}*/ }*/
bool serializeMeasurement(Measurement& m, Print& f) { bool serializeMeasurement(Measurement& m, Print& f) {
f.println(m.sensorID);
StaticJsonBuffer<MEASUREMENT_JSON_SIZE> jsonBuffer; StaticJsonBuffer<MEASUREMENT_JSON_SIZE> jsonBuffer;
JsonObject& root = jsonBuffer.createObject(); JsonObject& root = jsonBuffer.createObject();
// TODO: fix data model // TODO: replace temporary data model
root["date"] = m.timeStamp; root["createdAt"] = m.timeStamp;
root["lat"] = m.lat; root["lat"] = m.lat;
root["lng"] = m.lng; root["lng"] = m.lng;
root["value"] = m.value; root["value"] = m.value;
root["sensorId"] = m.sensorID;
root.printTo(f); root.printTo(f);
f.print("\n");
return root.success(); return root.success();
} }
@ -75,24 +73,12 @@ class Storage {
if (!dir.next()) return measurement; // abort if storage is empty if (!dir.next()) return measurement; // abort if storage is empty
String fileName = dir.fileName(); String fileName = dir.fileName();
File f = dir.openFile("r"); File f = dir.openFile("r");
measurement = f.readStringUntil('\n'); // assumes that the data does not contain any \n! measurement = f.readString();
f.close(); f.close();
SPIFFS.remove(fileName); SPIFFS.remove(fileName);
return measurement; 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/") { uint16_t size(const char* directory = "/measurements/") {
Dir dir = SPIFFS.openDir(directory); Dir dir = SPIFFS.openDir(directory);
uint16_t i = 0; uint16_t i = 0;

Loading…
Cancel
Save