#include <stdio.h>
#include <string.h>
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "esp_vfs.h"
#include "JSON_helpers.h" // Include your JSON helper functions
#include "cJSON.h" // Include cJSON library
static const char *TAG = "JSON_WEATHER_DEMO"; // Tag for logging
#define WEATHER_JSON_PATH "/spiffs/weather_data.json" // Path to the weather_data.json file
int json_get_int(cJSON *obj, const char *key, int defval) // Retrieve an integer value from a JSON object by key
{
cJSON *it = cJSON_GetObjectItem(obj, key);
if (it != NULL && cJSON_IsNumber(it)) {
return it->valueint;
} else {
return defval;
}
}
const char *json_get_str(cJSON *obj, const char *key, const char *defval) // Retrieve a string value from a JSON object by key
{
cJSON *it = cJSON_GetObjectItem(obj, key);
if (it != NULL && cJSON_IsString(it)) {
return it->valuestring;
} else {
return defval;
}
}
void app_main(void)
{
fs_mount_or_die(); // Mount the filesystem or terminate if it fails
// Create JSON object
cJSON *root = cJSON_CreateObject();
if (root == NULL) {
ESP_LOGE(TAG, "Failed to create cJSON object");
return;
}
// Fake sensor readings
int pot_raw = 2500; // pretend potentiometer value
int temperature_c = 23; // pretend 23°C
int humidity_percent = 45; // pretend 45% RH
cJSON_AddStringToObject(root, "device", "ESP32S3_WeatherNode");
cJSON_AddNumberToObject(root, "pot_raw", pot_raw);
cJSON_AddNumberToObject(root, "temperature_c", temperature_c);
cJSON_AddNumberToObject(root, "humidity_percent", humidity_percent);
cJSON_AddStringToObject(root, "status", "OK");
// STEP 3: Save JSON to file using your helper
save_json(WEATHER_JSON_PATH, root);
// JSON object is no longer needed after saving
cJSON_Delete(root);
ESP_LOGI(TAG, "Wrote demo JSON to %s", WEATHER_JSON_PATH);
fs_print_file(WEATHER_JSON_PATH); // Print JSON file to terminal
// STEP 4: Read JSON file back into a buffer
char buffer[256] = {0};
if (!read_small_file(WEATHER_JSON_PATH, buffer, sizeof(buffer))) {
ESP_LOGE(TAG, "Failed to read back %s", WEATHER_JSON_PATH);
return;
}
// STEP 5: Parse JSON string into cJSON object
cJSON *parsed = cJSON_Parse(buffer);
if (parsed == NULL) {
ESP_LOGE(TAG, "Failed to parse JSON from %s", WEATHER_JSON_PATH);
return;
}
// STEP 6: Read values using helper functions
const char *device = json_get_str(parsed, "device", "unknown");
pot_raw = json_get_int(parsed, "pot_raw", -1);
temperature_c = json_get_int(parsed, "temperature_c", -100);
humidity_percent = json_get_int(parsed, "humidity_percent", -1);
const char *status = json_get_str(parsed, "status", "UNKNOWN");
// STEP 7: Print values to serial monitor
ESP_LOGI(TAG, "Device : %s", device);
ESP_LOGI(TAG, "Pot raw : %d", pot_raw);
ESP_LOGI(TAG, "Temperature (C) : %d", temperature_c);
ESP_LOGI(TAG, "Humidity (%%) : %d", humidity_percent);
ESP_LOGI(TAG, "Status : %s", status);
// Clean up parsed JSON object
cJSON_Delete(parsed);
// Delay for 1000 ms (1000 ms / portTICK_PERIOD_MS)
vTaskDelay(1000 / portTICK_PERIOD_MS);
}