Weird behavior when reading temperature with DallasTemperature.h

Hi,
I’m experiencing an inexplicable behavior from my DS18B20 temperature sensor,
It seems to return an error value (-127) if the float variable storing the temperature is not
printed out before it’s use.

Not working code slimmed down:

#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2

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

void setup(){
  Serial.begin(9600);
  while(!Serial);

  sensors.begin();
  Serial.println(sensors.getDeviceCount());
}

void log(int num,float v){
  char s[20];

  //Serial.println(v);//commented print
  dtostrf(v,0,2,s);
  Serial.print(s);
  snprintf(s,19,"%d;%s\r\n",num,s);
  ...
}

float getTemperature(){
  float t;

  sensors.requestTemperatures();
  t=sensors.getTempCByIndex(0);
  //Serial.println(t);//commented print
  return t;
}

Perfectly working code:

#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2

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

void setup(){
  Serial.begin(9600);
  while(!Serial);

  sensors.begin();
  Serial.println(sensors.getDeviceCount());
}

void log(int num,float v){
  char s[20];

  Serial.println(v);// !!!not commented commented print
  dtostrf(v,0,2,s);
  Serial.print(s);
  snprintf(s,19,"%d;%s\r\n",num,s);
  ...
}

float getTemperature(){
  float t;

  sensors.requestTemperatures();
  t=sensors.getTempCByIndex(0);
  //Serial.println(t);//commented print
  return t;
}

Everything is of course part of a much bigger program, but the presence or not-presence
of that print makes all the difference.
There are two Serial.print(); ,any of them fixes the problem. (both too)
Without a print, the sensor doesn’t even show up on deviceCount

To be fair I’m not exactly new to programming (especially in C), but here,
I’ve really ran out of ideas. I lost so much time after this so here I am,
begging desperately for help

Arduino Leonardo
Arduino IDE 1.8.4
Windows 10

Thank you

Your code won't compile. There's no loop function.

Please post a short code that actually compiles and demonstrates the problem.

Everything is of course part of a much bigger program

Of course, the part you did not post is where the actual error is.

A value of -127 indicates that the DS18B20 isn't wired to the Arduino correctly.
How have you wired the 3 pins of the sensor to the Arduino?
Do you have a 4.7k pullup resistor on the data line? Have you wired the sensor with parasite power or normal power?

Pete

Ok, I'll post a runnable code if I will be able to reproduce the error.
I posted a slimmed down version because a print() instruction makes all the difference between a completely working code and a -127 mess.
The sensor works fine, (no parasite) as the provided library examples and my program (with the print) works fine.
really, by simply toggling the comment from that print, everything works fine.

I'll try to make an mcve, when I'll actually be able to identify and isolate the problem (I understand that the print() call isn't a proper fix)

Making a proper MCVE may actually reveal the problem.

I can’t manage to replicate the problem, I corrected the “snprintf(s,19,”%d;%s\r\n",num,s)" call and implemented a temporary string but apart from that I’m still clueless.
I can’t understand anyway how something could interfere with sensors.begin(); or sensors.getDeviceCount(); inside setup().

I dismounted and remounted the code too many times by now, these are the behaviours I found:

void log(int num,float v){
  char s[20],t[10];

//  Serial.print(v);

  dtostrf(v,0,2,t);
  Serial.print("< ");
  Serial.print(num);
  ...
}

float getTemperature(){
  float t;

//slog("# attempt to read temperature  #\r\n");
  sensors.requestTemperatures();
  for(i=1;i<4&&(t=sensors.getTempCByIndex(0))==-127;i++){
    slog("#      ..failed, try ");
    slog(i);
    slog("..       #\r\n");
    delay(1000);
    sensors.requestTemperatures();
  }
  if(i==4){
    slog("#    attempt dropped, -127     #\r\n");
  }
  return t;
}

Two commented lines:
-if slog is commented, it works, even without Serial.print();
-if Serial.print(); is not commented, everything runs well no matter what.

And by well, I mean that getDeviceCount() returns 1 (yes the one on the setup), and sensors.getTempCByIndex(0); is correct.

Even if the log() and getTemperature() functions are a rather closed system, (a float variable and that’s all), I was thinking about a buffer overflow somewhere in the program that corrupts some part of the program (never happened before to me, but I don’t know, I’m new on Arduino).
But why the hell should getDeviceCount(), one of the first instructions, be affected??
What’s happening!

All the library examples, and attempts of mcve works correctly.
The sensor is wired up correctly

Full sketch attached

Acquarius.ino (4.6 KB)

Did you try starting with the MCVE and then adding back features one at a time until it breaks?