Linux programming DS18B20 temperature probe for Raspberry Pi

table of Contents

(A) Project
(II) code, and debug
(c) summary

(A) Project

     DS18B20 digital temperature sensor is used, which is a digital output signal having a small size, low cost hardware, anti-interference ability, high accuracy, this experiment supports the project is based on raspberry party DS18B20 line temperature sensor. 1 ( line protocol), and then detect the temperature achieved by programming.

DQ is a digital signal input / output terminal;
the GND of the power supply;
the VCC is the external power supply input terminal;
wherein the two connection ports are as follows:
Ground terminal raspberry faction (pin 06 feet) -------- ---- port GND DS18B20's;
raspberry pie gpio 04 terminal (pin 07 feet) -------------- port of the DS18B20 DQ;
raspberry pie 3.3V terminal (pin 01 pin) ------------- connection port VCC of DS18B20;

(B) the code and debugging

1. Log in to the Raspberry Pi SecurtCRT remote server, and locate the file location, as shown below in w1_slave file in the root directory:
File location
first call open (), read (), close () three basic documents I / O functions to read the contents of the file:

  int main(int argc, char **argv)
  {    
     int fd;
       
     char buf[128];
      
     fd=open("/sys/bus/w1/devices/28-041731f7c0ff/w1_slave",O_RDONLY);
      
     memset(buf,0,sizeof(buf);   //初始化buf的内存,避免结果出现多余的字符
     
     read(fd,buf,sizeof(buf));   
     
     printf("buf: %s\n"buf);
 
     close(fd);
 }

Results are as follows:
Here Insert Picture Description
2. If you want to filter out other extraneous characters, we need to call strstr () function to locate "t =" string, and then use printf () direct printing; because bit floating-point temperature, in order to read we need to call facilitate the atof () function (a: ASCII code, to: convert, f: float type) i.e.: convert the ASCII string corresponding to the float.
Code optimization results were as follows:

#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>

int main(int argc, char **argv)
{
     int fd;
     char buf[128];
     char *ptr;
     float temp;
     
     fd=open("/sys/bus/w1/devices/28-041731f7c0ff/w1_slave",O_RDONLY);
    
     memset(buf,0,sizeof(buf);   
     
     read(fd,buf,sizeof(buf));
    
     printf("buf: %s\n"buf);
     
     ptr=strstr(buf,"t=");
     if(!ptr)
     {
         printf("can not find t=string\n");
         return -1;
     }
     
     ptr +=2;
    
     printf("ptr= %s\n",ptr);
    
     temp = atof(ptr)/1000;
    
     printf("temperature: %f\n",temp);

     close(fd);
}

Results are as follows:
Here Insert Picture Description
3. In fact DS18b20 chip serial number is changing, but are beginning to "28-", so we need to further optimize the code using Strstr () function to match "/ sys / bus / w1 / devices / "path containing the" character string 28- ", reuse strncat () function is spliced to the lower path, so that regardless of how changes in the chip serial number can be accurately positioned to the corresponding path

The final code is as follows:

#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<errno.h>
#include<dirent.h>
#include<stdlib.h>
int main(int argc, char **argv)
{
    int           fd=-1;
    char          buf[128];
    char          *ptr = NULL;
    float         temp;
    DIR           *dirp=NULL;
    struct dirent *direntp = NULL;
    char          w1_path[32]="/sys/bus/w1/devices/";
    char          chip_sn[32];
    int           found=0;

    dirp=opendir(w1_path);
    if(!dirp)
    {
        printf("open folder %s failure:%s\n",w1_path,strerror(errno));
        return -1;
    }
    while(NULL!=(direntp=readdir(dirp)))
    {
        if(strstr(direntp->d_name,"28-"))
        {
            strncpy(chip_sn,direntp->d_name,sizeof(chip_sn));
            found=1;
        }               

    }
    closedir(dirp);
    if(!found)
    {
        printf("can not find ds18b20 chipset\n");
        return -2;
    }
    strncat(w1_path,chip_sn,sizeof(w1_path)-strlen(w1_path));

    strncat(w1_path,"w1_slave",sizeof(w1_path)-strlen(w1_path));

    printf("w1_path:%s\n",w1_path);



    fd=open("w1_path",O_RDONLY);
    if(fd<0)
    {
        printf("open file failure: %s\n",strerror(errno));
        return -3;
    }
    memset(buf,0,sizeof(buf));   

    if(read(fd,buf,sizeof(buf))<0);
    {
        printf("read data from fd=%d failure: %s\n",fd,strerror(errno));
        return -4;
    }

    printf("buf: %s\n",buf);

    ptr=strstr(buf,"t=");
    if(!ptr)
    {
        printf("can not find t=string\n");
        return -5;
    }

    ptr +=2;

    printf("ptr= %s\n",ptr);

    temp = atof(ptr)/1000;

    printf("temperature: %f\n",temp);

    close(fd);

    return 0;
}

(C) summary

     The project involves some mastery of raspberry pie and DS18B20 hardware knowledge, but the main challenge is to understand under Linux file IO system calls, and some c programming language ability, no place where also please exhibitions, I hope my summary can be helpful to you.

Published an original article · won praise 1 · views 20

Guess you like

Origin blog.csdn.net/weixin_45930204/article/details/104583400