json fix
This commit is contained in:
parent
339e009285
commit
06f118c2c7
|
|
@ -2413,14 +2413,6 @@ void loopDecoder() {
|
||||||
} else {
|
} else {
|
||||||
*gps = 0;
|
*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] = '{';
|
raw[0] = '{';
|
||||||
// Use same JSON format as for MQTT and HTML map........
|
// Use same JSON format as for MQTT and HTML map........
|
||||||
|
|
@ -2436,6 +2428,14 @@ void loopDecoder() {
|
||||||
gps);
|
gps);
|
||||||
int len = strlen(raw);
|
int len = strlen(raw);
|
||||||
#if 0
|
#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, "{"
|
int len = snprintf(raw, 1024, "{"
|
||||||
"\"res\": %d,"
|
"\"res\": %d,"
|
||||||
"\"type\": \"%s\","
|
"\"type\": \"%s\","
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,14 @@ const char *getType(SondeInfo *si) {
|
||||||
return sondeTypeStrSH[sonde.realType(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
|
// To be used by
|
||||||
// - MQTT
|
// - MQTT
|
||||||
// - rdzJSON (for Android app)
|
// - rdzJSON (for Android app)
|
||||||
|
|
@ -25,21 +33,37 @@ const char *getType(SondeInfo *si) {
|
||||||
int sonde2json(char *buf, int maxlen, SondeInfo *si)
|
int sonde2json(char *buf, int maxlen, SondeInfo *si)
|
||||||
{
|
{
|
||||||
SondeData *s = &(si->d);
|
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\","
|
"\"type\":\"%s\","
|
||||||
"\"id\": \"%s\"," // TODO: maybe remove in the future, ser is enough, client can calculate APRS id if needed
|
"\"id\": \"%s\"," // TODO: maybe remove in the future, ser is enough, client can calculate APRS id if needed
|
||||||
"\"ser\": \"%s\","
|
"\"ser\": \"%s\","
|
||||||
"\"frame\": %u," // raw frame, from sonde, can be 0. (TODO: add virtual frame # as in sondehub?)
|
"\"frame\": %u," // raw frame, from sonde, can be 0. (TODO: add virtual frame # as in sondehub?)
|
||||||
"\"vframe\": %d,"
|
"\"vframe\": %d,"
|
||||||
"\"time\": %u,"
|
"\"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,"
|
"\"sats\": %d,"
|
||||||
"\"freq\": %.2f,"
|
"\"freq\": %.2f,"
|
||||||
"\"rssi\": %d,"
|
"\"rssi\": %d,"
|
||||||
|
|
@ -48,7 +72,7 @@ int sonde2json(char *buf, int maxlen, SondeInfo *si)
|
||||||
"\"burstKT\": %d,"
|
"\"burstKT\": %d,"
|
||||||
"\"countKT\": %d,"
|
"\"countKT\": %d,"
|
||||||
"\"crefKT\": %d,"
|
"\"crefKT\": %d,"
|
||||||
"\"launchsite\": \"%d\","
|
"\"launchsite\": \"%s\","
|
||||||
"\"res\": %d",
|
"\"res\": %d",
|
||||||
getType(si),
|
getType(si),
|
||||||
s->id,
|
s->id,
|
||||||
|
|
@ -56,14 +80,6 @@ int sonde2json(char *buf, int maxlen, SondeInfo *si)
|
||||||
s->frame,
|
s->frame,
|
||||||
s->vframe,
|
s->vframe,
|
||||||
s->time,
|
s->time,
|
||||||
s->lat,
|
|
||||||
s->lon,
|
|
||||||
s->alt,
|
|
||||||
s->vs,
|
|
||||||
s->hs,
|
|
||||||
s->vs,
|
|
||||||
s->hs,
|
|
||||||
s->dir,
|
|
||||||
s->sats,
|
s->sats,
|
||||||
si->freq,
|
si->freq,
|
||||||
si->rssi,
|
si->rssi,
|
||||||
|
|
@ -84,21 +100,6 @@ int sonde2json(char *buf, int maxlen, SondeInfo *si)
|
||||||
if(n>=maxlen) return -1;
|
if(n>=maxlen) return -1;
|
||||||
buf += n; maxlen -= n;
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -31,6 +31,7 @@ void MQTT::init(const char* host, uint16_t port, const char* id, const char *use
|
||||||
mqttClient.setServer(ip, port);
|
mqttClient.setServer(ip, port);
|
||||||
snprintf(buffer, 20, "%s%04d", id, (int)random(0, 1000));
|
snprintf(buffer, 20, "%s%04d", id, (int)random(0, 1000));
|
||||||
buffer[20] = 0;
|
buffer[20] = 0;
|
||||||
|
Serial.print(buffer);
|
||||||
mqttClient.setClientId(buffer);
|
mqttClient.setClientId(buffer);
|
||||||
if (strlen(password) > 0) {
|
if (strlen(password) > 0) {
|
||||||
mqttClient.setCredentials(username, password);
|
mqttClient.setCredentials(username, password);
|
||||||
|
|
@ -47,8 +48,12 @@ void MQTT::publishUptime()
|
||||||
mqttClient.connect(); // ensure we've got connection
|
mqttClient.connect(); // ensure we've got connection
|
||||||
|
|
||||||
Serial.println("[MQTT] writing");
|
Serial.println("[MQTT] writing");
|
||||||
char payload[12];
|
//char payload[128];
|
||||||
snprintf(payload, 12, "%lu", millis());
|
//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];
|
char topic[128];
|
||||||
snprintf(topic, 128, "%s%s", this->prefix, "uptime");
|
snprintf(topic, 128, "%s%s", this->prefix, "uptime");
|
||||||
mqttClient.publish(topic, 1, 1, payload);
|
mqttClient.publish(topic, 1, 1, payload);
|
||||||
|
|
@ -147,6 +152,7 @@ void MQTT::publishPacket(SondeInfo *si)
|
||||||
strcat(payload, "}"); // terminate payload string
|
strcat(payload, "}"); // terminate payload string
|
||||||
|
|
||||||
char topic[128];
|
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);
|
mqttClient.publish(topic, 1, 1, payload);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
const char *version_name = "rdzTTGOsonde";
|
const char *version_name = "rdzTTGOsonde";
|
||||||
const char *version_id = "devel20220408";
|
const char *version_id = "devel20220414";
|
||||||
const int SPIFFS_MAJOR=2;
|
const int SPIFFS_MAJOR=2;
|
||||||
const int SPIFFS_MINOR=16;
|
const int SPIFFS_MINOR=16;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue