// Blynk Configuration
#define BLYNK_TEMPLATE_ID "TMPL6aMtKGnB6"
#define BLYNK_TEMPLATE_NAME "Thermal and pH Sensors"
#define BLYNK_AUTH_TOKEN "mjoI1jTfAzpa5C2OtJT3d3gW5lfraYnh" // Replace with your Blynk Auth Token
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// WiFi credentials
const char* WIFI_NAME = "Wokwi-GUEST";
const char* WIFI_PASSWORD = ""; // Replace with your WiFi Password
// Sensor and Button pins
const int POT_PIN = 34; // Use GPIO 34 for analog input on ESP32
const int BUTTON_PIN = 25; // Button pin
const int oneWireBus = 12; // GPIO where the DS18B20 is connected
// OneWire instance
OneWire oneWire(oneWireBus);
// Dallas Temperature sensor instance
DallasTemperature sensors(&oneWire);
WiFiClient client;
bool systemEnabled = true; // Variable to track system state
BlynkTimer timer;
void setup() {
Serial.begin(115200);
// Setup DS18B20 sensor
sensors.begin();
pinMode(POT_PIN, INPUT);
pinMode(BUTTON_PIN, INPUT_PULLDOWN); // Configure button pin as input with pull-down resistor
delay(1000);
// Connect to WiFi
WiFi.begin(WIFI_NAME, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("WiFi connected!");
Serial.println("Local IP: " + String(WiFi.localIP()));
WiFi.mode(WIFI_STA);
// Connect to Blynk
Blynk.begin(BLYNK_AUTH_TOKEN, WIFI_NAME, WIFI_PASSWORD);
// Setup a function to be called every 2 seconds
timer.setInterval(2000L, sendSensorData);
}
void loop() {
Blynk.run();
timer.run();
// Check button state
if (digitalRead(BUTTON_PIN) == HIGH) {
systemEnabled = !systemEnabled; // Toggle system state
delay(300); // Debounce delay
}
if (!systemEnabled) {
// System disabled
Serial.println("System Disabled");
}
}
void sendSensorData() {
if (systemEnabled) {
// Read temperature from DS18B20
sensors.requestTemperatures();
float temperatureC_ds = sensors.getTempCByIndex(0);
// Error checking for DS18B20 sensor
if (temperatureC_ds == DEVICE_DISCONNECTED_C) {
Serial.println("Error. No Sensor Detected");
Blynk.virtualWrite(V1, "Error"); // Send error message to Blynk
} else {
// Send temperature to Blynk
Blynk.virtualWrite(V1, temperatureC_ds);
Serial.print("DS18B20 Temperature: ");
Serial.print(temperatureC_ds);
Serial.println(" °C");
}
// Read pH value
float PHValue = map(analogRead(POT_PIN), 0, 4095, 0.0, 14.0);
// Send pH value to Blynk
Blynk.virtualWrite(V0, PHValue);
Serial.print("pH Value: ");
Serial.println(PHValue);
}
}