#include <DHT.h>
#include "HX711.h"
#include <LCD_I2C.h> // Include library LCD_I2C by BlackHack
LCD_I2C lcd(0x27, 16, 2); // Default address of most PCF8574 modules, change according
#define useCO2 1
#if useCO2
#include "DFRobot_CCS811.h"
/*
* IIC address default 0x5A, the address becomes 0x5B if the ADDR_SEL is soldered.
*/
//DFRobot_CCS811 CCS811(&Wire, /*IIC_ADDRESS=*/0x5A);
DFRobot_CCS811 CCS811;
#endif
HX711 escala;
unsigned long lcdUpdateTime = 5000; // Lcd update time for next sensor value
// Variables to hold sensor data values
int hIn = 0;
float tIn = 0; // or dhtIn.readTemperature(true) for Fahrenheit
int hOut = 0;
float tOut = 0; // or dhtIn.readTemperature(true) for Fahrenheit
int soundValue = 0; // Variable to hold sound sensor value
float peso = 0.0; // variable to hold weight sensor
int co2Value = 0; // read CO2 value
int tvocValue = 0; // Read TVOC value
// Pins Definition
#define DHTPINin 2 // What digital pin we're connected to
#define DHTPINout 3 // What digital pin we're connected to
const byte soundSensorPin = A0; // Pin analógico del sensor de sonido
const byte DOUT_PIN = 4; // Pin digital conectado a DOUT del módulo HX711
const byte SCK_PIN = 5; // Pin digital conectado a SCK del módulo HX711
// Uncomment whatever type you're using!
#define DHTTYPEout DHT22 // DHT 11 for Ioutside temperature
#define DHTTYPEin DHT22 // DHT 22 for inside temperature
DHT dhtIn(DHTPINin, DHTTYPEin);
DHT dhtOut(DHTPINout, DHTTYPEout);
void setup() {
Serial.begin(9600);
escala.begin(DOUT_PIN, SCK_PIN);
pinMode(soundSensorPin, INPUT); // Set sound sensor pin as input
dhtIn.begin(); // Initialize DHT in sensor
dhtOut.begin(); // Initialize DHT OUT sensor
lcd.begin(); // If you are using more I2C devices using the Wire library use lcd.begin(false)
// this stop the library(LCD_I2C) from calling Wire.begin()
lcd.backlight();
delay(1000);
}
void loop() {
// Read inside dht sensor and display on lcd
hIn = dhtIn.readHumidity();
tIn = dhtIn.readTemperature(); // or dhtIn.readTemperature(true) for Fahrenheit
if (isnan(hIn) || isnan(tIn)) {
Serial.println("Failed to read from Inside DHT sensor!");
hIn = 0;
tIn = 0;
}
displayMessage(0, 0, "Humidity In:", HIGH);
displayMessage(12, 0, hIn);
displayMessage(15, 0, "% ", LOW);
displayMessage(0, 1, "Temp In:", LOW);
displayMessage(9, 1, tIn);
displayMessage(15, 1, "C ", LOW);
lcd.print(" *C ");
delay(lcdUpdateTime);
// display outside temperature
hOut = dhtOut.readHumidity();
tOut = dhtOut.readTemperature(); // or dhtIn.readTemperature(true) for Fahrenheit
if (isnan(hOut) || isnan(tOut)) {
Serial.println("Failed to read from Outside DHT sensor!");
hOut = 0;
tOut = 0;
}
displayMessage(0, 0, "Humidity Out:", HIGH);
displayMessage(12, 0, hOut);
displayMessage(15, 0, "% ", LOW);
displayMessage(0, 1, "Temp Out:", LOW);
displayMessage(9, 1, tOut);
displayMessage(15, 1, "C", LOW);
delay(lcdUpdateTime);
Serial.print("Inside Temp:");
Serial.print(tIn);
Serial.print(" | Humidity:");
Serial.print(hIn);
Serial.print(" | Outside Temp:");
Serial.print(tOut);
Serial.print(" | Humidity:");
Serial.println(hOut);
// Read sound sensor and Weight sensor
// Lectura del sensor de sonido
int soundValue = analogRead(soundSensorPin);
Serial.print("Nivel de sonido: ");
Serial.println(soundValue);
// Lectura de peso
peso = escala.get_units(); // Lee el peso con 10 lecturas de promedio
Serial.print("Peso: ");
Serial.print(peso);
Serial.println(" kg");
displayMessage(0, 0, "Sound:", HIGH);
displayMessage(6, 0, soundValue);
displayMessage(0, 1, "Weight:", LOW);
displayMessage(8, 1, peso);
delay(lcdUpdateTime);
#if useCO2
// Read CO2 sensor data
if (CCS811.checkDataReady() == true) {
co2Value = CCS811.getCO2PPM(); // read CO2 value
tvocValue = CCS811.getTVOCPPB(); // Read TVOC value
Serial.print("CO2: ");
Serial.print(co2Value);
Serial.print("ppm, TVOC: ");
Serial.print(tvocValue);
Serial.println("ppb");
displayMessage(0, 0, "CO2 Value:", HIGH);
displayMessage(10, 0, co2Value);
lcd.print(" ppm ");
displayMessage(0, 1, "TVOC Value:", LOW);
displayMessage(12, 1, tOut);
lcd.print(" ppb ");
delay(lcdUpdateTime);
} else {
Serial.println("Data is not ready!");
displayMessage(0, 0, "CO2 Sensor Error!", HIGH);
delay(lcdUpdateTime);
}
// /*!
// * @brief Set baseline
// * @param get from getBaseline.ino
// */
CCS811.writeBaseLine(0x447B);
#endif
delay(2000);
}
void displayMessage(int x, int y, const String& msg, bool clearScreen) {
// This function will receive cursor x, y position and message to display and a flag to clear previous display or not
if (clearScreen) { // If clear screen flag is high clear lcd
lcd.clear();
delay(500); // Wait for 500 seconds
}
lcd.setCursor(x, y); // Set cursor at x, y position
lcd.print(msg); // Print msg on lcd
}
void displayMessage(int x, int y, int value) { // This function will receive cursor x, y position and integer value to display
lcd.setCursor(x, y); // Set cursor at x, y position
lcd.print(value); // Print msg on lcd
lcd.print(" "); // Remove any extra value
}
void displayMessage(int x, int y, float value) { // This function will receive cursor x, y position and integer value to display
lcd.setCursor(x, y); // Set cursor at x, y position
lcd.print(value); // Print msg on lcd
lcd.print(" "); // Remove any extra value
}