I have written a small test code for reading temp and publishing.
Every second read fails for some reason and I cannot see why from the code.
Program print output…
Attempting Sample
20.12
Attempting Sample
No Reading
Attempting Sample
20.12
Attempting Sample
No Reading
Attempting Sample
No Reading
Attempting Sample
20.12
I have the DS1820 permanently connected to 5V supply and have a 4k7 pull up to +3.3V on D4.
Any help much appreciated. Code below
#include "DS18.h"
DS18 sensor(D4);
#define led D7
char message[20];
unsigned long lastUploadTime = 0UL; //publish time var
const unsigned long updateFrequency = 600000UL; // Update frequency in milliseconds (10 mins).
unsigned long lastSampleTime = 0UL; //sample time var
const unsigned long sampleFrequency = 5000UL; // Update frequency in milliseconds (5 seconds).
void setup() {
Serial.begin(115200);
pinMode(led,OUTPUT);
sprintf(message,"0.0");
}
void loop() {
unsigned long now = millis();
if(now-lastSampleTime > sampleFrequency){
Serial.println("Attempting Sample");
lastSampleTime = now;
if(sensor.read()){
sprintf(message,"%3.2f",sensor.celsius());
Serial.println(message);
}
else{
Serial.println("No Reading");
}
digitalWrite(led, HIGH); //Blink D7 to show sample taken
delay(50);
digitalWrite(led, LOW);
}
if(now - lastUploadTime > updateFrequency)
{
lastUploadTime = now;
Serial.print("Published");
Particle.publish("f",message);
}
}