From 06f118c2c7f574a740699f1c6998736846235459 Mon Sep 17 00:00:00 2001 From: "Hansi, dl9rdz" Date: Thu, 14 Apr 2022 15:04:04 +0000 Subject: [PATCH] json fix --- RX_FSK/RX_FSK.ino | 16 +++++------ RX_FSK/src/json.cpp | 67 +++++++++++++++++++++++---------------------- RX_FSK/src/mqtt.cpp | 12 ++++++-- RX_FSK/version.h | 2 +- 4 files changed, 52 insertions(+), 45 deletions(-) diff --git a/RX_FSK/RX_FSK.ino b/RX_FSK/RX_FSK.ino index dd0fcff..0ed51d4 100644 --- a/RX_FSK/RX_FSK.ino +++ b/RX_FSK/RX_FSK.ino @@ -2413,14 +2413,6 @@ void loopDecoder() { } else { *gps = 0; } - //maintain backwords compatibility - float lat = isnan(s->d.lat) ? 0 : s->d.lat; - float lon = isnan(s->d.lon) ? 0 : s->d.lon; - float alt = isnan(s->d.alt) ? -1 : s->d.alt; - float vs = isnan(s->d.vs) ? 0 : s->d.vs; - float hs = isnan(s->d.hs) ? 0 : s->d.hs; - float dir = isnan(s->d.dir) ? 0 : s->d.dir; - // raw[0] = '{'; // Use same JSON format as for MQTT and HTML map........ @@ -2436,6 +2428,14 @@ void loopDecoder() { gps); int len = strlen(raw); #if 0 + //maintain backwords compatibility + float lat = isnan(s->d.lat) ? 0 : s->d.lat; + float lon = isnan(s->d.lon) ? 0 : s->d.lon; + float alt = isnan(s->d.alt) ? -1 : s->d.alt; + float vs = isnan(s->d.vs) ? 0 : s->d.vs; + float hs = isnan(s->d.hs) ? 0 : s->d.hs; + float dir = isnan(s->d.dir) ? 0 : s->d.dir; + int len = snprintf(raw, 1024, "{" "\"res\": %d," "\"type\": \"%s\"," diff --git a/RX_FSK/src/json.cpp b/RX_FSK/src/json.cpp index 1a2f192..a64aad7 100644 --- a/RX_FSK/src/json.cpp +++ b/RX_FSK/src/json.cpp @@ -18,6 +18,14 @@ const char *getType(SondeInfo *si) { return sondeTypeStrSH[sonde.realType(si)]; } +int float2json(char **buf, int *maxlen, char *fmt, float value) { + if(isnan(value)) return 0; + int n = snprintf(*buf, *maxlen, fmt, value); + if(n>*maxlen) return -1; + *buf += n; *maxlen -= n; + return n; +} + // To be used by // - MQTT // - rdzJSON (for Android app) @@ -25,21 +33,37 @@ const char *getType(SondeInfo *si) { int sonde2json(char *buf, int maxlen, SondeInfo *si) { SondeData *s = &(si->d); - int n = snprintf(buf, maxlen, + int n; + + n = float2json(&buf, &maxlen, "\"lat\": %.5f,", s->lat); + if(n<0) return -1; + n = float2json(&buf, &maxlen, "\"lon\": %.5f,", s->lon); + if(n<0) return -1; + n = float2json(&buf, &maxlen, "\"alt\": %.1f,", s->alt); + if(n<0) return -1; + n = float2json(&buf, &maxlen, "\"vs\": %.1f,", s->vs); + if(n<0) return -1; + n = float2json(&buf, &maxlen, "\"hs\": %.1f,", s->hs); + if(n<0) return -1; + n = float2json(&buf, &maxlen, "\"climb\": %.1f,", s->vs); // used by HTML map, to be removed (-> vs) + if(n<0) return -1; + n = float2json(&buf, &maxlen, "\"speed\": %.1f,", s->hs); // used by HTML map, to be removed (-> hs) + if(n<0) return -1; + n = float2json(&buf, &maxlen, "\"dir\": %.1f,", s->dir); + if(n<0) return -1; + n = float2json(&buf, &maxlen, "\"temp\": %.1f,", s->temperature ); + if(n<0) return -1; + n = float2json(&buf, &maxlen, "\"humidity\": %.1f,", s->relativeHumidity); + if(n<0) return -1; + n = float2json(&buf, &maxlen, "\"pressure\": %.1f,", s->pressure); + if(n<0) return -1; + n = snprintf(buf, maxlen, "\"type\":\"%s\"," "\"id\": \"%s\"," // TODO: maybe remove in the future, ser is enough, client can calculate APRS id if needed "\"ser\": \"%s\"," "\"frame\": %u," // raw frame, from sonde, can be 0. (TODO: add virtual frame # as in sondehub?) "\"vframe\": %d," "\"time\": %u," - "\"lat\": %.5f," - "\"lon\": %.5f," - "\"alt\": %.1f," - "\"vs\": %.1f," - "\"hs\": %.1f," - "\"climb\": %.1f," // used by HTML map, to be removed (-> vs) - "\"speed\": %.1f," // used by HTML map, to be removed (-> hs) - "\"dir\": %.1f," "\"sats\": %d," "\"freq\": %.2f," "\"rssi\": %d," @@ -48,7 +72,7 @@ int sonde2json(char *buf, int maxlen, SondeInfo *si) "\"burstKT\": %d," "\"countKT\": %d," "\"crefKT\": %d," - "\"launchsite\": \"%d\"," + "\"launchsite\": \"%s\"," "\"res\": %d", getType(si), s->id, @@ -56,14 +80,6 @@ int sonde2json(char *buf, int maxlen, SondeInfo *si) s->frame, s->vframe, s->time, - s->lat, - s->lon, - s->alt, - s->vs, - s->hs, - s->vs, - s->hs, - s->dir, s->sats, si->freq, si->rssi, @@ -84,21 +100,6 @@ int sonde2json(char *buf, int maxlen, SondeInfo *si) if(n>=maxlen) return -1; buf += n; maxlen -= n; } - if ( !isnan( s->temperature ) ) { - n = snprintf(buf, maxlen, ",\"temp\": %.1f", s->temperature ); - if(n>=maxlen) return -1; - buf += n; maxlen -=n; - } - if ( !isnan( s->relativeHumidity) ) { - n = snprintf(buf, maxlen, ",\"humidity\": %.1f", s->relativeHumidity); - if(n>=maxlen) return -1; - buf += n; maxlen -=n; - } - if ( !isnan( s->pressure) ) { - n = snprintf(buf, maxlen, ",\"pressure\": %.1f", s->pressure ); - if(n>=maxlen) return -1; - buf += n; maxlen -=n; - } return 0; } diff --git a/RX_FSK/src/mqtt.cpp b/RX_FSK/src/mqtt.cpp index 3e0b463..02567c2 100644 --- a/RX_FSK/src/mqtt.cpp +++ b/RX_FSK/src/mqtt.cpp @@ -31,6 +31,7 @@ void MQTT::init(const char* host, uint16_t port, const char* id, const char *use mqttClient.setServer(ip, port); snprintf(buffer, 20, "%s%04d", id, (int)random(0, 1000)); buffer[20] = 0; + Serial.print(buffer); mqttClient.setClientId(buffer); if (strlen(password) > 0) { mqttClient.setCredentials(username, password); @@ -47,8 +48,12 @@ void MQTT::publishUptime() mqttClient.connect(); // ensure we've got connection Serial.println("[MQTT] writing"); - char payload[12]; - snprintf(payload, 12, "%lu", millis()); + //char payload[128]; + //snprintf(payload, 12, "%lu", millis()); + //snprintf(payload, 124, "{\"uptime\": %lu," "\"user\": \"%s\"", millis(), username ); + char payload[128]; + snprintf(payload, 128, "{\"uptime\": %d, \"user\": \"%s\", \"rxlat\": %.5f, \"rxlon\": %.5f}", millis(), username, sonde.config.rxlat, sonde.config.rxlon ); + Serial.println(payload); char topic[128]; snprintf(topic, 128, "%s%s", this->prefix, "uptime"); mqttClient.publish(topic, 1, 1, payload); @@ -147,6 +152,7 @@ void MQTT::publishPacket(SondeInfo *si) strcat(payload, "}"); // terminate payload string char topic[128]; - snprintf(topic, 128, "%s%s", this->prefix, "packet"); + snprintf(topic, 128, "%s%s", this->prefix, "data"); + Serial.print(payload); mqttClient.publish(topic, 1, 1, payload); } diff --git a/RX_FSK/version.h b/RX_FSK/version.h index 992ac42..fe78c3e 100644 --- a/RX_FSK/version.h +++ b/RX_FSK/version.h @@ -1,4 +1,4 @@ const char *version_name = "rdzTTGOsonde"; -const char *version_id = "devel20220408"; +const char *version_id = "devel20220414"; const int SPIFFS_MAJOR=2; const int SPIFFS_MINOR=16;