Hi, I am using a DS18b20 (in the metal capsuled waterproof version) to monitor temperature. Strangely, it seems to fail in a small temperature range, reporting higher values. Above and below, everything is fine. Is my sensor just broken or does anybody have any idea?
The attached chart shows values collected with an ESP8266 and averaged over 5min. This is the code, based on the example sketch:
#include <OneWire.h>
#include <DallasTemperature.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266mDNS.h>
#include <PubSubClient.h>
#define min(a,b) ((a)<(b)?(a):(b))
const char *ssid = "SSID";
const char *password = "PSK";
const char *mqtt_server = "192.168.2.6";
const char *mqtt_user = "aquatemp";
const char *mqtt_pw = "PW";
const char *mqtt_state_topic = "home-le/firstfloor/diningroom/aquarium/temperature";
unsigned long mqttReconnectTime = millis();
unsigned long mqttReconnectInterval = 5000;
unsigned long reportTime = millis();
unsigned long reportInterval = 300000;
unsigned int valueCount = 0;
float valueSum = 0;
WiFiClient espClient;
PubSubClient mqttclient(espClient);
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS D6
const int ledPin = BUILTIN_LED;
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
// arrays to hold device address
DeviceAddress insideThermometer;
void setup(void)
{
// start serial port
Serial.begin(9600);
digitalWrite ( ledPin, LOW );
Serial.begin ( 115200 );
WiFi.begin ( ssid, password );
Serial.println ( "" );
// Wait for connection
while ( WiFi.status() != WL_CONNECTED ) {
delay ( 500 );
Serial.print ( "." );
}
Serial.println ( "" );
Serial.print ( "Connected to " );
Serial.println ( ssid );
Serial.print ( "IP address: " );
Serial.println ( WiFi.localIP() );
if ( MDNS.begin ( "aquatemp" ) ) {
Serial.println ( "MDNS responder started" );
}
mqttclient.setServer(mqtt_server, 1883);
mqttclient.setCallback(mqtt_callback);
digitalWrite ( ledPin, HIGH );
Serial.println("Dallas Temperature IC Control Library Demo");
// locate devices on the bus
Serial.print("Locating devices...");
sensors.begin();
Serial.print("Found ");
Serial.print(sensors.getDeviceCount(), DEC);
Serial.println(" devices.");
// report parasite power requirements
Serial.print("Parasite power is: ");
if (sensors.isParasitePowerMode()) Serial.println("ON");
else Serial.println("OFF");
// Method 1:
// search for devices on the bus and assign based on an index. ideally,
// you would do this to initially discover addresses on the bus and then
// use those addresses and manually assign them (see above) once you know
// the devices on your bus (and assuming they don't change).
if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0");
Serial.print("Device 0 Address: ");
printAddress(insideThermometer);
Serial.println();
// set the resolution to 9 bit (Each Dallas/Maxim device is capable of several different resolutions)
sensors.setResolution(insideThermometer, 12);
Serial.print("Device 0 Resolution: ");
Serial.print(sensors.getResolution(insideThermometer), DEC);
Serial.println();
}
void mqtt_callback(char* topic, byte* payload, unsigned int length) {
}
// function to print the temperature for a device
void printTemperature(DeviceAddress deviceAddress)
{
float tempC = sensors.getTempC(deviceAddress);
Serial.print("Temp C: ");
Serial.print(tempC);
Serial.print(" Temp F: ");
Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit
}
void loop(void)
{
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
delay(10);
sensors.requestTemperatures(); // Send the command to get temperatures
valueSum += sensors.getTempC(insideThermometer);
valueCount++;
if (!mqttclient.connected()) {
mqtt_reconnect();
}
else {
mqttclient.loop();
}
if (millis() - reportTime > reportInterval)
{
if (mqttclient.connected()) {
char outputStr[5];
float temp = valueSum / valueCount;
reportTime = millis();
sprintf(outputStr, "%d.%d", (int)temp, (int)(100 * (temp - (int)temp)));
if (mqttclient.publish(mqtt_state_topic, outputStr, false)) {
Serial.print("Published ");
Serial.print(outputStr);
Serial.print(" from ");
Serial.print(valueCount);
Serial.println(" measurements.");
} else {
Serial.println("Publishing failed.");
}
} else {
Serial.println("MQTT not connected");
}
valueSum = 0;
valueCount = 0;
}
}
// function to print a device address
void printAddress(DeviceAddress deviceAddress)
{
for (uint8_t i = 0; i < 8; i++)
{
if (deviceAddress[i] < 16) Serial.print("0");
Serial.print(deviceAddress[i], HEX);
}
}
void mqtt_reconnect() {
// Loop until we're reconnected
if (!mqttclient.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (mqttclient.connect("aquatemp", mqtt_user, mqtt_pw)) {
Serial.println("connected");
} else {
Serial.print("failed, rc=");
Serial.print(mqttclient.state());
Serial.println(" try again in 5 seconds");
}
}
}