/**
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"
//**TODO***//
// Include the required libraries
#include "WiFi.h"
#include "LiquidCrystal_I2C.h"
#include "ThingSpeak.h"
// GPIOs
//*************Sensors
const int DHT_PIN = 15; // Pin number to which the DHT22 sensor is connected
//*************Actuators
//**TODO***//
const int LED_PIN = 32; // Define your actuators - Connect a LED to any available pinout on the ESP32
// CONNECTIVITY
//*************I2C connection
//**TODO***//
// Connect an LCD display to the I2C pins (SCL, SDA), and be sure to check the pinout of the ESP32
// Define I2C connection for LCD
const int SCL_PIN = 22;
const int SDA_PIN = 21;
#define I2C_ADDR 0x27
#define LCD_COLUMNS 16
#define LCD_LINES 2
//*************WiFi connection
//**TODO***//
String ssid = "Wokwi-GUEST";
String password = "";
// 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!)
unsigned long Channel_ID = 2318125; //Ingrese su numero de canal de ThingSpeak
const char * WriteAPIKey = "4JHAM6PBXSVVBYHN"; //Ingrese su clave de API de escritura de canal
//*************Objects creation & variables definition
DHTesp dhtSensor; // Create an instance of the DHTesp library
//**TODO***//
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
WiFiClient client;
// 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(LED_PIN, OUTPUT);
// Create a LCD initialization
lcd.init();
lcd.backlight();
// Print something
lcd.setCursor(0, 0);
lcd.print("Hello, world!");
lcd.setCursor(0, 1);
lcd.print("Wokwi Online IoT");
// Create a WiFi Connection initialization
Serial.begin(9600);
Serial.print("Connecting to WiFi");
WiFi.begin(ssid, password, 6);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
// Create a ThinkSpeak Connection initialization
ThingSpeak.begin(client); //Iniciar el servidor de ThingSpeak
}
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(LED_PIN, HIGH);
}else{
digitalWrite(LED_PIN, LOW);
}
// --Logic to display the current temperature and humidity values on the LCD screen.
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("Temp: "+ String(data.temperature, 2) + " " + "C" + char(223));
lcd.setCursor(0, 1);
lcd.print("Humidity: " + String(data.humidity, 1) + "%");
// --Logic to update sensor readings on ThingSpeak at regular intervals.
ThingSpeak.setField(1, data.temperature);
ThingSpeak.setField(2, data.humidity);
ThingSpeak.writeFields(Channel_ID, WriteAPIKey);
delay(10000); // Delay for 10 seconds
}