From 364c5dd69955f280a7c4b63f5f64b2f85833d39f Mon Sep 17 00:00:00 2001 From: Hendrik Langer Date: Fri, 9 Aug 2019 21:34:52 +0200 Subject: [PATCH] mqtt default values --- src/main.cpp | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index b139123..d401d6a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -403,19 +403,19 @@ void receiveMqtt(const char* topic, const char* data, int data_len) { ESP_LOGW(TAG, "Error parsing JSON, code: %s", err.c_str()); } else { // got json - sensor->temperature = jsonDoc["temperature"].as(); - sensor->humidity = jsonDoc["humidity"].as(); - sensor->pressure = jsonDoc["pressure"].as(); - sensor->voc = jsonDoc["voc"].as(); - sensor->lux = jsonDoc["lux"].as(); - sensor->uvi = jsonDoc["uvi"].as(); - sensor->uva = jsonDoc["uva"].as(); - sensor->uvb = jsonDoc["uvb"].as(); - sensor->pm10 = jsonDoc["pm10"].as(); - sensor->pm25 = jsonDoc["pm2.5"].as(); - sensor->battery = jsonDoc["voltage"].as(); - sensor->rssi = jsonDoc["rssi"].as(); - sensor->lastUpdate = jsonDoc["timestamp"].as(); + if (jsonDoc.containsKey("temperature")) sensor->temperature = jsonDoc["temperature"].as(); + if (jsonDoc.containsKey("humidity")) sensor->humidity = jsonDoc["humidity"].as(); + if (jsonDoc.containsKey("pressure")) sensor->pressure = jsonDoc["pressure"].as(); + if (jsonDoc.containsKey("voc")) sensor->voc = jsonDoc["voc"].as(); + if (jsonDoc.containsKey("lux")) sensor->lux = jsonDoc["lux"].as(); + if (jsonDoc.containsKey("uvi")) sensor->uvi = jsonDoc["uvi"].as(); + if (jsonDoc.containsKey("uva")) sensor->uva = jsonDoc["uva"].as(); + if (jsonDoc.containsKey("uvb")) sensor->uvb = jsonDoc["uvb"].as(); + if (jsonDoc.containsKey("pm10")) sensor->pm10 = jsonDoc["pm10"].as(); + if (jsonDoc.containsKey("pm2.5")) sensor->pm25 = jsonDoc["pm2.5"].as(); + if (jsonDoc.containsKey("voltage")) sensor->battery = jsonDoc["voltage"].as(); + if (jsonDoc.containsKey("rssi")) sensor->rssi = jsonDoc["rssi"].as(); + if (jsonDoc.containsKey("timestamp")) sensor->lastUpdate = jsonDoc["timestamp"].as(); ESP_LOGI(TAG, "got new values from %s, timestamp: %lu", topic, sensor->lastUpdate); ESP_LOGI(TAG, "%lu seconds ago", topic, getTimestamp() - sensor->lastUpdate); }