/*
Realtime Temperature and Humidty Monitoring using Arduino Example for Wokwi.com
To view the data:
Now click on the DHT22 sensor in the simulation,
change the temperature and humidity, and you should see
the message appear on the lCD 16X2 display
<!--
Software License Agreement (BSD)
\file sketch.ino
\authors Anditya Sridamar Pratyasta <https://www.linkedin.com/company/eltrolab-robotics/>,
\copyright Copyright (c) 2024, Eltrolab Robotics, Inc., All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that
the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the
following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of Eltrolab Robotics nor the names of its contributors may be used to endorse or promote
products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WAR-
RANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, IN-
DIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-->
https://wokwi.com/projects/401665709747905537
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
// Define the number of sensors
#define NUM_SENSORS 5
// Define the pins where the sensors are connected
const int sensorPins[NUM_SENSORS] = {2, 3, 4, 5, 6};
// Initialize DHT sensors
DHT dhtSensors[NUM_SENSORS] = {
DHT(sensorPins[0], DHT22),
DHT(sensorPins[1], DHT22),
DHT(sensorPins[2], DHT22),
DHT(sensorPins[3], DHT22),
DHT(sensorPins[4], DHT22)
};
// Initialize the LCD, address 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
unsigned long previousMillis = 0;
const long interval = 2000; // interval at which to read sensors and switch pages
int currentPage = 0;
void setup() {
// Initialize the LCD with the correct parameters
lcd.init();
lcd.backlight();
// Initialize all DHT sensors
for (int i = 0; i < NUM_SENSORS; i++) {
dhtSensors[i].begin();
}
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
// Clear the LCD
lcd.clear();
// Display data for the current page
displayData(currentPage);
// Move to the next page
currentPage = (currentPage + 1) % ((NUM_SENSORS + 1) / 2); // +1 to handle odd number of sensors
}
}
void displayData(int page) {
int sensorIndex1 = page * 2;
int sensorIndex2 = sensorIndex1 + 1;
// Buffers for formatted temperature strings
char temp1Str[6]; // "XX.X/0"
char temp2Str[6]; // "XX.X/0"
// Read and display data from the first sensor on the current page
if (sensorIndex1 < NUM_SENSORS) {
float temp1 = dhtSensors[sensorIndex1].readTemperature();
float hum1 = dhtSensors[sensorIndex1].readHumidity();
dtostrf(temp1, 4, 1, temp1Str); // format temperature to 1 decimal place
lcd.setCursor(0, 0);
lcd.print("T");
lcd.print(sensorIndex1 + 1);
lcd.print(":");
//lcd.print(temp1);
lcd.print(temp1Str);
lcd.print((char)223); // Degree symbol
//lcd.print("C H"); // Print celcius symbol
lcd.print(" H");
lcd.print(sensorIndex1 + 1);
lcd.print(":");
lcd.print(hum1);
//lcd.print("%"); // Persentage symbol
}
// Read and display data from the second sensor on the current page
if (sensorIndex2 < NUM_SENSORS) {
float temp2 = dhtSensors[sensorIndex2].readTemperature();
float hum2 = dhtSensors[sensorIndex2].readHumidity();
dtostrf(temp2, 4, 1, temp2Str); // Format temperature to 1 decimal place
lcd.setCursor(0, 1);
lcd.print("T");
lcd.print(sensorIndex2 + 1);
lcd.print(":");
//lcd.print(temp2);
lcd.print(temp2Str);
lcd.print((char)223); // Degree symbol
//lcd.print("C H"); // Print celcius symbol
lcd.print(" H");
lcd.print(sensorIndex2 + 1);
lcd.print(":");
lcd.print(hum2);
lcd.print("%");
}
}