DS18B20 dropps to -127

The DS18B20 dropps to -127.

Here is my code:

How can edit the code so it’s not showing the -127 value?

e.g. by using the the crcCheck() function that comes with the DS18B20 library as it’s used in the lib’s sample

    while (!ds18b20.crcCheck() && dsAttempts < 4)
    {
      Serial.printf("Bad value (%d)", dsAttempts++);
      if (dsAttempts == 3)
        delay(1000);
      ds18b20.resetsearch();
      _temp = ds18b20.getTemperature();
    }
    Serial.println(_temp);

    if(dsAttempts < 4)
    { // we have a valid reading (smooth with gliding avg over two readings)
      // use the valid reading 
    }
1 Like

Thanx for quick response. Can you please add crcCheck-function in the right place in my code?

// This #include statement was automatically added by the Particle IDE.
#include "OneWire/OneWire.h"

// This #include statement was automatically added by the Particle IDE.
#include "spark-dallas-temperature/spark-dallas-temperature.h"

// This #include statement was automatically added by the Particle IDE.
#include "HttpClient/HttpClient.h"

#define ONE_WIRE_BUS D4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensor(&oneWire);
 
float temperature = 0.0;
char resultstr[64];
HttpClient http;
 
#define VARIABLE_ID "my id"
#define TOKEN "my token"
 
http_header_t headers[] = {
      { "Content-Type", "application/json" },
      { "X-Auth-Token" , TOKEN },
    { NULL, NULL } // NOTE: Always terminate headers will NULL
};
 
http_request_t request;
http_response_t response;
 
void setup() {
    request.port = 80;
    request.hostname = "things.ubidots.com";
    request.path = "/api/v1.6/variables/"VARIABLE_ID"/values";
    Serial.begin(9600);
    sensor.begin();
}
 
void loop() {
 sensor.requestTemperatures();
 temperature=sensor.getTempCByIndex( 0 );
 sprintf(resultstr, "{\"value\":%.4f}",temperature); 
 request.body = resultstr;//Sending presence to Ubidots
 http.post(request, response, headers);
 Serial.println(response.status); //For debug only
 Serial.println(response.body);
 delay(10000);

}

I appreciate your help!

Sorry, not like that.
You are using the Spark-Dallas-Library which doesn’t sport that function and I’m not familiar with (yet).
But you should be able to swap out that library for the DS18B20 library I use and have good results with.

Sorry for my unknowledge about this, but i have now swap the spark-library with the DS18B20 and still igot error… :disappointed_relieved:

Try this

#include "DS18B20/DS18B20.h"
#include "HttpClient/HttpClient.h"
#include <math.h>

#define ONE_WIRE_BUS D4
//OneWire oneWire(ONE_WIRE_BUS);
//DallasTemperature sensor(&oneWire);
DS18B20 sensor(ONE_WIRE_BUS);

float temperature = 0.0;
char resultstr[64];
HttpClient http;
 
#define VARIABLE_ID "my id"
#define TOKEN "my token"
 
http_header_t headers[] = {
      { "Content-Type", "application/json" },
      { "X-Auth-Token" , TOKEN },
    { NULL, NULL } // NOTE: Always terminate headers will NULL
};
 
http_request_t request;
http_response_t response;
 
void setup() {
    request.port = 80;
    request.hostname = "things.ubidots.com";
    request.path = "/api/v1.6/variables/"VARIABLE_ID"/values";
    Serial.begin(9600);
    //sensor.begin();
}
 
float getTemp();

void loop() {
  //sensor.requestTemperatures();
  //temperature=sensor.getTempCByIndex( 0 );
  temperature = getTemp();
  if (temperature != NAN) {
    sprintf(resultstr, "{\"value\":%.4f}",temperature); 
    request.body = resultstr;//Sending presence to Ubidots
    http.post(request, response, headers);
    Serial.println(response.status); //For debug only
    Serial.println(response.body);
    delay(10000);
  }
}

float getTemp()
{
    int dsAttempts = 0;
    float celsius;
    //float farenheit;
    
    if(!sensor.search()) {
      sensor.resetsearch();
      celsius = sensor.getTemperature();
      while (!sensor.crcCheck() && dsAttempts < 4) {
        Serial.printlnf("Caught bad value (%d)", dsAttempts);
        dsAttempts++;
        if (dsAttempts == 3){
          delay(1000);
        }
        sensor.resetsearch();
        celsius = sensor.getTemperature();
      }
      //fahrenheit = sensor.convertToFahrenheit(celsius);
    }
    if (dsAttempts < 4) {
        return celsius;
    }
    
    return NAN; // signal bad value (Not-a-Number)
}

It’s work, but now it’s toggles between 0 an actually temperature.

Maybe i should try another DS18B20-sensor?

Sorry for the late response, but I’ve recently also run into some troubles with this sensor (or actually the library) in connection with deep sleep.
But after a minimal adaptation to the library things seem to work more reliable.


float DS18B20::getTemperature(){
    ds->reset();
    //ds->select(addr); // <-- replace this with the following line
    ds->write(0xCC);        // Skip ROM for single drop bus
    ds->write(0x44);        // start conversion, with parasite power on at the end
    delay(750);             // 750ms for 12bit read
    // we might do a ds.depower() here, but the reset will take care of it.
    ds->reset();
    //ds->select(addr); // <-- replace this with the following line
    ds->write(0xCC);        // Skip ROM for single drop bus
    ds->write(0xBE);        // Read Scratchpad
    ...
}

And with that the reading gets a bit simpler too

float getTemp()
{
  int   i = 0;
  float _temp;
  
  do {
    _temp = ds18b20.getTemperature();
  } while(!ds18b20.crcCheck() && DS18B20_MAXRETRY > i++);
  if (i < DS18B20_MAXRETRY) return _temp;
  
  return -99.9;
}
3 Likes