From dd12251e43174281c737ca0868d5e6fcb8711558 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Wed, 5 Sep 2018 13:18:22 +0200 Subject: [PATCH] Do not quote numbers in MQTT JSON payloads --- code/espurna/mqtt.ino | 11 ++++++++++- code/espurna/utils.ino | 1 + 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/code/espurna/mqtt.ino b/code/espurna/mqtt.ino index 79f4bff7..017240c2 100644 --- a/code/espurna/mqtt.ino +++ b/code/espurna/mqtt.ino @@ -574,7 +574,16 @@ unsigned char _mqttBuildTree(JsonObject& root, char parent) { JsonObject& elements = root.createNestedObject(element.topic); unsigned char num = _mqttBuildTree(elements, i); if (0 == num) { - root.set(element.topic, element.message); + if (isNumber(element.message)) { + double value = atof(element.message); + if (value == int(value)) { + root.set(element.topic, int(value)); + } else { + root.set(element.topic, value); + } + } else { + root.set(element.topic, element.message); + } } } } diff --git a/code/espurna/utils.ino b/code/espurna/utils.ino index ff57da9c..1203788d 100644 --- a/code/espurna/utils.ino +++ b/code/espurna/utils.ino @@ -451,6 +451,7 @@ int __get_adc_mode() { bool isNumber(const char * s) { unsigned char len = strlen(s); + if (0 == len) return false; bool decimal = false; for (unsigned char i=0; i