commit 7c423a82669d736536b5b031f9390e1174528622 Author: EMOTIONS-HUNTER Date: Wed Jun 10 11:31:34 2026 -0600 Primer commit: Estructura base del proyecto Implementación de un Sistema de Monitoreo de Temperatura Basado en Raspberry Pi, Sensor EZO™ RTD y Sonda PT1000 Completada diff --git a/2MbNe2guV-beaglebone_black_pinmap.png b/2MbNe2guV-beaglebone_black_pinmap.png new file mode 100644 index 0000000..0fca38a Binary files /dev/null and b/2MbNe2guV-beaglebone_black_pinmap.png differ diff --git a/C8ZLtB-py-basic-ui.jpg b/C8ZLtB-py-basic-ui.jpg new file mode 100644 index 0000000..c13d47c Binary files /dev/null and b/C8ZLtB-py-basic-ui.jpg differ diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..e342247 --- /dev/null +++ b/Readme.md @@ -0,0 +1,248 @@ +# Readme basic-aqm + +# Air Quality Monitor +The Air Quality Monitor (AQM) project is a large development that requieres hardware, back-end programmming for the sensor, and front-end programming/configuration to display the measuring data. **To read about each step please visit the specific repositories.** + +This repository is about the setup and configuration of the sensors, back-end (APIs), and front-end (dashboard). + +# Sensors setup +The project uses a PCB custom designed that includes the following sensors: + +- HTU21D: Board Mount Humidity Sensors I.C 20D RH/T with I2C +- BH1750: Ambient Light Sensors Ambient Light Sensor Digital 16bit Serial I2C +- ZMOD4410: Air Quality Sensors TVOC IAQ Sensor with I2C output +- BMP581: Board Mount Pressure Sensors The BMP581 is a very small, low-power and low-noise 24-bit absolute barometric pressure sensor. + +**Todo: add here the pcb image** + +**However, you can connect the sensor-module version that is already prepared to communicate by I2C, like the one shown in the figure; htu21d module.** + + +![HTU21D-Module-Pinout](TZCBIN8qL-HTU21D-Module-Pinout.png) + +## The HTU21D sensor +In case you want to test the application without the complete AQM-Hat, the sensor must be connected to the second I2C peripherical by: + +P9.03 -> +3.3V (2nd pin - left position) +P9.01 -> DGND (1st pin - left position) +P9.19 -> I2C2_SCL (10th pin - left position) +P9.20 -> I2C2_SDA (10th pin - right position) + + +![beaglebone_black_pinmap](2MbNe2guV-beaglebone_black_pinmap.png) + +## Reading Data from HTU21D +The HTU21D sensor uses specific commands to read temperature and humidity data. Here’s how you can properly read and interpret this data. But first let us check if the device is connected and detected by the B3. + +Before starting, install the bash tools to work with I2C: + +```sh +sudo apt update +sudo apt install i2c-tools +``` + +Also install the build essential libraries to compile in C: + +```sh +sudo apt update +sudo apt install build-essential +``` + +Next, to check if the sensor is connected and detected by the B3 into the I2C port 2, try the next command: + +```sh +sudo i2cdetect -y 2 + + +Warning: Can't use SMBus Quick Write command, will skip some addresses + 0 1 2 3 4 5 6 7 8 9 a b c d e f +00: +10: +20: +30: -- -- -- -- -- -- -- -- +40: +50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +60: +70: +``` + +This output **looks like there is no sensor connected at any address**. Nevertheless, for some reason the detection command is not checking the `0x40` line-address for the I2C. Thus we can try to directly read the specific addresses. To read the temperature, send the temperature measurement command (0xE3 for no hold mode or 0xF3 for hold mode) and then read two bytes of data. + +```sh +sudo i2cget -y 2 0x40 0xE3 w +0x3871 +``` + +if you get a similar output in the terminal the sensor is properly connected and ready. + +## Converting temperature measurements + +**Pending Todo** + +# Back-end Apps + +## Compiling and testing the sensor C-Programs + +In the repository folder, go to the `sensors` sub-folder and make each main.c file to read and store the measurements: + +```sh +$ cd sensors/HTU21D +$ make +gcc -c -o main.o main.c -I. +gcc -o HTU21D main.o htu21d.o -I. -li2c +$ ./HTU21D +{ "temperature": 24.60, "humidity": 52.50 } +``` + +The previous output indicates that the `HTU21D` executable is now working. **Do the same for all sensors.** +```sh +cd sensors/BMP180 +make +cd .. +cd sensors/XXXX +make +cd .. +``` +Finally, let us prepare the `JSON` files to test the Dashboard: +```sh +./sensors/HTU21D/HTU21D > ./data/HTU21D.json +... +``` + +# Front-end Dashboard +This project provides a basic web dashboard to test the functionality of the AQM-hat. The way it works and a improved version are presented later, after understanding how to prepare data and the web server to visualize the dashboard. + +## Configuring Nginx + +First install the Nginx package in the B3 board: +```sh +sudo apt update +sudo apt install nginx +``` + +Next, let's redirect the configuration file to our repository folder: +```sh +sudo vi /etc/nginx/sites-available/default +``` + +Replace the line: +``` +root /var/www/html; +``` + +with: +``` +root /home/debian/path/to/your/repository; +``` + +in my particular case: + +```sh +root /home/debian/lwc/7-air-quality-ui/basic-ui; +``` + +```sh +sudo systemctl restart nginx +``` + + +Open the web interface and type the IP addess of your device or visit the hostname + .local (`http://bbgmarx.local`); you should see a UI similar to the next one: + +![basic-ui](C8ZLtB-py-basic-ui.jpg) + + + +# Simple Sensor Dashboard +A simple sensor dashboard has been developed to fetch the data from the `JSON` files and then show them in a HTML fashion. The dashboard UI es updated every 60 seconds and considers a minimal HTML with CSS and JavaScript. + +```html + + + + + + Simple Sensor Dashboard + + + + +

Sensor Data

+
+
Temperature
+
--
+
+
+
Humidity
+
--
+
+ + + + +``` +## Code Explanation + +### HTML Structure + +1. **Header**: + - Sets the character encoding and viewport for responsiveness. + - Styles the page with a basic CSS structure for sensor data presentation. + +2. **Sensor Display Elements**: + - The dashboard includes two sensors: **Temperature** and **Humidity**. + - Each sensor reading is displayed in a `.sensor` box with a title and value. + +### CSS Styling + +- **General Styles**: Applies a basic font style (Arial) and centers the text. +- **Sensor Box Styling**: `.sensor` boxes have a border, padding, and margin to create separation between sensor readings. + +### JavaScript Logic + +1. **Data Fetching (`fetchData`)**: + - Uses `fetch()` to retrieve JSON data for temperature and humidity. + - Updates the corresponding HTML elements with the latest data. + +2. **Automatic Refresh**: + - `setInterval(fetchData, 60000);` updates the sensor data every 60 seconds, ensuring it stays current. + + +Ensure that the JSON file is regularly updated by a background service or script to keep the data current. + + +# Data updating + +Configure root’s crontab to update data every 5 minutes + +```sh +sudo crontab -e +``` + +Edit it like so: + +```sh +*/5 * * * * /usr/bin/python -m mh_z19 > /home/pi/anavi-phat-sensors-ui/data/MH_Z19.json +*/5 * * * * /home/pi/anavi-phat-sensors-ui/sensors/HTU21D/HTU21D > /home/pi/anavi-phat-sensors-ui/data/HTU21D.json +*/5 * * * * /home/pi/anavi-phat-sensors-ui/sensors/BMP180/BMP180 > /home/pi/anavi-phat-sensors-ui/data/BMP180.json +*/5 * * * * /home/pi/anavi-phat-sensors-ui/sensors/BH1750/BH1750 > /home/pi/anavi-phat-sensors-ui/data/BH1750.json +``` \ No newline at end of file diff --git a/TZCBIN8qL-HTU21D-Module-Pinout.png b/TZCBIN8qL-HTU21D-Module-Pinout.png new file mode 100644 index 0000000..0a839de Binary files /dev/null and b/TZCBIN8qL-HTU21D-Module-Pinout.png differ diff --git a/data/EZORTD.csv b/data/EZORTD.csv new file mode 100644 index 0000000..62866ed --- /dev/null +++ b/data/EZORTD.csv @@ -0,0 +1 @@ +timestamp,temperature diff --git a/data/EZORTD.json b/data/EZORTD.json new file mode 100644 index 0000000..55068dc --- /dev/null +++ b/data/EZORTD.json @@ -0,0 +1 @@ +{ "temperature": 24.414 } diff --git a/data/HTU21D.json b/data/HTU21D.json new file mode 100644 index 0000000..45644cb --- /dev/null +++ b/data/HTU21D.json @@ -0,0 +1 @@ +{ "temperature": 24.60, "humidity": 52.68 } \ No newline at end of file diff --git a/images/cloud-outline.svg b/images/cloud-outline.svg new file mode 100644 index 0000000..5adca63 --- /dev/null +++ b/images/cloud-outline.svg @@ -0,0 +1 @@ +ionicons-v5-f diff --git a/images/sunny-outline.svg b/images/sunny-outline.svg new file mode 100644 index 0000000..2ce7367 --- /dev/null +++ b/images/sunny-outline.svg @@ -0,0 +1 @@ +ionicons-v5-q diff --git a/images/thermometer-outline.svg b/images/thermometer-outline.svg new file mode 100644 index 0000000..8af9f0d --- /dev/null +++ b/images/thermometer-outline.svg @@ -0,0 +1 @@ +ionicons-v5-q diff --git a/images/water-outline.svg b/images/water-outline.svg new file mode 100644 index 0000000..536edef --- /dev/null +++ b/images/water-outline.svg @@ -0,0 +1 @@ +ionicons-v5-r diff --git a/index-css.html b/index-css.html new file mode 100644 index 0000000..d4ea5b5 --- /dev/null +++ b/index-css.html @@ -0,0 +1,147 @@ + + + + + + Air Quality Monitor + + + + + +

Room Air Quality Monitor

+
+
+
+ Temperature Icon + Temperature +
+
--
+
+
+ +
+
+ Humidity Icon + Humidity +
+
--
+
%
+
+ +
+
+ CO2 Icon + CO₂ +
+
--
+
ppm
+
+ +
+
+ Pressure Icon + Pressure +
+
--
+
hPa
+
+
+ + + + diff --git a/index.html b/index.html new file mode 100644 index 0000000..4ce96fa --- /dev/null +++ b/index.html @@ -0,0 +1,45 @@ + + + + + + Simple Sensor Dashboard + + + + +

EZO RTD Temperature Monitor

+ +
+
Temperature
+
--
+
--
+
+ + + + + diff --git a/sensors/EZORTD/EZORTD b/sensors/EZORTD/EZORTD new file mode 100755 index 0000000..ce10696 Binary files /dev/null and b/sensors/EZORTD/EZORTD differ diff --git a/sensors/EZORTD/EZORTD_DAEMON b/sensors/EZORTD/EZORTD_DAEMON new file mode 100755 index 0000000..3d4bd4b Binary files /dev/null and b/sensors/EZORTD/EZORTD_DAEMON differ diff --git a/sensors/EZORTD/Makefile b/sensors/EZORTD/Makefile new file mode 100644 index 0000000..9298b5c --- /dev/null +++ b/sensors/EZORTD/Makefile @@ -0,0 +1,12 @@ +CC=gcc +CFLAGS=-I. +OBJ=main.o ezortd.o + +%.o: %.c + $(CC) -c -o $@ $< $(CFLAGS) + +EZORTD: $(OBJ) + $(CC) -o $@ $^ $(CFLAGS) + +clean: + rm -f EZORTD *.o \ No newline at end of file diff --git a/sensors/EZORTD/ezortd.c b/sensors/EZORTD/ezortd.c new file mode 100644 index 0000000..b28ab46 --- /dev/null +++ b/sensors/EZORTD/ezortd.c @@ -0,0 +1,47 @@ +#include +#include +#include +#include +#include +#include + +#include "ezortd.h" + +int getTemperature(int fd, double *temperature) +{ + if(ioctl(fd, I2C_SLAVE, EZORTD_I2C_ADDR) < 0) + { + perror("ioctl"); + return -1; + } + + char cmd[] = "R"; + + if(write(fd, cmd, strlen(cmd)) < 0) + { + perror("write"); + return -1; + } + + usleep(1000000); + + unsigned char response[32]; + + int n = read(fd, response, sizeof(response)); + + if(n < 0) + { + perror("read"); + return -1; + } + + if(response[0] != 0x01) + { + fprintf(stderr,"EZO error code: %d\n", response[0]); + return -1; + } + + *temperature = atof((char *)&response[1]); + + return 0; +} \ No newline at end of file diff --git a/sensors/EZORTD/ezortd.h b/sensors/EZORTD/ezortd.h new file mode 100644 index 0000000..9f287d8 --- /dev/null +++ b/sensors/EZORTD/ezortd.h @@ -0,0 +1,8 @@ +#ifndef EZORTD_H +#define EZORTD_H + +#define EZORTD_I2C_ADDR 0x66 + +int getTemperature(int fd, double *temperature); + +#endif \ No newline at end of file diff --git a/sensors/EZORTD/ezortd.o b/sensors/EZORTD/ezortd.o new file mode 100644 index 0000000..581db03 Binary files /dev/null and b/sensors/EZORTD/ezortd.o differ diff --git a/sensors/EZORTD/ezortd_daemon.c b/sensors/EZORTD/ezortd_daemon.c new file mode 100644 index 0000000..857bcec --- /dev/null +++ b/sensors/EZORTD/ezortd_daemon.c @@ -0,0 +1,77 @@ +#include +#include +#include +#include + +#include "ezortd.h" + +int main() +{ + int fd; + + fd = open("/dev/i2c-1", O_RDWR); + + if (fd < 0) + { + perror("open"); + return -1; + } + + while (1) + { + double temperature; + + if (getTemperature(fd, &temperature) == 0) + { + FILE *f = fopen( + "/home/cristian/airquality/basic-ui-dashboard/data/EZORTD.json", + "w"); + + if (f) + { + time_t now = time(NULL); + struct tm *t = localtime(&now); + + char timestamp[32]; + + strftime(timestamp, + sizeof(timestamp), + "%Y-%m-%d %H:%M:%S", + t); + fprintf(f, + "{ \"temperature\": %.3f }\n", + temperature); + + fclose(f); + } + } + + FILE *log = fopen( + "/home/cristian/airquality/basic-ui-dashboard/logs/temp.csv", + "a"); + + if (log) + { + time_t now = time(NULL); + struct tm *t = localtime(&now); + + char timestamp[32]; + + strftime(timestamp, + sizeof(timestamp), + "%Y-%m-%d %H:%M:%S", + t); + + fprintf(log, + "%ld,%.3f\n", + now, + temperature); + + fclose(log); + } + + sleep(1); + } + + return 0; +} diff --git a/sensors/EZORTD/main.c b/sensors/EZORTD/main.c new file mode 100644 index 0000000..69d0e6e --- /dev/null +++ b/sensors/EZORTD/main.c @@ -0,0 +1,37 @@ +#include +#include +#include +#include +#include + +#include "ezortd.h" + +int main() +{ + int fd; + + fd = open("/dev/i2c-1", O_RDWR); + + if(fd < 0) + { + fprintf(stderr, + "ERROR: Unable to access EZO RTD: %s\n", + strerror(errno)); + return -1; + } + + double temperature = 0; + + if(getTemperature(fd, &temperature) < 0) + { + fprintf(stderr, + "ERROR: Failed to read temperature\n"); + return -1; + } + + printf("{ "); + printf("\"temperature\": %.3f ", temperature); + printf("}"); + + return 0; +} \ No newline at end of file diff --git a/sensors/EZORTD/main.o b/sensors/EZORTD/main.o new file mode 100644 index 0000000..09982c9 Binary files /dev/null and b/sensors/EZORTD/main.o differ diff --git a/sensors/HTU21D/.htu21d.c.swp b/sensors/HTU21D/.htu21d.c.swp new file mode 100644 index 0000000..f5a189b Binary files /dev/null and b/sensors/HTU21D/.htu21d.c.swp differ diff --git a/sensors/HTU21D/.htu21d.h.swp b/sensors/HTU21D/.htu21d.h.swp new file mode 100644 index 0000000..1e647ae Binary files /dev/null and b/sensors/HTU21D/.htu21d.h.swp differ diff --git a/sensors/HTU21D/.main.c.swp b/sensors/HTU21D/.main.c.swp new file mode 100644 index 0000000..62204f9 Binary files /dev/null and b/sensors/HTU21D/.main.c.swp differ diff --git a/sensors/HTU21D/HTU21D b/sensors/HTU21D/HTU21D new file mode 100755 index 0000000..01e26dd Binary files /dev/null and b/sensors/HTU21D/HTU21D differ diff --git a/sensors/HTU21D/Makefile b/sensors/HTU21D/Makefile new file mode 100644 index 0000000..858846b --- /dev/null +++ b/sensors/HTU21D/Makefile @@ -0,0 +1,16 @@ +CC=gcc +CFLAGS=-I. +DEPS = +OBJ = main.o htu21d.o +EXTRA_LIBS=-li2c + +%.o: %.c $(DEPS) + $(CC) -c -o $@ $< $(CFLAGS) + +HTU21D: $(OBJ) + $(CC) -o $@ $^ $(CFLAGS) $(EXTRA_LIBS) + +.PHONY: clean + +clean: + rm -f HTU21D $(OBJ) diff --git a/sensors/HTU21D/Readme.md b/sensors/HTU21D/Readme.md new file mode 100644 index 0000000..cd00d0a --- /dev/null +++ b/sensors/HTU21D/Readme.md @@ -0,0 +1,180 @@ +# Introduction +This repository guides you to implement and use the HTU21D temperature and humidity sensor by using I2C communication. The repository contains the following files: + +- `htu21d.h` library header files +- `htu21d.c` implemenation methods file +- `main.c` an example file to test I2C communication using the library `htu21d` +- `example` output file precompiled in a raspberry pi zero that returns temperature and humidity + +# Setup the raspberry + +## Enable I2C on the Raspberry Pi: +- Run sudo raspi-config. +- Navigate to Interfacing Options → I2C and enable it. +- Reboot the Pi. + +## Install I2C Tools and Development Libraries: + +``` +sudo apt-get update +sudo apt-get install i2c-tools libi2c-dev +``` +## Check if the sensor is detected: + +``` +sudo i2cdetect -y 1 +``` +This command should show an address for the HTU21D, typically 0x40. +``` + 0 1 2 3 4 5 6 7 8 9 a b c d e f +00: -- -- -- -- -- -- -- -- +10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +40: 40 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +70: -- -- -- -- -- -- -- -- +``` + +# My own header files for HTU21D sensor +This is a detailed guide to configure and create your own files to comunicate with each sensor used by the air-quality sensor. + +## Header +Create the Header File `htu21d.h` with Header Guard and Includes: + +```c +#ifndef HTU21D_H +#define HTU21D_H + +// I2C Address +#define HTU21D_I2C_ADDR 0x40 +// Commands +#define HTU21D_TEMP 0xE3 +#define HTU21D_HUMID 0xE5 +#define HTU21D_RESET 0xFE + + +// Function declarations: + +// Temp +int getTemperature(int fd, double *temperature); +// Humidity +int getHumidity(int fd, double *humidity); + +#endif // HTU21D_H +``` + +## Implement the Sensor Communication `htu21d.c` + +```c +#include //to send commands to and receive from I2C device +#include //setting up and controlling the I2C device settings +#include //definitions for system calls and structures specific to I2C +#include //SMBus commands in a more standardized way for I2C +#include //perror + +#include "htu21d.h" // my own header file + +// Reset function: +int reset(int fd) +{ + if(0 > ioctl(fd, I2C_SLAVE, HTU21D_I2C_ADDR)) + { + perror("Failed to open the bus"); + return -1; + } + i2c_smbus_write_byte(fd, HTU21D_RESET); + return 0; +} + +// Get temperature: +int getTemperature(int fd, double *temperature) +{ + reset(fd); + char buf[3]; + __s32 res = i2c_smbus_read_i2c_block_data(fd, HTU21D_TEMP,3,buf); + if(res<0) + { + perror("Failed to read from the device"); + return -1; + } + *temperature = -46.85 + 175.72 * (buf[0]*256 + buf[1]) / 65536.0; + return 0; +} + + +// Get humidity: +int getHumidity(int fd, double *humidity) +{ + reset(fd); + char buf[3]; + __s32 res = i2c_smbus_read_i2c_block_data(fd, HTU21D_HUMID, 3, buf); + if(res<0) + { + perror("Failed to read from the device"); + return -1; + } + *humidity = -6 + 125 * (buf[0]*256 + buf[1]) / 65536.0; + return 0; +} +``` + + +### Using the library + +```c +#include +#include +#include +#include +#include + +#include "htu21d.h" + +int main() +{ + char filename[20]; + snprintf(filename, 19, "/dev/i2c-%d", 1); + int fd = open(filename, O_RDWR); + if (0 > fd) + { + fprintf(stderr, "ERROR: Unable to access HTU21D sensor module: %s\n", strerror (errno)); + exit(-1); + } + // Retrieve temperature and humidity + double temperature = 0; + double humidity = 0; + if ( (0 > getHumidity(fd, &humidity)) || (0 > getTemperature(fd, &temperature)) ) + { + fprintf(stderr, "ERROR: HTU21D sensor module not found\n"); + exit(-1); + } + + // Print temperature and humidity on the screen + printf("HTU21D Sensor Module\n"); + printf("%5.2fC\n", temperature); + printf("%5.2f%%rh\n", humidity); + + return 0; +} +``` + +# Compiling and testing +Then to properly compile whitout a make file: +```sh +gcc -o example main.c htu21d.c -li2c +``` +or +```sh +gcc -o example main.c htu21d.c -I. -li2c +``` + +### Wiring htu21d to Rasp-zero +Htu -> Rasp-Zero +VIN -> GPIO 1 +GND -> GPIO 9 or (6) +SCL -> GPIO 5 +SDA -> GPIO 3 + +![Raspberry Pi Zero GPIO layout](rpiz.png) diff --git a/sensors/HTU21D/htu21d.c b/sensors/HTU21D/htu21d.c new file mode 100644 index 0000000..6b88115 --- /dev/null +++ b/sensors/HTU21D/htu21d.c @@ -0,0 +1,52 @@ +#include //to send commands to and receive from I2C device +#include //setting up and controlling the I2C device settings +#include //definitions for system calls and structures specific to I2C +#include //SMBus commands in a more standardized way for I2C +#include //perror + +#include "htu21d.h" // my own header file + +// Reset function: +int reset(int fd) +{ + if(0 > ioctl(fd, I2C_SLAVE, HTU21D_I2C_ADDR)) + { + perror("Failed to open the bus"); + return -1; + } + i2c_smbus_write_byte(fd, HTU21D_RESET); + return 0; +} + +// Get temperature: +int getTemperature(int fd, double *temperature) +{ + reset(fd); + char buf[3]; + __s32 res = i2c_smbus_read_i2c_block_data(fd, HTU21D_TEMP,3,buf); + if(res<0) + { + perror("Failed to read from the device"); + return -1; + } + *temperature = -46.85 + 175.72 * (buf[0]*256 + buf[1]) / 65536.0; + return 0; +} + + +// Get humidity: +int getHumidity(int fd, double *humidity) +{ + reset(fd); + char buf[3]; + __s32 res = i2c_smbus_read_i2c_block_data(fd, HTU21D_HUMID, 3, buf); + if(res<0) + { + perror("Failed to read from the device"); + return -1; + } + *humidity = -6 + 125 * (buf[0]*256 + buf[1]) / 65536.0; + return 0; +} + + diff --git a/sensors/HTU21D/htu21d.h b/sensors/HTU21D/htu21d.h new file mode 100644 index 0000000..444c464 --- /dev/null +++ b/sensors/HTU21D/htu21d.h @@ -0,0 +1,19 @@ +#ifndef HTU21D_H +#define HTU21D_H + +// I2C Address +#define HTU21D_I2C_ADDR 0x40 +// Commands +#define HTU21D_TEMP 0xE3 +#define HTU21D_HUMID 0xE5 +#define HTU21D_RESET 0xFE + + +// Function declarations: + +// Temp +int getTemperature(int fd, double *temperature); +// Humidity +int getHumidity(int fd, double *humidity); + +#endif // HTU21D_H diff --git a/sensors/HTU21D/htu21d.o b/sensors/HTU21D/htu21d.o new file mode 100644 index 0000000..14a097e Binary files /dev/null and b/sensors/HTU21D/htu21d.o differ diff --git a/sensors/HTU21D/main.c b/sensors/HTU21D/main.c new file mode 100644 index 0000000..2055fdf --- /dev/null +++ b/sensors/HTU21D/main.c @@ -0,0 +1,39 @@ +#include +#include +#include +#include +#include + +#include "htu21d.h" + +int main() +{ + char filename[20]; + snprintf(filename, 19, "/dev/i2c-%d", 1); + int fd = open(filename, O_RDWR); + if (0 > fd) + { + fprintf(stderr, "ERROR: Unable to access HTU21D sensor module: %s\n", strerror (errno)); + exit(-1); + } + + // Retrieve temperature and humidity + double temperature = 0; + double humidity = 0; + if ( (0 > getHumidity(fd, &humidity)) || (0 > getTemperature(fd, &temperature)) ) + { + fprintf(stderr, "ERROR: HTU21D sensor module not found\n"); + exit(-1); + } + + // Print temperature and humidity on the screen + printf("{ "); + printf("\"temperature\": %5.2f, ", temperature); + printf("\"humidity\": %5.2f ", humidity); + printf("}"); + //printf("HTU21D Sensor Module\n"); + //printf("%5.2fC\n", temperature); + //printf("%5.2f%%rh\n", humidity); + + return 0; +} diff --git a/sensors/HTU21D/main.o b/sensors/HTU21D/main.o new file mode 100644 index 0000000..220d155 Binary files /dev/null and b/sensors/HTU21D/main.o differ diff --git a/sensors/HTU21D/rpiz.png b/sensors/HTU21D/rpiz.png new file mode 100644 index 0000000..05c09da Binary files /dev/null and b/sensors/HTU21D/rpiz.png differ