commit db28d00f63d80f767b2335c2593f6e6504b4c852 Author: Beagle User Date: Sat Nov 23 17:40:08 2024 +0000 basic UI dashboard 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..9fb1bbc --- /dev/null +++ b/index.html @@ -0,0 +1,43 @@ + + + + + + Simple Sensor Dashboard + + + + +

Sensor Data

+
+
Temperature
+
--
+
+
+
Humidity
+
--
+
+ + + + 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..cfa4d20 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..2ccd401 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..1caa407 --- /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", 2); + 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..c807cf7 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