#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#include <Adafruit_ILI9341.h>
#include <Adafruit_GFX.h>
// Define the number of sensors and their type
#define NUM_SENSORS 6
#define DHTTYPE DHT22 // DHT 22 (AM2302)
// Define the pins for the sensors
const int DHTPins[NUM_SENSORS] = {13, 14, 25, 26, 27, 32};
// Create an array of DHT objects
DHT_Unified dht[NUM_SENSORS] = {
DHT_Unified(DHTPins[0], DHTTYPE),
DHT_Unified(DHTPins[1], DHTTYPE),
DHT_Unified(DHTPins[2], DHTTYPE),
DHT_Unified(DHTPins[3], DHTTYPE),
DHT_Unified(DHTPins[4], DHTTYPE),
DHT_Unified(DHTPins[5], DHTTYPE)
};
// Define ILI9341 display connections
#define TFT_CS 15
#define TFT_RST 2
#define TFT_DC 4
#define TFT_MOSI 23
#define TFT_CLK 18
#define TFT_MISO 19
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
// Variables to store high and low values
float high_temp = -100.0, low_temp = 100.0;
float high_rh = -100.0, low_rh = 100.0;
unsigned long refresh = 0;
///////////////////////// SET COLOR FUNCTIONS /////////////////////////
void temp_color(float x) {
if (x >= 104) {
tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
} else if (x >= 95) {
tft.setTextColor(ILI9341_PINK, ILI9341_BLACK);
} else if (x >= 85) {
tft.setTextColor(ILI9341_MAGENTA, ILI9341_BLACK);
} else if (x >= 77) {
tft.setTextColor(ILI9341_RED, ILI9341_BLACK);
} else if (x >= 68) {
tft.setTextColor(ILI9341_ORANGE, ILI9341_BLACK);
} else if (x >= 59) {
tft.setTextColor(ILI9341_YELLOW, ILI9341_BLACK);
} else if (x >= 50) {
tft.setTextColor(ILI9341_GREENYELLOW, ILI9341_BLACK);
} else if (x >= 41) {
tft.setTextColor(ILI9341_GREEN, ILI9341_BLACK);
} else if (x >= 23) {
tft.setTextColor(ILI9341_CYAN, ILI9341_BLACK);
} else if (x >= 5) {
tft.setTextColor(ILI9341_BLUE, ILI9341_BLACK);
} else if (x < 5) {
tft.setTextColor(ILI9341_CASET, ILI9341_BLACK);
}
}
void rh_color(float x) {
if (x >= 70) {
tft.setTextColor(ILI9341_RED, ILI9341_BLACK);
} else if (x >= 60) {
tft.setTextColor(ILI9341_ORANGE, ILI9341_BLACK);
} else if (x >= 30) {
tft.setTextColor(ILI9341_GREEN, ILI9341_BLACK);
} else if (x < 30) {
tft.setTextColor(ILI9341_BLUE, ILI9341_BLACK);
}
}
void setup() {
Serial.begin(115200);
// Initialize the display
tft.begin();
tft.setRotation(1);
tft.fillScreen(ILI9341_BLACK);
tft.setTextSize(2);
// Initialize the DHT sensors
for (int i = 0; i < NUM_SENSORS; i++) {
dht[i].begin();
sensor_t sensor;
dht[i].temperature().getSensor(&sensor);
dht[i].humidity().getSensor(&sensor);
}
refresh = millis();
}
void loop() {
for (int i = 0; i < NUM_SENSORS; i++) {
// Get temperature and humidity event
sensors_event_t temp_event, humidity_event;
dht[i].temperature().getEvent(&temp_event);
dht[i].humidity().getEvent(&humidity_event);
float temperature = temp_event.temperature;
float humidity = humidity_event.relative_humidity;
// Update high and low values
if (millis() >= refresh + (24 * 60 * 60 * 1000)) { // Reset High and Low after 24 hours
high_temp = temperature;
low_temp = temperature;
high_rh = humidity;
low_rh = humidity;
refresh = millis();
} else {
if (temperature >= high_temp) { high_temp = temperature; }
if (temperature <= low_temp) { low_temp = temperature; }
if (humidity >= high_rh) { high_rh = humidity; }
if (humidity <= low_rh) { low_rh = humidity; }
}
// Print temperature and humidity to Serial Monitor
Serial.printf("TEMP: %0.1f, HIGH: %0.1f, LOW: %0.1f\n", temperature, high_temp, low_temp);
Serial.printf("RH: %0.1f, HIGH: %0.1f, LOW: %0.1f\n", humidity, high_rh, low_rh);
// Display temperature and humidity on ILI9341 screen
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(0, 20);
tft.setTextSize(2);
tft.setTextColor(ILI9341_WHITE);
tft.print("Sensor ");
tft.print(i + 1);
// Add headline
tft.setTextSize(2);
tft.setTextColor(ILI9341_WHITE);
tft.setCursor(50, 20);
tft.print("3D-MES Filament Lager");
// Print "Temperature" label
tft.setTextSize(2);
tft.setTextColor(ILI9341_WHITE);
tft.setCursor(0, 50);
tft.print("Temperature");
// Print current temperature
tft.setTextSize(5);
tft.setTextColor(ILI9341_GREEN, ILI9341_BLACK);
tft.fillRect(0, 120, tft.width(), 50, ILI9341_BLACK); // Clear the area
temp_color(temperature);
tft.setCursor(0, 85);
tft.print(temperature, 1);
// Print "Humidity" label
tft.setTextSize(2);
tft.setTextColor(ILI9341_WHITE);
tft.setCursor(0, 165);
tft.print("Humidity");
// Print current humidity
tft.setTextSize(5);
tft.setTextColor(ILI9341_GREEN, ILI9341_BLACK);
tft.fillRect(0, 180, tft.width(), 50, ILI9341_BLACK); // Clear the area
rh_color(humidity);
tft.setCursor(0, 180);
tft.print(humidity, 1);
// Print high and low values in smaller fonts
tft.setTextSize(2);
tft.setTextColor(ILI9341_GREEN, ILI9341_BLACK);
tft.fillRect(0, 155, tft.width(), 40, ILI9341_BLACK); // Clear the area
temp_color(high_temp);
tft.setCursor(40, 155);
tft.print(high_temp, 1);
temp_color(low_temp);
tft.setCursor(tft.width() / 2 + 45, 155);
tft.print(low_temp, 1);
tft.fillRect(0, 230, tft.width(), 40, ILI9341_BLACK); // Clear the area
rh_color(high_rh);
tft.setCursor(40, 230);
tft.print(high_rh, 1);
rh_color(low_rh);
tft.setCursor(tft.width() / 2 + 35, 230);
tft.print(low_rh, 1);
delay(2000); // Update every 2 seconds
}
}