Guo Wenxue
2022-09-21 2e7765f468c46728df16faa903d1f4b8a17f76fd
mqttd/hal/ds18b20.c
@@ -20,6 +20,8 @@
#include <time.h>
#include <errno.h>
#include "lylib/logger.h"
/* File Content:
   pi@raspberrypi:~/guowenxue $ cat /sys/bus/w1/devices/28-041731f7c0ff/w1_slave 
   3a 01 4b 46 7f ff 0c 10 a5 : crc=a5 YES
@@ -48,7 +50,7 @@
     *+-------------------------------------------------------------------+*/
    if((dirp = opendir(w1_path)) == NULL)
    {
        printf("opendir error: %s\n", strerror(errno));
        log_err("opendir '%s' error: %s\n", w1_path, strerror(errno));
        return -2;
    }
@@ -66,7 +68,7 @@
    if( !found )
    {
            printf("Can not find ds18b20 in %s\n", w1_path);
            log_fatal("Can not find ds18b20 in %s\n", w1_path);
            return -3;
    }
@@ -77,20 +79,20 @@
    /* open file /sys/bus/w1/devices/28-xxxx/w1_slave to get temperature */
    if( (fd=open(w1_path, O_RDONLY)) < 0 ) 
    { 
            printf("open %s error: %s\n", w1_path, strerror(errno));
            log_err("open %s error: %s\n", w1_path, strerror(errno));
            return -4;
    } 
    
    if(read(fd, buf, sizeof(buf)) < 0)
    {
            printf("read %s error: %s\n", w1_path, strerror(errno));
            log_err("read %s error: %s\n", w1_path, strerror(errno));
            return -5;
    } 
    
    ptr = strstr(buf, "t=");
    if( !ptr )
    {
            printf("ERROR: Can not get temperature\n");
            log_err("ERROR: Can not get temperature\n");
            return -6;
    }