#include <Arduino.h>
#include <Wire.h>
#include "RTClib.h"
String iniContent =
"[FirstMed]\n"
"Name = 'Paracetamol'\n"
"Notif_time = 07:00:00, 13:00:00, 17:00:00, 20:00:00\n"
"When = 'Before Meal'\n"
"[SecondMed]\n"
"Name = 'Vitamin C'\n"
"Notif_time = 20:00:00\n"
"When = 'Before Bed'\n"
"[ThirdMed]\n"
"Name = 'Star'\n"
"Notif_time = 17:00:00, 20:00:00\n"
"When = 'After Meal'\n";
// Variable to keep track of med info
String FirstMedName;
String SecondMedName;
String ThirdMedName;
String FirstMedWhen;
String SecondMedWhen;
String ThirdMedWhen;
// Arrays to keep track of notification time for each med
String notif1[4];
String notif2[4];
String notif3[4];
void setup() {
Serial.begin(115200);
parseIni(iniContent); // Save Config file contents into arrays and vars
printMedications();
}
void loop() {
// Your loop code
}
void parseIni(String ini) {
String lines[12]; // Adjust size based on expected lines
int lineCount = 0;
// Split the string by newline characters
int startIndex = 0;
int endIndex = ini.indexOf('\n');
while (endIndex != -1) {
lines[lineCount++] = ini.substring(startIndex, endIndex);
startIndex = endIndex + 1;
endIndex = ini.indexOf('\n', startIndex);
}
lines[lineCount++] = ini.substring(startIndex); // Last line
// Process each section
for (int i = 0; i < lineCount; i++) {
if (lines[i].startsWith("[FirstMed]")) {
FirstMedName = getValue(lines[++i]); // Get name
fillNotifArray(notif1, lines[++i]); // Get notif_time
FirstMedWhen = getValue(lines[++i]); // Get when
} else if (lines[i].startsWith("[SecondMed]")) {
SecondMedName = getValue(lines[++i]); // Get name
fillNotifArray(notif2, lines[++i]); // Get notif_time
SecondMedWhen = getValue(lines[++i]); // Get when
} else if (lines[i].startsWith("[ThirdMed]")) {
ThirdMedName = getValue(lines[++i]); // Get name
fillNotifArray(notif3, lines[++i]); // Get notif_time
ThirdMedWhen = getValue(lines[++i]); // Get when
}
}
}
// Function to extract the value after '='
String getValue(String line) {
return line.substring(line.indexOf('=') + 2, line.length());
}
// Function to fill notification time arrays
void fillNotifArray(String notif[], String line) {
// Initialize the array with empty strings
for (int i = 0; i < 4; i++) {
notif[i] = "";
}
// Split the notif_time string by commas
int startIndex = line.indexOf('=') + 2; // Start after '='
int endIndex = line.indexOf(',', startIndex);
int index = 0;
while (endIndex != -1 && index < 4) {
notif[index++] = line.substring(startIndex, endIndex); // Extract time
startIndex = endIndex + 2; // Move past the comma and space
endIndex = line.indexOf(',', startIndex);
}
// Add the last time (if exists)
if (startIndex < line.length() && index < 4) {
notif[index] = line.substring(startIndex); // Add the last one
}
}
void printMedications() {
// Print FirstMed
Serial.println("First Medication:");
Serial.print("Name: ");
Serial.println(FirstMedName);
Serial.print("When: ");
Serial.println(FirstMedWhen);
Serial.print("Notification Times: ");
for (int i = 0; i < 4; i++) {
Serial.print(notif1[i]);
if (i < 3) Serial.print(", "); // Print comma between times
}
Serial.println("\n");
// Print SecondMed
Serial.println("Second Medication:");
Serial.print("Name: ");
Serial.println(SecondMedName);
Serial.print("When: ");
Serial.println(SecondMedWhen);
Serial.print("Notification Times: ");
for (int i = 0; i < 4; i++) {
Serial.print(notif2[i]);
if (i < 3) Serial.print(", "); // Print comma between times
}
Serial.println("\n");
// Print ThirdMed
Serial.println("Third Medication:");
Serial.print("Name: ");
Serial.println(ThirdMedName);
Serial.print("When: ");
Serial.println(ThirdMedWhen);
Serial.print("Notification Times: ");
for (int i = 0; i < 4; i++) {
Serial.print(notif3[i]);
if (i < 3) Serial.print(", "); // Print comma between times
}
Serial.println("\n");
}