// Blynk settings
#define BLYNK_TEMPLATE_ID "TMPL2CttZpODg"
#define BLYNK_TEMPLATE_NAME "Exercice2"
#define BLYNK_AUTH_TOKEN "RZ7VL8jjvgFV0pbN1VkZ57I3okUO7hP3"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
// Define DHT sensor
#define DHTPIN 4
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
// Variables for ThingSpeak update
unsigned long previousThingSpeakUpdate = 0;
const unsigned long thingSpeakUpdateInterval = 15000; // Update interval in milliseconds (15 seconds)
// Blynk virtual pins for displaying temperature and humidity
#define VIRTUAL_PIN_TEMP V1
#define VIRTUAL_PIN_HUMID V2
// Define pin numbers for LCD
#define I2C_ADDR 0x27
#define LCD_COLS 16
#define LCD_ROWS 2
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLS, LCD_ROWS);
void setup() {
// Debug console
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
// Initialize DHT sensor
dht.begin();
// Connect to Wi-Fi
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// Initialize LCD
lcd.init();
lcd.backlight(); // turn on backlight
}
void loop() {
Blynk.run();
// Read humidity and temperature from DHT sensor
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
// Update Blynk app with temperature and humidity values
Blynk.virtualWrite(VIRTUAL_PIN_TEMP, temperature);
Blynk.virtualWrite(VIRTUAL_PIN_HUMID, humidity);
// Display temperature and humidity on LCD
displayOnLCD(temperature, humidity);
// Update ThingSpeak every specified interval
unsigned long currentMillis = millis();
if (currentMillis - previousThingSpeakUpdate >= thingSpeakUpdateInterval) {
// Save the last update time
previousThingSpeakUpdate = currentMillis;
// Update ThingSpeak channel with temperature and humidity values using HTTP GET
updateThingSpeakField(1, temperature);
updateThingSpeakField(2, humidity);
}
// Add a delay to avoid continuous rapid readings
delay(1000);
}
void updateThingSpeakField(int field, float value) {
// You can replace this function with the ThingSpeak update logic if needed
// For now, it's just a placeholder
}
// BLYNK_WRITE function to handle virtual pin writes
BLYNK_WRITE(V1) {
// Get value of temperature directly from DHT sensor
float temperature = dht.readTemperature();
// Add your logic here based on the received temperature value
Serial.print("Received Temperature: ");
Serial.println(temperature);
}
BLYNK_WRITE(V2) {
// Get value of humidity directly from DHT sensor
float humidity = dht.readHumidity();
// Add your logic here based on the received humidity value
Serial.print("Received Humidity: ");
Serial.println(humidity);
}
// Function to display temperature and humidity on LCD
void displayOnLCD(float temp, float humid) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temp);
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("Humidity: ");
lcd.print(humid);
lcd.print("%");
}