I am having a small issue with millis() rollover on one of my nodes that monitors its own uptime. On most nodes that do similar, the millis() rollover happens after approximately 49.7049 days.
49.7049 * 24hrs * 60mins * 60seconds = 4,294,503.36 which is pretty close to the max value of a uint32_t, so I'm sure its being hit if you multiply by 1000
45.5104 * 24hrs * 60mins * 60seconds = 3,932,098.56 which is not very close to the max value of a uint32_t.
I noticed 45.5104 * 24hrs * 60mins = 65,534.976 which is the max value of uint16_t.
My code to calculate minutes and log to MQTT is as follows:
char str[4];
itoa(ip[3], str, 10);
char str1[] = "Node : \r\n";
memcpy(&str1[5], &str, strlen(str));
uint32_t seconds = millis() / 1000UL;
uint32_t minutes = seconds / 60UL;
char strMinutes[7];
utoa(minutes, strMinutes, 10);
memcpy(&str1[9], &strMinutes, strlen(strMinutes));
client.publish("upTime", str1);
then on a NodeRed node, receiving the MQTT message, I have the following:
var myArray = msg.payload.split(":");
msg.topic = myArray[0];
msg.payload = myArray[1];
msg.payload = msg.payload / 60;
msg.payload = msg.payload / 24;
msg.payload = msg.payload.toFixed(4);
return msg;
Is my math wrong, or is this device really looping after 45.5104 days?
Working code from another node:
uint32_t seconds = millis() / 1000UL;
uint32_t minutes = seconds / 60UL;
uint32_t hours = minutes / 60UL;
uint8_t days = hours / 24UL;
seconds %= 60;
minutes %= 60;
hours %= 24;
sprintf_P(buffer, PSTR("%u days, %lu hours %lu minutes %lu"), days, hours, minutes, seconds);
The failing code is running on an Arduino Pro Micro. Working code is on Pro Micro, Mega and Nano.
This is hard for me to debug, because it takes about 45 days to see if any changes worked.