rdz_ttgo_sonde/RX_FSK/src/mqtt.cpp

142 wiersze
3.8 KiB
C++
Czysty Zwykły widok Historia

2020-11-25 10:01:18 +00:00
#include <Arduino.h>
#include "mqtt.h"
#include <WiFi.h>
#include <AsyncMqttClient.h>
2020-12-01 08:45:47 +00:00
#include <ESPmDNS.h>
2021-10-04 06:04:51 +00:00
#include "RS41.h"
2020-12-01 08:45:47 +00:00
TimerHandle_t mqttReconnectTimer;
2020-11-25 10:01:18 +00:00
void mqttCallback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i=0;i<length;i++) {
Serial.print((char)payload[i]);
}
Serial.println();
}
2021-10-03 08:03:41 +00:00
static char buffer[21];
2020-12-01 08:45:47 +00:00
void MQTT::init(const char* host, uint16_t port, const char* id, const char *username, const char *password, const char *prefix)
2020-11-25 10:01:18 +00:00
{
2020-12-01 08:45:47 +00:00
WiFi.hostByName(host, this->ip);
2020-11-25 10:01:18 +00:00
this->port = port;
this->username = username;
this->password = password;
this->prefix = prefix;
Serial.println("[MQTT] pubsub client");
mqttClient.setServer(ip, port);
snprintf(buffer, 20, "%s%04d", id, (int)random(0, 1000));
2021-10-03 08:03:41 +00:00
buffer[20] = 0;
mqttClient.setClientId(buffer);
2020-11-25 10:01:18 +00:00
if (strlen(password) > 0) {
mqttClient.setCredentials(username, password);
}
}
void MQTT::connectToMqtt() {
Serial.println("Connecting to MQTT...");
mqttClient.connect();
}
void MQTT::publishUptime()
{
2020-12-01 08:45:47 +00:00
mqttClient.connect(); // ensure we've got connection
2020-11-25 10:01:18 +00:00
Serial.println("[MQTT] writing");
char payload[12];
snprintf(payload, 12, "%lu", millis());
char topic[128];
snprintf(topic, 128, "%s%s", this->prefix, "uptime");
mqttClient.publish(topic, 1, 1, payload);
}
void MQTT::publishPacket(SondeInfo *si)
2020-11-25 10:01:18 +00:00
{
SondeData *s = &(si->d);
2020-12-01 08:45:47 +00:00
mqttClient.connect(); // ensure we've got connection
2020-11-25 10:01:18 +00:00
char payload[1024];
snprintf(payload, 1024, "{"
"\"active\": %d,"
"\"freq\": %.2f,"
"\"id\": \"%s\","
"\"ser\": \"%s\","
"\"validId\": %d,"
"\"launchsite\": \"%s\","
"\"lat\": %.5f,"
"\"lon\": %.5f,"
"\"alt\": %.1f,"
"\"vs\": %.1f,"
"\"hs\": %.1f,"
"\"dir\": %.1f,"
"\"sats\": %d,"
"\"validPos\": %d,"
"\"time\": %u,"
"\"frame\": %u,"
2020-11-25 10:01:18 +00:00
"\"validTime\": %d,"
"\"rssi\": %d,"
"\"afc\": %d,"
"\"rxStat\": \"%s\","
"\"rxStart\": %u,"
"\"norxStart\": %u,"
"\"viewStart\": %u,"
2020-11-25 10:01:18 +00:00
"\"lastState\": %d,"
"\"launchKT\": %d,"
"\"burstKT\": %d,"
"\"countKT\": %d,"
2021-10-04 06:04:51 +00:00
"\"crefKT\": %d",
(int)si->active,
si->freq,
2020-11-25 10:01:18 +00:00
s->id,
s->ser,
(int)s->validID,
si->launchsite,
2020-11-25 10:01:18 +00:00
s->lat,
s->lon,
s->alt,
s->vs,
s->hs,
s->dir,
s->sats,
s->validPos,
s->time,
s->frame,
(int)s->validTime,
si->rssi,
si->afc,
si->rxStat,
si->rxStart,
si->norxStart,
si->viewStart,
si->lastState,
2020-11-25 10:01:18 +00:00
s->launchKT,
s->burstKT,
s->countKT,
s->crefKT
);
2021-10-04 06:04:51 +00:00
if ( !isnan( s->temperature ) ) {
snprintf(payload, 1024, "%s%s%.1f", payload, ",\"temperature\": ", s->temperature );
}
if ( !isnan( s->relativeHumidity ) ) {
snprintf(payload, 1024, "%s%s%.1f", payload, ",\"relativeHumidity\": ", s->relativeHumidity );
}
if ( !isnan( s->pressure ) ) {
snprintf(payload, 1024, "%s%s%.1f", payload, ",\"pressure\": ", s->pressure );
}
if ( !isnan( s->batteryVoltage && s->batteryVoltage > 0 ) ) {
snprintf(payload, 1024, "%s%s%.1f", payload, ",\"batteryVoltage\": ", s->batteryVoltage );
}
char subtype[11];
if ( RS41::getSubtype( subtype, 11, si) == 0 ) {
snprintf(payload, 1024, "%s%s%s%s", payload, ",\"subtype\": \"", subtype, "\"" );
}
snprintf(payload, 1024, "%s%s", payload, "}" ); // terminate payload string
2020-11-25 10:01:18 +00:00
char topic[128];
snprintf(topic, 128, "%s%s", this->prefix, "packet");
mqttClient.publish(topic, 1, 1, payload);
}