From 6e2f39ee2b228ef6510a8ae392eea9370f791e6a Mon Sep 17 00:00:00 2001 From: root Date: Sun, 11 Mar 2018 11:15:49 +0800 Subject: [PATCH] 20180311 --- raspberrypi_zero/gpio/wind.c | 55 ++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 raspberrypi_zero/gpio/wind.c diff --git a/raspberrypi_zero/gpio/wind.c b/raspberrypi_zero/gpio/wind.c new file mode 100644 index 0000000..3461708 --- /dev/null +++ b/raspberrypi_zero/gpio/wind.c @@ -0,0 +1,55 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#define N 80 + +int main(int argc, char *argv[]) +{ + char path[50] = "/sys/bus/w1/devices/"; + char rom[20]; + char buffer[N+1]; + DIR *dirp; + struct dirent *direntp; + FILE *fp; + char *temp; + float value; + + system("sudo modprobe w1-gpio"); + system("sudo modprobe w1-therm"); + if((dirp = opendir(path)) == NULL) { + exit(0); + } + + while((direntp = readdir(dirp)) != NULL) { + if(strstr(direntp->d_name, "28-0")) { + strcpy(rom, direntp->d_name); + } + } + closedir(dirp); + + strcat(path, rom); + strcat(path, "/w1_slave"); + //printf("%s\n", path); + + if ((fp = fopen(path, "r")) < 0) { + exit(0); /* 文件不存在,则退出. */ + } + while(fgets(buffer, N, fp) != NULL){ + ; + } + + temp = strchr(buffer, 't'); + sscanf(temp, "t=%s", temp); + value = atof(temp) / 1000; + printf("%.0f\n", value); + + fclose(fp); + return 0; +} +