if sending mqtt fails, log to sd

This commit is contained in:
theFeiter 2022-07-04 21:54:39 +02:00
parent 60242ab2cd
commit b7dcadf011
2 changed files with 65 additions and 46 deletions

View File

@ -55,10 +55,8 @@ void setup() {
DBG("WiFi Connected");
client.setServer(mqtt_server, mqtt_port);
if (!client.connected()) {
DBG("Connecting MQTT...");
reconnect();
reconnect(5);
}
DBG("MQTT Connected");
client.setCallback(mqtt_callback);
#else
begin_espnow();
@ -130,13 +128,12 @@ void loop() {
getSerial();
}
getLoRa();
#ifdef USE_WIFI
#ifdef USE_WIFI
if (!client.connected()) {
DBG("Connecting MQTT...");
reconnect();
reconnect(1, true);
}
client.loop();
#endif
client.loop(); // for recieving incoming messages and maintaining connection
#endif
#ifdef USE_SD_LOG
unsigned long current_millis = millis();
if(current_millis-last_millis > 100){

View File

@ -173,6 +173,56 @@ void getSerial() {
}
}
void logToSD() {
#ifdef USE_SD_LOG
DBG("Logging to SD card.");
DynamicJsonDocument doc(24576);
for (int i = 0; i < ln; i++) {
doc[i]["id"] = theData[i].id;
doc[i]["type"] = theData[i].t;
doc[i]["data"] = theData[i].d;
}
String outgoingString;
serializeJson(doc, outgoingString);
File logfile = SD.open(SD_FILENAME, FILE_WRITE);
logfile.print(tenths_of_a_second_since_reset/10.0);
logfile.print(" : ");
logfile.println(outgoingString);
logfile.close();
#endif
}
void reconnect(int attempts){
reconnect(attempts, false);
}
void reconnect(int attempts, bool silent) {
#ifdef USE_WIFI
if(!silent) DBG("Connecting MQTT...");
for (int i = 1; i<=attempts; i++) {
// Attempt to connect
if (client.connect("FDRS_GATEWAY", mqtt_user, mqtt_pass)) {
// Subscribe
client.subscribe(TOPIC_COMMAND);
if(!silent) DBG(" MQTT Connected");
return;
} else {
if(!silent) {
char msg[15];
sprintf(msg, " Attempt %d/%d",i,attempts);
DBG(msg);
}
if(attempts=!1){
delay(3000);
}
}
}
if(!silent) DBG(" Connecting MQTT failed.");
#endif
}
void mqtt_callback(char* topic, byte * message, unsigned int length) {
String incomingString;
DBG(topic);
@ -199,6 +249,14 @@ void mqtt_callback(char* topic, byte * message, unsigned int length) {
}
}
void mqtt_publish(const char* payload){
#ifdef USE_WIFI
if(!client.publish(TOPIC_DATA, payload)){
DBG("Error on sending MQTT");
logToSD();
}
#endif
}
void getLoRa() {
#ifdef USE_LORA
@ -225,27 +283,6 @@ void getLoRa() {
#endif
}
void logToSD() {
#ifdef USE_SD_LOG
DBG("Logging to SD card.");
DynamicJsonDocument doc(24576);
for (int i = 0; i < ln; i++) {
doc[i]["id"] = theData[i].id;
doc[i]["type"] = theData[i].t;
doc[i]["data"] = theData[i].d;
}
String outgoingString;
serializeJson(doc, outgoingString);
File logfile = SD.open(SD_FILENAME, FILE_WRITE);
logfile.print(tenths_of_a_second_since_reset/10.0);
logfile.print(" : ");
logfile.println(outgoingString);
logfile.close();
#endif
}
void sendESPNOW(uint8_t address) {
DBG("Sending ESP-NOW.");
uint8_t NEWPEER[] = {MAC_PREFIX, address};
@ -303,7 +340,7 @@ void sendMQTT() {
}
String outgoingString;
serializeJson(doc, outgoingString);
client.publish(TOPIC_DATA, (char*) outgoingString.c_str());
mqtt_publish((char*) outgoingString.c_str());
#endif
}
@ -524,25 +561,10 @@ void releaseMQTT() {
}
String outgoingString;
serializeJson(doc, outgoingString);
client.publish(TOPIC_DATA, (char*) outgoingString.c_str());
mqtt_publish((char*) outgoingString.c_str());
lenMQTT = 0;
#endif
}
void reconnect() {
#ifdef USE_WIFI
// Loop until reconnected
while (!client.connected()) {
// Attempt to connect
if (client.connect("FDRS_GATEWAY", mqtt_user, mqtt_pass)) {
// Subscribe
client.subscribe(TOPIC_COMMAND);
} else {
DBG("Connecting MQTT.");
delay(5000);
}
}
#endif
}
void begin_espnow() {
DBG("Initializing ESP-NOW!");
WiFi.mode(WIFI_STA);