#define BLYNK_TEMPLATE_ID "TMPL6Gd5yjdP8"
#define BLYNK_TEMPLATE_NAME "NTC"
#define BLYNK_AUTH_TOKEN "3IpVwVr33yR2v15DpN9Zj466PqfBFrlB"
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <math.h>
#define RELAY_PIN 32
#define BUZZER_PIN 2
#define LED_PIN 15
#define NTC_PIN 34 // Pin data untuk NTC thermistor (analog input)
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Add the BETA constant for the NTC thermistor
#define BETA 3950 // Example value, you may need to adjust this for your thermistor
void setup() {
Wire.begin(21, 22);
delay(200);
lcd.init();
lcd.backlight();
lcd.print("LCD OK!");
delay(1000);
Serial.begin(115200);
Serial.println("System Initialized");
pinMode(RELAY_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
Blynk.begin(auth, ssid, pass); // Initialize Blynk connection
}
void loop() {
int analogValue = analogRead(NTC_PIN); // Read the analog value from the thermistor pin
float celsius = 1 / (log(1 / (4095. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
Serial.print("Temperature: ");
Serial.print(celsius); // Print the correct temperature variable
Serial.println(" C");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp:");
lcd.setCursor(0, 1);
lcd.print(celsius); // Print the correct temperature variable
lcd.print(" C");
if (celsius > 30.0) { // Example threshold: 30°C
Blynk.virtualWrite(V0, 1); // Send status to Blynk (V0 pin)
Serial.println("High Temp Detected!");
// Activate relay, buzzer, and LED
digitalWrite(RELAY_PIN, HIGH);
digitalWrite(LED_PIN, HIGH);
tone(BUZZER_PIN, 1000); // Turn on buzzer
} else {
Blynk.virtualWrite(V0, 0); // Send status to Blynk (V0 pin)
// Deactivate relay, LED, and buzzer
digitalWrite(RELAY_PIN, LOW);
digitalWrite(LED_PIN, LOW);
noTone(BUZZER_PIN);
}
delay(1000);
Blynk.run(); // Call this function to keep Blynk running
}
// BLYNK_WRITE for controlling the LED from Blynk app
BLYNK_WRITE(V2) {
int pinValue = param.asInt(); // Get the value from virtual pin V2 (Button widget)
if (pinValue == 1) { // If button is pressed
digitalWrite(LED_PIN, HIGH); // Turn on the LED
Serial.println("LED ON via Blynk");
} else { // If button is released
digitalWrite(LED_PIN, LOW); // Turn off the LED
Serial.println("LED OFF via Blynk");
}
}