From a38652f599c7df77305fc91f51afa40ad4ad2048 Mon Sep 17 00:00:00 2001 From: Brian Rogers Date: Mon, 13 Apr 2026 15:52:17 -0600 Subject: [PATCH] adding curl call to reading server --- Makefile | 4 ++-- src/main.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index d3e2d35..528f330 100644 --- a/Makefile +++ b/Makefile @@ -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} diff --git a/src/main.c b/src/main.c index 5d0a718..6190cd5 100644 --- a/src/main.c +++ b/src/main.c @@ -15,8 +15,10 @@ #include #include #include +#include #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; }