树莓派获取DS18B20温度数值

代码

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <wiringPi.h>

int Get_FilePath(char *path);    // 获取文件路径
int Get_DS18B20_Temp(double *temp, char *path);
	// 获取温度数值
/*
 * The main function
 */
int main(void)
{
	double	temp;
	char path[50] = "/sys/bus/w1/devices/";  // DS18B20是利用单总线协议进行通讯的,设备都是以文件形式存在
	Get_FilePath(path);   // 获取DS18B20存放温度数据的文件/sys/bus/w1/devices/28-xxxxxx/w1_slave' “28-”后面的DS18B20的序号。
	while(1){

		if (Get_DS18B20_Temp(&temp, path) < 0)
		{
			printf("ERROR: DS18B20 Get Temperature Failure\n");
			//return -1;
		}
		printf("Temperature is: %.3f C\n", temp);
		delay(1000);
		delay(1000);	
		delay(1000);			

	}
	return 0;
} /*-----End of main function-----*/

/*
 *获取文件路径
 */
int Get_FilePath(char *path){

	char			 dir_name[20];
	int				 found = -1;
	DIR 		    *dirp;
	struct dirent	*direntp;

	//清空dir_name内存空间的值,避免随机值产生乱码
	memset(dir_name, 0, sizeof(dir_name));

	//打开文件夹"/sys/bus/w1/devices/"
	if ((dirp = opendir(path)) == NULL)
	{
		printf("ERROR: Open Directory '%s' Failure: %s\n", path, strerror(errno));
		return -1;
	}
	printf("Open Directory '%s' Successfully\n", path);
	
	//在文件夹中找DS18B20的文件夹28-xx
	while ((direntp = readdir(dirp)) != NULL)
	{
		if (strstr(direntp->d_name, "28-") == NULL)
			continue;
		
		strncpy(dir_name, direntp->d_name, strlen(direntp->d_name));
		printf("Find file: %s\n", dir_name);
		found = 1;
		break;
	}
	closedir(dirp);
	
	//找不到该文件夹
	if (found == -1)
	{
		printf("ERROR: Can not find DS18B20 in %s\n", path);
		return -2;
	}
	
	//将文件夹名和文件名分别拼接到path上	
	strncat(path, dir_name, strlen(dir_name));
	strncat(path, "/w1_slave", sizeof(path)-strlen(path));

	return 1;
}



/*
 * 获取DS18B20测量的温度信息
 */
int Get_DS18B20_Temp(double *temp, char *path)
{
	int 			 fd = -1;
	int 			 rv = -1;
	char			*ptr;
	char 			 buf[128];
	
	//清空buf内存空间的值,避免随机值产生乱码
	memset(buf, 0, sizeof(buf));
	//打开w1_slave文件
	if ((fd = open(path, O_RDONLY)) < 0)
	{
		printf("ERROR: Open file '%s' Failure: %s\n", path, strerror(errno));
		return -3;
	}
	printf("Open file '%s' fd[%d] Successfully\n", path, fd);
	
	//从w1_slave中读取内容
	if ((rv = read(fd, buf, sizeof(buf))) < 0)
	{
		printf("ERROR: Read data from file '%s' Failure: %s\n", path, strerror(errno));
		rv = -4;
		goto cleanup;
	}
	printf("Read %dB data from file '%s'\n", rv, path);
	
	//从buf里匹配"t=",并将ptr移到数据的首地址上
	if ((ptr = strstr(buf, "t=") + 2) == NULL)
	{
		printf("ERROR: Find data Failure: %s", strerror(errno));
		rv = -5;
		goto cleanup;
	}
	
	//将数据转为double型,除1000得到正常的十进制温度
	*temp = atof(ptr)/1000;

cleanup:
	if (fd > 0)
		close(fd);

	return rv;
}

使能树莓派内核的单总线协议驱动模块(1-Wire)

【C语言】树莓派(Raspberry Pi)+DS18B20 获取当前温度

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

39度C

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值