From 5aa275bcc5aedd3b3aedf218cd4ddb8048673f07 Mon Sep 17 00:00:00 2001 From: Timo Kokkonen Date: Wed, 21 Apr 2021 11:12:45 +0300 Subject: [PATCH] tempd: Attempt retry if 1wire sensor returns "85" Result 85 is typical for DS18B20 sensors when some error happens with them. If so, attempt retry up to three times in order to recover from the error. If still error, return error instead of "85" as temperature value. Signed-off-by: Timo Kokkonen --- tempd.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tempd.c b/tempd.c index 51b9ded..5120b00 100644 --- a/tempd.c +++ b/tempd.c @@ -20,7 +20,9 @@ static int read_1wire_temp(const char *path, double *temp) char buf[16]; int fd; int ret; + int retries = 0; +retry: fd = open(path, O_RDONLY); if (fd < 0) { printf("%s: Failed to open file %s: %m\n", __func__, path); @@ -42,6 +44,15 @@ static int read_1wire_temp(const char *path, double *temp) out_close: close(fd); + if (*temp == 85) { + ret = -1; + if (retries < 3) { + retries++; + printf("Retrying 1wire read, try %d\n", retries); + goto retry; + } + } + return ret; } -- 2.45.0