Browse Source

upgrade ArduinoJSON to API v6

main
Hendrik Langer 5 years ago
parent
commit
fb66ae53f0
  1. 47
      src/main.cpp

47
src/main.cpp

@ -144,69 +144,68 @@ void lightsleep() {
}
void ICACHE_FLASH_ATTR getValuesJSON(char* buffer, const size_t buf_len, int format) {
StaticJsonBuffer<MQTT_MAX_PACKET_SIZE> jsonBuffer; // ToDo: buf_len
StaticJsonDocument<MQTT_MAX_PACKET_SIZE> jsonDoc; // ToDo: buf_len
if (format == 1) {
JsonArray& array = jsonBuffer.createArray();
JsonArray array = jsonDoc.to<JsonArray>();
JsonObject& temperatureObject = array.createNestedObject();
JsonObject temperatureObject = array.createNestedObject();
temperatureObject["sensor"] = SENSOR1_ID;
temperatureObject["value"] = sd.temperature;
JsonObject& humidityObject = array.createNestedObject();
JsonObject humidityObject = array.createNestedObject();
humidityObject["sensor"] = SENSOR2_ID;
humidityObject["value"] = sd.humidity;
JsonObject& pressureObject = array.createNestedObject();
JsonObject pressureObject = array.createNestedObject();
pressureObject["sensor"] = SENSOR3_ID;
pressureObject["value"] = sd.pressure;
if (sd.sds_updated) {
JsonObject& pm10Object = array.createNestedObject();
JsonObject pm10Object = array.createNestedObject();
pm10Object["sensor"] = SENSOR4_ID;
pm10Object["value"] = sd.p10;
JsonObject& pm25Object = array.createNestedObject();
JsonObject pm25Object = array.createNestedObject();
pm25Object["sensor"] = SENSOR5_ID;
pm25Object["value"] = sd.p25;
}
if (sd.cpm > 0) {
JsonObject& cpmObject = array.createNestedObject();
JsonObject cpmObject = array.createNestedObject();
cpmObject["sensor"] = SENSOR6_ID;
cpmObject["value"] = sd.radioactivity;
}
if (sd.voltage > 2.5 ) {
JsonObject& voltageObject = array.createNestedObject();
JsonObject voltageObject = array.createNestedObject();
voltageObject["sensor"] = SENSOR7_ID;
voltageObject["value"] = sd.voltage;
}
if (sd.rssi != 0) {
JsonObject& rssiObject = array.createNestedObject();
JsonObject rssiObject = array.createNestedObject();
rssiObject["sensor"] = SENSOR8_ID;
rssiObject["value"] = sd.rssi;
}
array.printTo(buffer, buf_len);
serializeJson(jsonDoc, buffer, buf_len);
} else if (format == 2) {
JsonObject& root = jsonBuffer.createObject();
root["temperature"] = sd.temperature;
root["humidity"] = sd.humidity;
root["pressure"] = sd.pressure;
jsonDoc["temperature"] = sd.temperature;
jsonDoc["humidity"] = sd.humidity;
jsonDoc["pressure"] = sd.pressure;
if (sd.sds_updated) {
root["pm10"] = sd.p10;
root["pm2.5"] = sd.p25;
jsonDoc["pm10"] = sd.p10;
jsonDoc["pm2.5"] = sd.p25;
}
if (sd.cpm > 0) {
root["cpm"] = sd.cpm;
root["radioactivity"] = sd.radioactivity;
jsonDoc["cpm"] = sd.cpm;
jsonDoc["radioactivity"] = sd.radioactivity;
}
if (sd.voltage > 2.5 ) {
root["voltage"] = sd.voltage;
jsonDoc["voltage"] = sd.voltage;
}
if (sd.rssi != 0) {
root["rssi"] = sd.rssi;
jsonDoc["rssi"] = sd.rssi;
}
// root["millis"] = millis();
// root["heap"] = ESP.getFreeHeap();
// jsonDoc["millis"] = millis();
// jsonDoc["heap"] = ESP.getFreeHeap();
root.printTo(buffer, buf_len);
serializeJson(jsonDoc, buffer, buf_len);
}
}

Loading…
Cancel
Save