Testing Temperature Function 0810
parent
cc9137629d
commit
aacc46ef0e
@ -0,0 +1,33 @@
|
||||
#include <unistd.h> //i2c
|
||||
#include <sys/ioctl.h>
|
||||
#include <linux/i2c-dev.h>
|
||||
#include <i2c/smbus.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#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
|
||||
|
||||
@ -0,0 +1,28 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
#include "htu21d.h"
|
||||
|
||||
int main(int argc,char *argv[]){
|
||||
int fd;
|
||||
char path[20];
|
||||
snprintf(path, sizeof(path), "/dev/i2c-%d", 1);
|
||||
fd = open(path, O_RDWR);
|
||||
if(fd<0){
|
||||
//printf("Error");
|
||||
perror("Unable to access the i2c bus path\n");
|
||||
return -1;
|
||||
}
|
||||
i2cReset(fd);
|
||||
double temperature;
|
||||
int status = getTemp(fd, &temperature);
|
||||
if(status<0){
|
||||
perror("Unable to get temperature\n");
|
||||
return -1;
|
||||
}
|
||||
printf("%5.2f C\n", temperature);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Reference in New Issue