adding curl call to reading server

This commit is contained in:
2026-04-13 15:52:17 -06:00
parent 876a7866ed
commit a38652f599
2 changed files with 57 additions and 2 deletions
+2 -2
View File
@@ -3,8 +3,8 @@ DEBUG_DIR := build/Debug
BIN_DEBUG_DIR := bin/Debug
RELEASE_DIR := build/Release
BIN_RELEASE_DIR := bin/Release
CFLAGS += $(shell pkg-config --cflags json-c)
LDFLAGS += $(shell pkg-config --libs json-c)
CFLAGS += $(shell pkg-config --cflags json-c libcurl)
LDFLAGS += $(shell pkg-config --libs json-c libcurl)
debug:
gcc -Wall -fPIC -pg -g -c src/bme280/bme280.c -o $(DEBUG_DIR)/bme280.o -std=${STANDARD} ${LDFLAGS}
+55
View File
@@ -15,8 +15,10 @@
#include <time.h>
#include <json-c/json_object.h>
#include <json-c/json_tokener.h>
#include <curl/curl.h>
#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'
@@ -203,6 +205,58 @@ int8_t save_data_to_json_file(struct bme280_data *sensor_readings_data)
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 readings_result;
@@ -227,6 +281,7 @@ int8_t read_sensor_data_normal_mode(struct bme280_dev *dev)
readings_result = bme280_get_sensor_data(BME280_ALL, &sensor_readings_data, dev);
print_sensor_data(&sensor_readings_data);
save_data_to_json_file(&sensor_readings_data);
post_reading(&sensor_readings_data);
return readings_result;
}