#include <ArduinoJson.h>
#include <FS.h>
#include "SPIFFS.h"
void setup() {
Serial.begin(115200);
if (!SPIFFS.begin(true)) {
Serial.println("An Error has occurred while mounting SPIFFS");
return;
}
// Read JSON file
File file = SPIFFS.open("/jsondata.json", "r");
if (!file) {
Serial.println("Failed to open file for reading");
return;
}
// Read file into a string
String jsonString;
while (file.available()) {
jsonString += char(file.read());
}
// Close the file
file.close();
// Read JSON file
// Read file into a string
// Deserialize JSON
StaticJsonDocument<4096> doc;
DeserializationError error = deserializeJson(doc, jsonString);
if (error) {
Serial.print("deserializeJson() failed: ");
Serial.println(error.c_str());
return;
}
// Access the data in the JSON object
JsonArray weekData = doc.as<JsonArray>();
// Define current time (just as an example)
int currentHour = 14; // For example, current hour is 2 PM
// Loop through each day in the week
for (int day = 0; day < weekData.size(); day++) {
Serial.print("Day ");
Serial.println(day + 1);
JsonArray dayData = weekData[day].as<JsonArray>();
for (int entry = 0; entry < dayData.size(); entry++) {
JsonObject event = dayData[entry].as<JsonObject>();
const char* start = event["Start"];
const char* finish = event["Finish"];
// Extract start hour
int startHour = atoi(start + 11); // Assuming the format is always "YYYY-MM-DD HH:mm"
// Extract finish hour
int finishHour = atoi(finish + 11); // Assuming the format is always "YYYY-MM-DD HH:mm"
// Check if current hour is within the activity time range
if (currentHour >= startHour && currentHour <= finishHour) {
Serial.print("Event ");
Serial.print(entry + 1);
Serial.println(" is active now!");
}
}
}
}
void loop() {
// Nothing to do here
}