Skip to content
Snippets Groups Projects
Verified Commit 904ca075 authored by Zoe Michaela Dietmar Pfister's avatar Zoe Michaela Dietmar Pfister :gay_pride_flag:
Browse files

WIP: move logging to sd function to separate file

parent 8c84b0c1
No related branches found
No related tags found
3 merge requests!39Merge Develop into Main,!25Minor additions to host,!24Refactor of Host ESP32
......@@ -6,6 +6,7 @@
#define HOST_CENTRAL_MAST_DEFINITIONS_H
#include "Arduino.h"
#include "ESP32Time.h"
#define uS_TO_S_FACTOR 1000000ULL // Conversion factor for micro seconds to seconds
#define TIME_TO_SLEEP 5 // Time ESP32 will go to sleep (in seconds)
......@@ -22,7 +23,6 @@
#define SD_CS 13
#define LED_PIN 12
const String INFLUXDB_TOKEN =
"dUh2gbVLv7e3egqocxriDsJQNUacA9qZ5YXsYtdnVAglnHgy4nx-jDVO7nGlSF34BosfnuwnUDaviC7dQeC5RQ==";
extern ESP32Time rtc;
#endif // HOST_CENTRAL_MAST_DEFINITIONS_H
//
// Created by zoe on 1/24/23.
//
#include "SDCardLogger.h"
namespace SDCardLogger {
char log_print_buffer[512];
bool printToSerial = false;
void printDebugToSerial(bool printToSerialAsWell) {
printToSerial = printToSerialAsWell;
}
int vprintf_into_sd(const char *szFormat, va_list args) {
String logstring = "[" + rtc.getDateTime() + "] ";
logstring += szFormat;
// write evaluated format string into buffer
int ret = vsnprintf(log_print_buffer, sizeof(log_print_buffer), logstring.c_str(), args);
String date = rtc.getDate();
String filename = "/log_" + date + ".txt";
// output is now in buffer. write to file.
if (ret >= 0) {
if (!SD.exists(filename)) {
File writeLog = SD.open(filename, FILE_WRITE);
if (!writeLog)
Serial.println("Couldn't open " + filename + " for writing");
delay(50);
writeLog.close();
}
File logFile = SD.open(filename, FILE_APPEND);
// debug output
if (printToSerial) {
vprintf(logstring.c_str(), args);
}
logFile.write((uint8_t *)log_print_buffer, (size_t)ret);
// to be safe in case of crashes: flush the output
logFile.flush();
logFile.close();
}
return ret;
}
} // namespace SDCardLogger
//
// Created by zoe on 1/24/23.
//
#ifndef HOST_CENTRAL_MAST_SDCARDLOGGER_H
#define HOST_CENTRAL_MAST_SDCARDLOGGER_H
#include "Definitions.h"
#include "SD.h"
namespace SDCardLogger {
void printDebugToSerial(bool printToSerialAsWell);
int vprintf_into_sd(const char *szFormat, va_list args);
}; // namespace SDCardLogger
#endif // HOST_CENTRAL_MAST_SDCARDLOGGER_H
......@@ -3,10 +3,12 @@
#include "ConnectionManager.h"
#include "FS.h"
#include "SD.h"
#include "SDCardLogger.h"
#include "SPI.h"
#include "time.h"
#include <Arduino.h>
#include <ArduinoJson.h>
#include <Definitions.h>
#include <ESP32Time.h>
#include <WiFi.h>
#include <esp_log.h>
......@@ -38,20 +40,6 @@ const char apn[] = "m2m.public.at"; // SET TO YOUR APN
const char gprsUser[] = "";
const char gprsPass[] = "";
#define uS_TO_S_FACTOR 1000000ULL // Conversion factor for micro seconds to seconds
#define TIME_TO_SLEEP 5 // Time ESP32 will go to sleep (in seconds)
#define UART_BAUD 115200
#define PIN_DTR 25
#define PIN_TX 27
#define PIN_RX 26
#define PWR_PIN 4
#define SD_MISO 2
#define SD_MOSI 15
#define SD_SCLK 14
#define SD_CS 13
#define LED_PIN 12
const GPRSCredentials gprsCredentials = GPRSCredentials("m2m.public.at", "", "");
......@@ -97,39 +85,6 @@ void on_data_sent(const uint8_t *mac_addr, esp_now_send_status_t status) {
// go to sleep
}
static char log_print_buffer[512];
int vprintf_into_sd(const char *szFormat, va_list args) {
String logstring = "[" + rtc.getDateTime() + "] ";
logstring += szFormat;
// write evaluated format string into buffer
int ret = vsnprintf(log_print_buffer, sizeof(log_print_buffer), logstring.c_str(), args);
String date = rtc.getDate();
String filename = "/log_" + date + ".txt";
// output is now in buffer. write to file.
if (ret >= 0) {
if (!SD.exists(filename)) {
File writeLog = SD.open(filename, FILE_WRITE);
if (!writeLog)
Serial.println("Couldn't open " + filename + " for writing");
delay(50);
writeLog.close();
}
File logFile = SD.open(filename, FILE_APPEND);
// debug output
vprintf(logstring.c_str(), args);
logFile.write((uint8_t *)log_print_buffer, (size_t)ret);
// to be safe in case of crashes: flush the output
logFile.flush();
logFile.close();
}
return ret;
}
String getMacAddressAsString(const uint8_t *mac);
DynamicJsonDocument parseReceivedJsonData(char *data);
......@@ -336,8 +291,10 @@ void setup() {
delay(10);
setupSDCard();
SDCardLogger::printDebugToSerial(true);
// https://stackoverflow.com/questions/60442350/arduinos-esp-log-set-vprintf-does-not-work-on-esp32
// esp_log_set_vprintf(&vprintf_into_sd);
esp_log_set_vprintf(&SDCardLogger::vprintf_into_sd);
esp_log_level_set("*", ESP_LOG_VERBOSE);
esp_log_write(ESP_LOG_DEBUG, TAG.c_str(), "%s", WiFi.macAddress().c_str());
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment