Nodemcu trouble with avg. calculation

Hi

I have a nodemcu (esp8266) that I have connected to a AHT10 sensor.

I made a code that worked fine that showed live, min and max temperature/humidity, and ofcourse average temperature/humidity. however, one day it just stopped working. in other words bootlooping with "error" 2 and 6.

I thought it was the nodemcu that was faulty, but after using the elimination method, I found out that it was my code that made it faulty. more precise, how I calculated avg. values.

However, I really dont understand why it makes the nodemcu bootloop.

int x = 0;
//--------------------time
int timeinc = 0;
int minutesinh = 10;//3600;
int hourinc = 0;

//--------------------what day it is
int whatday = 0;
int dayinc = 0;
int wday = 0;

//---------------------average values
int avgT = 0;
int avgH = 0;
int Tavg = 0;
int Havg = 0;

//--------------------maximum and minimum values
int Tmax = 0;
int Tmin = 100;
int Hmax = 0;
int Hmin = 100;

//--------------------todays temperature and humidity
int timearrayT[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int timearrayH[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};


int dayone[7][2][25] = {{{}}}; // this week temp and humidity

void loop(){  

unsigned long currentMillis = millis();

if (currentMillis - previousMillis >= interval) { //-------every second do this

previousMillis = currentMillis;
sensors_event_t humidity, temp;
aht.getEvent(&humidity, &temp);
int ahtT = temp.temperature;
int ahtH = humidity.relative_humidity;

  t = ahtT; //------------------------------------------ populate values
  h = ahtH;

  if (t > Tmax) { //-----------check if value is less or more than max/min and update if so
  Tmax = t;
}
if (t < Tmin) {
  Tmin = t;
}
if (h > Hmax) {
  Hmax = h;
}
if (h < Hmin) {
  Hmin = h;
}

  timeinc += 1; //---------------------------------------one second is elapsed
}


if (timeinc >= minutesinh) { //--------------------------if one hour is elapsed
  
  timearrayT[hourinc] = t; //----------------------------add values to daily array
  timearrayH[hourinc] = h;

  avgT += t; //------------------------------------------add values to average
  avgH += h;

This is where the fault is. if I divide by ex. 2 (avgT/2) it works, but when dividing with a variable, the nodemcu bootloops.

 Tavg = avgT/hourinc; // -------------------------------calculate average
  Havg = avgH/hourinc; 
  
  hourinc += 1; //---------------------------------------update hour count

  timeinc = 0; // ---------------------------------------reset second count
}

if (hourinc > 24) { //------------------------------------24 hours is elapsed
  hourinc = 0;

  if (dayinc == 7){ // -----------------------------------one week elapsed 
    dayinc = 0;
  }
  for (int i = 0; i < 24; i++) {
      dayone[dayinc][0][i] = timearrayT[i]; //-------------move previous day to history
      dayone[dayinc][1][i] = timearrayH[i];
       
      timearrayT[i] = 0; //------delete previous day from daily array
      timearrayH[i] = 0;
  }
  avgT = 0; //---------------------------------------------reset average
  avgH = 0;

  Tmax = 0; //----------------------------------------------reset max/min values
  Tmin = 100;
  Hmax = 0;
  Hmin = 100;

  dayinc +=1; //--------------------------------------------update day counter
}

}

Divide-by-zero error?

1 Like

Totally correct, I didnt pick that up. I must have changed the order sometime when updating the code.

Thanks for the tip, it works just fine as it used to do when in this order;

hourinc += 1;
avgT += t;
avgH += h;

Tavg = avgT/hourinc;
Havg = avgH/hourinc; 

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.