From f251c2b51e0752a80a22e29bf145f1b38e506cd3 Mon Sep 17 00:00:00 2001 From: Beagle User Date: Thu, 29 May 2025 18:03:17 +0000 Subject: [PATCH] Adding the folder with the drivers for the sensors HTU21D and BD1750. --- 7-sensors/BH1750_C.c | 50 ++++++++++++++++++++++++ 7-sensors/BH1750_H.h | 17 ++++++++ 7-sensors/HTU21D.c | 49 +++++++++++++++++++++++ 7-sensors/HTU21D_H.h | 19 +++++++++ 7-sensors/ReadMe.md | 0 7-sensors/executable_1 | Bin 0 -> 13216 bytes 7-sensors/main.c | 86 +++++++++++++++++++++++++++++++++++++++++ 7 files changed, 221 insertions(+) create mode 100644 7-sensors/BH1750_C.c create mode 100644 7-sensors/BH1750_H.h create mode 100644 7-sensors/HTU21D.c create mode 100644 7-sensors/HTU21D_H.h create mode 100644 7-sensors/ReadMe.md create mode 100755 7-sensors/executable_1 create mode 100644 7-sensors/main.c 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 0000000000000000000000000000000000000000..6511113e9952ff45a3484b1b212f83c8bc7e125d GIT binary patch literal 13216 zcmeHOeQ;FQb-%kSBq0QdPYVIgLy`@Ste4Y&)$7APpHJu%5K9EP|11__8E|Dieu~7T zWx^7ZAzLUGP~QFabRjg9LWNM|0gB49EKjgdfS~08}x3U_JtEz+RyJ0M^UWe*Bg~UJ4mu1U!Lq-G*-HYX~8qpk)sax-qc7kyyS0u$gkB ze%T<{V|gG^pG(7y2z(enKEziO|#{>mqO zo&Ws8;+E%kulwN(w8MI6i#klyXTO-3uR&(~j-QjtX(&HMYV}$yNqcj}8A++1Y9J+1 zyan_hij==I@%wZEzpsG*Cxf2>`nbWf{E-6wwE{j|z`tL>|EPdpXz&*3XAGY8^%d}^ z3iy`__!|ZMV+H&d3;6pC-U9uC!Lxp0cWm8Y$DFLQGm&$h?A8r!$xPbW8tF_r!nSv& zGHE;KMzXGLE7fro6HQS&m+I`v*-vH@u48xhxsDji%sP>nO|~CMzPHnXHRC!KE`cG$U>zlNMdkWG3f`oQpqLiqbD!c`yI$H^6)myck)ROcYF9L z4{ryagT=Lj7z-~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qgbh3OqE*s31cl zLq!-G88ZIR;8i4|kuetaXoNyy1QGM7nyQ^J7LI6?)-j7lR$&mP9m{3xI4++NpvfRg zQ?+9g>WC%Mc2CZU>9|N!Va)#=V>AU3l18wlp{he9jjXY*jTg`fThgh@!D<$$*iFNP zjK0h|xRbV3BmtU$kW)Rw7>hb?fT@fPHSCCBt}lgKdtf)Ka9mS}1$DCBB8VsiCy0F} z*x8dnq;n$XWwo?+)VYzJLdxQiTwDZWeQDTJ*v%@*6HYeA_*1FoF*ambCrN!pbSGUA zl&%cAPA_P#N{BLY&IMaGZwO+HV!a~h#MP2i>S{GAbxp%)B$bGw!VC;41DLBK$Y293 zaUsn`TsNEO>~XPk{Jx3b19&Xqqm1vC1o^GNGe8+r4S+{Y>gl-iTF^S_t^IyvXg$6Q z5iG3)to1^mZ{S^@_Krb^PzhQeZ>UGu2;dQsdi)+iI0)cljgKqp@VmsHdpz~{{eqA` zdLa)*Wcnn2`OSfPeAgv3f!6ghpRfzSZxp2Y4ot8>vtHT)D^ovydE} +#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; +}