Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DS1820 improve logging of hex values / fix calc of negative temperatures #1416

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
43 changes: 31 additions & 12 deletions src/driver/drv_ds1820_simple.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,21 @@
#define interrupts() taskEXIT_CRITICAL()
#endif

// temperature value meaning "undefined"
// -12700 ~ -127.00°
#define DSUNDEFTEMP -12700
// timeout in seconds for unsudccessfull readings of sensor
// temperature will be set "undefined" after this time
// 300 seconds = 5 Minutes
#define DSTIMEOUT 30



static uint8_t dsread = 0;
static int Pin;
static int t = -127;
static int t = DSUNDEFTEMP;
static int errcount = 0;
static int lastconv; // secondsElapsed on last successfull reading
static int lastconv=0; // secondsElapsed on last successfull reading
static uint8_t ds18_family = 0;
static int ds18_conversionPeriod = 0;

Expand Down Expand Up @@ -360,7 +370,9 @@ void DS1820_driver_Init()

void DS1820_AppendInformationToHTTPIndexPage(http_request_t* request)
{
hprintf255(request, "<h5>DS1820 Temperature: %.2f C (read %i secs ago)</h5>", (float)t / 100, g_secondsElapsed - lastconv);
hprintf255(request, "<h5>DS18%s20 Temperature: ", ds18_family == 0x28 ? "B" : "20/DS18S20");
if (t != DSUNDEFTEMP) hprintf255(request, "%.2f C (read %i secs ago)</h5>", (float)t / 100, g_secondsElapsed - lastconv);
else hprintf255(request, "-</h5>");
}

int DS1820_DiscoverFamily()
Expand All @@ -384,7 +396,7 @@ int DS1820_DiscoverFamily()
if(crc != ROM[7])
{
// This might mean bad signal integrity or multiple 1-wire devices on the bus
DS1820_LOG(DEBUG, "Discover CRC failed (CRC=%x != calculated:%x)", ROM[7], crc);
DS1820_LOG(DEBUG, "Discover CRC failed (CRC=0x%02X != calculated:0x%02X)", ROM[7], crc);
return 0;
}

Expand All @@ -393,12 +405,12 @@ int DS1820_DiscoverFamily()
if(family == 0x10 || family == 0x28)
{
ds18_family = family;
DS1820_LOG(INFO, "Discover Family - discovered %x", family);
DS1820_LOG(INFO, "Discover Family - discovered 0x%02X", family);
return 1;
}
else
{
DS1820_LOG(DEBUG, "Discover Family %x not supported", family);
DS1820_LOG(DEBUG, "Discover Family 0x%02X not supported", family);
return 0;
}
}
Expand All @@ -408,6 +420,12 @@ void DS1820_OnEverySecond()
// for now just find the pin used
Pin = PIN_FindPinIndexForRole(IOR_DS1820_IO, 99);
uint8_t scratchpad[9], crc;
// handle reading timout - set local temp to "UNDEF" value ...
if ( (t != DSUNDEFTEMP) && lastconv && (g_secondsElapsed - lastconv > DSTIMEOUT)) {
t = DSUNDEFTEMP;
// .. but don't publish this value - so value can "time out"
// CHANNEL_Set(g_cfg.pins.channels[Pin], t, CHANNEL_SET_FLAG_SILENT);
}
if(Pin != 99)
{
// only if pin is set
Expand All @@ -432,8 +450,8 @@ void DS1820_OnEverySecond()
if(crc != scratchpad[8])
{
errcount++;
DS1820_LOG(ERROR, "Read CRC=%x != calculated:%x (errcount=%i)", scratchpad[8], crc, errcount);
DS1820_LOG(ERROR, "Scratchpad Data Read: %x %x %x %x %x %x %x %x %x",
DS1820_LOG(ERROR, "Read CRC=0x%02X != calculated:0x%02X (errcount=%i)", scratchpad[8], crc, errcount);
DS1820_LOG(ERROR, "Scratchpad Data Read: 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X",
scratchpad[0], scratchpad[1], scratchpad[2], scratchpad[3], scratchpad[4],
scratchpad[5], scratchpad[6], scratchpad[7], scratchpad[8]);

Expand All @@ -449,7 +467,7 @@ void DS1820_OnEverySecond()
int16_t dT = 128 * (scratchpad[7] - scratchpad[6]);
dT /= scratchpad[7];
raw = 64 * (raw & 0xFFFE) - 32 + dT;
DS1820_LOG(DEBUG, "family=%x, raw=%i, count_remain=%i, count_per_c=%i, dT=%i", ds18_family, raw, scratchpad[6], scratchpad[7], dT);
DS1820_LOG(DEBUG, "family=0x%02X, raw=%i, count_remain=%i, count_per_c=%i, dT=%i", ds18_family, raw, scratchpad[6], scratchpad[7], dT);
}
else
{ // DS18B20
Expand All @@ -458,18 +476,19 @@ void DS1820_OnEverySecond()
else if(cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
else if(cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
raw = raw << 3; // multiply by 8
DS1820_LOG(DEBUG, "family=%x, raw=%i, cfg=%x (%i bit resolution)", ds18_family, raw, cfg, 9 + (cfg) / 32);
DS1820_LOG(DEBUG, "family=0x%02X, raw=%i, cfg=0x%02X (%i bit resolution)", ds18_family, raw, cfg, 9 + (cfg) / 32);
}

// Raw is t * 128
t = (raw / 128) * 100; // Whole degrees
int frac = (raw % 128) * 100 / 128; // Fractional degrees
t += t > 0 ? frac : -frac;
t += frac;

dsread = 0;
lastconv = g_secondsElapsed;
CHANNEL_Set(g_cfg.pins.channels[Pin], t, CHANNEL_SET_FLAG_SILENT);
DS1820_LOG(INFO, "Temp=%i.%02i", (int)t / 100, (int)t % 100);
DS1820_LOG(INFO, "Temp=%0.2f", (float)t / 100);

}
}
else if(dsread == 0 && (g_secondsElapsed % ds18_conversionPeriod == 0 || lastconv == 0))
Expand Down
Loading