mirror of
https://github.com/timmbogner/Farm-Data-Relay-System
synced 2024-11-10 07:10:42 +00:00
Merge pull request #56 from thefeiter/buffer-log-writes
Buffer log writes before saving them to the file.
This commit is contained in:
commit
d952081a22
@ -85,53 +85,59 @@ void setup() {
|
||||
|
||||
void loop() {
|
||||
#ifdef ESPNOWG_DELAY
|
||||
if (millis() > timeESPNOWG) {
|
||||
timeESPNOWG += ESPNOWG_DELAY;
|
||||
if ((millis() - timeESPNOWG) >= ESPNOWG_DELAY) {
|
||||
timeESPNOWG = millis();
|
||||
if (lenESPNOWG > 0) releaseESPNOW(0);
|
||||
}
|
||||
#endif
|
||||
#ifdef ESPNOW1_DELAY
|
||||
if (millis() > timeESPNOW1) {
|
||||
timeESPNOW1 += ESPNOW1_DELAY;
|
||||
if ((millis() - timeESPNOW1) >= ESPNOW1_DELAY) {
|
||||
timeESPNOW1 = millis();
|
||||
if (lenESPNOW1 > 0) releaseESPNOW(1);
|
||||
}
|
||||
#endif
|
||||
#ifdef ESPNOW2_DELAY
|
||||
if (millis() > timeESPNOW2) {
|
||||
timeESPNOW2 += ESPNOW2_DELAY;
|
||||
if ((millis() - timeESPNOW2) >= ESPNOW2_DELAY) {
|
||||
timeESPNOW2 = millis();
|
||||
if (lenESPNOW2 > 0) releaseESPNOW(2);
|
||||
}
|
||||
#endif
|
||||
#ifdef SERIAL_DELAY
|
||||
if (millis() > timeSERIAL) {
|
||||
timeSERIAL += SERIAL_DELAY;
|
||||
if ((millis() - timeSERIAL) >= SERIAL_DELAY) {
|
||||
timeSERIAL = millis();
|
||||
if (lenSERIAL > 0) releaseSerial();
|
||||
}
|
||||
#endif
|
||||
#ifdef MQTT_DELAY
|
||||
if (millis() > timeMQTT) {
|
||||
timeMQTT += MQTT_DELAY;
|
||||
if ((millis() - timeMQTT) >= MQTT_DELAY) {
|
||||
timeMQTT = millis();
|
||||
if (lenMQTT > 0) releaseMQTT();
|
||||
}
|
||||
#endif
|
||||
#ifdef LORAG_DELAY
|
||||
if (millis() > timeLORAG) {
|
||||
timeLORAG += LORAG_DELAY;
|
||||
if ((millis() - timeLORAG) >= LORAG_DELAY) {
|
||||
timeLORAG = millis();
|
||||
if (lenLORAG > 0) releaseLoRa(0);
|
||||
}
|
||||
#endif
|
||||
#ifdef LORA1_DELAY
|
||||
if (millis() > timeLORA1) {
|
||||
timeLORA1 += LORA1_DELAY;
|
||||
if ((millis() - timeLORA1) >= LORA1_DELAY) {
|
||||
timeLORA1 = millis();
|
||||
if (lenLORA1 > 0) releaseLoRa(1);
|
||||
}
|
||||
#endif
|
||||
#ifdef LORA2_DELAY
|
||||
if (millis() > timeLORA2) {
|
||||
timeLORA2 += LORA2_DELAY;
|
||||
if ((millis() - timeLORA2) >= LORA2_DELAY) {
|
||||
timeLORA2 = millis();
|
||||
if (lenLORA2 > 0) releaseLoRa(2);
|
||||
}
|
||||
#endif
|
||||
#if defined (USE_SD_LOG) || defined (USE_FS_LOG)
|
||||
if ((millis() - timeLOGBUF) >= LOGBUF_DELAY){
|
||||
timeLOGBUF = millis();
|
||||
if (logBufferPos > 0) releaseLogBuffer();
|
||||
}
|
||||
#endif
|
||||
|
||||
while (UART_IF.available()) {
|
||||
getSerial();
|
||||
@ -145,13 +151,13 @@ void loop() {
|
||||
|
||||
timeClient.update(); //update internal clock if possible
|
||||
#endif
|
||||
#ifdef USE_SD_LOG
|
||||
unsigned long current_millis = millis();
|
||||
if(current_millis-last_millis >= 1000){
|
||||
seconds_since_reset+=(current_millis-last_millis)/1000;
|
||||
last_millis=current_millis;
|
||||
}
|
||||
#endif
|
||||
#if defined (USE_SD_LOG) || defined (USE_FS_LOG)
|
||||
unsigned long current_millis = millis();
|
||||
if(current_millis-last_millis >= 1000){
|
||||
seconds_since_reset+=(current_millis-last_millis)/1000;
|
||||
last_millis=current_millis;
|
||||
}
|
||||
#endif
|
||||
if (newData) {
|
||||
switch (newData) {
|
||||
case event_espnowg:
|
||||
|
@ -88,9 +88,12 @@ uint8_t LoRa2[] = {mac_prefix[3], mac_prefix[4], LORA2_PEER};
|
||||
//uint8_t LoRaAddress[] = {0x42, 0x00};
|
||||
#endif
|
||||
|
||||
#ifdef USE_SD_LOG
|
||||
#if defined (USE_SD_LOG) || defined (USE_FS_LOG)
|
||||
unsigned long last_millis = 0;
|
||||
unsigned long seconds_since_reset = 0;
|
||||
char logBuffer[512];
|
||||
uint16_t logBufferPos = 0; // datatype depends on size of sdBuffer
|
||||
uint32_t timeLOGBUF = 0;
|
||||
#endif
|
||||
|
||||
DataReading theData[256];
|
||||
@ -191,37 +194,46 @@ void getSerial() {
|
||||
|
||||
}
|
||||
}
|
||||
void sendSD(const char filename[32]) {
|
||||
#ifdef USE_SD_LOG
|
||||
DBG("Logging to SD card.");
|
||||
File logfile = SD.open(filename, FILE_WRITE);
|
||||
for (int i = 0; i < ln; i++) {
|
||||
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);
|
||||
}
|
||||
#if defined (USE_SD_LOG) || defined (USE_FS_LOG)
|
||||
void releaseLogBuffer()
|
||||
{
|
||||
#ifdef USE_SD_LOG
|
||||
DBG("Releasing Log buffer to SD");
|
||||
File logfile = SD.open(SD_FILENAME, FILE_WRITE);
|
||||
logfile.print(logBuffer);
|
||||
logfile.close();
|
||||
#endif
|
||||
#endif
|
||||
#ifdef USE_FS_LOG
|
||||
DBG("Releasing Log buffer to internal flash.");
|
||||
File logfile = LittleFS.open(FS_FILENAME, "a");
|
||||
logfile.print(logBuffer);
|
||||
logfile.close();
|
||||
#endif
|
||||
memset(&(logBuffer[0]), 0, sizeof(logBuffer)/sizeof(char));
|
||||
logBufferPos = 0;
|
||||
}
|
||||
void sendFS(const char filename[32]) {
|
||||
#ifdef USE_FS_LOG
|
||||
DBG("Logging to internal flash.");
|
||||
File logfile = LittleFS.open(filename, "a");
|
||||
for (int i = 0; i < ln; i++) {
|
||||
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);
|
||||
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);
|
||||
}
|
||||
logfile.close();
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
void reconnect(short int attempts, bool silent) {
|
||||
#ifdef USE_WIFI
|
||||
@ -283,8 +295,7 @@ void mqtt_publish(const char* payload) {
|
||||
#ifdef USE_WIFI
|
||||
if (!client.publish(TOPIC_DATA, payload)) {
|
||||
DBG(" Error on sending MQTT");
|
||||
sendSD(SD_FILENAME);
|
||||
sendFS(FS_FILENAME);
|
||||
sendLog();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
@ -86,9 +86,10 @@
|
||||
#define TOPIC_COMMAND "fdrs/command"
|
||||
|
||||
|
||||
// SD card logging config -- Needed only for SD-card logging
|
||||
#define SD_SS 0 //SD card Chipselect pin (Use a different pins for LoRa and SD)
|
||||
#define SD_FILENAME "fdrs_log.csv" // length max. 32
|
||||
// Logging settings
|
||||
#define LOGBUF_DELAY 10000 // Log Buffer Delay - in milliseconds -- Needed only for SD-card OR internal flash logging
|
||||
|
||||
// Internal flash logging config -- Needed only for internal flash logging
|
||||
#define FS_FILENAME "fdrs_log.csv" // length max. 32
|
||||
#define SD_SS 0 //SD card Chipselect pin (Use a different pins for LoRa and SD) -- Needed only for SD-card logging
|
||||
#define SD_FILENAME "fdrs_log.csv" // length max. 32 -- Needed only for SD-card logging
|
||||
|
||||
#define FS_FILENAME "fdrs_log.csv" // length max. 32 -- Needed only for internal flash logging
|
||||
|
Loading…
Reference in New Issue
Block a user