From: Timo Kokkonen Date: Wed, 21 Apr 2021 08:12:45 +0000 (+0300) Subject: tempd: Attempt retry if 1wire sensor returns "85" X-Git-Url: http://git.itanic.dy.fi/?p=BME280_driver;a=commitdiff_plain;h=5aa275bcc5aedd3b3aedf218cd4ddb8048673f07 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 --- 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; }