Raspberry Pi + temperature module

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_45380951/article/details/100526680

Temperature module (18B20)

DS18B20 digital temperature sensor is used, the output is a digital signal;
characteristics: small size, low cost hardware, anti-interference ability, high precision, easy wiring;
after packaging to be used in many applications, such as pipes, threaded , magnetic adsorption, encapsulated stainless steel, a variety of models (LTM8877, LTM8874 etc.).
The different applications have different appearance, the package may be used cable channel temperature, water circulation temperature furnace, boiler temperature, room temperature, temperature measurement agricultural greenhouses, clean room temperature, and other temperature ammunition non-limiting temperature applications. Wear-resistant touch, small size, easy to use, packaged in various forms, for a variety of digital temperature measurement and control field devices small space.

FIG module (out raspberry send default connection pin 7, is generally used in power 3.3v)

#####

Upgrading the kernel

sudo apt-get update
sudo apt-get upgrade

Change setting

sudo nano /boot/config.txt

Add commands to the end of the manual

dtoverlay=w1 -gpio -pullup,gpiopin=4
Save and reboot raspberry pie, according to ctr+Xexit can be achieved.

Confirm whether the device is to take effect

sudo modprobe w1-gpio
sudo modprobe w1-therm
cd /sys/bus/w1/devices/
ls

show result

Here Insert Picture Description
28-0316977999cf my external temperature sensor devices are different for each client displayed, the serial number of the sensor.

View the current temperature

cd 28-0316977999cf
cat w1_slave

show result

Here Insert Picture Description
Finally, a second row is the temperature value, the current temperature is 28 degrees.

Write code

#include <fcntl.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <wiringPi>
int ds18b20_get_temperature(float * temp)
{
    char w1_path[50] = "/sys/bus/w1/devices/";
    char buf[128];
    int fd = -1; 
    char ID[20];
    DIR *dirp;
    struct dirent *direntp;
    int found = 0;
    char *ptr;
    if (temp == NULL)
    {   
        printf("argument error");
        return -1; 
    }   

    if ( (dirp = opendir(w1_path)) == NULL)
    {   
        printf("open %s failure.\n", w1_path);  
        return -2; 
    }   

    while ( (direntp = readdir(dirp)) != NULL)
    {   
        if (strstr(direntp->d_name, "28-") != NULL)
        {   
            strncpy(ID, direntp->d_name, 20 - strlen(ID));
            found = 1;
        }  
    }
    closedir(dirp);
    
    if (found != 1)
    {
        printf("can't find ds18b20.\n");
        return -3;
    }

    strncat(w1_path, ID, sizeof(w1_path)-strlen(w1_path));
    strncat(w1_path, "/w1_slave", 50-strlen(w1_path));

    if ( (fd = open(w1_path, O_RDONLY)) < 0)
    {
        printf("open %s failure.\n", w1_path);
        return -4;
    }

    if ( read(fd, buf, sizeof(buf)) < 0)
    {
        printf("read %s failure.", w1_path);
        return -5;
    }
    close(fd);

    ptr = strstr(buf, "t=");

    if ( ptr == NULL)
    {
        printf("get temperature failure.\n");
        return -6;
    }

    *temp = atof(ptr + 2)/1000;
    return 0;
}
int main(int argc, char *argv[])
{
    float temp = 0;
    if ( ds18b20_get_temperature(&temp) < 0)
    {   
        printf("get temperature error.\n");
        return 1;
    }   
    printf("Temperature is %f C\n", temp);
    return 0;
}

summary:
temperature and humidity module look 18D20, because the beginning did not see the type of module to do a lot of wasted effort, should not pick out the mouth raspberry pie pin 7.

Guess you like

Origin blog.csdn.net/weixin_45380951/article/details/100526680