diff --git a/7-sensors/BH1750_C.c b/7-sensors/BH1750_C.c new file mode 100644 index 0000000..7556b9e --- /dev/null +++ b/7-sensors/BH1750_C.c @@ -0,0 +1,50 @@ +#include +#include //to print error +#include + +#include //header file for standard I2c send and receive commands +#include //setup I2c devices +#include //definitions and structures +#include //smbus commands + +#include "BH1750_H.h" + +//Declarations of functions +// Brightness: +int get_lux(int fd, double *lux){ + + // Establish the direction of the slave + if (ioctl(fd, I2C_SLAVE, BH1750_ADDR) < 0){ + perror("Error to stablish the direction of I2C"); + //close(fd); + return 1; + } + + // Send the command for the mesaurment + if(i2c_smbus_write_byte(fd, BH1750_OTHR) < 0){ + perror("Failed to send the command for mesaurment "); + //close(fd); + return 1; + } + + usleep(180000); + + char buffer[2]; + __s32 res = i2c_smbus_read_i2c_block_data(fd, 0x00, sizeof(buffer), buffer); + if (res != 2){ + perror("Error -1: Failed to read from the device"); + return 1; + } + *lux = ((buffer[0] << 8) | buffer[1]) / 1.2; + return 0; +} + +// Reset Sensor +/*int reset_BH1750(int fd){ + if(0 > ioctl(fd, I2C_SLAVE, BH1750_ADDR)){ + perror("Error -2: Failed to reset"); + return -2; + } + i2c_smbus_write_byte(fd, BH1750_RESET); + return 0; +}*/ \ No newline at end of file diff --git a/7-sensors/BH1750_H.h b/7-sensors/BH1750_H.h new file mode 100644 index 0000000..9ea89f9 --- /dev/null +++ b/7-sensors/BH1750_H.h @@ -0,0 +1,17 @@ +#ifndef BH1750_h //Definicion del archivo para header +#define BH1750_h + +#define BH1750_ADDR 0x23 //Definition of sensor address direction + +//Commands for reading of temperature or humidity +#define BH1750_PON 0x01 //Command for power on +#define BH1750_OTHR 0x20 //Command for One Time H-Resolution +#define BH1750_RESET 0xFE //Command for Reset the sensor + +//Function declarations +// Get the brightness: +int get_lux(int fd, double *lux); +// Reset: +int reset_BH1750(int fd); + +#endif \ No newline at end of file diff --git a/7-sensors/HTU21D.c b/7-sensors/HTU21D.c new file mode 100644 index 0000000..997fedc --- /dev/null +++ b/7-sensors/HTU21D.c @@ -0,0 +1,49 @@ +#include +#include //to print error + +#include //header file for standard I2c send and receive commands +#include //setup I2c devices +#include //definitions and structures +#include //smbus commands + +#include "HTU21D_H.h" + +//Declarations of functions +// Temperature: +int get_temp(int fd, double *temp){ + reset_sensor(fd); + char buffer[3]; + __s32 res = i2c_smbus_read_i2c_block_data(fd, HTU21D_TEMP, sizeof(buffer), buffer); + if (res < 0){ + perror("Error -1: Failed to read from the device"); + return -1; + } + *temp = -46.85+175.72*(buffer[0]*256+buffer[1])/65536.0; + return 0; +} + +// Humidity +//int get_hum(int fd, double *hum); +int get_hum(int fd, double *hum){ + reset_sensor(fd); + char buffer[3]; + __s32 res = i2c_smbus_read_i2c_block_data(fd, HTU21D_HUM, sizeof(buffer), buffer); + if ( res < 0 ){ + perror("Error -3: Failed to read humidity "); + return -1; + } + *hum = -6+125*(buffer[0]*256+buffer[1])/65536.0; + return 0; +} + +// Reset Sensor +int reset_sensor(int fd){ + if(0 > ioctl(fd, I2C_SLAVE, HTU21D_ADDR)){ + perror("Error -2: Failed to reset"); + return -2; + } + i2c_smbus_write_byte(fd, HTU21D_RESET); + return 0; +} + + diff --git a/7-sensors/HTU21D_H.h b/7-sensors/HTU21D_H.h new file mode 100644 index 0000000..e0ebcff --- /dev/null +++ b/7-sensors/HTU21D_H.h @@ -0,0 +1,19 @@ +#ifndef HTU21D_H //Definicion del archivo para header +#define HTU21D_H + +#define HTU21D_ADDR 0x40 //Definition of sensor address direction + +//Commands for reading of temperature or humidity +#define HTU21D_TEMP 0xE3 +#define HTU21D_HUM 0xE5 +#define HTU21D_RESET 0xFE + +//Function declarations +// Temperature: +int get_temp(int fd, double *temp); +// Humidity: +int get_hum(int fd, double *hum); +// Reset: +int reset_sensor(int fd); + +#endif diff --git a/7-sensors/ReadMe.md b/7-sensors/ReadMe.md new file mode 100644 index 0000000..e69de29 diff --git a/7-sensors/executable_1 b/7-sensors/executable_1 new file mode 100755 index 0000000..6511113 Binary files /dev/null and b/7-sensors/executable_1 differ diff --git a/7-sensors/main.c b/7-sensors/main.c new file mode 100644 index 0000000..ad7d63c --- /dev/null +++ b/7-sensors/main.c @@ -0,0 +1,86 @@ +/*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; +}