Skip to content
This repository has been archived by the owner on Aug 27, 2023. It is now read-only.

Commit

Permalink
Logging is supported
Browse files Browse the repository at this point in the history
  • Loading branch information
hezy0426 committed Aug 9, 2023
1 parent b50a841 commit a2587fe
Show file tree
Hide file tree
Showing 11 changed files with 252 additions and 30 deletions.
4 changes: 2 additions & 2 deletions components/SDCard/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
idf_component_register(SRCS "SDCard.cpp"
idf_component_register(SRCS "dataLogging.cpp" "SDCard.cpp"
INCLUDE_DIRS "include"
REQUIRES "sdmmc" "fatfs" "vfs")
REQUIRES "sdmmc" "fatfs" "vfs" "webServer")
50 changes: 41 additions & 9 deletions components/SDCard/SDCard.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
#include <stdio.h>
/**
* 08/04/2023 - Ruizhe He, this source file contains code to mount and unmount
* a SD card from the ESP 32 board.
*/
#include "SDCard.h"
#include "esp_log.h"

#define MOUNT_POINT "/sdcard"

/**
* This function will mount the SD card
*/
esp_err_t initi_sd_card(void)
{
sdmmc_host_t host = SDMMC_HOST_DEFAULT();
Expand All @@ -25,6 +30,9 @@ esp_err_t initi_sd_card(void)
return err;
}

/**
* This function will unmount the sd card
*/
esp_err_t unmount_sd_card(void)
{
esp_err_t err = esp_vfs_fat_sdcard_unmount(MOUNT_POINT, card);
Expand All @@ -38,29 +46,43 @@ esp_err_t unmount_sd_card(void)
return err;
}

void testingCode(char *jsonString)
/**
* This function will write the parameter string into a txt file
*/
int logStringToFile(const char *formattedString, char *fileName)
{
if (!SDCARDMOUNTED)
return;
const char *file_hello = MOUNT_POINT "/hello.txt";
return 0;

ESP_LOGI(TAG_SD, "Opening file %s", file_hello);
FILE *f = fopen(file_hello, "w");
std::string fullPath;
fullPath.append(MOUNT_POINT).append("/").append(fileName);
ESP_LOGI(TAG_SD, "Opening file %s", fullPath.c_str());

FILE *f = fopen(fullPath.c_str(), "a");
if (f == NULL)
{
ESP_LOGE(TAG_SD, "Failed to open file for writing");
return;
return 0;
}
fprintf(f, "The Data is:\n%s", jsonString);
fprintf(f, "%s\n", formattedString);
fclose(f);
ESP_LOGI(TAG_SD, "SUCCESS");
return 1;
}

/**
* Retrun a bool value whether the SD card is mounted or not
*/
bool isMounted(void)
{
return SDCARDMOUNTED;
}

/**
* This function will return a bool value whether we retrieved the value
* successfully or not. Two parameters will be used to pass in and out
* the free space and total space.
*/
bool SD_getFreeSpace(uint32_t *tot, uint32_t *free)
{
FATFS *fs;
Expand All @@ -82,4 +104,14 @@ bool SD_getFreeSpace(uint32_t *tot, uint32_t *free)
return true;
}
return false;
}

int hasFile(char *fileName)
{
struct stat st;
std::string filePath;
filePath.append(MOUNT_POINT).append("/").append(fileName);
if (stat(filePath.c_str(), &st) == 0)
return 1;
return 0;
}
159 changes: 159 additions & 0 deletions components/SDCard/dataLogging.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
#include "dataLogging.h"

#define HEADER1 "Data Date (UTC),Milliseconds,b_dc_power,i_dc_power,calc_add_power,gen_power,load_power,b_dc_volts,b_dc_amps,i_ac_volts_in,b_amph_in_out,b_state_of_charge,i_dc_volts,i_dc_amps,i_ac_volts_out,i_amps_out,i_amps_in,i_ac_hz,i_status,i_fault,i_temp_transformer,i_temp_fet,i_temp_battery,a_gen_run_hours_since_boot,a_gen_runtime_decihours,age_inverter,a_temperature"
#define HEADER2 "Data Date (UTC),Milliseconds,DC Power (RMK),DC Power (Inverter),Calculated Renewable Power,AC In Power,AC Out Power,VDC (BMK),DC Amps (BMK),VAC In (Inverter),Ah In/Out (BMK),SOC (BMK),VDC (Inveter),DC Amps (Inverter),VAC Out (Inverter),AC Amps Out,AC Amps IN,AC Out Hz,Inverter Status,Fault Code,Transformer Temp (C ),FET Temp (C ),Battery Temp (C ),Total Generator Runtime (Since AGS Boot),Generator Runtime (Current Cycle),Inverter Age (255 indicates old data),AGS Temp (C )"

time_t epoch_time = 1691547163;
const TickType_t xDelay = 10000;
TaskHandle_t task_handle;

// int generateHeadlines(char **headlineA, char **headlineB)
// {
// std::string ha, hb;
// ha += "Data Date (UTC),Milliseconds";
// hb += "Data Date (UTC),Milliseconds";
// dataNowString = getDataNow();
// cJSON *dataNowJson = cJSON_Parse(dataNowString);
// cJSON *channelJson = cJSON_Parse(channel_string);

// if (!dataNowJson || !channelJson)
// {
// cJSON_Delete(dataNowJson);
// cJSON_Delete(channelJson);
// return 0;
// }

// cJSON *dataNowObj = cJSON_GetObjectItem(dataNowJson, "data");
// cJSON *channelObj = cJSON_GetObjectItem(channelJson, "data");

// if (!dataNowObj || !channelObj)
// {
// cJSON_Delete(dataNowJson);
// cJSON_Delete(channelJson);
// return 0;
// }

// cJSON *dataNowSubObj = NULL;
// cJSON *channelSubObj = NULL;
// cJSON_ArrayForEach(dataNowSubObj, dataNowObj)
// {
// cJSON *channel = cJSON_GetObjectItemCaseSensitive(dataNowSubObj, "channel");
// ha += channel->valueString;

// cJSON_ArrayForEach(channelSubObj, channelObj)
// {
// cJSON *id = cJSON_GetObjectItemCaseSensitive(channelSubObj, "id");
// if (strcmp(id->valuestring, channel->valuestring) == 0)
// {
// cJSON *descriptionA = cJSON_GetObjectItemCaseSensitive(channelSubObj, "description");
// hb += descriptionA->valueString;
// break;
// }
// }
// }

// return 1;
// }

void dataNowLog(void *pv_args)
{
std::string tempString;
struct tm *currentTime;

for (;;)
{
// Move into the loop so when a new day comes, it will generate a new file
char fileName[15];
char time_str[25];
currentTime = localtime(&epoch_time);
strftime(fileName, sizeof fileName, "%Y%m%d.csv", currentTime);
std::mt19937_64 rng(epoch_time);
std::uniform_real_distribution<double> dist(0.0, 1.0);

// Create the file if it doesn't exist. Add the headers to the file
if (!hasFile(fileName))
{
std::string headers;
headers.append(HEADER1).append("\n").append(HEADER2);
if (logStringToFile(headers.c_str(), fileName))
;
}

strftime(time_str, sizeof(time_str), "%Y/%m/%d %H:%M:%S", currentTime);
currentTime = NULL;
tempString.append(time_str).append(",0,");
// b_dc_power
tempString.append(std::to_string(25.0 + 5.0 * dist(rng))).append(",");
// i_dc_power
tempString.append(std::to_string(25.0 + 5.0 * dist(rng))).append(",");
// calc_add_power
tempString.append(std::to_string(25.0 + 5.0 * dist(rng))).append(",");
// gen_power
tempString.append(std::to_string(25.0 + 5.0 * dist(rng))).append(",");
// load_power
tempString.append(std::to_string(25.0 + 5.0 * dist(rng))).append(",");
tempString.append(std::to_string(25.0 + 5.0 * dist(rng))).append(",");
tempString.append(std::to_string(25.0 + 5.0 * dist(rng))).append(",");
tempString.append(std::to_string(25.0 + 5.0 * dist(rng))).append(",");
tempString.append(std::to_string(25.0 + 5.0 * dist(rng))).append(",");
tempString.append(std::to_string(25.0 + 5.0 * dist(rng))).append(",");
tempString.append(std::to_string(25.0 + 5.0 * dist(rng))).append(",");
tempString.append(std::to_string(25.0 + 5.0 * dist(rng))).append(",");
tempString.append(std::to_string(25.0 + 5.0 * dist(rng))).append(",");
tempString.append(std::to_string(25.0 + 5.0 * dist(rng))).append(",");
tempString.append(std::to_string(25.0 + 5.0 * dist(rng))).append(",");
tempString.append(std::to_string(25.0 + 5.0 * dist(rng))).append(",");
tempString.append(std::to_string(25.0 + 5.0 * dist(rng))).append(",");
tempString.append(std::to_string(25.0 + 5.0 * dist(rng))).append(",");
tempString.append(std::to_string(25.0 + 5.0 * dist(rng))).append(",");
tempString.append(std::to_string(25.0 + 5.0 * dist(rng))).append(",");
tempString.append(std::to_string(25.0 + 5.0 * dist(rng))).append(",");
tempString.append(std::to_string(25.0 + 5.0 * dist(rng))).append(",");
tempString.append(std::to_string(25.0 + 5.0 * dist(rng))).append(",");
// age_inverter
tempString.append("1,").append(std::to_string(25.0 + 5.0 * dist(rng)));

if (logStringToFile(tempString.c_str(), fileName))
{
ESP_LOGI("Logging", "Successfully did one");
}
tempString.clear();
epoch_time += 10;
// TODO: Need the format of the file
vTaskDelay(xDelay);
}
}

/**
* This function will mount the SD card and then start the logging of live data.
*/
void startLogging()
{
if (!isMounted())
{
initi_sd_card();
BaseType_t ret = xTaskCreate(dataNowLog, "dataNow_Logging_Task", 8192, NULL, 5, &task_handle);
if (ret == pdPASS)
{
ESP_LOGI("Logging", "Successfully started the task");
}
}
// TODO: confirm the priority with BA
}

/**
* Stop the logging of live data when the function is called.
*/
void stopLogging()
{
if (task_handle)
{
vTaskDelete(task_handle);
task_handle = NULL;
}
}

void starterFunction()
{
startLogging();
}
9 changes: 8 additions & 1 deletion components/SDCard/include/SDCard.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
#pragma once

#ifndef SDCARD_H
#define SDCARD_H

#include <stdio.h>
#include <string>
#include <sys/stat.h>
#include "esp_log.h"
#include "driver/sdmmc_host.h"
#include "driver/sdmmc_defs.h"
#include "sdmmc_cmd.h"
Expand All @@ -11,8 +17,9 @@ static sdmmc_card_t *card;
static bool SDCARDMOUNTED = false;
esp_err_t initi_sd_card(void);
esp_err_t unmount_sd_card(void);
void testingCode(char *jsonString);
int logStringToFile(const char *formattedString, char *fileName);
bool isMounted(void);
bool SD_getFreeSpace(uint32_t *tot, uint32_t *free);
int hasFile(char *fileName);

#endif // SDCARD_H
18 changes: 18 additions & 0 deletions components/SDCard/include/dataLogging.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include "Data2Web.h"
#include "SDCard.h"

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"

#include <cstdint>
#include <sys/stat.h>
#include <string>
#include <time.h>
#include <random>

void startLogging();
void stopLogging();
void starterFunction();
8 changes: 0 additions & 8 deletions components/webServer/Data2Web.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@
*/

#include "Data2Web.h"
#include "cJSON.h"
#include <cstdint>
#include <string>
#include <cstring>
#include <time.h>
#include <random>
#include "SDCard.h"

/**
Expand Down Expand Up @@ -123,9 +117,7 @@ char *getHostInfoJson()
char *JSONString = NULL;
cJSON *JSONObj = NULL;
cJSON *drives = NULL;
cJSON *hostName = NULL;
cJSON *sd_card = NULL;
cJSON *media_usb = NULL;
uint32_t tot, free;
bool ret;
if (!isMounted())
Expand Down
9 changes: 9 additions & 0 deletions components/webServer/include/Data2Web.h
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
#pragma once

#include "cJSON.h"
#include <cstdint>
#include <string>
#include <cstring>
#include <time.h>
#include <random>

char *getDataNow(long long time);
char *getHostInfoJson();
Loading

0 comments on commit a2587fe

Please sign in to comment.