kopia lustrzana https://github.com/dl9rdz/rdz_ttgo_sonde
better mqtt reconnect
rodzic
20a5d72805
commit
aacd86aa8a
|
@ -1,6 +1,8 @@
|
|||
#include "../features.h"
|
||||
#if FEATURE_MQTT
|
||||
|
||||
#include "core.h"
|
||||
|
||||
#include <Arduino.h>
|
||||
#include "conn-mqtt.h"
|
||||
#include <WiFi.h>
|
||||
|
@ -23,6 +25,9 @@ extern const char *version_id;
|
|||
|
||||
TimerHandle_t mqttReconnectTimer;
|
||||
|
||||
extern t_wifi_state wifi_state;
|
||||
|
||||
|
||||
/* Global initalization (on TTGO startup) */
|
||||
void MQTT::init() {
|
||||
}
|
||||
|
@ -75,8 +80,8 @@ void MQTT::updateStation( PosInfo *pi ) {
|
|||
if(!sonde.config.mqtt.active)
|
||||
return;
|
||||
|
||||
int now = millis();
|
||||
if ( (lastMqttUptime == 0 || (lastMqttUptime + 60000 < now) || (lastMqttUptime > now))) {
|
||||
unsigned long now = millis();
|
||||
if ( (lastMqttUptime == 0) || (now - lastMqttUptime > 60000) ) {
|
||||
publishUptime();
|
||||
lastMqttUptime = now;
|
||||
}
|
||||
|
@ -87,14 +92,21 @@ String MQTT::getStatus() {
|
|||
}
|
||||
|
||||
// Internal (private) functions
|
||||
//void MQTT::connectToMqtt() {
|
||||
// Serial.println("Connecting to MQTT...");
|
||||
// mqttClient.connect();
|
||||
//}
|
||||
int MQTT::connectToMqtt() {
|
||||
if(mqttClient.connected())
|
||||
return 1;
|
||||
if(wifi_state != WIFI_CONNECTED)
|
||||
return 0;
|
||||
Serial.println("MQTT not connected, connecting....");
|
||||
mqttClient.connect();
|
||||
return 1;
|
||||
}
|
||||
|
||||
void MQTT::publishUptime()
|
||||
{
|
||||
mqttClient.connect(); // ensure we've got connection
|
||||
// ensure we've got connection
|
||||
if(!connectToMqtt())
|
||||
return;
|
||||
|
||||
Serial.println("[MQTT] writing");
|
||||
char payload[256];
|
||||
|
@ -114,7 +126,9 @@ void MQTT::publishUptime()
|
|||
void MQTT::publishPacket(SondeInfo *si)
|
||||
{
|
||||
SondeData *s = &(si->d);
|
||||
mqttClient.connect(); // ensure we've got connection
|
||||
// ensure we've got connection
|
||||
if(!connectToMqtt())
|
||||
return;
|
||||
|
||||
char payload[1024];
|
||||
payload[0] = '{';
|
||||
|
|
|
@ -41,7 +41,7 @@ private:
|
|||
//void init(const char *host, uint16_t port, const char *id, const char *username, const char *password, const char *prefix);
|
||||
void publishPacket(SondeInfo *s);
|
||||
void publishUptime();
|
||||
//void connectToMqtt();
|
||||
int connectToMqtt();
|
||||
|
||||
unsigned long lastMqttUptime = 0;
|
||||
boolean mqttEnabled;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
const char *version_name = "rdzTTGOsonde";
|
||||
const char *version_id = "devel20240225";
|
||||
const char *version_id = "devel20240316";
|
||||
const int SPIFFS_MAJOR=2;
|
||||
const int SPIFFS_MINOR=17;
|
||||
|
|
Ładowanie…
Reference in New Issue