// Click the Play button on the Simulation screen ---->>
// you can click the DHT22 object and play around the humidity and temperature
#include <WiFi.h>
#include <LiquidCrystal_I2C.h>
#include <DHTesp.h>
#define NTP_SERVER "pool.ntp.org"
#define UTC_OFFSET 0
#define UTC_OFFSET_DST 0
// LCD
LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 16, 2); // 16 characters, 2 lines
// DHT Sensor
const int DHT_PIN = 15; // the data pin from DHT22 is connected to pin 15 on ESP32
DHTesp dhtSensor;
// the resistor is 10 Kilo Ohm
// https://lastminuteengineers.com/esp32-dht11-dht22-web-server-tutorial/
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
// https://wokwi.com/projects/321525495180034642
LCD.init();
LCD.backlight();
LCD.setCursor(0, 0);
LCD.print("Connecting to ");
LCD.setCursor(0, 1);
LCD.print("WiFi ");
WiFi.begin("Wokwi-GUEST", "", 6); // SSID, Password, Channel(optional)
while (WiFi.status() != WL_CONNECTED) {
delay(250);
spinner();
}
// print wifi status
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
LCD.clear();
LCD.setCursor(0, 0);
LCD.println("On");
LCD.setCursor(0, 1);
LCD.println("Updating time...");
configTime(UTC_OFFSET, UTC_OFFSET_DST, NTP_SERVER); // get time from the NTP server
// set Time Zone to WIB (UTC+7)
setenv("TZ","WIB-7",1); // Now adjust the TZ. Clock settings are adjusted to show the new local time
tzset();
//https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv
//https://randomnerdtutorials.com/esp32-ntp-timezones-daylight-saving/
// setup DHT22
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
}
void loop() {
// put your main code here, to run repeatedly:
printLocalTime();
printTempAndHum();
delay(1000); // delay for 1 second
}
void spinner() {
// Spinner is a procedure to display a very simple animation
// while the ESP32 is trying to connect to the WiFi
static int8_t counter = 0;
const char* glyphs = "oO0"; // "\xa1\xa5\xdb";
LCD.setCursor(15, 1);
LCD.print(glyphs[counter++]);
if (counter == strlen(glyphs)) {
counter = 0;
}
}
void printLocalTime() {
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) { // if failed to get local time, print error
LCD.setCursor(0, 1);
LCD.println("CErr"); // stands of Connection Error
return;
}
LCD.setCursor(4, 0);
LCD.println(&timeinfo, "%H:%M:%S %Z"); // print hour minute second and timezone
// LCD.setCursor(0, 1);
// LCD.println(&timeinfo, "%d/%m/%Y %Z");
}
void printTempAndHum() {
// https://wokwi.com/projects/322410731508073042
TempAndHumidity data = dhtSensor.getTempAndHumidity(); // store the data to a variable
const String Temperature = String(data.temperature, 0); // store the temperature as string
const String Humidity = String(data.humidity, 0); // save the humidity as string
// print log
Serial.println("Temp: " + String(data.temperature, 2) + "°C");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
Serial.println("---");
String message = "Tem:" + Temperature + "C Hum:" + Humidity + "%"; // store the full message to to a variable
// Print the message on the second row
LCD.setCursor(0,1);
LCD.println(message);
// Set the buzzer to buzz when the temperature or humidity is not within the threshold.
// Once the simulation run,
// click the DHT22 and play around the temperature and humidity to test the Buzzer.
if(data.temperature < 20 or data.temperature > 24) {
tone(25, 262, 250); // Plays 262Hz tone for 0.250 seconds
}
if(data.humidity < 40 or data.humidity > 60) {
tone(25, 523, 250); // Plays 523Hz tone for 0.250 seconds
}
}