read data from hyt 271 (c code)

on

plaatje + text

pi@raspberrypi ~/bin $ vi readhyt.c

#include <stdio.h> 
#include <fcntl.h> /*file control options. ie for open file use O_RDWR*/
#include <sys/ioctl.h>
#include <linux/i2c-dev.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h> /* sleep */
#include <dirent.h>

int main()
{
int fh; 
unsigned char buf[4];
float h, t;

fh = open("/dev/i2c-1", O_RDWR);

ioctl(fh,I2C_SLAVE,0x28);

buf[0] = 0;
write(fh,buf,1);

usleep(60*1000);

read(fh,buf,4);

h = ((buf[0] & 0x3f) << 8 | buf[1]) * (100.0 / 0x3fff);
t = (buf[2] << 8 | (buf[3] & 0xfc)) * (165.0 / 0xfffc) - 40;

printf("0x%x 0x%x 0x%x 0x%x %4.2f %4.2f\n", buf[0],buf[1],buf[2],buf[3],h,t);

close(fh);
return 0;

}

$gcc readhyt.c -o readhyt

used variables:

fh file handle

h humidity

t temperature

$./readhyt
0x00 0x00 0x00 0x00 32.22 22.34

that part with buf[0] & 0x3f << 8 is called a mask.