diff --git a/main/app/odroid-power-mate.c b/main/app/odroid-power-mate.c index 08be324..09a8acb 100644 --- a/main/app/odroid-power-mate.c +++ b/main/app/odroid-power-mate.c @@ -11,6 +11,7 @@ #include "nvs_flash.h" #include "system.h" #include "wifi.h" +#include "storage.h" void app_main(void) { @@ -30,6 +31,8 @@ void app_main(void) } ESP_ERROR_CHECK(ret); + storage_init(); + ESP_ERROR_CHECK(esp_netif_init()); ESP_ERROR_CHECK(esp_event_loop_create_default()); diff --git a/main/include/storage.h b/main/include/storage.h new file mode 100644 index 0000000..c459294 --- /dev/null +++ b/main/include/storage.h @@ -0,0 +1,6 @@ +#ifndef STORAGE_H_ +#define STORAGE_H_ + +void storage_init(void); + +#endif /* STORAGE_H_ */ diff --git a/main/service/datalog.c b/main/service/datalog.c deleted file mode 100644 index d626c12..0000000 --- a/main/service/datalog.c +++ /dev/null @@ -1,157 +0,0 @@ -#include "datalog.h" -#include -#include -#include -#include -#include -#include "esp_littlefs.h" -#include "esp_log.h" - -static const char* TAG = "datalog"; -static const char* LOG_FILE_PATH = "/littlefs/datalog.csv"; - -#define MAX_LOG_SIZE (700 * 1024) - -void datalog_init(void) -{ - ESP_LOGI(TAG, "Initializing DataLog with LittleFS"); - - esp_vfs_littlefs_conf_t conf = { - .base_path = "/littlefs", - .partition_label = "littlefs", - .format_if_mount_failed = true, - .dont_mount = false, - }; - - esp_err_t ret = esp_vfs_littlefs_register(&conf); - - if (ret != ESP_OK) - { - if (ret == ESP_FAIL) - { - ESP_LOGE(TAG, "Failed to mount or format filesystem"); - } - else if (ret == ESP_ERR_NOT_FOUND) - { - ESP_LOGE(TAG, "Failed to find LittleFS partition"); - } - else - { - ESP_LOGE(TAG, "Failed to initialize LittleFS (%s)", esp_err_to_name(ret)); - } - return; - } - - size_t total = 0, used = 0; - ret = esp_littlefs_info(conf.partition_label, &total, &used); - if (ret != ESP_OK) - { - ESP_LOGE(TAG, "Failed to get LittleFS partition information (%s)", esp_err_to_name(ret)); - } - else - { - ESP_LOGI(TAG, "Partition size: total: %d, used: %d", total, used); - } - - // Check if file exists - FILE* f = fopen(LOG_FILE_PATH, "r"); - if (f == NULL) - { - ESP_LOGI(TAG, "Log file not found, creating new one."); - FILE* f_write = fopen(LOG_FILE_PATH, "w"); - if (f_write == NULL) - { - ESP_LOGE(TAG, "Failed to create log file."); - } - else - { - // Add header for 3 channels - fprintf(f_write, - "timestamp,usb_voltage,usb_current,usb_power,main_voltage,main_current,main_power,vin_voltage,vin_" - "current,vin_power\n"); - fclose(f_write); - } - } - else - { - // Here we could check if the header is correct, but for now we assume it is. - ESP_LOGI(TAG, "Log file found."); - fclose(f); - } -} - -void datalog_add(uint32_t timestamp, const channel_data_t* channel_data) -{ - struct stat st; - if (stat(LOG_FILE_PATH, &st) == 0) - { - if (st.st_size >= MAX_LOG_SIZE) - { - ESP_LOGI(TAG, "Log file size (%ld) >= MAX_LOG_SIZE (%d). Truncating.", st.st_size, MAX_LOG_SIZE); - - const char* temp_path = "/littlefs/datalog.tmp"; - FILE* f_read = fopen(LOG_FILE_PATH, "r"); - FILE* f_write = fopen(temp_path, "w"); - - if (f_read == NULL || f_write == NULL) - { - ESP_LOGE(TAG, "Failed to open files for truncation."); - if (f_read) - fclose(f_read); - if (f_write) - fclose(f_write); - return; - } - - char line[256]; - // Copy header - if (fgets(line, sizeof(line), f_read) != NULL) - { - fputs(line, f_write); - } - - // Skip the oldest data line - if (fgets(line, sizeof(line), f_read) == NULL) - { - // No data lines to skip, something is wrong if we are truncating - } - - // Copy the rest of the lines - while (fgets(line, sizeof(line), f_read) != NULL) - { - fputs(line, f_write); - } - - fclose(f_read); - fclose(f_write); - - // Replace the old log with the new one - if (remove(LOG_FILE_PATH) != 0) - { - ESP_LOGE(TAG, "Failed to remove old log file."); - } - if (rename(temp_path, LOG_FILE_PATH) != 0) - { - ESP_LOGE(TAG, "Failed to rename temp file."); - return; - } - } - } - - FILE* f = fopen(LOG_FILE_PATH, "a"); - if (f == NULL) - { - ESP_LOGE(TAG, "Failed to open log file for appending."); - return; - } - - fprintf(f, "%lu", timestamp); - for (int i = 0; i < 3; ++i) - { - fprintf(f, ",%.3f,%.3f,%.3f", channel_data[i].voltage, channel_data[i].current, channel_data[i].power); - } - fprintf(f, "\n"); - fclose(f); -} - -const char* datalog_get_path(void) { return LOG_FILE_PATH; } diff --git a/main/service/datalog.h b/main/service/datalog.h deleted file mode 100644 index 71bfded..0000000 --- a/main/service/datalog.h +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef MAIN_SERVICE_DATALOG_H_ -#define MAIN_SERVICE_DATALOG_H_ - -#include - -#define NUM_CHANNELS 3 - -typedef struct -{ - float voltage; - float current; - float power; -} channel_data_t; - -void datalog_init(void); -void datalog_add(uint32_t timestamp, const channel_data_t* channel_data); -const char* datalog_get_path(void); - -#endif /* MAIN_SERVICE_DATALOG_H_ */ diff --git a/main/service/monitor.c b/main/service/monitor.c index 4770753..0655ef3 100644 --- a/main/service/monitor.c +++ b/main/service/monitor.c @@ -6,7 +6,6 @@ #include #include #include "climit.h" -#include "datalog.h" #include "esp_log.h" #include "esp_netif.h" #include "esp_timer.h" @@ -94,8 +93,6 @@ static void sensor_timer_callback(void* arg) uint32_t uptime_sec = (uint32_t)(uptime_us / 1000000); uint32_t timestamp = (uint32_t)time(NULL); - channel_data_t channel_data_log[NUM_CHANNELS]; - StatusMessage message = StatusMessage_init_zero; message.which_payload = StatusMessage_sensor_data_tag; SensorData* sensor_data = &message.payload.sensor_data; @@ -115,9 +112,6 @@ static void sensor_timer_callback(void* arg) current /= 1000.0f; // mA to A power = voltage * current; - // For datalog - channel_data_log[i] = (channel_data_t){.voltage = voltage, .current = current, .power = power}; - // For protobuf channels[i]->voltage = voltage; channels[i]->current = current; @@ -282,8 +276,6 @@ void init_status_monitor() lim = atof(buf); climit_set_usb(lim); - datalog_init(); - const esp_timer_create_args_t sensor_timer_args = {.callback = &sensor_timer_callback, .name = "sensor_reading_timer"}; const esp_timer_create_args_t wifi_timer_args = {.callback = &status_wifi_callback, .name = "wifi_status_timer"}; diff --git a/main/service/storage.c b/main/service/storage.c new file mode 100644 index 0000000..35999d2 --- /dev/null +++ b/main/service/storage.c @@ -0,0 +1,53 @@ +#include "storage.h" +#include +#include +#include +#include +#include "esp_littlefs.h" +#include "esp_log.h" + +static const char* TAG = "datalog"; + +#define MAX_LOG_SIZE (700 * 1024) + +void storage_init(void) +{ + ESP_LOGI(TAG, "Initializing DataLog with LittleFS"); + + esp_vfs_littlefs_conf_t conf = { + .base_path = "/littlefs", + .partition_label = "littlefs", + .format_if_mount_failed = true, + .dont_mount = false, + }; + + esp_err_t ret = esp_vfs_littlefs_register(&conf); + + if (ret != ESP_OK) + { + if (ret == ESP_FAIL) + { + ESP_LOGE(TAG, "Failed to mount or format filesystem"); + } + else if (ret == ESP_ERR_NOT_FOUND) + { + ESP_LOGE(TAG, "Failed to find LittleFS partition"); + } + else + { + ESP_LOGE(TAG, "Failed to initialize LittleFS (%s)", esp_err_to_name(ret)); + } + return; + } + + size_t total = 0, used = 0; + ret = esp_littlefs_info(conf.partition_label, &total, &used); + if (ret != ESP_OK) + { + ESP_LOGE(TAG, "Failed to get LittleFS partition information (%s)", esp_err_to_name(ret)); + } + else + { + ESP_LOGI(TAG, "Partition size: total: %d, used: %d", total, used); + } +} diff --git a/main/service/webserver.c b/main/service/webserver.c index 8cd4f00..bb2f3db 100644 --- a/main/service/webserver.c +++ b/main/service/webserver.c @@ -1,7 +1,6 @@ #include "webserver.h" #include #include -#include "datalog.h" #include "esp_http_server.h" #include "esp_log.h" #include "esp_wifi.h" @@ -43,38 +42,6 @@ static esp_err_t index_handler(httpd_req_t* req) return ESP_OK; } -static esp_err_t datalog_download_handler(httpd_req_t* req) -{ - const char* filepath = datalog_get_path(); - FILE* f = fopen(filepath, "r"); - if (f == NULL) - { - ESP_LOGE(TAG, "Failed to open datalog file for reading"); - httpd_resp_send_404(req); - return ESP_FAIL; - } - - httpd_resp_set_type(req, "text/csv"); - httpd_resp_set_hdr(req, "Content-Disposition", "attachment; filename=\"datalog.csv\""); - - char buffer[1024]; - size_t bytes_read; - while ((bytes_read = fread(buffer, 1, sizeof(buffer), f)) > 0) - { - if (httpd_resp_send_chunk(req, buffer, bytes_read) != ESP_OK) - { - ESP_LOGE(TAG, "File sending failed!"); - fclose(f); - httpd_resp_send_chunk(req, NULL, 0); - httpd_resp_send_500(req); - return ESP_FAIL; - } - } - - fclose(f); - httpd_resp_send_chunk(req, NULL, 0); - return ESP_OK; -} void start_webserver(void) { @@ -94,10 +61,6 @@ void start_webserver(void) httpd_uri_t index = {.uri = "/", .method = HTTP_GET, .handler = index_handler, .user_ctx = NULL}; httpd_register_uri_handler(server, &index); - httpd_uri_t datalog_uri = { - .uri = "/datalog.csv", .method = HTTP_GET, .handler = datalog_download_handler, .user_ctx = NULL}; - httpd_register_uri_handler(server, &datalog_uri); - register_wifi_endpoint(server); register_ws_endpoint(server); register_control_endpoint(server);