# \[RESOLVED\] DS18B20 Library Modifications

**URL:** <https://community.particle.io/t/resolved-ds18b20-library-modifications/25980>\
**Category:** Libraries\
**Created:** [September 21, 2016, 10:05am UTC](https://community.particle.io/t/resolved-ds18b20-library-modifications/25980 "2016-09-21T10:05:06Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![joearkay](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/joearkay/32/10831_2.png) [@joearkay](https://community.particle.io/u/joearkay)\
**Post date:** [September 21, 2016, 10:05am UTC](https://community.particle.io/t/resolved-ds18b20-library-modifications/25980/1 "2016-09-21T10:05:06Z")

</div>

Hi All,

Apologies for the huge code dump…but why won’t this compile? I’ve been modifying the ‘Ds18b20’ library to add support for multiple sensors on different pins. I want to pass an ‘instance’ of each sensor to the getTemp() function. IDK, maybe I’ve been programming Java too much recently and I’m missing something obvious?!

I get the error message  
`error: variable or field 'getTemp' declared void`  
however I know this is because I’m trying to pass the type ‘DS18B20’ to getTemp(), what am I doing wrong here?

Thanks

code:

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

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

SYSTEM_MODE(SEMI_AUTOMATIC); //comment this in when debugging - prevents Particle connecting to 3G network

int sensorPin = D6;
DS18B20 eBayTemp = DS18B20(sensorPin); //Sets Pin for Water Temp Sensor
int led = D7;
char szInfo[64];
float pubTemp;
double celsius;
unsigned int Metric_Publish_Rate = 30000;
unsigned int MetricnextPublishTime;
int DS18B20nextSampleTime;
int DS18B20_SAMPLE_INTERVAL = 2500;
int dsAttempts = 0;

void setup() {
    Time.zone(-5);
    Particle.syncTime();
    pinMode(sensorPin, INPUT);
    Serial.begin(115200);
}

void loop() {

if (millis() > DS18B20nextSampleTime){
  getTemp(eBayTemp);
  }

}

void getTemp(DS18B20 sensor){
    if(!sensor.search()){
      sensor.resetsearch();
      celsius = sensor.getTemperature();
      while (!sensor.crcCheck() && dsAttempts < 4){
        Serial.println("Caught bad value.");
        dsAttempts++;
        Serial.print("Attempts to Read: ");
        Serial.println(dsAttempts);
        if (dsAttempts == 3){
          delay(1000);
        }
        sensor.resetsearch();
        celsius = sensor.getTemperature();
        continue;
      }
      dsAttempts = 0;
      DS18B20nextSampleTime = millis() + DS18B20_SAMPLE_INTERVAL;
    }
    char buffer [50];
    sprintf(buffer,"Temperature of sensor on pin %d is %f \0",sensorPin, celsius);
    
    Serial.println(buffer);
}
```

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [September 21, 2016, 10:14am UTC](https://community.particle.io/t/resolved-ds18b20-library-modifications/25980/2 "2016-09-21T10:14:31Z")

</div>

If you want to pass an object, you should either use a reference (`void getTemp(DS18B20 & sensor)`) or a pointer (`void getTemp(DS18B20* sensor)`).

But I guess you’ll have got more than just that one line as error message(s). Can you post the full output?

---

<div class="post-metadata">

**Author:** ![joearkay](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/joearkay/32/10831_2.png) [@joearkay](https://community.particle.io/u/joearkay)\
**Post date:** [September 21, 2016, 10:17am UTC](https://community.particle.io/t/resolved-ds18b20-library-modifications/25980/3 "2016-09-21T10:17:24Z")

</div>

Sure, thanks.

Error message:

```
    ds18b20_test.cpp:6:14: error: variable or field 'getTemp' declared void
 void getTemp(DS18B20 sensor);
              ^

ds18b20_test.cpp:6:14: error: 'DS18B20' was not declared in this scope
ds18b20_test.cpp: In function 'void loop()':
ds18b20_test.cpp:31:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     pinMode(sensorPin, INPUT);
                ^
ds18b20_test.cpp:32:19: error: 'getTemp' was not declared in this scope
     Serial.begin(115200);
                   ^
Error: Could not compile. Please review your code.

```

The error about the signed and unsigned int goes away when there are no other compilation issues 🙂

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [September 21, 2016, 10:18am UTC](https://community.particle.io/t/resolved-ds18b20-library-modifications/25980/4 "2016-09-21T10:18:14Z")

</div>

BTW, I just recalled that I had a similar discussion a few days ago here

> [@DS18B20 Switching pins during execution](https://community.particle.io/t/ds18b20-switching-pins-during-execution/25727):
>
> As I said in another post, I have a code working in an arduino and I want to port to photon. In my code, I have 5 positions where a DS18B20 sensor can be placed. During execution, the user can plug it out from Pos1 and plugin the sensor to Pos2, and after a while, put it back to Pos1, remove the sensor completely or whatever. Of course, the user has to tell the photon that the pin has change. I tried with the current libraries in Particle cloud but it seems that I have to declare the pin at st…

---

<div class="post-metadata">

**Author:** ![joearkay](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/joearkay/32/10831_2.png) [@joearkay](https://community.particle.io/u/joearkay)\
**Post date:** [September 21, 2016, 10:19am UTC](https://community.particle.io/t/resolved-ds18b20-library-modifications/25980/5 "2016-09-21T10:19:16Z")

</div>

I saw this, read through it for inspiration 😉

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [September 21, 2016, 10:20am UTC](https://community.particle.io/t/resolved-ds18b20-library-modifications/25980/6 "2016-09-21T10:20:28Z")

</div>

> [@joearkay](#):
>
> ds18b20\_test.cpp:6:14: error: 'DS18B20' was not declared in this scope

That's the important bit.  
You need to find out why it's not declared in this scope.

BTW, the signed/unsigned message is a _warning_ (which doesn't go away but is not displayed) without _errors_.

* * *

_Update:_

```cpp
 void getTemp(DS18B20 sensor);

```

Given this line - quoted in the error message - doesn't exist in your code, it could be that the Wiring preprocessor is tripping you up.

You could add this to your `.ino` file

```cpp
#pragma SPARK_NO_PREPROCESSOR
#include "Paritlce.h"
#include "Particle-OneWire.h"
#include "DS18B20.h"

void getTemp(DS18B20 & sensor);

```

---

<div class="post-metadata">

**Author:** ![joearkay](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/joearkay/32/10831_2.png) [@joearkay](https://community.particle.io/u/joearkay)\
**Post date:** [September 21, 2016, 10:26am UTC](https://community.particle.io/t/resolved-ds18b20-library-modifications/25980/7 "2016-09-21T10:26:30Z")

</div>

I thought it would be declared in the header file for the library? Could you explain what you mean by

> [@ScruffR](#):
>
> it could be that the Wiring preprocessor is tripping you up.

Thanks

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [September 21, 2016, 10:32am UTC](https://community.particle.io/t/resolved-ds18b20-library-modifications/25980/8 "2016-09-21T10:32:04Z")

</div>

See my edit above.

The Wiring preprocessor helps non-C/C++ people (when it works correctly) by adding “forgotten” forward declarations/prototypes of functions that will be implemented _after_ their first use (and it also includes `Particle.h`).  
But sometimes - like here - it doesn’t do its job well.  
I guess the output of the preprocessor of your code looks something like this - which would explain the error message

```cpp
// --------------- this part was added by the Wiring preprocessor ------------------
// some comments (to account for the error occuring on line 6
#include "Particle.h"  

// note: at this point DS18B20.h wasn't included yet
void getTemp(DS18B20 sensor);  
// --------------- this part was added by the Wiring preprocessor ------------------

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

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

SYSTEM_MODE(SEMI_AUTOMATIC); //comment this in when debugging - prevents Particle connecting to 3G network

int sensorPin = D6;
DS18B20 eBayTemp = DS18B20(sensorPin); //Sets Pin for Water Temp Sensor
int led = D7;
char szInfo[64];
float pubTemp;
double celsius;
...

```

---

<div class="post-metadata">

**Author:** ![joearkay](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/joearkay/32/10831_2.png) [@joearkay](https://community.particle.io/u/joearkay)\
**Post date:** [September 21, 2016, 10:37am UTC](https://community.particle.io/t/resolved-ds18b20-library-modifications/25980/9 "2016-09-21T10:37:00Z")

</div>

@ScruffR You’re a genius, thanks for your help! My strengths lie in Java so migrating to C/C++ will happen with hiccups I’m sure!

---

<div class="post-metadata">

**Author:** ![peekay123](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/peekay123/32/810_2.png) [@peekay123](https://community.particle.io/u/peekay123)\
**Post date:** [September 21, 2016, 12:32pm UTC](https://community.particle.io/t/resolved-ds18b20-library-modifications/25980/10 "2016-09-21T12:32:26Z")

</div>

@joearkay, tiny little thing… In setup() you call `Particle.syncTime();` but you also have `SYSTEM_MODE(SEMI_AUTOMATIC);`. The cloud won’t be connected to sync the time!

---

<div class="post-metadata">

**Author:** ![joearkay](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/joearkay/32/10831_2.png) [@joearkay](https://community.particle.io/u/joearkay)\
**Post date:** [September 21, 2016, 12:37pm UTC](https://community.particle.io/t/resolved-ds18b20-library-modifications/25980/11 "2016-09-21T12:37:07Z")

</div>

I just throw `SYSTEM_MODE(SEMI_AUTOMATIC);.` into the Particle when I’m serially debugging, so I don’t have to wait for the Particle to connect to my patchy 3G GSM signal 🙂

---

<div class="post-metadata">

**Author:** ![peekay123](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/peekay123/32/810_2.png) [@peekay123](https://community.particle.io/u/peekay123)\
**Post date:** [September 21, 2016, 12:48pm UTC](https://community.particle.io/t/resolved-ds18b20-library-modifications/25980/12 "2016-09-21T12:48:13Z")

</div>

@joearkay, FYI, when you run `SYSTEM_MODE(AUTOMATIC);` which is the default mode, time is synched automatically when the cloud connection is established. However, it may take a few seconds during which the RTC on the Photon will be incorrect if power has been removed and the RTC is wiped. You can do a simple test to see if the time is valid by checking that `Time.year() > 1970`. I usually run this code to do the check:

```auto
while (Time.year() <= 1970) {
    delay(10);
}

Note that the delay(10) will call Particle.process() in the background.

```

😉

---

<div class="post-metadata">

**Author:** ![joearkay](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/joearkay/32/10831_2.png) [@joearkay](https://community.particle.io/u/joearkay)\
**Post date:** [September 21, 2016, 12:50pm UTC](https://community.particle.io/t/resolved-ds18b20-library-modifications/25980/13 "2016-09-21T12:50:13Z")

</div>

Ah I like that. the reason I was calling `Particle.syncTime();` originally was due to the face that my first message would always send with a time stamp that was pre-epoch!

---

<div class="post-metadata">

**Author:** ![joearkay](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/joearkay/32/10831_2.png) [@joearkay](https://community.particle.io/u/joearkay)\
**Post date:** [September 23, 2016, 9:29am UTC](https://community.particle.io/t/resolved-ds18b20-library-modifications/25980/14 "2016-09-23T09:29:11Z")

</div>

Guys…you know when you have been looking at the same piece of code for days and you can’t work out what doesn’t work?

I’m running this modified version of the example again, and for some odd reason, half of the values return “0.00”…

Here is the code:

```
    #include "Particle-OneWire.h"
#include "DS18B20.h"

double getTemp(DS18B20 sensor); 

SYSTEM_MODE(SEMI_AUTOMATIC); //comment this in when debugging - prevents Particle connecting to 3G network

int genericPin = D6;
int loxonePin = D5;

DS18B20 generic = DS18B20(genericPin); 
DS18B20 loxone = DS18B20(loxonePin);
char szInfo[64];
int DS18B20nextSampleTime;
int DS18B20_SAMPLE_INTERVAL = 20000; //2500 default

void setup() {
    Time.zone(-5);
    Particle.syncTime();
    
    pinMode(genericPin, INPUT);
    pinMode(loxonePin, INPUT);

    Serial.begin(115200);
}

void loop() {

//if (millis() > DS18B20nextSampleTime){
    
    sprintf(szInfo, "generic: %2.2f", getTemp(generic));
    Serial.println(szInfo); 
    
    
    sprintf(szInfo, "loxone: %2.2f", getTemp(loxone));
    Serial.println(szInfo); 
    delay(10000);

 // }
}

double getTemp(DS18B20 sensor){
    
    int dsAttempts = 0;
    double celsius;
    if(!sensor.search()){
      sensor.resetsearch();
      celsius = sensor.getTemperature();
      //Serial.println(celsius);
      //return celsius;
      while (!sensor.crcCheck() && dsAttempts < 4){
        //Serial.println("Caught bad value.");
        dsAttempts++;
        //Serial.print("Attempts to Read: ");
        //Serial.println(dsAttempts);
        if (dsAttempts == 3){
          delay(1000);
        }
        sensor.resetsearch();
        celsius = sensor.getTemperature();
        continue;
        //return celsius;
      }
      dsAttempts = 0;
      //DS18B20nextSampleTime = millis() + DS18B20_SAMPLE_INTERVAL;
      //Serial.println(fahrenheit);
    }
    return celsius;
}

```

And here is a sample from the serial monitor:

```
    Opening serial monitor for com port: "COM7"
generic: 23.19
loxone: 24.00
generic: 0.00
loxone: 0.00
generic: 23.19
loxone: 24.00
generic: 0.00
loxone: 0.00
generic: 23.19
loxone: 24.00
generic: 0.00
loxone: 0.00
generic: 23.19
loxone: 24.00
generic: 0.00
loxone: 0.00
generic: 23.25
loxone: 24.00
generic: 0.00
loxone: 0.00
generic: 23.25
loxone: 24.00
generic: 0.00
loxone: 0.00
generic: 23.25
loxone: 24.06
generic: 0.00
loxone: 0.00

```

I’m sure I’ll feel like an idiot when someone tells me what I’m doing wrong, but I’m at the tipping point!!

Cheers 😆

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [September 23, 2016, 1:53pm UTC](https://community.particle.io/t/resolved-ds18b20-library-modifications/25980/15 "2016-09-23T13:53:27Z")

</div>

You might want to put back the debug prints inside that `getTemp()` 😉

---

<div class="post-metadata">

**Author:** ![joearkay](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/joearkay/32/10831_2.png) [@joearkay](https://community.particle.io/u/joearkay)\
**Post date:** [September 23, 2016, 2:12pm UTC](https://community.particle.io/t/resolved-ds18b20-library-modifications/25980/16 "2016-09-23T14:12:22Z")

</div>

@ScruffR I’ve had those uncommented for a bit. All I noticed is the ‘bad attempts’ at the first call to this, as the probes are just stablising after boot up. But I still can’t figure out for the life of me why the ‘celsius’ value isn’t being placed into the ‘celsius’ variable every OTHER call.

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [September 23, 2016, 2:22pm UTC](https://community.particle.io/t/resolved-ds18b20-library-modifications/25980/17 "2016-09-23T14:22:36Z")

</div>

I guess this is the reason

```auto
  double celsius;
  if(!sensor.search()) {
    ...
  }
  return celsius;

```

---

<div class="post-metadata">

**Author:** ![joearkay](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/joearkay/32/10831_2.png) [@joearkay](https://community.particle.io/u/joearkay)\
**Post date:** [September 23, 2016, 2:38pm UTC](https://community.particle.io/t/resolved-ds18b20-library-modifications/25980/18 "2016-09-23T14:38:33Z")

</div>

@ScruffR: The original code wrote to a global variable, ‘celsius’, but because I’m creating multiple instances of the DS18B20, I can’t do that, so i had to take this approach. I’ve simply replaced the ‘Serial.print()’ statements with ‘return’ statements, to get the celsius value back, but this is obviously not working…

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [September 23, 2016, 2:42pm UTC](https://community.particle.io/t/resolved-ds18b20-library-modifications/25980/19 "2016-09-23T14:42:19Z")

</div>

I guess your `sensor.search()` can’t find the sensor and hence will not return a valid temperature.  
To test this theory, you can just declare `double celsius = 123.45;` and see if that is coming back now instead of `0.0`.

I’d acutally expect random values to come back, since automatic variables aren’t initialzed by default, but maybe a previous call leaves a 0.0 on the stack.

---

<div class="post-metadata">

**Author:** ![joearkay](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/joearkay/32/10831_2.png) [@joearkay](https://community.particle.io/u/joearkay)\
**Post date:** [September 23, 2016, 2:53pm UTC](https://community.particle.io/t/resolved-ds18b20-library-modifications/25980/20 "2016-09-23T14:53:51Z")

</div>

That’s what I thought with regards to the ‘0.0’! Iv’e changed it and still ‘0.0’ back! Crazy times! Perhaps this library isn’t written in such a way that I can do this!

[Next page](https://community.particle.io/t/resolved-ds18b20-library-modifications/25980.md?page=2)
