]> git.itanic.dy.fi Git - BME280_driver/commitdiff
tempd: Attempt retry if 1wire sensor returns "85"
authorTimo Kokkonen <timo.t.kokkonen@iki.fi>
Wed, 21 Apr 2021 08:12:45 +0000 (11:12 +0300)
committerTimo Kokkonen <timo.t.kokkonen@iki.fi>
Wed, 21 Apr 2021 08:14:37 +0000 (11:14 +0300)
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 <timo.t.kokkonen@iki.fi>
tempd.c

diff --git a/tempd.c b/tempd.c
index 51b9dedc2961fdeb886ef79b895b193c3cf221a1..5120b00b6d0de4bc6a5a843efff9168699c546a0 100644 (file)
--- 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;
 }