@@ -218,6 +218,14 @@ bool digitalLowAlarm = false;
218218float largeDecreaseThreshold = 24.0 ;
219219int largeDecreaseWaitHours = 2 ;
220220
221+ // Calibration point structure
222+ #define MAX_CALIBRATION_POINTS 10
223+
224+ struct CalibrationPoint {
225+ float sensorValue; // Raw sensor reading (voltage, current, etc.)
226+ float actualHeight; // Actual measured height in inches
227+ };
228+
221229// Multi-site / Multi-tank support - all tanks configured via TANKA, TANKB, etc.
222230// Supports up to 26 tanks (A-Z), adjust array size below if needed
223231struct TankEntry {
@@ -269,16 +277,7 @@ int normalSleepHours = 1; // Normal sleep duration between readings
269277bool wakeOnPingEnabled = true ; // Wake on ping functionality
270278
271279// Height calibration system
272- #define MAX_CALIBRATION_POINTS 10
273-
274- struct CalibrationPoint {
275- float sensorValue; // Raw sensor reading (voltage, current, etc.)
276- float actualHeight; // Actual measured height in inches
277- };
278-
279- // Per-tank level measurements in inches
280- float tankCurrentInches[26 ]; // Current reading per tank
281- bool tankAlarmSent[26 ]; // Alarm state per tank
280+ // Note: MAX_CALIBRATION_POINTS and CalibrationPoint moved above TankEntry struct
282281
283282
284283// Network configuration (loaded from SD card - REQUIRED)
@@ -293,6 +292,12 @@ String decreaseLogFile = "decrease_log.txt";
293292String reportLogFile = " report_log.txt" ;
294293String systemLogFile = " system_events.log" ;
295294
295+ // Forward declarations
296+ void logEvent (String event, int tankIdx = -1 );
297+ void checkLargeDecrease (int tankIdx);
298+ float getTankLevelInches ();
299+ float interpolateHeight (float sensorValue, int tankIdx);
300+
296301// Helper to create a sanitized log filename from site and tank info
297302String getTankLogFileName (int tankIdx) {
298303 if (tankIdx < 0 || tankIdx >= tankCount) {
@@ -457,7 +462,7 @@ void loop() {
457462 }
458463
459464 // Check for large decrease in level (first tank only for legacy)
460- checkLargeDecrease ();
465+ checkLargeDecrease (0 ); // Use first tank (index 0)
461466 }
462467
463468 // Check if it's time for hourly log entry
@@ -483,7 +488,7 @@ void loop() {
483488 if (connectToCellular ()) {
484489 checkForIncomingData (); // Check for any incoming data first
485490 checkForServerCommands ();
486- processIncomingSMS (); // Check for incoming SMS commands
491+ // processIncomingSMS(); // Function not implemented yet
487492 checkServerCommands = true ;
488493 }
489494 lastServerCheckTime = millis ();
@@ -657,8 +662,8 @@ int readAnalogVoltageSensor() {
657662
658663 float avgVoltage = totalVoltage / numReadings;
659664
660- // Convert voltage to inches
661- float levelInches = convertToInches (avgVoltage);
665+ // Convert voltage to inches (using first tank - index 0)
666+ float levelInches = convertToInches (avgVoltage, 0 );
662667
663668#ifdef ENABLE_SERIAL_DEBUG
664669 if (ENABLE_SERIAL_DEBUG) {
@@ -698,8 +703,8 @@ int readCurrentLoopSensor() {
698703 return LOW; // Default to normal state on error
699704 }
700705
701- // Convert current to inches
702- float levelInches = convertToInches (current);
706+ // Convert current to inches (using first tank - index 0)
707+ float levelInches = convertToInches (current, 0 );
703708
704709#ifdef ENABLE_SERIAL_DEBUG
705710 if (ENABLE_SERIAL_DEBUG) {
@@ -1203,7 +1208,7 @@ bool ensureSDCardReady() {
12031208 return lastState;
12041209}
12051210
1206- void logEvent (String event, int tankIdx = - 1 ) {
1211+ void logEvent (String event, int tankIdx) {
12071212 // Log events to SD card with timestamp
12081213 if (ensureSDCardReady ()) {
12091214 String logFileName;
@@ -1488,11 +1493,10 @@ float convertToInches(float sensorValue, int tankIdx) {
14881493 }
14891494}
14901495
1491- // This function is now obsolete and has been replaced by per-tank logic
1492- /*
1496+ // This function provides backward compatibility for legacy single-tank setups
14931497float getTankLevelInches () {
14941498#if SENSOR_TYPE == DIGITAL_FLOAT
1495- return convertToInches(currentLevelState);
1499+ return convertToInches (currentLevelState, 0 );
14961500
14971501#elif SENSOR_TYPE == ANALOG_VOLTAGE
14981502 // Read analog sensor and calculate inches
@@ -1507,20 +1511,19 @@ float getTankLevelInches() {
15071511 }
15081512
15091513 float avgVoltage = totalVoltage / numReadings;
1510- return convertToInches(avgVoltage);
1514+ return convertToInches (avgVoltage, 0 );
15111515
15121516#elif SENSOR_TYPE == CURRENT_LOOP
15131517 // Read current loop sensor and calculate inches
15141518 float current = readCurrentLoopValue ();
15151519 if (current < 0 ) return -1.0 ; // Error indicator
15161520
1517- return convertToInches(current);
1521+ return convertToInches (current, 0 );
15181522
15191523#else
15201524 return -1.0 ; // Error
15211525#endif
15221526}
1523- */
15241527
15251528// Convert inches to feet and inches format
15261529String formatInchesToFeetInches (float totalInches) {
@@ -1561,7 +1564,7 @@ void logHourlyData(int tankIdx) {
15611564 String logEntry = timestamp + " ,HOURLY," + feetInchesFormat + " ," +
15621565 changePrefix + changeFeetInchesFormat;
15631566
1564- File hourlyFile = SD.open (logFileName, FILE_APPEND );
1567+ File hourlyFile = SD.open (logFileName, FILE_WRITE );
15651568 if (hourlyFile) {
15661569 hourlyFile.println (logEntry);
15671570 hourlyFile.close ();
@@ -1590,7 +1593,7 @@ void logDailyDataForTank(int idx) {
15901593 String logEntry = timestamp + " ,DAILY," +
15911594 feetInchesFormat + " ," + changePrefix + changeFeetInchesFormat;
15921595
1593- File dailyFile = SD.open (logFileName, FILE_APPEND );
1596+ File dailyFile = SD.open (logFileName, FILE_WRITE );
15941597 if (dailyFile) {
15951598 dailyFile.println (logEntry);
15961599 dailyFile.close ();
@@ -1610,7 +1613,7 @@ void logAlarmEvent(int tankIdx, String alarmState) {
16101613 String timestamp = getDateTimestamp ();
16111614 String logEntry = timestamp + " ,ALARM," + alarmState;
16121615
1613- File alarmFile = SD.open (logFileName, FILE_APPEND );
1616+ File alarmFile = SD.open (logFileName, FILE_WRITE );
16141617 if (alarmFile) {
16151618 alarmFile.println (logEntry);
16161619 alarmFile.close ();
@@ -1645,7 +1648,7 @@ void logLargeDecrease(int tankIdx, float totalDecrease) {
16451648
16461649 String logEntry = timestamp + " ,DECREASE," + decreaseFeetInchesFormat;
16471650
1648- File decreaseFile = SD.open (logFileName, FILE_APPEND );
1651+ File decreaseFile = SD.open (logFileName, FILE_WRITE );
16491652 if (decreaseFile) {
16501653 decreaseFile.println (logEntry);
16511654 decreaseFile.close ();
@@ -2225,9 +2228,10 @@ void sendPowerFailureNotificationToServer() {
22252228 logEvent (" Power failure notification sent to server for daily email tracking" );
22262229}
22272230
2228- // ========== HEIGHT CALIBRATION FUNCTIONS ==========
2231+ // ========== HEIGHT CALIBRATION FUNCTIONS (OBSOLETE - replaced by per-tank calibration in .cfg files) ==========
22292232
2230- // Load calibration data from SD card
2233+ // Obsolete - Load calibration data from SD card
2234+ /*
22312235void loadCalibrationData() {
22322236 calibrationDataLoaded = false;
22332237 numCalibrationPoints = 0;
@@ -2310,8 +2314,10 @@ void saveCalibrationData() {
23102314 calibFile.close();
23112315 logEvent("Calibration data saved: " + String(numCalibrationPoints) + " points");
23122316}
2317+ */
23132318
2314- // Add a new calibration point
2319+ // Obsolete - Add a new calibration point
2320+ /*
23152321void addCalibrationPoint(float sensorValue, float actualHeight) {
23162322 if (numCalibrationPoints >= MAX_CALIBRATION_POINTS) {
23172323 // Remove oldest point to make room
@@ -2345,6 +2351,7 @@ void addCalibrationPoint(float sensorValue, float actualHeight) {
23452351 ", height=" + String(actualHeight, 2) + " inches";
23462352 logEvent(logMsg);
23472353}
2354+ */
23482355
23492356// Interpolate height from a sensor value using the tank's calibration points
23502357float interpolateHeight (float sensorValue, int tankIdx) {
@@ -2454,7 +2461,7 @@ void addCalibrationPointToCfg(int tankIdx, float sensorValue, float actualHeight
24542461 return ;
24552462 }
24562463
2457- File configFile = SD.open (fileName, FILE_APPEND );
2464+ File configFile = SD.open (fileName, FILE_WRITE );
24582465 if (configFile) {
24592466 String calLine = " CAL_POINT = " + String (sensorValue, 4 ) + " ," + String (actualHeight, 2 );
24602467 configFile.println (calLine);
0 commit comments