Quick update...
Still have the same issue September 17th to October 9th so thats 23 days of runtime.
Project is at a site 4hrs away and I have disabled the serial output as well so I will need to physically drove there to restart and start logging serially again but....
I run a fair few functions like this for example...
int TempCount = 0;
void Update_temp()
{
TempCount++;
if (TempCount >= 43200) {
TempCount = 0;
temp = modem.getTemperature();
char tempString[8];
dtostrf(temp, 1, 2, tempString);
mqtt.publish(topicTemperature, tempString);
}
}
So am I on the right track here?
If TempCount is unsigned int (16-bit unsigned): An unsigned int can hold a maximum value of 65,535. With Update_temp() called once per second: The counter would overflow after 65,535 seconds.
Converting this to days:
65,535 seconds ÷ (60 × 60) = ~18.2 hours.
So, using unsigned int would extend the overflow time to about 18 hours, but this is still far less than 23 days, leading to communication loss well before the end of the period.
If TempCount is unsigned long (32-bit unsigned):
An unsigned long can hold values up to 4,294,967,295.
With Update_temp() called once per second: The counter would overflow after 4,294,967,295 seconds.
Using an unsigned long would give a much longer time before overflow, and the 23 days of operation is well within the capacity of this data type.
Unsigned long: Use for counters that need to handle long time periods (e.g., over several days or weeks).
Unsigned int: If you know that a counter will never exceed 65,535 (around 18 hours for 1-second increments), you could consider using unsigned int instead to save memory (if your microcontroller has limited RAM).
Change to ...
Using unsigned long allows these counters to handle much larger values (up to 4,294,967,295) without risk of overflow for an extended period (years, depending on increment frequency).
unsigned long TempCount = 0;
unsigned long TempCount2 = 0;
unsigned long OverTempCount = 0;
unsigned long DelaytempCount = 0;
unsigned long CheckBalanceCount = 0;
unsigned long VoltagePSCount = 0;
unsigned long VoltagePSCount2 = 0;
unsigned long SDCardCount = 0;
unsigned long SDCardCount2 = 0;
unsigned long CSGtempCount = 0;
unsigned long Check_SMSCount = 0;
Variables are just flags that store HIGH or LOW or true/false states, consider using bool to reduce memory usage.
Boolean Values (PAlarm, LStatus, PoffStatus, Ponoff):
bool PAlarm = LOW;
bool LStatus = LOW;
bool PoffStatus = LOW;
bool Ponoff = LOW;
So am I heading in the right direction?
bool is used for simple true/false decisions.
unsigned int is used for medium-range positive numbers, up to 65,535 on most microcontrollers (or higher on 32-bit systems).
unsigned long is used when you need a larger range of positive numbers, up to 4.2 billion, such as for long-duration timers or large counters.