⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 reader.c

📁 SHT11温度湿度传感器的详细资料包获C代码和使用方法
💻 C
字号:
#include <math.h>#include <stdio.h>#include <stdlib.h>#include <getopt.h>#include <termio.h>#include <errno.h>#include <fcntl.h>#include <time.h>#define TEMP 0x01#define HUMI 0x02#define DEW  0x04#define TIME 0x08enum {LONG, SHORT};calc_sht11(float *p_temp, float *p_hum) {  const float C1 = -4.0;  const float C2 = 0.0405;  const float C3 = -0.0000028;  const float T1 = 0.01;  const float T2 = 0.0008;  float rh = *p_hum;  float t = *p_temp;  float rh_lin, rh_true, t_C;  t_C = t*0.01 - 40;  rh_lin = C3*rh*rh + C2*rh + C1;  rh_true =(t_C-25) * (T1+T2*rh) + rh_lin;  if (rh_true > 100) rh_true = 100;  if (rh_true < 0.1) rh_true = 0.1;  *p_hum = rh_true;  *p_temp = t_C;}float calc_dewpoint(float t, float h) {  float logEx, dew_point;  logEx=0.66077 + 7.5 * t / (237.3 + t) + (log10(h)-2);  dew_point = (logEx - 0.66077) * 237.3 / (0.66077 + 7.5 - logEx);  return dew_point;}void init_tty(char *s) {  int fd;  struct termio term;  if ((fd = open(s, O_RDONLY)) == -1) {    fprintf(stderr, "Can't open %s for reading\n",s);    exit(errno);  }  if ( ioctl(fd, TCGETA, &term) == -1 ) {    fprintf(stderr, "%s is not a tty\n",s);    exit(errno);  }  term.c_cflag &= ~CBAUD;  term.c_cflag |= B9600;  ioctl(fd, TCSETAW, &term);  close(fd);}int main(int argc, char **argv) {  unsigned int hum,temp;  float h,t;  unsigned char displaymode = LONG;  unsigned char output = 0;  int  count = -1;    int c;  int digit_optind = 0;  char device[20] = "/dev/ttyS0";  FILE *f;  time_t now;  struct tm *tmstruct;  char ts[20];  while (1) {    int this_option_optind = optind ? optind : 1;    int option_index = 0;    static struct option long_options[] = {      {"timestamp", 0, 0, 'a'},      {"count", 1, 0, 'c'},      {"device", 1, 0, 'd'},      {"help", 0, 0, 'h'},      {"dewpoint", 0, 0, 'p'},      {"short", 0, 0, 's'},      {"temperature", 0, 0, 't'},      {"humidity", 0, 0, 'u'},      {0, 0, 0, 0}    };    c = getopt_long (argc, argv, "ac:d:hpstu", long_options, &option_index);    if (c == -1)      break;    switch (c) {      case 'a':        output|=TIME;        break;      case 'c':        count = atoi(optarg);        break;      case 'd':        strncpy(device, optarg, 20);        break;      case 'h':        printf ("Usage: %s [OPTION]...\n", argv[0]);        printf ("\n");        printf ("-a, --timestamp\t\tShow timestamp\n");        printf ("-c, --count=COUNT\t\tDisplay COUNT lines and exit\n");        printf ("-d, --device\t\tSet serial interface (default: /dev/ttyS0)\n");        printf ("-h, --help\t\tDisplay this help and exit\n");        printf ("-p, --dewpoint\t\tCalculate dew point\n");        printf ("-s, --short\t\tShort output\n");        printf ("-t, --temperature\tShow temperature\n");        printf ("-u, --humidity\t\tShow humidity\n");        printf ("\n");        exit(0);        break;      case 'p':        output|=DEW;        break;      case 's':        displaymode = SHORT;        break;      case 't':        output|=TEMP;        break;      case 'u':        output|=HUMI;        break;    }  }  init_tty(device);  if ((f = fopen(device, "r")) == NULL) {    fprintf(stderr, "Can't open %s for reading\n",device);    exit(errno);  }  while(count--) {    if (count<0) count=-1;    fscanf(f,"%x %x", &temp, &hum);    h=(float)hum; t=(float)temp;    calc_sht11(&t, &h);    time(&now);    tmstruct = localtime(&now);    if (output == 0) output = TEMP|HUMI|DEW|TIME;    if (displaymode == LONG) {      strftime(ts, 20, "%x %X", tmstruct);      if (output & TIME) printf("Time: %s\t", ts);      if (output & TEMP) printf("Temp: %5.1fC\t", t);      if (output & HUMI) printf("Hum: %5.1f%%\t", h);      if (output & DEW)  printf("Dew point: %5.1fC\t", calc_dewpoint(t,h));    } else {      strftime(ts, 20, "%s", tmstruct);      if (output & TIME) printf("%s\t", ts);      if (output & TEMP) printf("%5.1f\t", t);      if (output & HUMI) printf("%5.1f\t", h);      if (output & DEW)  printf("%5.1f\t", calc_dewpoint(t,h));    }    printf("\n");    fflush(stdout);  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -