guowenxue
2023-07-10 5e9d03d507aad324a803eb8795e0eed6fb671761
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/*********************************************************************************
 *      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 <guowenxue@gmail.com>
 *      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 <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include <sys/stat.h>
#include <linux/i2c-dev.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#include <stdint.h>
#include <time.h>
#include <errno.h>
#include <string.h>
 
 
#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<size; i++)
    {
        printf("%02x ", buf[i]);
    }
    printf("\n");
 
    return ;
}
 
 
 
#ifdef I2C_API_RDWR  /* Use I2C userspace driver read/write API */
 
int sht2x_softreset(int fd)
{
    uint8_t           buf[4];
 
    if( fd<0 )
    {
        printf("%s line [%d] %s() get invalid input arguments\n", __FILE__, __LINE__, __func__ );
        return -1;
    }
 
    /* software reset SHT2x */
    memset(buf, 0, sizeof(buf));
 
    buf[0] = SOFTRESET;
    write(fd, buf, 1);
 
    msleep(50);
 
    return 0;
}
 
int sht2x_init(void)
{
    int     fd;
    
    if( (fd=open("/dev/i2c-1", O_RDWR)) < 0)
    {
        printf("i2c device open failed: %s\n", strerror(errno));
               return -1;
    }
    
    /* set I2C mode and SHT2x slave address */
    ioctl(fd, I2C_TENBIT, 0);    /* Not 10-bit but 7-bit mode */
    ioctl(fd, I2C_SLAVE, 0x40); /* set SHT2x slava address 0x40*/
 
    if( sht2x_softreset(fd) < 0 )
    {
        printf("SHT2x softreset failure\n");
        return -2;
    }
 
    return fd;
}
 
int sht2x_get_temp_humidity(int fd, float *temp, float *rh)
{
    uint8_t           buf[4];
 
    if( fd<0 || !temp || !rh )
    {
        printf("%s line [%d] %s() get invalid input arguments\n", __FILE__, __LINE__, __func__ );
        return -1;
    }
 
    /* send trigger temperature measure command and read the data */
    memset(buf, 0, sizeof(buf));
    buf[0]=TRIGGER_TEMPERATURE_NO_HOLD;
    write(fd, buf, 1);
 
    msleep(85); /* datasheet: typ=66, max=85 */
 
    memset(buf, 0, sizeof(buf));
    read(fd, buf, 3);
    dump_buf("Temperature sample data: ", buf, 3);
    *temp = 175.72 * (((((int) buf[0]) << 8) + buf[1]) / 65536.0) - 46.85;
    
    /* send trigger humidity measure command and read the data */
    memset(buf, 0, sizeof(buf));
    buf[0] = TRIGGER_HUMIDITY_NO_HOLD;    
    write(fd, buf, 1);
 
    msleep(29); /* datasheet: typ=22, max=29 */
    memset(buf, 0, sizeof(buf));
 
    read(fd, buf, 3);
    dump_buf("Relative humidity sample data: ", buf, 3);
    *rh = 125 * (((((int) buf[0]) << 8) + buf[1]) / 65536.0) - 6;
 
    return 0;
}
 
int sht2x_get_serialnumber(int fd, uint8_t *serialnumber, int size)
{
    uint8_t           buf[4];
 
    if( fd<0 || !serialnumber || size!=8 )
    {
        printf("%s line [%d] %s() get invalid input arguments\n", __FILE__, __LINE__, __func__ );
        return -1;
    }
 
    /* Read SerialNumber from Location 1 */
    memset(buf, 0, sizeof(buf));
    buf[0] = 0xfa;  /* command for readout on-chip memory */
    buf[1] = 0x0f;  /* on-chip memory address */
    write(fd, buf, 2);
 
    memset(buf, 0, sizeof(buf));
    read(fd, buf, 4);
 
    serialnumber[5]=buf[0]; /* Read SNB_3 */
    serialnumber[4]=buf[1]; /* Read SNB_2 */
    serialnumber[3]=buf[2]; /* Read SNB_1 */
    serialnumber[2]=buf[3]; /* Read SNB_0 */
    
    /* Read SerialNumber from Location 2 */
    memset(buf, 0, sizeof(buf) );
    buf[0]=0xfc;  /* command for readout on-chip memory */
    buf[1]=0xc9;  /* on-chip memory address */
    write(fd, buf, 2);
 
    memset(buf, 0, sizeof(buf) );
    read(fd, buf, 4);
 
    serialnumber[1]=buf[0]; /* Read SNC_1 */
    serialnumber[0]=buf[1]; /* Read SNC_0 */
    serialnumber[7]=buf[2]; /* Read SNA_1 */
    serialnumber[6]=buf[3]; /* Read SNA_0 */
 
    dump_buf("SHT2x Serial number: ", serialnumber, 8);
 
    return 0;
}
 
#elif (defined I2C_API_IOCTL)  /* Use I2C userspace driver read/write API */
 
int sht2x_softreset(int fd)
{
           struct i2c_msg               msg;
    struct i2c_rdwr_ioctl_data   sht2x_data;
    uint8_t                      buf[2];
 
 
    if( fd<0 )
    {
        printf("%s line [%d] %s() get invalid input arguments\n", __FILE__, __LINE__, __func__ );
        return -1;
    }
 
    msg.addr= 0x40; 
    msg.flags=0; //write
    msg.len= 1; 
    msg.buf= buf; 
    msg.buf[0]=SOFTRESET;
 
    sht2x_data.nmsgs= 1; 
    sht2x_data.msgs= &msg;
 
    if( ioctl(fd, I2C_RDWR, &sht2x_data) < 0 )
    {
        printf("%s() ioctl failure: %s\n", __func__, strerror(errno));
        return -2;
    }
 
    msleep(50);
 
    return 0;
}
 
 
int sht2x_init(void)
{
    int     fd;
    
    if( (fd=open("/dev/i2c-1", O_RDWR)) < 0)
    {
        printf("i2c device open failed: %s\n", strerror(errno));
               return -1;
    }
 
    if( sht2x_softreset(fd) < 0 )
    {
        printf("SHT2x softreset failure\n");
        return -2;
    }
 
    return fd;
}
 
 
int sht2x_get_serialnumber(int fd, uint8_t *serialnumber, int size)
{
           struct i2c_msg               msgs[2];
    struct i2c_rdwr_ioctl_data   sht2x_data;
    uint8_t                      sbuf[2];
    uint8_t                      rbuf[4];
 
    if( fd<0 )
    {
        printf("%s line [%d] %s() get invalid input arguments\n", __FILE__, __LINE__, __func__ );
        return -1;
    }
 
    
    /*+------------------------------------------+
     *|     Read SerialNumber from Location 1    |
     *+------------------------------------------+*/
 
    msgs[0].addr= 0x40; 
    msgs[0].flags=0; //write
    msgs[0].len= 2; 
    msgs[0].buf= sbuf; 
    msgs[0].buf[0]=0xfa;  /* command for readout on-chip memory */
    msgs[0].buf[1]=0x0f;  /* on-chip memory address */
    
    msgs[1].addr=0x40; 
    msgs[1].flags=I2C_M_RD; //write
    msgs[1].len= 4; 
    msgs[1].buf= rbuf; 
 
    sht2x_data.nmsgs= 2; 
    sht2x_data.msgs= msgs;
 
    if( ioctl(fd, I2C_RDWR, &sht2x_data) < 0 )
    {
        printf("%s() ioctl failure: %s\n", __func__, strerror(errno));
        return -2;
    }
    
    serialnumber[5]=rbuf[0]; /* Read SNB_3 */
           serialnumber[4]=rbuf[1]; /* Read SNB_2 */
           serialnumber[3]=rbuf[2]; /* Read SNB_1 */
           serialnumber[2]=rbuf[3]; /* Read SNB_0 */
 
 
    /*+------------------------------------------+
     *|     Read SerialNumber from Location 2    |
     *+------------------------------------------+*/
    
    msgs[0].addr= 0x40; 
    msgs[0].flags=0; //write
    msgs[0].len= 2; 
    msgs[0].buf= sbuf; 
    msgs[0].buf[0]=0xfc;  /* command for readout on-chip memory */
    msgs[0].buf[1]=0xc9;  /* on-chip memory address */
    
    msgs[1].addr=0x40; 
    msgs[1].flags=I2C_M_RD; //write
    msgs[1].len= 4; 
    msgs[1].buf= rbuf; 
 
    sht2x_data.nmsgs= 2; 
    sht2x_data.msgs= msgs;
 
    if( ioctl(fd, I2C_RDWR, &sht2x_data) < 0 )
    {
        printf("%s() ioctl failure: %s\n", __func__, strerror(errno));
        return -2;
    }
    
    serialnumber[1]=rbuf[0]; /* Read SNC_1 */
    serialnumber[0]=rbuf[1]; /* Read SNC_0 */
    serialnumber[7]=rbuf[2]; /* Read SNA_1 */
    serialnumber[6]=rbuf[3]; /* Read SNA_0 */
 
    dump_buf("SHT2x Serial number: ", serialnumber, 8);
 
    return 0;
}
 
 
int sht2x_get_temp_humidity(int fd, float *temp, float *rh)
{
           struct i2c_msg               msg;
    struct i2c_rdwr_ioctl_data   sht2x_data;
    uint8_t                      buf[4];
 
    if( fd<0 )
    {
        printf("%s line [%d] %s() get invalid input arguments\n", __FILE__, __LINE__, __func__ );
        return -1;
    }
 
    
    /*+------------------------------------------+
     *|       measure and get temperature        |
     *+------------------------------------------+*/
 
    msg.addr= 0x40; 
    msg.flags=0; //write
    msg.len= 1; 
    msg.buf= buf; 
    msg.buf[0]=TRIGGER_TEMPERATURE_NO_HOLD;  /* trigger temperature without hold I2C bus */
 
    sht2x_data.nmsgs= 1; 
    sht2x_data.msgs= &msg;
 
    if( ioctl(fd, I2C_RDWR, &sht2x_data) < 0 )
    {
        printf("%s() ioctl failure: %s\n", __func__, strerror(errno));
        return -2;
    }
 
    msleep(85);
    
    memset(buf, 0, sizeof(buf));
    msg.addr=0x40; 
    msg.flags=I2C_M_RD; //write
    msg.len= 3; 
    msg.buf= buf; 
 
    sht2x_data.nmsgs= 1; 
    sht2x_data.msgs= &msg;
 
    if( ioctl(fd, I2C_RDWR, &sht2x_data) < 0 )
    {
        printf("%s() ioctl failure: %s\n", __func__, strerror(errno));
        return -2;
    }
    
    //dump_buf("Temperature sample data: ", buf, 3);
    *temp = 175.72 * (((((int) buf[0]) << 8) + buf[1]) / 65536.0) - 46.85;
 
    
    /*+------------------------------------------+
     *|    measure and get relative humidity     |
     *+------------------------------------------+*/
 
    msg.addr= 0x40; 
    msg.flags=0; //write
    msg.len= 1; 
    msg.buf= buf; 
    msg.buf[0]=TRIGGER_HUMIDITY_NO_HOLD;  /* trigger humidity without hold I2C bus */
 
    sht2x_data.nmsgs= 1; 
    sht2x_data.msgs= &msg;
 
    if( ioctl(fd, I2C_RDWR, &sht2x_data) < 0 )
    {
        printf("%s() ioctl failure: %s\n", __func__, strerror(errno));
        return -2;
    }
 
    msleep(29);
    
    memset(buf, 0, sizeof(buf));
    msg.addr=0x40; 
    msg.flags=I2C_M_RD; //write
    msg.len= 3; 
    msg.buf= buf; 
 
    sht2x_data.nmsgs= 1; 
    sht2x_data.msgs= &msg;
 
    if( ioctl(fd, I2C_RDWR, &sht2x_data) < 0 )
    {
        printf("%s() ioctl failure: %s\n", __func__, strerror(errno));
        return -2;
    }
 
    //dump_buf("Relative humidity sample data: ", buf, 3);
           *rh = 125 * (((((int) buf[0]) << 8) + buf[1]) / 65536.0) - 6;
    
    return 0;
}
 
#endif