/********************************************************************************* * Copyright: (C) 2018 LingYun IoT System Studio * All rights reserved. * * Filename: sht20.c * Description: This file is temperature and relative humidity sensor SHT20 code * * Version: 1.0.0(2018/10/14) * Author: Guo Wenxue * ChangeLog: 1, Release initial version on "2018/10/14 12:13:26" * * Pin connection: * * vcc --- #Pin17 ( 3.3v ) * GND --- #Pin20 ( GND ) * SDA --- #Pin02 ( SDA ) * SCL --- #Pin03 ( SCL ) * * /boot/config.txt: * * dtparam=i2c_arm=on * ********************************************************************************/ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define SOFTRESET 0xFE #define TRIGGER_TEMPERATURE_NO_HOLD 0xF3 #define TRIGGER_HUMIDITY_NO_HOLD 0xF5 //#define I2C_API_IOCTL /* Use I2C userspace driver ioctl API */ #define I2C_API_RDWR /* Use I2C userspace driver read/write API */ static inline void msleep(unsigned long ms); static inline void dump_buf(const char *prompt, uint8_t *buf, int size); int sht2x_init(void); int sht2x_get_serialnumber(int fd, uint8_t *serialnumber, int size); int sht2x_get_temp_humidity(int fd, float *temp, float *rh); int main(int argc, char **argv) { int fd; float temp; float rh; uint8_t serialnumber[8]; fd = sht2x_init(); if(fd < 0) { printf("SHT2x initialize failure\n"); return 1; } if( sht2x_get_serialnumber(fd, serialnumber, 8) < 0) { printf("SHT2x get serial number failure\n"); return 3; } if( sht2x_get_temp_humidity(fd, &temp, &rh) < 0 ) { printf("SHT2x get get temperature and relative humidity failure\n"); return 3; } printf("Temperature=%lf ℃ relative humidity=%lf%\n", temp, rh); close(fd); } static inline void msleep(unsigned long ms) { struct timespec cSleep; unsigned long ulTmp; cSleep.tv_sec = ms / 1000; if (cSleep.tv_sec == 0) { ulTmp = ms * 10000; cSleep.tv_nsec = ulTmp * 100; } else { cSleep.tv_nsec = 0; } nanosleep(&cSleep, 0); } static inline void dump_buf(const char *prompt, uint8_t *buf, int size) { int i; if( !buf ) { return ; } if( prompt ) { printf("%s ", prompt); } for(i=0; i