Compare commits
2 Commits
9c58357a4c
...
041ba75d8a
Author | SHA1 | Date | |
---|---|---|---|
041ba75d8a | |||
0f080312ab |
14
Makefile
14
Makefile
@ -3,18 +3,16 @@ DEBUG_DIR := build/Debug
|
|||||||
BIN_DEBUG_DIR := bin/Debug
|
BIN_DEBUG_DIR := bin/Debug
|
||||||
RELEASE_DIR := build/Release
|
RELEASE_DIR := build/Release
|
||||||
BIN_RELEASE_DIR := bin/Release
|
BIN_RELEASE_DIR := bin/Release
|
||||||
CFLAGS += $(shell pkg-config --cflags json-c)
|
|
||||||
LDFLAGS += $(shell pkg-config --libs json-c)
|
|
||||||
|
|
||||||
debug:
|
debug:
|
||||||
gcc -Wall -fPIC -pg -g -c src/bme280/bme280.c -o $(DEBUG_DIR)/bme280.o -std=${STANDARD} ${LDFLAGS}
|
gcc -Wall -fPIC -pg -g -c src/bme280/bme280.c -o $(DEBUG_DIR)/bme280.o -std=${STANDARD}
|
||||||
gcc -Wall -fPIC -pg -g -c src/main.c -o $(DEBUG_DIR)/main.o -std=${STANDARD} ${LDFLAGS}
|
gcc -Wall -fPIC -pg -g -c src/main.c -o $(DEBUG_DIR)/main.o -std=${STANDARD}
|
||||||
gcc -Wall -fPIC -pg -g -o $(BIN_DEBUG_DIR)/tempRecord $(DEBUG_DIR)/main.o $(DEBUG_DIR)/bme280.o -std=${STANDARD} ${LDFLAGS}
|
gcc -Wall -fPIC -pg -g -o $(BIN_DEBUG_DIR)/tempRecord $(DEBUG_DIR)/main.o $(DEBUG_DIR)/bme280.o -std=${STANDARD}
|
||||||
|
|
||||||
release:
|
release:
|
||||||
gcc -Wall -fPIC -O2 -c src/bme280/bme280.c -o $(RELEASE_DIR)/bme280.o -std=${STANDARD} ${LDFLAGS}
|
gcc -Wall -fPIC -O2 -c src/bme280/bme280.c -o $(RELEASE_DIR)/bme280.o -std=${STANDARD}
|
||||||
gcc -Wall -fPIC -O2 -c src/main.c -o $(RELEASE_DIR)/main.o -std=${STANDARD} ${LDFLAGS}
|
gcc -Wall -fPIC -O2 -c src/main.c -o $(RELEASE_DIR)/main.o -std=${STANDARD}
|
||||||
gcc -Wall -fPIC -O2 -o $(BIN_RELEASE_DIR)/tempRecord $(RELEASE_DIR)/main.o $(RELEASE_DIR)/bme280.o -std=${STANDARD} ${LDFLAGS}
|
gcc -Wall -fPIC -O2 -o $(BIN_RELEASE_DIR)/tempRecord $(RELEASE_DIR)/main.o $(RELEASE_DIR)/bme280.o -std=${STANDARD}
|
||||||
|
|
||||||
|
|
||||||
.PHONY clean:
|
.PHONY clean:
|
||||||
|
105
src/main.c
105
src/main.c
@ -13,8 +13,6 @@
|
|||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <json-c/json_object.h>
|
|
||||||
#include <json-c/json_tokener.h>
|
|
||||||
|
|
||||||
#define IIC_Dev "/dev/i2c-1"
|
#define IIC_Dev "/dev/i2c-1"
|
||||||
|
|
||||||
@ -48,33 +46,6 @@ double pascals_to_mbar(double pascals)
|
|||||||
return pascals / 100;
|
return pascals / 100;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *parse_json_contents(FILE* filePtr, size_t size)
|
|
||||||
{
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
int8_t user_i2c_read(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len)
|
int8_t user_i2c_read(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len)
|
||||||
{
|
{
|
||||||
write(file_descriptor, ®_addr, 1);
|
write(file_descriptor, ®_addr, 1);
|
||||||
@ -112,78 +83,44 @@ void print_sensor_data(struct bme280_data *sensor_readings_data)
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
int8_t save_data_to_json_file(struct bme280_data *sensor_readings_data)
|
int8_t save_data_to_csv_file(struct bme280_data *sensor_readings_data)
|
||||||
{
|
{
|
||||||
time_t rawtime = time(NULL);
|
time_t rawtime = time(NULL);
|
||||||
struct tm *timeinfo;
|
struct tm *timeinfo;
|
||||||
char dateString[32];
|
char dateString[32];
|
||||||
char timeString[32];
|
char datetimeString[32];
|
||||||
char filename[100];
|
char filename[100];
|
||||||
char *jsonBuffer;
|
FILE *csvFilePtr;
|
||||||
FILE *jsonFilePtr;
|
|
||||||
|
|
||||||
double fahrenheit;
|
double celsius = sensor_readings_data->temperature;
|
||||||
double celsius;
|
double fahrenheit = celsius_to_fahrenheit(sensor_readings_data->temperature);
|
||||||
double pascals;
|
double pascals = sensor_readings_data->pressure;
|
||||||
double millibars;
|
double millibars = pascals_to_mbar(sensor_readings_data->pressure);
|
||||||
double humidity;
|
double humidity = sensor_readings_data->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();
|
|
||||||
struct json_object *pressure_object = json_object_new_object();
|
|
||||||
|
|
||||||
timeinfo = localtime(&rawtime);
|
timeinfo = localtime(&rawtime);
|
||||||
strftime(dateString, 32, "%F", timeinfo);
|
strftime(dateString, 32, "%F", timeinfo);
|
||||||
strftime(timeString, 32, "%F_%H-%M-%S", timeinfo);
|
strftime(datetimeString, 32, "%F %H:%M:%S", timeinfo);
|
||||||
|
|
||||||
snprintf(filename, sizeof(filename), "readings_%s.json", dateString);
|
snprintf(filename, sizeof(filename), "/home/pi/readings/readings_%s.csv", dateString);
|
||||||
|
|
||||||
if (!check_file_exists(filename))
|
if (!check_file_exists(filename))
|
||||||
{
|
{
|
||||||
printf("File for today does not exist! Creating... \n");
|
printf("File for today does not exist! Creating... \n");
|
||||||
FILE *jsonFile = fopen(filename, "w");
|
FILE *csvFile = fopen(filename, "w");
|
||||||
fputs("{}", jsonFile);
|
fputs("Date,Celsius,Fahrenheit,Pascals,Millibars,Humidity Percent\n", csvFile);
|
||||||
fclose(jsonFile);
|
fclose(csvFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
jsonFilePtr = fopen(filename, "r");
|
csvFilePtr = fopen(filename, "a");
|
||||||
if (jsonFilePtr == NULL)
|
if (csvFilePtr == NULL)
|
||||||
{
|
|
||||||
printf("Failed to open %s for reading...\n", filename);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
jsonBuffer = parse_json_contents(jsonFilePtr, 10); // 10 as initial size
|
|
||||||
//fread(jsonBuffer, 1024, 1, jsonFilePtr);
|
|
||||||
fclose(jsonFilePtr);
|
|
||||||
|
|
||||||
parsed_json = json_tokener_parse(jsonBuffer);
|
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
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(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);
|
|
||||||
|
|
||||||
jsonFilePtr = fopen(filename, "w");
|
|
||||||
if (jsonFilePtr == NULL)
|
|
||||||
{
|
{
|
||||||
printf("Failed to open %s for writing...\n", filename);
|
printf("Failed to open %s for writing...\n", filename);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
fputs(json_object_to_json_string_ext(parsed_json, JSON_C_TO_STRING_PRETTY), jsonFilePtr);
|
fprintf(csvFilePtr, "%s,%0.5f,%0.5f,%0.5f,%0.5f,%0.5f\n", datetimeString, celsius, fahrenheit, pascals, millibars, humidity);
|
||||||
fclose(jsonFilePtr);
|
fclose(csvFilePtr);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -209,7 +146,7 @@ int8_t read_sensor_data_normal_mode(struct bme280_dev *dev)
|
|||||||
readings_result = bme280_set_sensor_mode(BME280_NORMAL_MODE, dev);
|
readings_result = bme280_set_sensor_mode(BME280_NORMAL_MODE, dev);
|
||||||
readings_result = bme280_get_sensor_data(BME280_ALL, &sensor_readings_data, dev);
|
readings_result = bme280_get_sensor_data(BME280_ALL, &sensor_readings_data, dev);
|
||||||
//print_sensor_data(&sensor_readings_data);
|
//print_sensor_data(&sensor_readings_data);
|
||||||
save_data_to_json_file(&sensor_readings_data);
|
save_data_to_csv_file(&sensor_readings_data);
|
||||||
|
|
||||||
return readings_result;
|
return readings_result;
|
||||||
}
|
}
|
||||||
@ -236,9 +173,11 @@ int main(int argc, char* argv[])
|
|||||||
dev.delay_ms = user_delay_ms;
|
dev.delay_ms = user_delay_ms;
|
||||||
|
|
||||||
init_result = bme280_init(&dev);
|
init_result = bme280_init(&dev);
|
||||||
printf("\r\n BME280 Init Result is:%d \r\n", init_result);
|
printf("\n\nBME280 Init Result is: %d\n\n", init_result);
|
||||||
|
|
||||||
read_sensor_data_normal_mode(&dev);
|
read_sensor_data_normal_mode(&dev);
|
||||||
|
|
||||||
|
printf("Recorded readings.\n\n");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user