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)