[SOLVED] Reading DS18B20 in the correct sequence

madepablo: It seems we are doing the same thing in different places...

I'm working on waste heat recovery. This uses 3xDS18B20 + a water turbine. The ski lodge is a source of very cold water, 1.3C

Yes, the rtc battery is an issue i will try to solve in the future.

There was discussion a few days ago about RTC batteries.

About the header, It is not fundamental for me, but it is a requirement.

OK, enough said!

Ok, after some playing, here is the next step.

I readed the ID of each sensor and ordered them in the sequence i want to be readed. Following the recomendations done in the previous posts, each sequence (of each bus) is saved into an array, latter passed to the prodecure to read each id of the array and request the temperature from that sensor.

Here is the arrays declaration just to illustrate it:

uint8_t airsensors[1][8] = {
  {0x28,0x96,0x38,0x20,0x05,0x00,0x00,0xE2}
};

uint8_t snowsensors[14][8] = {
  {0x28,0x72,0x87,0x05,0x05,0x00,0x00,0xC6},
  {0x28,0x72,0x8E,0x04,0x05,0x00,0x00,0xBA},
  {0x28,0x22,0xF8,0x05,0x05,0x00,0x00,0xEA},
  {0x28,0xB4,0xAE,0x04,0x05,0x00,0x00,0x6F},
  {0x28,0xCB,0x84,0x04,0x05,0x00,0x00,0x96},
  {0x28,0x79,0x10,0x06,0x05,0x00,0x00,0x50},
  {0x28,0x52,0x30,0x05,0x05,0x00,0x00,0x6E},
  {0x28,0xF8,0x77,0x05,0x05,0x00,0x00,0x45},
  {0x28,0x60,0x41,0x06,0x05,0x00,0x00,0xC5},
  {0x28,0xD2,0x8F,0x04,0x05,0x00,0x00,0x2B},
  {0x28,0x04,0xA4,0x05,0x05,0x00,0x00,0x5A},
  {0x28,0x51,0xF7,0x05,0x05,0x00,0x00,0x44},
  {0x28,0x7E,0x2E,0x05,0x05,0x00,0x00,0x7B},
  {0x28,0x42,0xEF,0x04,0x05,0x00,0x00,0x8B}
};

uint8_t surfacesensors[1][8] = {
  {0x28,0x4A,0x86,0x20,0x05,0x00,0x00,0xB6}
};

uint8_t groundsensors[10][8]={
  {0x28,0x1C,0xC4,0x04,0x05,0x00,0x00,0x3E},
  {0x28,0xD4,0x2B,0x05,0x05,0x00,0x00,0x3A},
  {0x28,0x7E,0xFD,0x05,0x05,0x00,0x00,0x6B},
  {0x28,0xA5,0x57,0x04,0x05,0x00,0x00,0x56},
  {0x28,0xAF,0x1C,0x06,0x05,0x00,0x00,0x07},
  {0x28,0x7D,0x1E,0x05,0x05,0x00,0x00,0xA6},
  {0x28,0x6D,0xA6,0x04,0x05,0x00,0x00,0x03},
  {0x28,0x1E,0x86,0x05,0x05,0x00,0x00,0xB5},
  {0x28,0x3F,0x36,0x05,0x05,0x00,0x00,0x7B},
  {0x28,0xB4,0xFB,0x05,0x05,0x00,0x00,0xA7}
};

and this is the code i added int he loop to read each sensor in that sequence, including some error management, and the data save into a file:

sensors1.requestTemperatures();
    temp = sensors1.getTempC(airsensors[0]);
    datafile.print(temp);
    datafile.print(";");
    if ((temp == -127)||(temp == 85)){
      e = 6;
    }
    sensors2.requestTemperatures();
    for(int i = 0; i<14; i++){     
      temp = sensors2.getTempC(snowsensors[i]);
      datafile.print(temp);
      datafile.print(";");
      if ((temp == -127)||(temp == 85)){
        e = 7;
      }
    }
    sensors3.requestTemperatures();
    temp = sensors3.getTempC(surfacesensors[0]);
    datafile.print(temp);
    datafile.print(";");
    if ((temp == -127)||(temp == 85)){
      e = 8;
    }
    sensors4.requestTemperatures();
    for(int i = 0; i<10; i++){     
      temp = sensors4.getTempC(groundsensors[i]);
      datafile.print(temp);
      datafile.print(";");
      if ((temp == -127)||(temp == 85)){
        e = 9;
      }
    }

My next steps from here to save memory (my code is 26.5K/30.7k):
1.- To create a more simple loop which sould be able to read the four bus (probably, such as you said, by the use of other array containing the busses IDs).
2.- To pass the sensor IDs array to an eeprom (by the use of other code), and made this loop to read the sensors ID from the eeprom.

So, thank you guys for your great comments!

small tip

for(int i = 0; i<10; i++){ // int takes 2 bytes

for (uint8_t i = 0; i<10; i++){ // takes 1 byte uint8_t == unsigned int of 8 bits

for every loop you win a byte, add’s up :wink:

if you look at the addresses of the sensors you see that only byte 1,2,3,and 7 differ and that the other 4 are constant. You could compress the array size and make an "expand function" to convert the 4 bytes to an 8 byte address.

Thanks so much for the tip! Yes, every Byte is gold! so, thanks for the piece of code! About the second tip, I will try to see how to do it, because you are right, it could be more simple in your way. Thanks!

BTW,

I am trying to made a simpler code. For that reason, i try to apply your idea of use arrays to call the sensors in the different buses.

For that reason, i added this to the initialization section of the code:

OneWire oneWire1(AirTemp);                // Configure library to read air temperature sensor
DallasTemperature sensors1(&oneWire1);
OneWire oneWire2(SnowTemp);               // Configure library to read snow temperature sensors
DallasTemperature sensors2(&oneWire2);
OneWire oneWire3(SurfaceTemp);            // Configure library to read surface temperature sensor
DallasTemperature sensors3(&oneWire3);
OneWire oneWire4(GroundTemp);             // Configure library to read ground temperature sensors
DallasTemperature sensors4(&oneWire4);
DallasTemperature bus[4] = {sensors1, sensors2, sensors3, sensors4};  // Sensors buses in the device
const int sensorsnumber[4] = {1,14,1,10}; // Number of sensors in each bus

and this to the setup in order to check each bus have the addecuate number of sensors:

  for (int i=0; i=3; i++){   // Check if all the sensors in each bus are woking properly
    Serial.println(bus[i].getDeviceCount());
    if ((bus[j].getDeviceCount())<(sensorsnumber[j]));
      Serial.print("error on bus ");
      Serial.println(i);
  }

The program compile correctly, but the problem it return infinite messages of “error on bus 30” in the serial port…

If i write this:

  Serial.print(bus[0].getDeviceCount());
  Serial.print(";");
  Serial.print(bus[1].getDeviceCount());
  Serial.print(";");
  Serial.print(bus[2].getDeviceCount());
  Serial.print(";");
  Serial.println(bus[3].getDeviceCount());

but it return 0 for each bus…
So, i think there is something wrong in the bus declaration…

i also wrote this in the initialization section:

DallasTemperature bus[4] = {sensors1(&oneWire1), sensors2(&oneWire2), sensors3(&oneWire3), sensors4(&oneWire4)};

but it does not compile…

So, any idea about where could be the problem will be wellcome!

One small step:

By changing the definition in the loop the sequence error seems to be solved:

for (uint8_t i=0; i<4; i++){  
    if ((bus[i].getDeviceCount())<(sensorsnumber[i])){
      Serial.print("error on bus ");
      Serial.println(i);
    }
  }

Now i still have error on getting device count (0) in each bus, but the infinite error call is solved.

After studing the code, i think that the problem is located how the array of buses is defined:

DallasTemperature bus[4] = {sensors1, sensors2, sensors3, sensors4};

I don´t know how to define in other way what could be correct… i tryied this:

DallasTemperature bus[4] = {sensors1(&oneWire1), sensors2(&oneWire2), sensors3(&oneWire3), sensors4(&oneWire4};

But it is not recognized by arduino IDE:

no match for call to ‘(DallasTemperature) (OneWire*)’

So, still working on this.