(fixed previous commit)

This commit is contained in:
Hansi, dl9rdz 2021-07-27 16:50:46 +02:00
parent 473d35b312
commit 77181d9dad
1 changed files with 55 additions and 44 deletions

View File

@ -2232,6 +2232,8 @@ void loopDecoder() {
mqttclient.publishPacket(s); mqttclient.publishPacket(s);
} }
#endif #endif
} else {
sondehub_finish_data(&shclient, s, &sonde.config.sondehub);
} }
// always send data, even if not valid.... // always send data, even if not valid....
if (rdzclient.connected()) { if (rdzclient.connected()) {
@ -2739,6 +2741,7 @@ void loopWifiScan() {
while (WiFi.status() != WL_CONNECTED && cnt < MAXWIFIDELAY) { while (WiFi.status() != WL_CONNECTED && cnt < MAXWIFIDELAY) {
delay(500); delay(500);
Serial.print("."); Serial.print(".");
#if 0
if (cnt == 5) { if (cnt == 5) {
// my FritzBox needs this for reconnecting // my FritzBox needs this for reconnecting
WiFi.disconnect(true); WiFi.disconnect(true);
@ -2748,6 +2751,7 @@ void loopWifiScan() {
Serial.print(" with password "); Serial.println(fetchWifiPw(index)); Serial.print(" with password "); Serial.println(fetchWifiPw(index));
delay(500); delay(500);
} }
#endif
disp.rdis->drawString(15 * dispxs, lastl + dispys, _scan[cnt & 1]); disp.rdis->drawString(15 * dispxs, lastl + dispys, _scan[cnt & 1]);
cnt++; cnt++;
} }
@ -3000,19 +3004,19 @@ void loop() {
#endif #endif
#if FEATURE_SONDEHUB #if FEATURE_SONDEHUB
if (sonde.config.sondehub.active) { if (sonde.config.sondehub.active) {
unsigned long time_now = millis(); unsigned long time_now = millis();
// time_delta will be correct, even if time_now overflows // time_delta will be correct, even if time_now overflows
unsigned long time_delta = time_now - time_last_update; unsigned long time_delta = time_now - time_last_update;
if ((sonde.config.sondehub.chase == 0) && (time_delta >= SONDEHUB_STATION_UPDATE_TIME) && (wifi_state != WIFI_APMODE)) { // 60 min if ((sonde.config.sondehub.chase == 0) && (time_delta >= SONDEHUB_STATION_UPDATE_TIME) && (wifi_state != WIFI_APMODE)) { // 60 min
sondehub_station_update(&shclient, &sonde.config.sondehub); sondehub_station_update(&shclient, &sonde.config.sondehub);
time_last_update = time_now; time_last_update = time_now;
}
else if ((sonde.config.sondehub.chase == 1) && (time_delta >= SONDEHUB_MOBILE_STATION_UPDATE_TIME) && (wifi_state != WIFI_APMODE)) { // 30 sec
sondehub_station_update(&shclient, &sonde.config.sondehub);
time_last_update = time_now;
}
} }
else if ((sonde.config.sondehub.chase == 1) && (time_delta >= SONDEHUB_MOBILE_STATION_UPDATE_TIME) && (wifi_state != WIFI_APMODE)) { // 30 sec
sondehub_station_update(&shclient, &sonde.config.sondehub);
time_last_update = time_now;
}
}
#endif #endif
} }
@ -3046,7 +3050,7 @@ void sondehub_station_update(WiFiClient *client, struct st_sondehub *conf) {
"\"uploader_contact_email\": \"%s\",", "\"uploader_contact_email\": \"%s\",",
version_name, version_id, conf->callsign, conf->email); version_name, version_id, conf->callsign, conf->email);
w += strlen(w); w += strlen(w);
if ((conf->chase == 0) && (conf->lat[0] != '\0') && (conf->lon[0] != '\0')){ if ((conf->chase == 0) && (conf->lat[0] != '\0') && (conf->lon[0] != '\0')) {
if (conf->alt[0] != '\0') { if (conf->alt[0] != '\0') {
sprintf(w, sprintf(w,
"\"uploader_position\": [%s,%s,%s]," "\"uploader_position\": [%s,%s,%s],"
@ -3065,7 +3069,7 @@ void sondehub_station_update(WiFiClient *client, struct st_sondehub *conf) {
sprintf(w, sprintf(w,
"\"uploader_position\": [%.6f,%.6f,%d]," "\"uploader_position\": [%.6f,%.6f,%d],"
"\"uploader_antenna\": \"%s\"," "\"uploader_antenna\": \"%s\","
"\"mobile\": true" "\"mobile\": \"true\""
"}", "}",
gpsPos.lat, gpsPos.lon, gpsPos.alt, conf->antenna); gpsPos.lat, gpsPos.lon, gpsPos.alt, conf->antenna);
} }
@ -3116,8 +3120,9 @@ const char *dfmSubtypeStrSH[16] = { NULL, NULL, NULL, NULL, NULL, NULL,
#define SONDEHUB_TIME_THRESHOLD (3) #define SONDEHUB_TIME_THRESHOLD (3)
void sondehub_send_data(WiFiClient *client, SondeInfo *s, struct st_sondehub *conf) { void sondehub_send_data(WiFiClient *client, SondeInfo *s, struct st_sondehub *conf) {
Serial.println("sondehub_send_data()"); Serial.println("sondehub_send_data()");
Serial.printf("shState = %d\n", shState);
// max age of data in JSON request (in seconds) // max age of data in JSON request (in seconds)
#define SONDEHUB_MAXAGE 15 #define SONDEHUB_MAXAGE 15
#define MSG_SIZE 550 #define MSG_SIZE 550
@ -3148,13 +3153,6 @@ void sondehub_send_data(WiFiClient *client, SondeInfo *s, struct st_sondehub *co
return; return;
} }
// If there is an "old" pending collection of JSON data sets, send it even if no now data is received
if(shSate == SH_CONN_APPENDING && (now - shStart > SONDEHUB_MAXAGE+3) ) { // after MAXAGE seconds
sondehub_send_last(client, s, conf);
shState = SH_CONN_WAITACK;
shStart = 0;
}
// Check if current sonde data is valid. If not, don't do anything.... // Check if current sonde data is valid. If not, don't do anything....
if (*s->ser == 0) return; // Don't send anything without serial number if (*s->ser == 0) return; // Don't send anything without serial number
if (((int)s->lat == 0) && ((int)s->lon == 0)) return; // Sometimes these values are zeroes. Don't send those to the sondehub if (((int)s->lat == 0) && ((int)s->lon == 0)) return; // Sometimes these values are zeroes. Don't send those to the sondehub
@ -3207,11 +3205,11 @@ void sondehub_send_data(WiFiClient *client, SondeInfo *s, struct st_sondehub *co
"\"datetime\": \"%04d-%02d-%02dT%02d:%02d:%02d.000Z\"," "\"datetime\": \"%04d-%02d-%02dT%02d:%02d:%02d.000Z\","
"\"lat\": %.6f," "\"lat\": %.6f,"
"\"lon\": %.6f," "\"lon\": %.6f,"
"\"alt\": %.3f," "\"alt\": %.2f,"
"\"frequency\": %.3f," "\"frequency\": %.3f,"
"\"vel_h\": %.3f," "\"vel_h\": %.1f,"
"\"vel_v\": %.3f," "\"vel_v\": %.1f,"
"\"heading\": %.3f," "\"heading\": %.1f,"
"\"sats\": %d," "\"sats\": %d,"
"\"rssi\": %.1f,", "\"rssi\": %.1f,",
version_name, version_id, conf->callsign, version_name, version_id, conf->callsign,
@ -3252,28 +3250,28 @@ void sondehub_send_data(WiFiClient *client, SondeInfo *s, struct st_sondehub *co
if (((int)s->temperature != 0) && ((int)s->relativeHumidity != 0)) { if (((int)s->temperature != 0) && ((int)s->relativeHumidity != 0)) {
sprintf(w, sprintf(w,
"\"temp\": %.1f," "\"temp\": %.2f,"
"\"humidity\": %.1f,", "\"humidity\": %.2f,",
float(s->temperature), float(s->relativeHumidity) float(s->temperature), float(s->relativeHumidity)
); );
w += strlen(w); w += strlen(w);
} }
if ((conf->chase == 0) && (conf->lat[0] != '\0') && (conf->lon[0] != '\0')){ if ((conf->chase == 0) && (conf->lat[0] != '\0') && (conf->lon[0] != '\0')) {
if (conf->alt[0] != '\0') { if (conf->alt[0] != '\0') {
sprintf(w, sprintf(w,
"\"uploader_position\": [%s,%s,%s]," "\"uploader_position\": [%s,%s,%s],"
"\"uploader_antenna\": \"%s\"" "\"uploader_antenna\": \"%s\""
"}", "}",
conf->lat, conf->lon, conf->alt, conf->antenna conf->lat, conf->lon, conf->alt, conf->antenna
); );
} else { } else {
sprintf(w, sprintf(w,
"\"uploader_position\": [%s,%s,null]," "\"uploader_position\": [%s,%s,null],"
"\"uploader_antenna\": \"%s\"" "\"uploader_antenna\": \"%s\""
"}", "}",
conf->lat, conf->lon, conf->antenna conf->lat, conf->lon, conf->antenna
); );
} }
} }
else if (gpsPos.valid && gpsPos.lat != 0 && gpsPos.lon != 0) { else if (gpsPos.valid && gpsPos.lat != 0 && gpsPos.lon != 0) {
@ -3291,16 +3289,16 @@ void sondehub_send_data(WiFiClient *client, SondeInfo *s, struct st_sondehub *co
conf->antenna conf->antenna
); );
} }
if(shState == SH_CONN_IDLE) { if (shState != SH_CONN_APPENDING) {
sondehub_send_header(client, s, conf); sondehub_send_header(client, s, conf);
sondehub_send_next(client, s, conf, rs_msg, strlen(rs_msg), 1); sondehub_send_next(client, s, conf, rs_msg, strlen(rs_msg), 1);
shState = SH_CONN_APPENDING; shState = SH_CONN_APPENDING;
shStart = now; shStart = now;
} else { } else {
sondehub_send_next(client, s, conf, rs_msg, strlen(rs_msg), 0); sondehub_send_next(client, s, conf, rs_msg, strlen(rs_msg), 0);
} }
if(now - shStart > SONDEHUB_MAXAGE) { // after MAXAGE seconds if (now - shStart > SONDEHUB_MAXAGE) { // after MAXAGE seconds
sondehub_send_last(client, s, conf); sondehub_send_last(client, s, conf);
shState = SH_CONN_WAITACK; shState = SH_CONN_WAITACK;
shStart = 0; shStart = 0;
} }
@ -3310,29 +3308,42 @@ void sondehub_send_data(WiFiClient *client, SondeInfo *s, struct st_sondehub *co
//Serial.println(response); //Serial.println(response);
} }
void sondehub_finish_data(WiFiClient *client, SondeInfo *s, struct st_sondehub *conf) {
// If there is an "old" pending collection of JSON data sets, send it even if no now data is received
if (shState == SH_CONN_APPENDING) {
time_t now;
time(&now);
if (now - shStart > SONDEHUB_MAXAGE + 3) { // after MAXAGE seconds
sondehub_send_last(client, s, conf);
shState = SH_CONN_WAITACK;
shStart = 0;
}
}
}
void sondehub_send_header(WiFiClient *client, SondeInfo *s, struct st_sondehub *conf) { void sondehub_send_header(WiFiClient *client, SondeInfo *s, struct st_sondehub *conf) {
Serial.print("PUT /sondes/telemetry HTTP/1.1\r\n" Serial.print("PUT /sondes/telemetry HTTP/1.1\r\n"
"Host: "); "Host: ");
Serial.println(conf->host); Serial.println(conf->host);
Serial.println("accept: text/plain\r\n" Serial.println("accept: text/plain\r\n"
"Content-Type: application/json\r\n" "Content-Type: application/json\r\n"
"Transfer-Encoding: chunked\r\n"); "Transfer-Encoding: chunked\r\n");
client->print("PUT /sondes/telemetry HTTP/1.1\r\n" client->print("PUT /sondes/telemetry HTTP/1.1\r\n"
"Host: "); "Host: ");
client->println(conf->host); client->println(conf->host);
client->println("accept: text/plain\r\n" client->println("accept: text/plain\r\n"
"Content-Type: application/json\r\n" "Content-Type: application/json\r\n"
"Transfer-Encoding: chunked\r\n"); "Transfer-Encoding: chunked\r\n");
} }
void sondehub_send_next(WiFiClient *client, SondeInfo *s, struct st_sondehub *conf, char *chunk, int chunklen, int first) { void sondehub_send_next(WiFiClient *client, SondeInfo *s, struct st_sondehub *conf, char *chunk, int chunklen, int first) {
// send next chunk of JSON request // send next chunk of JSON request
client->printf("%x\r\n", chunklen + 1); client->printf("%x\r\n", chunklen + 1);
client->write(first ? "[":",",1); client->write(first ? "[" : ",", 1);
client->write(chunk, chunklen); client->write(chunk, chunklen);
client->print("\r\n"); client->print("\r\n");
Serial.printf("%x\r\n", chunklen + 1); Serial.printf("%x\r\n", chunklen + 1);
Serial.write(first ? "[":",",1); Serial.write(first ? "[" : ",", 1);
Serial.write(chunk, chunklen); Serial.write(chunk, chunklen);
Serial.print("\r\n"); Serial.print("\r\n");
} }