46 lines
879 B
C
46 lines
879 B
C
#include "json/json_convert.h"
|
|
#include <cJSON.h>
|
|
#include <esp_log.h>
|
|
|
|
char *createJSON(float fahr, float cel, float hum) {
|
|
char *json = NULL;
|
|
|
|
cJSON *fahrenheit = NULL;
|
|
cJSON *celsius = NULL;
|
|
cJSON *humidity = NULL;
|
|
|
|
cJSON *data = cJSON_CreateObject();
|
|
if (!data) {
|
|
return NULL;
|
|
}
|
|
|
|
fahrenheit = cJSON_CreateNumber(fahr);
|
|
if (!fahrenheit) {
|
|
cJSON_Delete(data);
|
|
return NULL;
|
|
}
|
|
|
|
cJSON_AddItemToObject(data, "fahrenheit", fahrenheit);
|
|
|
|
celsius = cJSON_CreateNumber(cel);
|
|
if (!celsius) {
|
|
ESP_LOGE("JSON", "Failed to create celsius JSON");
|
|
cJSON_Delete(data);
|
|
return NULL;
|
|
}
|
|
|
|
cJSON_AddItemToObject(data, "celsius", celsius);
|
|
|
|
humidity = cJSON_CreateNumber(hum);
|
|
if (!humidity) {
|
|
cJSON_Delete(data);
|
|
return NULL;
|
|
}
|
|
|
|
cJSON_AddItemToObject(data, "humidity", humidity);
|
|
|
|
json = cJSON_Print(data);
|
|
|
|
return json;
|
|
}
|