April 28, 2015

NodeMCU tmr.time() and tmr.now() bugs


The NodeMCU firmware has some nasty bugs in the tmr.time() and tmr.now() functions so don't use them!

From what I have observed in v0.95 and v0.9.6-dev_20150406 the following happens.

tmr.time(), that returns system time in seconds, makes a jump after 25430 seconds (about 7 hours).

       25427
       25428
       25429
       25430
       27043
       27044
       27045
       27046
       27047

tmr.now(), that returns system time in us, does at some point in time freeze and returns the same value for every call.

Theses bugs makes the functions unusable. The bugs seems to originate from the ESP8266 SDK used for the build rather that the implementation of NodeMCU it self. This will probably make it hard to fix. Luckily there is is a workaround since tmr.alarm() works fine. With tmr.alarm() you can easily create your own time measuring function. At least if you are fine with ms resolution. Hers thee code:

perid_ms = 100
timestamp = 0
tmr.alarm(0, period_ms, 1, function()
  timestamp = timestamp + 1
  end )

Then you can use the variable timestamp in you code to make time measurements.