Skip to content

Instantly share code, notes, and snippets.

@manio
Created January 11, 2018 17:33
Show Gist options
  • Save manio/76d86eab72b325490f33ce136179316b to your computer and use it in GitHub Desktop.
Save manio/76d86eab72b325490f33ce136179316b to your computer and use it in GitHub Desktop.
Sample program to obtain a humidity from DS2438
//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