/*Example code using Htu21d */ #include #include #include #include //additional libraries: #include #include "HTU21D_H.h" #include "BH1750_H.h" #include #define I2C_PATH "/dev/i2c-%d" #define I2C_PORT_2 2 #define I2C_PORT_1 1 #define HTU21D_json_path "/home/debian/CODIGOS/8-react-ui-sensors/ui/HTU21D.json" #define BH1750_json_path "/home/debian/CODIGOS/8-react-ui-sensors/ui/BH1750.json" int sensor; int main() { //printf("Select wich sensor would you like to get data: 1 -> HTU21D 2 -> BH1750 \n"); //scanf("%d", &sensor); //sensor = 2; while (1) { //if (sensor == 1){ char filePath[99]; snprintf(filePath, sizeof(filePath), I2C_PATH, I2C_PORT_2); int fd = open(filePath, O_RDWR); //to do.... complete the structure for the cases when we cant open the file if(fd < 0){ fprintf(stderr, "Error: Unable to access HTU21D sensor: %s \n ", strerror(errno)); exit(-1); } double Temperature = 10; double Humidity = 0; if((get_temp(fd, &Temperature)<0) || (get_hum(fd, &Humidity)<0)){ fprintf(stderr, "Error -404: Cant do the messaurment \n "); exit(-1); } //printf("Temperature : %5.2f �C\n", Temperature); //printf("Humidity : %5.2f % \n", Humidity); FILE *archivo = fopen(HTU21D_json_path, "w"); if (archivo == NULL){ perror("The file can not open "); return -1; } else{ fprintf(archivo, "{\"Temperature\" : %5.2f , \"Humidity\" : %5.2f}", Temperature, Humidity); } fclose(archivo); printf ("{\"Temperature\" : %5.2f , \"Humidity\" : %5.2f}", Temperature, Humidity); //} else if (sensor == 2){ //char filePath[99]; //snprintf(filePath, sizeof(filePath), I2C_PATH, I2C_PORT_1); //int fd = open(filePath, O_RDWR); //to do.... complete the structure for the cases when we cant open the file if(fd < 0){ fprintf(stderr, "Error: Unable to access HTU21D sensor: %s \n ", strerror(errno)); exit(-1); } double Bright = 0; if((get_lux(fd, &Bright) < 0)){ fprintf(stderr, "Error -404: Cant do the messaurment \n "); exit(-1); } //printf("Temperature : %5.2f �C\n", Temperature); //printf("Humidity : %5.2f % \n", Humidity); FILE *archivo_1 = fopen(BH1750_json_path, "w"); if (archivo_1 == NULL){ perror("The file can not open "); return -1; } else{ fprintf(archivo_1, "{\"Brightness\" : %5.2f }", Bright); } fclose(archivo_1); printf ("{\"Brightness\" : %5.2f }\n", Bright); //} usleep(2000000); } return 0; }