You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
922 B
C

/* Example code of using htu21d.h
Israel Herrera
20/03/2025
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//Aditional librarys
#include <errno.h>
#include <fcntl.h>
#include "htu21d.h"
#define I2C_PATH "/dev/i2c-%d"
#define I2C_PORT 2
int main(){
char filePath[20];
snprintf(filePath, sizeof(filePath), I2C_PATH, I2C_PORT );
int fd = open(filePath, O_RDWR);
if(fd<0){
fprintf(stderr, "Error: Unable to access HTU21D sensor: %s",strerror(errno));
exit(-1);
}
//measurements
double temperature=0;
double humidity=0;
if((getTemp(fd, &temperature)<0)||(getHum(fd, &humidity)<0)){
fprintf(stderr,"Error -404: Measurments not read");
exit(-1);
}
//printf("HTU21D Module \n");
//printf("%5.2fC \n", temperature);
//printf("%5.2fC \n", humidity);
printf("{");
printf("\"temperature\": %5.2f,", temperature);
printf("\"humidity\": %5.2f", humidity);
printf("}");
return 0;
}