#include <ESP8266WiFi.h> // Library to enable WiFi for ESP8266
#include <BlynkSimpleEsp8266.h> // Blynk library for ESP8266 to connect and control via Blynk app
#include <OneWire.h> // OneWire library to communicate with the temperature sensor (DS18B20)
#include <DallasTemperature.h> // DallasTemperature library to handle temperature data from the DS18B20 sensor
#include <LiquidCrystal_I2C.h> // Library to control an I2C-based LCD display
LiquidCrystal_I2C lcd(0x27, 16, 2); // Create an LCD object with I2C address 0x27, 16 columns, 2 rows
#define BLYNK_PRINT Serial // Define Blynk print to use the Serial monitor
#define ONE_WIRE_BUS 12 // Define the data pin (GPIO12) where the DS18B20 temperature sensor is connected
int fanPin = 16; // Define the GPIO pin (GPIO16) where the fan is connected
int dutyCycle = 0; // Variable to store the PWM duty cycle for fan speed control
float temp = 0; // Variable to store the temperature value
int threshold = 30; // Default threshold temperature value (30°C)
OneWire oneWire(ONE_WIRE_BUS); // Create a OneWire object to communicate with the temperature sensor
DallasTemperature sensors(&oneWire); // Create a DallasTemperature object to handle temperature readings
WidgetLED FAN(V0); // Create a virtual LED in the Blynk app to show fan status (V0 virtual pin)
char auth[] = "*********************"; // Blynk authentication token (replace with your token)
char ssid[] = "*********************"; // WiFi SSID (replace with your WiFi network name)
char pass[] = "*********************"; // WiFi password (replace with your WiFi password)
void setup() {
Serial.begin(115200); // Start the Serial monitor with baud rate of 115200 for debugging
sensors.begin(); // Start the temperature sensor
pinMode(fanPin, OUTPUT); // Set the fanPin as an OUTPUT pin to control the fan
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the LCD backlight
lcd.setCursor(0, 0); // Set the LCD cursor to the first row, first column
lcd.print(" Temperature "); // Display "Temperature" on the LCD
lcd.setCursor(0, 1); // Set the LCD cursor to the second row, first column
lcd.print("Monitoring System"); // Display "Monitoring System" on the LCD
delay(4000); // Wait for 4 seconds to let the message display
lcd.clear(); // Clear the LCD screen
analogWriteRange(100); // Set the range for PWM from 0 to 100 (for controlling fan speed)
analogWriteFreq(10000); // Set the PWM frequency to 10 kHz
Blynk.begin(auth, ssid, pass, "blynk.cloud", 80); // Connect to Blynk cloud with the given credentials
}
BLYNK_WRITE(V7) {
threshold = param.asInt(); // Read the threshold value from Blynk app (slider on virtual pin V7)
Serial.print(" The Threshold value is: ");
Serial.println(threshold); // Print the new threshold value to the Serial monitor
Serial.println();
}
void controlFanSpeed(int fanSpeedPercent) {
analogWrite(fanPin, fanSpeedPercent); // Adjust the fan speed using PWM according to the percentage
Serial.print("Fan Speed: ");
Serial.print(fanSpeedPercent); // Print the current fan speed to the Serial monitor
Serial.println("%");
lcd.setCursor(0, 1); // Set the LCD cursor to the second row
lcd.print("Fan Speed: ");
lcd.print(fanSpeedPercent); // Display the current fan speed on the LCD
lcd.print("%");
}
void loop() {
Blynk.run(); // Run the Blynk process to handle communication with the Blynk app
sensors.requestTemperatures(); // Request the temperature from the DS18B20 sensor
temp = sensors.getTempCByIndex(0); // Get the temperature in Celsius from the sensor (index 0 since only one sensor is connected)
Serial.print("Temperature: ");
Serial.print(temp); // Print the temperature value to the Serial monitor
Serial.println("*C");
lcd.setCursor(0, 0); // Set the LCD cursor to the first row
lcd.print("Temp: ");
lcd.print(temp); // Display the temperature value on the LCD
lcd.print("*C");
Blynk.virtualWrite(V3, temp); // Send the temperature value to the Blynk app (virtual pin V3)
if (temp >= threshold) { // If the temperature exceeds or equals the threshold
FAN.on(); // Turn on the virtual LED on the Blynk app (indicating the fan is on)
// Map the temperature to the fan speed: threshold (min) to 55°C (max) maps to 10% (min) to 100% (max) fan speed
int fanSpeedPercent = map(temp, threshold, 55, 10, 100);
controlFanSpeed(fanSpeedPercent); // Control the fan speed based on the temperature
Blynk.virtualWrite(V4, fanSpeedPercent); // Send the fan speed percentage to the Blynk app (virtual pin V4)
}
else if (temp < threshold) { // If the temperature is below the threshold
FAN.off(); // Turn off the virtual LED on the Blynk app (indicating the fan is off)
int fanSpeedPercent = 0; // Set fan speed to 0%
controlFanSpeed(fanSpeedPercent); // Stop the fan
Blynk.virtualWrite(V4, fanSpeedPercent); // Send 0% fan speed to the Blynk app (virtual pin V4)
}
}