Compare commits

...

4 Commits

5 changed files with 81 additions and 14 deletions
+1 -1
View File
@@ -2,4 +2,4 @@
bin/Debug/* bin/Debug/*
bin/Release/* bin/Release/*
build/Debug/* build/Debug/*
build/Release/* build/Release/*
+2 -2
View File
@@ -3,8 +3,8 @@ 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) CFLAGS += $(shell pkg-config --cflags json-c libcurl)
LDFLAGS += $(shell pkg-config --libs json-c) LDFLAGS += $(shell pkg-config --libs json-c libcurl)
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} ${LDFLAGS}
Binary file not shown.
BIN
View File
Binary file not shown.
+78 -11
View File
@@ -15,8 +15,10 @@
#include <time.h> #include <time.h>
#include <json-c/json_object.h> #include <json-c/json_object.h>
#include <json-c/json_tokener.h> #include <json-c/json_tokener.h>
#include <curl/curl.h>
#define IIC_Dev "/dev/i2c-1" #define IIC_Dev "/dev/i2c-1"
#define POST_URL "https://placeholder.example.com/readings"
int file_descriptor; // maybe file descriptor? example had it as 'fd' int file_descriptor; // maybe file descriptor? example had it as 'fd'
@@ -87,14 +89,14 @@ void print_sensor_data(struct bme280_data *sensor_readings_data)
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;
printf("%0.2f\xc2\xb0\x46 / %0.2f\xc2\xb0\x43, %0.2fPa, %0.2fmbar, %0.2f%% \r\n", fahrenheit, celsius, pascals, millibars, humidity); //printf("%0.2f\xc2\xb0\x46 / %0.2f\xc2\xb0\x43, %0.2fPa, %0.2fmbar, %0.2f%% \r\n", fahrenheit, celsius, pascals, millibars, humidity);
#else #else
uint32_t fahrenheit = celsius_to_fahrenheit(sensor_readings_data->temperature); uint32_t fahrenheit = celsius_to_fahrenheit(sensor_readings_data->temperature);
uint32_t celsius = sensor_readings_data->temperature; uint32_t celsius = sensor_readings_data->temperature;
uint32_t pascals = sensor_readings_data->pressure; uint32_t pascals = sensor_readings_data->pressure;
uint32_t millibars = pascals_to_mbar(sensor_readings_data->pressure); uint32_t millibars = pascals_to_mbar(sensor_readings_data->pressure);
uint32_t humidity = sensor_readings_data->humidity; uint32_t humidity = sensor_readings_data->humidity;
printf("%ld\xc2\xb0\x46 / %ld\xc2\xb0\x43, %ldPa, %ldmbar, %ld%% \r\n", fahrenheit, celsius, pascals, millibars, humidity); //printf("%ld\xc2\xb0\x46 / %ld\xc2\xb0\x43, %ldPa, %ldmbar, %ld%% \r\n", fahrenheit, celsius, pascals, millibars, humidity);
#endif #endif
} }
@@ -105,7 +107,6 @@ int8_t save_data_to_json_file(struct bme280_data *sensor_readings_data)
char dateString[32]; char dateString[32];
char timeString[32]; char timeString[32];
char filename[100]; char filename[100];
char jsonBuffer[1024];
FILE *jsonFilePtr; FILE *jsonFilePtr;
struct json_object *parsed_json; struct json_object *parsed_json;
@@ -115,11 +116,11 @@ int8_t save_data_to_json_file(struct bme280_data *sensor_readings_data)
timeinfo = localtime(&rawtime); timeinfo = localtime(&rawtime);
strftime(dateString, 32, "%F", timeinfo); strftime(dateString, 32, "%F", timeinfo);
strftime(timeString, 32, "%H_%M_%S", timeinfo); strftime(timeString, 32, "%H:%M:%S", timeinfo);
//puts(dateString); //puts(dateString);
//time(&rawtime); //time(&rawtime);
snprintf(filename, sizeof(filename), "readings_%s.json", dateString); snprintf(filename, sizeof(filename), "%s.json", dateString);
if (!check_file_exists(filename)) if (!check_file_exists(filename))
{ {
@@ -132,12 +133,24 @@ int8_t save_data_to_json_file(struct bme280_data *sensor_readings_data)
jsonFilePtr = fopen(filename, "r+"); jsonFilePtr = fopen(filename, "r+");
if (jsonFilePtr == NULL) if (jsonFilePtr == NULL)
{ {
sprintf("Failed to open %s", filename); fprintf(stderr, "Failed to open %s\n", filename);
exit(1); exit(1);
} }
fread(jsonBuffer, 1024, 1, jsonFilePtr); fseek(jsonFilePtr, 0, SEEK_END);
long fileSize = ftell(jsonFilePtr);
rewind(jsonFilePtr);
char *jsonBuffer = malloc(fileSize + 1);
if (jsonBuffer == NULL)
{
fprintf(stderr, "Failed to allocate buffer for %s\n", filename);
fclose(jsonFilePtr);
exit(1);
}
fread(jsonBuffer, 1, fileSize, jsonFilePtr);
jsonBuffer[fileSize] = '\0';
//printf("raw json read in: %s \n", jsonBuffer); //printf("raw json read in: %s \n", jsonBuffer);
parsed_json = json_tokener_parse(jsonBuffer); parsed_json = json_tokener_parse(jsonBuffer);
free(jsonBuffer);
//printf("json read in: %s \n", json_object_to_json_string(parsed_json)); //printf("json read in: %s \n", json_object_to_json_string(parsed_json));
@@ -183,14 +196,67 @@ int8_t save_data_to_json_file(struct bme280_data *sensor_readings_data)
#endif #endif
json_object_object_add(parsed_json, timeString, latest_reading_object); json_object_object_add(parsed_json, timeString, latest_reading_object);
//fprintf(jsonFilePtr, json_object_to_json_string(parsed_json)); fseek(jsonFilePtr, 0, SEEK_SET);
ftruncate(fileno(jsonFilePtr), 0);
fputs(json_object_to_json_string(parsed_json), jsonFilePtr); 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("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)); //printf("new json to write out: %s \n", json_object_to_json_string(parsed_json));
fclose(jsonFilePtr); fclose(jsonFilePtr);
return 0; return 0;
} }
void post_reading(struct bme280_data *sensor_readings_data)
{
time_t rawtime = time(NULL);
struct tm *timeinfo = localtime(&rawtime);
char read_at[20];
strftime(read_at, sizeof(read_at), "%Y-%m-%d %H:%M:%S", timeinfo);
struct json_object *payload = json_object_new_object();
struct json_object *temperature_object = json_object_new_object();
struct json_object *pressure_object = json_object_new_object();
#ifdef BME280_FLOAT_ENABLE
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(celsius_to_fahrenheit(sensor_readings_data->temperature)));
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(pascals_to_mbar(sensor_readings_data->pressure)));
json_object_object_add(payload, "humidity_percent", json_object_new_double(sensor_readings_data->humidity));
#else
json_object_object_add(temperature_object, "celsius", json_object_new_int(sensor_readings_data->temperature));
json_object_object_add(temperature_object, "fahrenheit", json_object_new_int(celsius_to_fahrenheit(sensor_readings_data->temperature)));
json_object_object_add(pressure_object, "pascals", json_object_new_int(sensor_readings_data->pressure));
json_object_object_add(pressure_object, "millibars", json_object_new_int(pascals_to_mbar(sensor_readings_data->pressure)));
json_object_object_add(payload, "humidity_percent", json_object_new_int(sensor_readings_data->humidity));
#endif
json_object_object_add(payload, "temperature", temperature_object);
json_object_object_add(payload, "pressure", pressure_object);
json_object_object_add(payload, "read_at", json_object_new_string(read_at));
const char *json_str = json_object_to_json_string(payload);
CURL *curl = curl_easy_init();
if (curl)
{
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Content-Type: application/json");
curl_easy_setopt(curl, CURLOPT_URL, POST_URL);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json_str);
CURLcode res = curl_easy_perform(curl);
if (res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
}
json_object_put(payload);
}
int8_t read_sensor_data_normal_mode(struct bme280_dev *dev) int8_t read_sensor_data_normal_mode(struct bme280_dev *dev)
{ {
int8_t readings_result; int8_t readings_result;
@@ -214,7 +280,8 @@ int8_t read_sensor_data_normal_mode(struct bme280_dev *dev)
dev->delay_ms(200); // wait for first measurement dev->delay_ms(200); // wait for first measurement
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_json_file(&sensor_readings_data);
post_reading(&sensor_readings_data);
return readings_result; return readings_result;
} }