jsontest will append as expected, bin/Debug/tempRecord doesn't
This commit is contained in:
+53
-56
@@ -38,7 +38,6 @@ void user_delay_ms(uint32_t period)
|
||||
usleep(period*1000);
|
||||
}
|
||||
|
||||
#ifdef BME280_FLOAT_ENABLE
|
||||
double celsius_to_fahrenheit(double celsius)
|
||||
{
|
||||
return (celsius * 1.8) + 32;
|
||||
@@ -48,17 +47,33 @@ double pascals_to_mbar(double pascals)
|
||||
{
|
||||
return pascals / 100;
|
||||
}
|
||||
#else
|
||||
uint32_t celsius_to_fahrenheit(uint32_t celsius)
|
||||
{
|
||||
return (celsius * 1.8) + 32;
|
||||
}
|
||||
|
||||
uint32_t pascals_to_mbar(uint32_t pascals)
|
||||
char *parse_json_contents(FILE* filePtr, size_t size)
|
||||
{
|
||||
return pascals / 100;
|
||||
char *str;
|
||||
int ch;
|
||||
size_t len = 0;
|
||||
|
||||
str = realloc(NULL, sizeof(*str)*size);
|
||||
if (!str) {
|
||||
return str;
|
||||
}
|
||||
while (EOF != (ch = fgetc(filePtr)) && ch != '\n')
|
||||
{
|
||||
str[len++] = ch;
|
||||
if (len == size)
|
||||
{
|
||||
str = realloc(str, sizeof(*str)*(size += 16)); // not sure why 16, was in example
|
||||
if (!str) {
|
||||
return str;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
str[len++] = '\0'; // ??? append a null character?
|
||||
|
||||
return realloc(str, sizeof(*str)*len);
|
||||
}
|
||||
#endif
|
||||
|
||||
int8_t user_i2c_read(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len)
|
||||
{
|
||||
@@ -104,9 +119,15 @@ int8_t save_data_to_json_file(struct bme280_data *sensor_readings_data)
|
||||
char dateString[32];
|
||||
char timeString[32];
|
||||
char filename[100];
|
||||
char jsonBuffer[1024];
|
||||
char *jsonBuffer;
|
||||
FILE *jsonFilePtr;
|
||||
|
||||
double fahrenheit;
|
||||
double celsius;
|
||||
double pascals;
|
||||
double millibars;
|
||||
double humidity;
|
||||
|
||||
struct json_object *parsed_json;
|
||||
struct json_object *latest_reading_object = json_object_new_object();
|
||||
struct json_object *temperature_object = json_object_new_object();
|
||||
@@ -114,9 +135,7 @@ int8_t save_data_to_json_file(struct bme280_data *sensor_readings_data)
|
||||
|
||||
timeinfo = localtime(&rawtime);
|
||||
strftime(dateString, 32, "%F", timeinfo);
|
||||
strftime(timeString, 32, "%H_%M_%S", timeinfo);
|
||||
//puts(dateString);
|
||||
//time(&rawtime);
|
||||
strftime(timeString, 32, "%F_%H-%M-%S", timeinfo);
|
||||
|
||||
snprintf(filename, sizeof(filename), "readings_%s.json", dateString);
|
||||
|
||||
@@ -128,64 +147,42 @@ int8_t save_data_to_json_file(struct bme280_data *sensor_readings_data)
|
||||
fclose(jsonFile);
|
||||
}
|
||||
|
||||
jsonFilePtr = fopen(filename, "r+");
|
||||
jsonFilePtr = fopen(filename, "r");
|
||||
if (jsonFilePtr == NULL)
|
||||
{
|
||||
sprintf("Failed to open %s", filename);
|
||||
printf("Failed to open %s for reading...\n", filename);
|
||||
exit(1);
|
||||
}
|
||||
fread(jsonBuffer, 1024, 1, jsonFilePtr);
|
||||
//printf("raw json read in: %s \n", jsonBuffer);
|
||||
jsonBuffer = parse_json_contents(jsonFilePtr, 10); // 10 as initial size
|
||||
//fread(jsonBuffer, 1024, 1, jsonFilePtr);
|
||||
fclose(jsonFilePtr);
|
||||
|
||||
parsed_json = json_tokener_parse(jsonBuffer);
|
||||
|
||||
//printf("json read in: %s \n", json_object_to_json_string(parsed_json));
|
||||
fahrenheit = celsius_to_fahrenheit(sensor_readings_data->temperature);
|
||||
celsius = sensor_readings_data->temperature;
|
||||
pascals = sensor_readings_data->pressure;
|
||||
millibars = pascals_to_mbar(sensor_readings_data->pressure);
|
||||
humidity = sensor_readings_data->humidity;
|
||||
|
||||
#ifdef BME280_FLOAT_ENABLE
|
||||
double fahrenheit = celsius_to_fahrenheit(sensor_readings_data->temperature);
|
||||
double celsius = sensor_readings_data->temperature;
|
||||
double pascals = sensor_readings_data->pressure;
|
||||
double millibars = pascals_to_mbar(sensor_readings_data->pressure);
|
||||
double humidity = sensor_readings_data->humidity;
|
||||
|
||||
json_object_object_add(temperature_object, "celsius", json_object_new_double(celsius));
|
||||
json_object_object_add(temperature_object, "celsius", json_object_new_double(sensor_readings_data->temperature));
|
||||
json_object_object_add(temperature_object, "fahrenheit", json_object_new_double(fahrenheit));
|
||||
json_object_object_add(pressure_object, "pascals", json_object_new_double(pascals));
|
||||
json_object_object_add(pressure_object, "pascals", json_object_new_double(sensor_readings_data->pressure));
|
||||
json_object_object_add(pressure_object, "millibars", json_object_new_double(millibars));
|
||||
|
||||
json_object_object_add(latest_reading_object, "temperature", temperature_object);
|
||||
json_object_object_add(latest_reading_object, "pressure", pressure_object);
|
||||
json_object_object_add(latest_reading_object, "humidity_percent", json_object_new_double(humidity));
|
||||
|
||||
//json_object_object_add(parsed_json, "timeString", latest_reading_object);
|
||||
|
||||
//fprintf(jsonFilePtr, "{\"temperature\":{\"celsius\":%0.2f,\"fahrenheit\":%0.2f}}", celsius, fahrenheit);
|
||||
#else
|
||||
uint32_t fahrenheit = celsius_to_fahrenheit(sensor_readings_data->temperature);
|
||||
uint32_t celsius = sensor_readings_data->temperature;
|
||||
uint32_t pascals = sensor_readings_data->pressure;
|
||||
uint32_t millibars = pascals_to_mbar(sensor_readings_data->pressure);
|
||||
uint32_t humidity = sensor_readings_data->humidity;
|
||||
|
||||
json_object_object_add(temperature_object, "celsius", json_object_new_int(celsius));
|
||||
json_object_object_add(temperature_object, "fahrenheit", json_object_new_int(fahrenheit));
|
||||
|
||||
json_object_object_add(pressure_object, "pascals", json_object_new_int(pascals));
|
||||
json_object_object_add(pressure_object, "millibars", json_object_new_int(millibars));
|
||||
|
||||
json_object_object_add(latest_reading_object, "temperature", temperature_object);
|
||||
json_object_object_add(latest_reading_object, "pressure", pressure_object);
|
||||
json_object_object_add(latest_reading_object, "humidity_percent", json_object_new_int(humidity));
|
||||
|
||||
//json_object_object_add(parsed_json, "timeString", latest_reading_object);
|
||||
|
||||
//fprintf(jsonFilePtr, "{\"temperature\":{\"celsius\":%ld,\"fahrenheit\":%ld}}", celsius, fahrenheit);
|
||||
#endif
|
||||
|
||||
json_object_object_add(parsed_json, timeString, latest_reading_object);
|
||||
//fprintf(jsonFilePtr, json_object_to_json_string(parsed_json));
|
||||
fputs(json_object_to_json_string(parsed_json), jsonFilePtr);
|
||||
printf("json to write out: %s \n", json_object_to_json_string(latest_reading_object));
|
||||
printf("new json to write out: %s \n", json_object_to_json_string(parsed_json));
|
||||
|
||||
jsonFilePtr = fopen(filename, "w");
|
||||
if (jsonFilePtr == NULL)
|
||||
{
|
||||
printf("Failed to open %s for writing...\n", filename);
|
||||
exit(1);
|
||||
}
|
||||
fputs(json_object_to_json_string_ext(parsed_json, JSON_C_TO_STRING_PRETTY), jsonFilePtr);
|
||||
fclose(jsonFilePtr);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user