mirror of
https://github.com/timmbogner/Farm-Data-Relay-System
synced 2024-11-10 07:10:42 +00:00
use buffering for all logs
This commit is contained in:
parent
876d860a05
commit
107c1413a1
@ -88,11 +88,16 @@ uint8_t LoRa2[] = {mac_prefix[3], mac_prefix[4], LORA2_PEER};
|
|||||||
//uint8_t LoRaAddress[] = {0x42, 0x00};
|
//uint8_t LoRaAddress[] = {0x42, 0x00};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef USE_SD_LOG
|
#ifndef USE_WIFI
|
||||||
unsigned long last_millis = 0;
|
unsigned long last_millis = 0;
|
||||||
unsigned long seconds_since_reset = 0;
|
unsigned long seconds_since_reset = 0;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if defined (USE_SD_LOG) || defined (USE_FS_LOG)
|
||||||
|
char logBuffer[512];
|
||||||
|
uint16_t logBufferPos = 0; // datatype depends on size of sdBuffer
|
||||||
|
#endif
|
||||||
|
|
||||||
DataReading theData[256];
|
DataReading theData[256];
|
||||||
uint8_t ln;
|
uint8_t ln;
|
||||||
uint8_t newData = event_clear;
|
uint8_t newData = event_clear;
|
||||||
@ -191,37 +196,46 @@ void getSerial() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void sendSD(const char filename[32]) {
|
#if defined (USE_SD_LOG) || defined (USE_FS_LOG)
|
||||||
#ifdef USE_SD_LOG
|
void releaseLogBuffer()
|
||||||
DBG("Logging to SD card.");
|
{
|
||||||
File logfile = SD.open(filename, FILE_WRITE);
|
#ifdef USE_SD_LOG
|
||||||
for (int i = 0; i < ln; i++) {
|
DBG("Releasing Log buffer to SD");
|
||||||
char linebuf[32];
|
File logfile = SD.open(SD_FILENAME, FILE_WRITE);
|
||||||
#ifdef USE_WIFI
|
logfile.print(logBuffer);
|
||||||
sprintf(linebuf, "%ld,%d,%d,%g", timeClient.getEpochTime(), theData[i].id, theData[i].t, theData[i].d);
|
|
||||||
#else
|
|
||||||
sprintf(linebuf, "%ld,%d,%d,%g", seconds_since_reset, theData[i].id, theData[i].t, theData[i].d);
|
|
||||||
#endif
|
|
||||||
logfile.println(linebuf);
|
|
||||||
}
|
|
||||||
logfile.close();
|
logfile.close();
|
||||||
#endif
|
#endif
|
||||||
}
|
#ifdef USE_FS_LOG
|
||||||
void sendFS(const char filename[32]) {
|
DBG("Releasing Log buffer to internal flash.");
|
||||||
#ifdef USE_FS_LOG
|
|
||||||
DBG("Logging to internal flash.");
|
|
||||||
File logfile = LittleFS.open(filename, "a");
|
File logfile = LittleFS.open(filename, "a");
|
||||||
for (int i = 0; i < ln; i++) {
|
logfile.print(logBuffer);
|
||||||
char linebuf[32];
|
|
||||||
#ifdef USE_WIFI
|
|
||||||
sprintf(linebuf, "%ld,%d,%d,%g", timeClient.getEpochTime(), theData[i].id, theData[i].t, theData[i].d);
|
|
||||||
#else
|
|
||||||
sprintf(linebuf, "%ld,%d,%d,%g", seconds_since_reset, theData[i].id, theData[i].t, theData[i].d);
|
|
||||||
#endif
|
|
||||||
logfile.println(linebuf);
|
|
||||||
}
|
|
||||||
logfile.close();
|
logfile.close();
|
||||||
|
#endif
|
||||||
|
memset(&(logBuffer[0]), 0, sizeof(logBuffer)/sizeof(char));
|
||||||
|
logBufferPos = 0;
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
void sendLog()
|
||||||
|
{
|
||||||
|
#if defined (USE_SD_LOG) || defined (USE_FS_LOG)
|
||||||
|
DBG("Logging to buffer");
|
||||||
|
for (int i = 0; i < ln; i++)
|
||||||
|
{
|
||||||
|
char linebuf[34]; // size depends on resulting length of the formatting string
|
||||||
|
#ifdef USE_WIFI
|
||||||
|
sprintf(linebuf, "%ld,%d,%d,%g\r\n", timeClient.getEpochTime(), theData[i].id, theData[i].t, theData[i].d);
|
||||||
|
#else
|
||||||
|
sprintf(linebuf, "%ld,%d,%d,%g\r\n", seconds_since_reset, theData[i].id, theData[i].t, theData[i].d);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (logBufferPos+strlen(linebuf) >= (sizeof(logBuffer)/sizeof(char))) // if buffer would overflow, release first
|
||||||
|
{
|
||||||
|
releaseLogBuffer();
|
||||||
|
}
|
||||||
|
memcpy(&logBuffer[logBufferPos], linebuf, strlen(linebuf)); //append line to buffer
|
||||||
|
logBufferPos+=strlen(linebuf);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
void reconnect(short int attempts, bool silent) {
|
void reconnect(short int attempts, bool silent) {
|
||||||
#ifdef USE_WIFI
|
#ifdef USE_WIFI
|
||||||
@ -283,8 +297,7 @@ void mqtt_publish(const char* payload) {
|
|||||||
#ifdef USE_WIFI
|
#ifdef USE_WIFI
|
||||||
if (!client.publish(TOPIC_DATA, payload)) {
|
if (!client.publish(TOPIC_DATA, payload)) {
|
||||||
DBG(" Error on sending MQTT");
|
DBG(" Error on sending MQTT");
|
||||||
sendSD(SD_FILENAME);
|
sendLog();
|
||||||
sendFS(FS_FILENAME);
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user