Created
January 11, 2018 17:33
-
-
Save manio/76d86eab72b325490f33ce136179316b to your computer and use it in GitHub Desktop.
Sample program to obtain a humidity from DS2438
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Sample program to obtain a humidity from DS2438 | |
//more info: | |
//https://skyboo.net/2017/03/ds2438-based-1-wire-humidity-sensor/ | |
//compile with: gcc -o humid get_humid.c | |
#include <stdio.h> | |
void get_humidity() | |
{ | |
FILE *fp; | |
float temp=0, vdd=0, vad=0, humid; | |
fp = fopen("/sys/bus/w1/devices/26-0000020df12e/temperature", "r"); | |
if (!fp) | |
return; | |
fscanf(fp, "%f", &temp); | |
fclose(fp); | |
fp = fopen("/sys/bus/w1/devices/26-0000020df12e/vdd", "r"); | |
if (!fp) | |
return; | |
fscanf(fp, "%f", &vdd); | |
fclose(fp); | |
fp = fopen("/sys/bus/w1/devices/26-0000020df12e/vad", "r"); | |
if (!fp) | |
return; | |
fscanf(fp, "%f", &vad); | |
fclose(fp); | |
temp /= 256; | |
vdd /= 100; | |
vad /= 100; | |
if (temp==0 || vdd==0 || vad==0) | |
return; | |
humid = (vad / vdd - 0.16) / 0.0062 / (1.0546 - 0.00216 * temp); | |
printf("humidity = %f %%RH\n", humid); | |
} | |
int main() | |
{ | |
get_humidity(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment