mirror of
https://github.com/timmbogner/Farm-Data-Relay-System
synced 2024-11-10 07:10:42 +00:00
if sending mqtt fails, log to sd
This commit is contained in:
parent
60242ab2cd
commit
b7dcadf011
@ -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();
|
||||
@ -132,10 +130,9 @@ void loop() {
|
||||
getLoRa();
|
||||
#ifdef USE_WIFI
|
||||
if (!client.connected()) {
|
||||
DBG("Connecting MQTT...");
|
||||
reconnect();
|
||||
reconnect(1, true);
|
||||
}
|
||||
client.loop();
|
||||
client.loop(); // for recieving incoming messages and maintaining connection
|
||||
#endif
|
||||
#ifdef USE_SD_LOG
|
||||
unsigned long current_millis = millis();
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user