diff --git a/.htu21d.c.swp b/.htu21d.c.swp new file mode 100644 index 0000000..1e65dad Binary files /dev/null and b/.htu21d.c.swp differ diff --git a/.main.c.swp b/.main.c.swp new file mode 100644 index 0000000..2ea5012 Binary files /dev/null and b/.main.c.swp differ diff --git a/htu21d b/htu21d new file mode 100755 index 0000000..1ce0bf6 Binary files /dev/null and b/htu21d differ diff --git a/htu21d.c b/htu21d.c new file mode 100644 index 0000000..8591706 --- /dev/null +++ b/htu21d.c @@ -0,0 +1,34 @@ +#include +#include +#include +#include +#include + +#include "htu21d.h" + +// i2cReset +int i2cReset(int fd){ + if(ioctl(fd, I2C_SLAVE, ADDR)<0){ + perror("Failed to open the I2C bus: reset\n"); + return -1; + } + i2c_smbus_write_byte(fd, RESET); + return 0; +} + +// get temp +int getTemp(int fd, double *temp){ + char buffer[3]; + __s32 response = i2c_smbus_read_i2c_block_data(fd, TEMP, 3, buffer); + if(response<0){ + perror("Failed to read temperature from sensor \n"); + return -1; + } + *temp = -46.85+175.72*(buffer[0]*256+buffer[1])/65536; + return 0; +} + +// get humidity +int getHum(int fd, double *humidity){ + return 0; +} diff --git a/htu21d.h b/htu21d.h index 2377b8e..12dd59e 100644 --- a/htu21d.h +++ b/htu21d.h @@ -12,8 +12,10 @@ #define RESET 0xFE // prototype functions: +int i2cReset(int fs); + int getTemp(int fd, double *temp); int getHum(int fd, double *hum); -#endif // HTU21D_H +#endif // HTUD_H diff --git a/main.c b/main.c new file mode 100644 index 0000000..b08afe9 --- /dev/null +++ b/main.c @@ -0,0 +1,30 @@ +#include +#include +#include +#include + +#include "htu21d.h" + +int main(int argc, char *argv[]){ + int fd; + int status; + double temperature; + + char path[20]; + + snprintf(path, sizeof(path), "/dev/i2c-%d", 1); + fd = open(path, O_RDWR); + if(fd<0){ + perror("Unable to access the i2c bus path\n"); + return -1; + } + i2cReset(fd); + status = getTemp(fd, &temperature); + if(status<0){ + perror("Unable to get temperature \n"); + return -1; + } + printf("%5.2f C\n", temperature); + return 0; +} +