#include #include #include #define CMD_MEASURE_TEMPERATURE_HOLD 0XE3 #define CMD_MEASURE_HUMIDITY_HOLD 0xE5 #define SI702X_ADDRESS 0x40 int main(void) { unsigned char rbuf[2]; unsigned char wbuf[1]; double temperature = 0; double humidity = 0; int status = 0; // Net name I2C_1 Physical address : 0x12C70000 int fd = open("/dev/i2c-4", O_RDWR); if (fd < 0) { printf("ERROR: open failed\n"); return -1; } status = ioctl(fd, I2C_SLAVE, SI702X_ADDRESS); if (status < 0) { printf("ERROR: ioctl error\n"); close(fd); return -1; } wbuf[0] = CMD_MEASURE_TEMPERATURE_HOLD; write(fd, wbuf, 1); if (read(fd, rbuf, 2) != 2) printf("Failed to read from the i2c bus.\n"); temperature = rbuf[1] | rbuf[0] << 8; printf("Temperature : %.2lf *C\n", (temperature*175.72/65536) - 46.85) ; wbuf[0] = CMD_MEASURE_HUMIDITY_HOLD; write(fd, wbuf, 1); if (read(fd, rbuf, 2) != 2) printf("Failed to read from the i2c bus.\n"); humidity = rbuf[1] | rbuf[0] << 8; printf("Humidity : %.2lf %%\n", (humidity*125.0/65536) - 6); close(fd); }