Whats happening here?

So basically i want to log temperature and time in arrays so i can send them via webserver to the clients when connecting. storing them "in the opposite way" (0 = latest and x = oldest) so i can loop the array backwards to send the data in the right order.

The temperature array using floats works perfectly.
The time array for minute and second is chewing on my esp somehow.

heres the code:

float storedtemps[3];

int storedminutes[3];
int storedseconds[maxstoredvalues];

void gettime(){
    for(int i = maxstoredvalues; i >= 0; i--){
        struct tm timeinfo;
        getLocalTime(&timeinfo);
        byte minutes = timeinfo.tm_min;
        byte seconds = timeinfo.tm_sec;
      
      if(i != 0){
          storedseconds[i] = storedseconds[i-1];
          storedminutes[i] = storedminutes[i-1];
      }else{
          storedseconds[0] = seconds;
          storedminutes[0] = minutes;
      }

    }
}

void getTemperatureandlog(){
  sensors.requestTemperatures();

  float temperature = sensors.getTempCByIndex(0);

    for(int i = maxstoredvalues; i >= 0; i--){
      if(i != 0){
          storedtemps[i] = storedtemps[i-1];
        }else{
          storedtemps[0] = temperature;
        }
    }
}

The Print Function:

void printarraycontents(){
      Serial.println("-----------NEW LOG----------");
      for(int i = 0; i <= maxstoredvalues; i++){
      Serial.print("Index : ");
      Serial.print(i);
      Serial.print("  ");
      Serial.print(storedminutes[i]);
      Serial.print(":");
      Serial.print(storedseconds[i]);
      Serial.print("    value : ");
      Serial.println(storedtemps[i]);
    }
}

Result:


The red (should be only right) is the seconds readings.
The blue ( should be only left) is the minute readings.
I dont know how that is even possible.

Side Note:
When changing the array types of time to "byte" instead of "int" the temperature readings in the "value" section are showing up again.

Part of the error occurs in the rest of the sketch that you did not show. Snippets are not useful.

Pretty sure the rest wont help, however here it is:
(yes i removed the wlan credentials)

#include <OneWire.h>
#include <DallasTemperature.h>
#include <WiFi.h>
#include "esp_sntp.h"
#include "time.h"


OneWire oneWire(4);
DallasTemperature sensors(&oneWire);

const int maxstoredvalues = 2;  //(wanted -1)

float storedtemps[maxstoredvalues];
byte storedhours[maxstoredvalues];
byte storedminutes[maxstoredvalues];

byte storedseconds[maxstoredvalues];

char * ssid = "i";
char * password = "";


void setup() {
  Serial.begin(115200);
   WiFi.begin(ssid,password);
  while(WiFi.status()!=WL_CONNECTED){
    Serial.print(".");
    delay(500);
  }
  sensors.begin();

  //Time Client
  esp_sntp_servermode_dhcp(1);
  configTime(3600, 0, "pool.ntp.org", "time.nist.gov");
}


void loop() {

getTemperatureandlog();
gettime();

delay(2500);

printarraycontents();
}

void gettime(){
        struct tm timeinfo;
        getLocalTime(&timeinfo);
        byte minutes = timeinfo.tm_min;
        byte seconds = timeinfo.tm_sec;
    for(int i = maxstoredvalues; i >= 0; i--){   
      if(i != 0){
          storedseconds[i] = storedseconds[i-1];
          storedminutes[i] = storedminutes[i-1];
      }else{
          storedseconds[0] = seconds;
          storedminutes[0] = minutes;
      }

    }
}

void getTemperatureandlog(){
  sensors.requestTemperatures();

  float temperature = sensors.getTempCByIndex(0);

    for(int i = maxstoredvalues; i >= 0; i--){
      if(i != 0){
          storedtemps[i] = storedtemps[i-1];
        }else{
          storedtemps[0] = temperature;
        }
    }
}

void printarraycontents(){
      Serial.println("-----------NEW LOG----------");
      for(int i = 0; i <= maxstoredvalues; i++){
      Serial.print("Index : ");
      Serial.print(i);
      Serial.print("  ");
      Serial.print(storedminutes[i]);
      Serial.print(":");
      Serial.print(storedseconds[i]);
      Serial.print("    value : ");
      Serial.println(storedtemps[i]);
    }
}

Overruns storedseconds. That would be undefined behaviour.

2 Likes

And you are wrong, again.

Now that we can see how maxstoredvalues is defined and used to declare arrays, the cause of the error identified by @Coding_Badly is self evident.

The guideline against using snippets exists for a very good reason, as illustrated in this topic.

2 Likes

This works in the "gettime" function flawlessly tho. and it starts at the last index and stops at 0. not sure how it would overrun, it also exits the loop

tried it with defining it seperatly, doesnt make any difference.

Very well. It's no problem to me if you don't want to correct a very elementary mistake.

  • I cannot stand code that is not formatted !
#include <OneWire.h>
#include <DallasTemperature.h>
#include <WiFi.h>
#include "esp_sntp.h"
#include "time.h"


OneWire oneWire(4);
DallasTemperature sensors(&oneWire);

const int maxstoredvalues = 2;  //(wanted -1)

float storedtemps[maxstoredvalues];
byte storedhours[maxstoredvalues];
byte storedminutes[maxstoredvalues];

byte storedseconds[maxstoredvalues];

char * ssid = "i";
char * password = "";


void setup()
{
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED)
  {
    Serial.print(".");
    delay(500);
  }
  sensors.begin();

  //Time Client
  esp_sntp_servermode_dhcp(1);
  configTime(3600, 0, "pool.ntp.org", "time.nist.gov");
}


void loop()
{

  getTemperatureandlog();
  gettime();

  delay(2500);

  printarraycontents();
}

void gettime()
{
  struct tm timeinfo;
  getLocalTime(&timeinfo);
  byte minutes = timeinfo.tm_min;
  byte seconds = timeinfo.tm_sec;
  for (int i = maxstoredvalues; i >= 0; i--)
  {
    if (i != 0)
    {
      storedseconds[i] = storedseconds[i - 1];
      storedminutes[i] = storedminutes[i - 1];
    }
    else
    {
      storedseconds[0] = seconds;
      storedminutes[0] = minutes;
    }

  }
}

void getTemperatureandlog()
{
  sensors.requestTemperatures();

  float temperature = sensors.getTempCByIndex(0);

  for (int i = maxstoredvalues; i >= 0; i--)
  {
    if (i != 0)
    {
      storedtemps[i] = storedtemps[i - 1];
    }
    else
    {
      storedtemps[0] = temperature;
    }
  }
}

void printarraycontents()
{
  Serial.println("-----------NEW LOG----------");
  for (int i = 0; i <= maxstoredvalues; i++)
  {
    Serial.print("Index : ");
    Serial.print(i);
    Serial.print("  ");
    Serial.print(storedminutes[i]);
    Serial.print(":");
    Serial.print(storedseconds[i]);
    Serial.print("    value : ");
    Serial.println(storedtemps[i]);
  }
}

Which makes it even more evident that your complete sketch is nescessary to analyse what the error is as the error still occurs.

Your initial code-snippet did NOT show

it only hsowed this

Your loop runs from
maxstoredvalues which is 2

down to i >= 0
which means i runs down to 0
and what you calculate here
storedseconds[i-1]

is 0 -1 = -1
though index MINUS 1 does not exist

2 Likes

You copied half the code there, or am i this tired?

    for(int i = maxstoredvalues; i >= 0; i--){   
      if(i != 0){
          storedseconds[i] = storedseconds[i-1];
          storedminutes[i] = storedminutes[i-1];
      }else{
        storedseconds[0] = seconds;
        storedminutes[0] = minutes;
      }

    }

only calculates it when its not 0, when its 0 itll use the value that it got from the beginning

not sure if i understand you correctly, your saying i shouldnt use a variable to set the length of an array?

#include <OneWire.h>
#include <DallasTemperature.h>
#include <WiFi.h>
#include "esp_sntp.h"
#include "time.h"


OneWire oneWire(4);
DallasTemperature sensors(&oneWire);

const int maxstoredvalues = 4;  //(wanted -1)

float storedtemps[4];
byte storedhours[4];
byte storedminutes[4];

byte storedseconds[4];

char * ssid = "";
char * password = "";


void setup() {
  Serial.begin(115200);
   WiFi.begin(ssid,password);
    while(WiFi.status()!=WL_CONNECTED){
      Serial.print(".");
      delay(500);
    }
  sensors.begin();

  //Time Client
  esp_sntp_servermode_dhcp(1);
  configTime(3600, 0, "pool.ntp.org", "time.nist.gov");
}


void loop() {

  getTemperatureandlog();
  gettime();

  delay(2500);

  printarraycontents();
}

void gettime(){
  struct tm timeinfo;
  getLocalTime(&timeinfo);
  byte minutes = timeinfo.tm_min;
  byte seconds = timeinfo.tm_sec;
    for(int i = maxstoredvalues; i >= 0; i--){   
      if(i != 0){
          storedseconds[i] = storedseconds[i-1];
          storedminutes[i] = storedminutes[i-1];
      }else{
        storedseconds[0] = seconds;
        storedminutes[0] = minutes;
      }

    }
}

void getTemperatureandlog(){
  sensors.requestTemperatures();

  float temperature = sensors.getTempCByIndex(0);

    for(int i = maxstoredvalues; i >= 0; i--){
      if(i != 0){
          storedtemps[i] = storedtemps[i-1];
        }else{
          storedtemps[0] = temperature;
        }
    }
}

void printarraycontents(){
      Serial.println("-----------NEW LOG----------");
      for(int i = 0; i <= maxstoredvalues; i++){
      Serial.print("Index : ");
      Serial.print(i);
      Serial.print("  ");
      Serial.print(storedminutes[i]);
      Serial.print(":");
      Serial.print(storedseconds[i]);
      Serial.print("    value : ");
      Serial.println(storedtemps[i]);
    }
}

Result:


Time Was: 00:25, format here is minute:second

If you define an array like this

means you have arrays with 2 elements

element with index 0
element with index 1

then you run a for-loop

But you try to access element with index 2
which does not exist because a 2 elements array has only index 0 and index 1

The graphic that you posted

seems to have 4 elements
starting at 0 going up to 3

So all in all always post your complete and most actual sketch that fits to the serial output that you use to explain the problem

2 Likes

info: i may have changed the array sizes between posts

i was not aware setting it differently would actually change the size of the array.

When using:

byte storedseconds[4];

Expected: Array With 5 values from 0 to 4.
Result:

When using a const variable for it (i expected to be exactly the same)

const int maxstoredvalues = 4; 
byte storedseconds[maxstoredvalues];

Result:

funny thing is, when i change the array types to int(of the time arrays), it breaks the entire temperature array(the seperate float array) and it just reads 0 in the print.

wait, am i tripping? defining an array with

int array[2];

would have 3 members, wouldnt it?
-Index 0
-Index 1
-Index 2

no only 2 elements
element 1: Index 0
element 2: Index 1

2 Likes


how am i getting 5 different temperature readings on the right?
and with the index aswell.

does is differ when using float arrays or int arrays?

const int maxstoredvalues = 4;
. . . 
for(int i = maxstoredvalues; i >= 0; i--){  
  • Your code says so.
    4, 3, 2, 1, 0



for(int i = maxstoredvalues - 1 ; i >= 0; i--){  
2 Likes

tried printing the array's position isnt shouldnt even have:


the value/temperature works as expected.
but the time... i really dont know whats going on there

You are breaking the rules and accessing memory that you have no right to access. Sometimes that works, other times it will crash the computer.

Why bother to post if you can't accept elementary advice, from people who do understand these points?

1 Like