From 2bdecf74dd531467a0513a257515a93645c48d19 Mon Sep 17 00:00:00 2001 From: Hendrik Langer Date: Tue, 26 Jun 2018 15:11:18 +0200 Subject: [PATCH] json in seperate method --- src/main.cpp | 140 +++++++++++++++++++++++++++------------------------ src/main.h | 2 +- 2 files changed, 76 insertions(+), 66 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 6f493a0..12db1bb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -109,6 +109,7 @@ struct __attribute__((packed)) SENSOR_DATA { float radioactivity; float voltage; float rssi; + bool sds_updated; } sd; static uint32_t cal = system_rtc_clock_cali_proc(); // WARNING: UPDATING THIS WILL MAKE THE rtcMillis() RETURN A LOWER VALUE THEN BEFORE, EVEN IF TIME PASSED, BREAKING INTERVAL CHECKS! @@ -142,6 +143,73 @@ void lightsleep() { //delay(1); } +void ICACHE_FLASH_ATTR getValuesJSON(char* buffer, const size_t buf_len, int format) { + StaticJsonBuffer jsonBuffer; // ToDo: buf_len + + if (format == 1) { + JsonArray& array = jsonBuffer.createArray(); + + JsonObject& temperatureObject = array.createNestedObject(); + temperatureObject["sensor"] = SENSOR1_ID; + temperatureObject["value"] = sd.temperature; + JsonObject& humidityObject = array.createNestedObject(); + humidityObject["sensor"] = SENSOR2_ID; + humidityObject["value"] = sd.humidity; + JsonObject& pressureObject = array.createNestedObject(); + pressureObject["sensor"] = SENSOR3_ID; + pressureObject["value"] = sd.pressure; + if (sd.sds_updated) { + JsonObject& pm10Object = array.createNestedObject(); + pm10Object["sensor"] = SENSOR4_ID; + pm10Object["value"] = sd.p10; + JsonObject& pm25Object = array.createNestedObject(); + pm25Object["sensor"] = SENSOR5_ID; + pm25Object["value"] = sd.p25; + } + if (sd.cpm > 0) { + JsonObject& cpmObject = array.createNestedObject(); + cpmObject["sensor"] = SENSOR6_ID; + cpmObject["value"] = sd.radioactivity; + } + if (sd.voltage > 2.5 ) { + JsonObject& voltageObject = array.createNestedObject(); + voltageObject["sensor"] = SENSOR7_ID; + voltageObject["value"] = sd.voltage; + } + if (sd.rssi != 0) { + JsonObject& rssiObject = array.createNestedObject(); + rssiObject["sensor"] = SENSOR8_ID; + rssiObject["value"] = sd.rssi; + } + + array.printTo(buffer, buf_len); + } else if (format == 2) { + JsonObject& root = jsonBuffer.createObject(); + + root["temperature"] = sd.temperature; + root["humidity"] = sd.humidity; + root["pressure"] = sd.pressure; + if (sd.sds_updated) { + root["pm10"] = sd.p10; + root["pm2.5"] = sd.p25; + } + if (sd.cpm > 0) { + root["cpm"] = sd.cpm; + root["radioactivity"] = sd.radioactivity; + } + if (sd.voltage > 2.5 ) { + root["voltage"] = sd.voltage; + } + if (sd.rssi != 0) { + root["rssi"] = sd.rssi; + } + root["millis"] = millis(); + root["heap"] = ESP.getFreeHeap(); + + root.printTo(buffer, sizeof(buffer)); + } +} + void ICACHE_FLASH_ATTR sendValues() { sd.temperature = dht.readTemperature(); sd.humidity = dht.readHumidity(); @@ -158,13 +226,13 @@ void ICACHE_FLASH_ATTR sendValues() { // sds.wakeup(); delay(30000); // working 30 seconds PmResult pm = sds.queryPm(); - bool sds_updated = false; + sd.sds_updated = false; if(pm.isOk() && (pm.pm25 != sd.p25 || pm.pm10 != sd.p10)) { sd.p25 = pm.pm25; sd.p10 = pm.pm10; - sds_updated = true; + sd.sds_updated = true; } else { - sds_updated = false; + sd.sds_updated = false; } // sds.sleep(); @@ -182,71 +250,13 @@ void ICACHE_FLASH_ATTR sendValues() { DEBUG_MSG("Humidity : %6.2f%% (DHT22)\n", sd.humidity); DEBUG_MSG("Temperature : %6.2f°C (BMP180)\n", sd.temp2); DEBUG_MSG("Pressure : %6.2fhPa (BMP180)\n", sd.pressure); - if (sds_updated) DEBUG_MSG("Particles 10 : %6.2fµg/m³ (SDS011)\n", sd.p10); - if (sds_updated) DEBUG_MSG("Particles 2.5: %6.2fµg/m³ (SDS011)\n", sd.p25); + if (sd.sds_updated) DEBUG_MSG("Particles 10 : %6.2fµg/m³ (SDS011)\n", sd.p10); + if (sd.sds_updated) DEBUG_MSG("Particles 2.5: %6.2fµg/m³ (SDS011)\n", sd.p25); if (sd.cpm > 0) DEBUG_MSG("Radiation : %6.2fµSv/h (J305)\n", sd.radioactivity); DEBUG_MSG("Voltage : %6.2fV (ESP8266)\n", sd.voltage); - StaticJsonBuffer jsonBuffer; - JsonArray& array = jsonBuffer.createArray(); - StaticJsonBuffer jsonBuffer2; - JsonObject& root = jsonBuffer.createObject(); - - JsonObject& temperatureObject = array.createNestedObject(); - temperatureObject["sensor"] = SENSOR1_ID; - temperatureObject["value"] = sd.temperature; - root["temperature"] = sd.temperature; - - JsonObject& humidityObject = array.createNestedObject(); - humidityObject["sensor"] = SENSOR2_ID; - humidityObject["value"] = sd.humidity; - root["humidity"] = sd.humidity; - - JsonObject& pressureObject = array.createNestedObject(); - pressureObject["sensor"] = SENSOR3_ID; - pressureObject["value"] = sd.pressure; - root["pressure"] = sd.pressure; - - if (sds_updated) { - JsonObject& pm10Object = array.createNestedObject(); - pm10Object["sensor"] = SENSOR4_ID; - pm10Object["value"] = sd.p10; - root["pm10"] = sd.p10; - - JsonObject& pm25Object = array.createNestedObject(); - pm25Object["sensor"] = SENSOR5_ID; - pm25Object["value"] = sd.p25; - root["pm2.5"] = sd.p25; - } - - if (sd.cpm > 0) { - JsonObject& cpmObject = array.createNestedObject(); - cpmObject["sensor"] = SENSOR6_ID; - cpmObject["value"] = sd.radioactivity; - root["cpm"] = sd.cpm; - root["radioactivity"] = sd.radioactivity; - } - - if (sd.voltage > 2.5 ) { - JsonObject& voltageObject = array.createNestedObject(); - voltageObject["sensor"] = SENSOR7_ID; - voltageObject["value"] = sd.voltage; - root["voltage"] = sd.voltage; - } - - if (sd.rssi != 0) { - JsonObject& rssiObject = array.createNestedObject(); - rssiObject["sensor"] = SENSOR8_ID; - rssiObject["value"] = sd.rssi; - root["rssi"] = sd.rssi; - } - - root["millis"] = millis(); - root["heap"] = ESP.getFreeHeap(); - - char buffer[MQTT_MAX_PACKET_SIZE]; - array.printTo(buffer, sizeof(buffer)); + getValuesJSON(buffer, MQTT_MAX_PACKET_SIZE, 1); WiFi.forceSleepWake(); delay(1); // yield(); @@ -359,7 +369,7 @@ void ICACHE_FLASH_ATTR sendValues() { } mqttclient.loop(); - root.printTo(buffer, sizeof(buffer)); + getValuesJSON(buffer, MQTT_MAX_PACKET_SIZE, 2); if (mqttclient.publish("sensor/esp-weatherstation/01/json", buffer, strlen(buffer))) { DEBUG_MSG("mqtt done\n"); } else { diff --git a/src/main.h b/src/main.h index 526fa18..beabb23 100644 --- a/src/main.h +++ b/src/main.h @@ -7,6 +7,6 @@ #define DEBUG_MSG(...) #endif -#define FW_VERSION 22 +#define FW_VERSION 23 #endif /* _MAIN_H */