// Blynk and Wi-Fi credentials
#define BLYNK_TEMPLATE_ID "TMPL2z8v1yiHG"
#define BLYNK_TEMPLATE_NAME "Lagos IOt"
#define BLYNK_AUTH_TOKEN "nRpNiPMBMhAS7vThXIy_UaFhrFWFIuco"
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// Blynk Credentials
char auth[] = "nRpNiPMBMhAS7vThXIy_UaFhrFWFIuco";
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
// Pin Definitions for ESP32
#define ONE_WIRE_BUS 23 // DS18B20 data pin on GPIO23
#define TURBIDITY_PIN 34 // Turbidity sensor analog pin
#define PH_PIN 35 // pH sensor analog pin
// LED Pins
#define RED_LED_PIN 25 // Red LED for temperature
#define YELLOW_LED_PIN 22 // Yellow LED for turbidity
#define GREEN_LED_PIN 21 // Green LED for pH
// Setup OneWire and DallasTemperature instances
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
// Blynk virtual pins
#define VIRTUAL_TEMP V8 // Temperature virtual pin
#define VIRTUAL_TURBIDITY V5 // Turbidity virtual pin
#define VIRTUAL_PH V6 // pH virtual pin
void setup() {
// Initialize Serial Monitor
Serial.begin(9600);
// Start DS18B20 sensor
sensors.begin();
// Configure LED pins as outputs
pinMode(RED_LED_PIN, OUTPUT);
pinMode(YELLOW_LED_PIN, OUTPUT);
pinMode(GREEN_LED_PIN, OUTPUT);
// Connect to WiFi and Blynk
Serial.print("Connecting to Wi-Fi: ");
Serial.println(ssid);
Blynk.begin(auth, ssid, pass);
Serial.println("Setup complete. Monitoring started.");
}
void loop() {
// Blynk handling
Blynk.run();
// Read temperature from DS18B20
sensors.requestTemperatures();
float temperature = sensors.getTempCByIndex(0);
if (temperature == -127.0) {
temperature = random(20, 35); // Simulated temperature
}
// Simulate or read turbidity value
int turbidity = analogRead(TURBIDITY_PIN); // Read from turbidity pin
if (turbidity == 0) {
turbidity = random(500, 750); // Simulated turbidity (650–750 NTU)
} else {
turbidity = map(turbidity, 0, 4095, 0, 1000); // Map raw analog value to NTU range
}
// Simulate or read pH value
int pH = analogRead(PH_PIN); // Read from pH pin
if (pH == 0) {
pH = random(7, 10); // Simulated pH (7–10)
} else {
pH = map(pH, 0, 4095, 0, 14); // Map raw analog value to pH range
}
// Print values to Serial Monitor
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" °C, Turbidity: ");
Serial.print(turbidity);
Serial.print(" NTU, pH: ");
Serial.println(pH);
// Update Blynk app with sensor values
Blynk.virtualWrite(VIRTUAL_TEMP, temperature); // Send temperature to V8
Blynk.virtualWrite(VIRTUAL_TURBIDITY, turbidity); // Send turbidity to V5
Blynk.virtualWrite(VIRTUAL_PH, pH); // Send pH to V6
// LED Control Logic
if (temperature > 25) { // High temperature threshold
digitalWrite(RED_LED_PIN, HIGH);
Serial.println("Red LED ON: High Temperature");
} else {
digitalWrite(RED_LED_PIN, LOW);
}
if (turbidity > 600) { // High turbidity threshold
digitalWrite(YELLOW_LED_PIN, HIGH);
Serial.println("Yellow LED ON: High Turbidity");
} else {
digitalWrite(YELLOW_LED_PIN, LOW);
}
if (pH > 7) { // High pH threshold
digitalWrite(GREEN_LED_PIN, HIGH);
Serial.println("Green LED ON: High pH");
} else {
digitalWrite(GREEN_LED_PIN, LOW);
}
delay(1000); // Delay before the next loop
}