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);
}
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
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
// --------------- 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;
...
@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!
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
@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:
while (Time.year() <= 1970) {
delay(10);
}
Note that the delay(10) will call Particle.process() in the background.
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!
@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.
@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…
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.
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!