Compare commits
3 Commits
892fcfca6c
...
master
Author | SHA1 | Date | |
---|---|---|---|
041ba75d8a
|
|||
0f080312ab
|
|||
9c58357a4c
|
16
Makefile
16
Makefile
@ -3,20 +3,20 @@ 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:
|
||||||
rm -rf -v !\(.gitignore\) $(DEBUG_DIR)/*
|
rm -rf -v !\(.gitignore\) $(DEBUG_DIR)/*
|
||||||
|
rm -rf -v !\(.gitignore\) $(BIN_DEBUG_DIR)/*
|
||||||
rm -rf -v !\(.gitignore\) $(RELEASE_DIR)/*
|
rm -rf -v !\(.gitignore\) $(RELEASE_DIR)/*
|
||||||
|
rm -rf -v !\(.gitignore\) $(BIN_RELEASE_DIR)/*
|
||||||
|
100
src/jsontest.c
Normal file
100
src/jsontest.c
Normal file
@ -0,0 +1,100 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <sys/ioctl.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include <json-c/json_object.h>
|
||||||
|
#include <json-c/json_tokener.h>
|
||||||
|
|
||||||
|
int8_t check_file_exists(const char *filename)
|
||||||
|
{
|
||||||
|
FILE *file;
|
||||||
|
file = fopen(filename, "r");
|
||||||
|
if (file != NULL)
|
||||||
|
{
|
||||||
|
fclose(file);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char* argv[])
|
||||||
|
{
|
||||||
|
time_t rawtime = time(NULL);
|
||||||
|
struct tm *timeinfo;
|
||||||
|
char dateString[32];
|
||||||
|
char timeString[32];
|
||||||
|
char filename[100];
|
||||||
|
char jsonBuffer[1024];
|
||||||
|
//char jsonStrOutput[4096];
|
||||||
|
FILE *jsonFilePtr;
|
||||||
|
|
||||||
|
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);
|
||||||
|
strftime(dateString, 32, "%F", timeinfo);
|
||||||
|
strftime(timeString, 32, "%F_%H-%M-%S", timeinfo);
|
||||||
|
|
||||||
|
snprintf(filename, sizeof(filename), "readings_%s.json", dateString);
|
||||||
|
|
||||||
|
if (!check_file_exists(filename))
|
||||||
|
{
|
||||||
|
printf("File for today does not exist! Creating...\n");
|
||||||
|
FILE *jsonFile = fopen(filename, "w");
|
||||||
|
fputs("{}", jsonFile);
|
||||||
|
fclose(jsonFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
jsonFilePtr = fopen(filename, "r");
|
||||||
|
if (jsonFilePtr == NULL)
|
||||||
|
{
|
||||||
|
printf("Failed to open %s for reading...\n", filename);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
fread(jsonBuffer, 1024, 1, jsonFilePtr);
|
||||||
|
fclose(jsonFilePtr);
|
||||||
|
printf("raw json read in: %s\n", jsonBuffer);
|
||||||
|
|
||||||
|
parsed_json = json_tokener_parse(jsonBuffer);
|
||||||
|
printf("json read in: %s\n", json_object_to_json_string_ext(parsed_json, JSON_C_TO_STRING_PRETTY));
|
||||||
|
|
||||||
|
double fahrenheit = (23 * 1.8) + 32;
|
||||||
|
double humidity = 100 - 80.82;
|
||||||
|
|
||||||
|
json_object_object_add(temperature_object, "fahrenheit", json_object_new_double(fahrenheit));
|
||||||
|
|
||||||
|
json_object_object_add(latest_reading_object, "temperature", temperature_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);
|
||||||
|
|
||||||
|
printf("json to write out: %s\n", json_object_to_json_string_ext(latest_reading_object, JSON_C_TO_STRING_PRETTY));
|
||||||
|
printf("new json to write out: %s\n", json_object_to_json_string_ext(parsed_json, JSON_C_TO_STRING_PRETTY));
|
||||||
|
|
||||||
|
//uint32_t jsonStrSize = sizeof(json_object_to_json_string_ext(parsed_json, JSON_C_TO_STRING_PRETTY));
|
||||||
|
//printf("jsonStrSize %i\n", jsonStrSize);
|
||||||
|
//char jsonStrOutput[] = json_object_to_json_string_ext(parsed_json, JSON_C_TO_STRING_PRETTY);
|
||||||
|
|
||||||
|
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);
|
||||||
|
//fwrite(json_object_to_json_string_ext(parsed_json), 1, jsonStrSize, jsonFilePtr);
|
||||||
|
fclose(jsonFilePtr);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
120
src/main.c
120
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"
|
||||||
|
|
||||||
@ -38,7 +36,6 @@ void user_delay_ms(uint32_t period)
|
|||||||
usleep(period*1000);
|
usleep(period*1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef BME280_FLOAT_ENABLE
|
|
||||||
double celsius_to_fahrenheit(double celsius)
|
double celsius_to_fahrenheit(double celsius)
|
||||||
{
|
{
|
||||||
return (celsius * 1.8) + 32;
|
return (celsius * 1.8) + 32;
|
||||||
@ -48,17 +45,6 @@ double pascals_to_mbar(double pascals)
|
|||||||
{
|
{
|
||||||
return pascals / 100;
|
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)
|
|
||||||
{
|
|
||||||
return pascals / 100;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
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)
|
||||||
{
|
{
|
||||||
@ -97,96 +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[1024];
|
FILE *csvFilePtr;
|
||||||
FILE *jsonFilePtr;
|
|
||||||
|
|
||||||
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);
|
|
||||||
strftime(dateString, 32, "%F", timeinfo);
|
|
||||||
strftime(timeString, 32, "%H_%M_%S", timeinfo);
|
|
||||||
//puts(dateString);
|
|
||||||
//time(&rawtime);
|
|
||||||
|
|
||||||
snprintf(filename, sizeof(filename), "readings_%s.json", dateString);
|
|
||||||
|
|
||||||
if (!check_file_exists(filename))
|
|
||||||
{
|
|
||||||
printf("File for today does not exist! Creating... \n");
|
|
||||||
FILE *jsonFile = fopen(filename, "w");
|
|
||||||
fputs("{}", jsonFile);
|
|
||||||
fclose(jsonFile);
|
|
||||||
}
|
|
||||||
|
|
||||||
jsonFilePtr = fopen(filename, "r+");
|
|
||||||
if (jsonFilePtr == NULL)
|
|
||||||
{
|
|
||||||
sprintf("Failed to open %s", filename);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
fread(jsonBuffer, 1024, 1, jsonFilePtr);
|
|
||||||
//printf("raw json read in: %s \n", jsonBuffer);
|
|
||||||
parsed_json = json_tokener_parse(jsonBuffer);
|
|
||||||
|
|
||||||
//printf("json read in: %s \n", json_object_to_json_string(parsed_json));
|
|
||||||
|
|
||||||
#ifdef BME280_FLOAT_ENABLE
|
|
||||||
double fahrenheit = celsius_to_fahrenheit(sensor_readings_data->temperature);
|
|
||||||
double celsius = sensor_readings_data->temperature;
|
double celsius = sensor_readings_data->temperature;
|
||||||
|
double fahrenheit = celsius_to_fahrenheit(sensor_readings_data->temperature);
|
||||||
double pascals = sensor_readings_data->pressure;
|
double pascals = sensor_readings_data->pressure;
|
||||||
double millibars = pascals_to_mbar(sensor_readings_data->pressure);
|
double millibars = pascals_to_mbar(sensor_readings_data->pressure);
|
||||||
double humidity = sensor_readings_data->humidity;
|
double humidity = sensor_readings_data->humidity;
|
||||||
|
|
||||||
json_object_object_add(temperature_object, "celsius", json_object_new_double(celsius));
|
timeinfo = localtime(&rawtime);
|
||||||
json_object_object_add(temperature_object, "fahrenheit", json_object_new_double(fahrenheit));
|
strftime(dateString, 32, "%F", timeinfo);
|
||||||
json_object_object_add(pressure_object, "pascals", json_object_new_double(pascals));
|
strftime(datetimeString, 32, "%F %H:%M:%S", timeinfo);
|
||||||
json_object_object_add(pressure_object, "millibars", json_object_new_double(millibars));
|
|
||||||
|
|
||||||
json_object_object_add(latest_reading_object, "temperature", temperature_object);
|
snprintf(filename, sizeof(filename), "/home/pi/readings/readings_%s.csv", dateString);
|
||||||
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);
|
if (!check_file_exists(filename))
|
||||||
|
{
|
||||||
|
printf("File for today does not exist! Creating... \n");
|
||||||
|
FILE *csvFile = fopen(filename, "w");
|
||||||
|
fputs("Date,Celsius,Fahrenheit,Pascals,Millibars,Humidity Percent\n", csvFile);
|
||||||
|
fclose(csvFile);
|
||||||
|
}
|
||||||
|
|
||||||
//fprintf(jsonFilePtr, "{\"temperature\":{\"celsius\":%0.2f,\"fahrenheit\":%0.2f}}", celsius, fahrenheit);
|
csvFilePtr = fopen(filename, "a");
|
||||||
#else
|
if (csvFilePtr == NULL)
|
||||||
uint32_t fahrenheit = celsius_to_fahrenheit(sensor_readings_data->temperature);
|
{
|
||||||
uint32_t celsius = sensor_readings_data->temperature;
|
printf("Failed to open %s for writing...\n", filename);
|
||||||
uint32_t pascals = sensor_readings_data->pressure;
|
exit(1);
|
||||||
uint32_t millibars = pascals_to_mbar(sensor_readings_data->pressure);
|
}
|
||||||
uint32_t humidity = sensor_readings_data->humidity;
|
fprintf(csvFilePtr, "%s,%0.5f,%0.5f,%0.5f,%0.5f,%0.5f\n", datetimeString, celsius, fahrenheit, pascals, millibars, humidity);
|
||||||
|
fclose(csvFilePtr);
|
||||||
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));
|
|
||||||
fclose(jsonFilePtr);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -212,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;
|
||||||
}
|
}
|
||||||
@ -239,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;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user