sensebox / OER

Documentation for the senseBox
12 stars 7 forks source link

Longterm stability of senseBox:home Arduino #17

Closed hdznrrd closed 8 years ago

hdznrrd commented 8 years ago

The sensebox at shackspace[0] seems to hang up after a prolonged runtime and requires a powercycle to work again. Is this a problem specific to our box or do other boxes show this behavior as well? Shot in the dark: the watchdog timer could be a way to at least fight the symptoms.

[0] https://opensensemap.org/explore/56a0de932cb6e1e41040a68b

frankolivergloeckner commented 8 years ago

Hi, We (SB_Bremen_1) experience similar problems. In our case the SenseBox is still working and accessible via LAN, but does not submit data to the server anymore. After a power cycle it works again. https://opensensemap.org/explore/56b3ca1a2cb6e1e410499cb5

hdznrrd commented 8 years ago

I just took a look at the code and found the culprit: it's millis() overflowing after ~50 days[0]. The following is the code doing the check inside loop():

if (millis() - oldTime > postingInterval) {
oldTime = millis(); 

Once millis() overflows you're done for and the code inside the conditional will never run again.

[0] https://www.arduino.cc/en/Reference/Millis

mpfeil commented 8 years ago

thanks @hdznrrd for reporting and troubleshooting this bug. thanks @frankolivergloeckner for confirming it.

We will try to find a solution [0] tomorrow.

[0] http://arduino.stackexchange.com/questions/12587/how-can-i-handle-the-millis-rollover

mpfeil commented 8 years ago

@hdznrrd and @frankolivergloeckner: what do you think about this fix / solution:

We removed the causing if clause from the loop() and added a sleep() method to the end.

// millis() rollover fix - http://arduino.stackexchange.com/questions/12587/how-can-i-handle-the-millis-rollover
void sleep(unsigned long ms) {            // ms: duration
  unsigned long start = millis();         // start: timestamp
  for (;;) {
    unsigned long now = millis();         // now: timestamp
    unsigned long elapsed = now - start;  // elapsed: duration
    if (elapsed >= ms)                    // comparing durations: OK
      return;
  }
}
void loop()
{

  // read sensor values and post them and so on...

  sleep(postingInterval);
}

Our hardware guy @janwirwahn is not available for 1,5 weeks. When he is back, we will check the fix and roll out an update for all boxes.

mpfeil commented 8 years ago

Fix is now live https://github.com/sensebox/openSenseMap-API/blob/master/files/template_home/template_home.ino#L197