Sorry for this up to an old post. But I am still having some issues with this topic. I did a test program to read 2 DS18B20 and modify 2 relays depending on the temperature read.
I simplified a bit the code because I had Adafruit PWM servo driver library to control some relays in a batch. Now it just measures 2 DS18B20 in D2 and D4 and switch on/off 2 relays in D3 and D6.
[code]// This #include statement was automatically added by the Particle IDE.
#include “DS18B20.h”
// This #include statement was automatically added by the Particle IDE.
#include “Particle-OneWire.h”
//#include “DS18B20/Particle-OneWire.h”
//#include “DS18B20/DS18B20.h”
#define TEMP_MAX_DAY 28
#define TEMP_MIN_DAY 27
#define PIN1 0
#define PIN2 1
#define PINTEMP D3
int led = D7;
char szInfo[64];
float pubTemp;
float celsius;
double fahrenheit;
DS18B20 ds18b20(D2);
unsigned int Metric_Publish_Rate = 5000;
unsigned int MetricnextPublishTime2;
unsigned int MetricnextPublishTime5;
int DS18B20nextSampleTime;
int DS18B20_SAMPLE_INTERVAL = 2500;
int dsAttempts = 0;
//DS18B20 ds18b20 = DS18B20(D2); //Sets Pin D2 for Water Temp Sensor
float tempe;
double max_val;
double min_val;
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x40);
void setup() {
pinMode(PINTEMP, OUTPUT);
pinMode(6, OUTPUT);
Time.zone(+2);
Particle.syncTime();
//pinMode(D2, INPUT);
//Particle.variable(“temp”, &tempe, DOUBLE);
Serial.begin(9600);
Particle.publish("start", "INIT", PRIVATE);
max_val = TEMP_MAX_DAY;
min_val = TEMP_MIN_DAY;
}
void loop() {
runManagement(2, 3);
delay(100);
runManagement(4, 6);
delay(1000);
}
void publishData(uint8_t position){
if(!ds18b20.crcCheck()){
return;
}
sprintf(szInfo, “POS: %d --> %2.2f”, position, celsius);
Particle.publish(“dsTmp”, szInfo, PRIVATE);
if(position == 2) {
MetricnextPublishTime2 = millis() + Metric_Publish_Rate;
} else if(position == 4) {
MetricnextPublishTime5 = millis() + Metric_Publish_Rate;
}
}
bool getTemperature(uint16_t pinSensor, float *value) {
int dsAttempts = 0;
float _tempWater;
ds18b20.setPin(pinSensor);
delay(200);
if(!ds18b20.search()) {
ds18b20.resetsearch();
_tempWater = ds18b20.getTemperature();
while (!ds18b20.crcCheck() && dsAttempts < 4) {
//Serial.printf(“Bad value (%d)”, dsAttempts++);
if (dsAttempts == 3)
delay(200);
ds18b20.resetsearch();
_tempWater = ds18b20.getTemperature();
}
*value = _tempWater;
return (dsAttempts < 4);
}
*value = -1.0;
return false;
}
void runManagement(uint16_t pinSensor, uint8_t pinRelay) {
//tempe = celsius;
bool checker = false;
do {
checker = getTemperature(pinSensor, &tempe);
delay(1000);
celsius = tempe;
} while(checker == FALSE);
if(tempe > max_val) {
digitalWrite(pinRelay, LOW);
} else if(tempe <= min_val) {
digitalWrite(pinRelay, HIGH);
}
if(pinSensor == 2 && millis() > MetricnextPublishTime2){
Time.zone(+2);
Particle.syncTime();
Serial.println(“Publishing now.”);
publishData(pinSensor);
}
if(pinSensor == 4 && millis() > MetricnextPublishTime5){
Time.zone(+2);
Particle.syncTime();
Serial.println(“Publishing now.”);
publishData(pinSensor);
}
}[/code]
I modified DS18B20 library. I added this function:
void DS18B20::setPin(uint16_t pin){
ds->setPin(pin);
}
And then, in OneWire library:
void OneWire::setPin(uint16_t pin){
pinMode(pin, INPUT);
_pin = pin;
}
In both libraries, this function is defined in .h file.
It works fine for a while. Sometimes 4 loops, sometimes 10… But after some time, I don’t get any more publishing on particle console. While, I get them from other devices. So console is working.
I tested with destructor and without it in DS18B20 library. Same result. What could be happening? Memory leak? How can I get the memory use? I would want to print it by uart.
I am very surprised because the arduino version of this code has been working for one and a half year without any single problem (A couple of restaring due to power outrage it the office).
Until now I have tested it for a couple of minutes and yes, it worked but if I leave it about 10 minutes, it get stuck. No response from uart, no response from console… The only thing which seems to work is cyan led.