/**
IoT Ecosystem Exercise for Temperature & Huminity Monitoring System for Wokwi
- Sensor: DHT22
- Actuators: LED, LCD
- Computing: ESP32
- Connectivity: WiFi, I2C, HTTP
- IoT Platform: ThingSpeak https://thingspeak.com/
*/
#include "DHTesp.h"
#include <LiquidCrystal_I2C.h>
//**TODO***//
// Include the required libraries
// GPIOs
//*************Sensors
const int DHT_PIN = 15; // Pin number to which the DHT22 sensor is connected
//*************Actuators
//**TODO***//
// Define your actuators - Connect a LED to any available pinout on the ESP32
const int ledPin = 13;
// CONNECTIVITY
//*************I2C connection
int lcdColumns = 16;
int lcdRows = 2;
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);
//*************WiFi connection
//**TODO***//
// Define WiFi credentials required for connecting to a public gateway. (Check https://docs.wokwi.com/guides/esp32-wifi)
//************* ThingSpeak connection
//**TODO***//
// Define the parameters for connecting to the ThingSpeak IoT platform (Googlee to find how to do it!)
//*************Objects creation & variables definition
DHTesp dhtSensor; // Create an instance of the DHTesp library
//**TODO***//
// Create the required objects
void setup() {
Serial.begin(115200); // Initialize the serial communication at a baud rate of 115200
dhtSensor.setup(DHT_PIN, DHTesp::DHT22); // Initialize the DHT22 sensor
//**TODO***//
// Pin configuration
pinMode (ledPin, OUTPUT);
// Create a LCD initialization
lcd.init();
lcd.backlight();
// Create a WiFi Connection initialization
// Create a ThinkSpeak Connection initialization
}
void loop() {
// Reading from DHT22 sensor
TempAndHumidity data = dhtSensor.getTempAndHumidity(); // Read temperature and humidity from the DHT22 sensor
// Storing current temperature & huminity values
float temp = data.temperature;
float hum = data.humidity;
// Printing values in the serial monitor
Serial.println("Temp: " + String(data.temperature, 2) + "°C"); // Print the temperature value with 2 decimal places
Serial.println("Humidity: " + String(data.humidity, 1) + "%"); // Print the humidity value with 1 decimal place
//**TODO***//
// --Logic to activate an alert using an LED actuator if the temperature surpasses the threshold of 40°C or if the humidity falls below the 20% threshold.
if ((data.temperature>40) || data.humidity <20){
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
digitalWrite (ledPin, HIGH);
// --Logic to display the current temperature and humidity values on the LCD screen.
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: " + String(data.temperature, 2) + " C");
lcd.setCursor(0,1);
lcd.print("Humidity: " + String(data.humidity, 1) + "%");
// --Logic to update sensor readings on ThingSpeak at regular intervals.
delay(500); // Delay for 10 seconds
}