And on the 49th day, it stopped…

I’ve had two people recently email me regarding their Raspberry Pi sensor monitoring projects stop after 49 days… The reason is simple, the solution slightly more complex, so what’s going on?

The wiringPi GPIO library has a function: millis() which returns an unsigned 32-bit integer representing the number of milliseconds since your program started (or more accurately since your program last called one of the wiringPiSetup() functions). A few quick sums demonstrates that this counter wraps round after about 49 days, so programs that naively add a fixed number into the result of millis() to get the next time then test millis() > nextTime  will fail after 49 days…

And for at least one case, I’m the guilty one here – in my maxdetect code (used for the rht03 and similar sensors) I use millis() to make sure the next time we access the hardware is inside the datasheet limits. (The datasheet suggests not reading more than once every 2 seconds).

It’s easy to pass blame and not have any idea what’s going on – C for example doesn’t flag any warnings of integer overflow for example… However solutions to integer overflow problems are well understood and not complex.

So I’m working on a fix to the maxdetect code (which needs re-writing anyway), but if you’re using millis() in your own application, and it stops after 49 days, then now you know why!

And a quick note to say – uptime – 49 days – who says the Raspberry Pi isn’t reliable, but others should note that this issue is applicable to Adruinos and other microcontrollers too! More-so if you are using the micros() function which wraps after 71 minutes!

Comments are closed.