#include <WiFi.h>
#include <HTTPClient.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
// Simulated WiFi credentials for Wokwi environment
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// ThingSpeak settings
const char* serverName = "http://api.thingspeak.com/update";
String apiKey = "WQOSOKN06RK7P8IA";
// Constants for sensors
#define TEMPSENSORPIN 32 // Analog pin for temperature sensor (potentiometer)
#define PHSENSORPIN 34 // Analog pin for pH sensor (potentiometer)
#define TURBIDITYPIN 35 // Analog pin for turbidity sensor (potentiometer)
#define LEDPIN 13 // Digital pin for LED
#define LCD_ADDR 0x27
// Initialize the LCD
LiquidCrystal_I2C lcd(LCD_ADDR, 16, 2);
// Safe limits
const float SAFE_TEMP_MIN = 0; // Example safe limit for temperature (min)
const float SAFE_TEMP_MAX = 100; // Example safe limit for temperature (max)
const float SAFE_PH_MIN = 0; // Example safe limit for pH (min)
const float SAFE_PH_MAX = 10; // Example safe limit for pH (max)
const float SAFE_TURBIDITY_MAX = 80; // Example safe limit for turbidity (max)
void setup() {
Serial.begin(115200); // Set the baud rate
// Connect to WiFi (simulated)
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi...");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println();
Serial.println("Connected to WiFi");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
// Setup code for sensors and alert components
pinMode(TEMPSENSORPIN, INPUT);
pinMode(PHSENSORPIN, INPUT);
pinMode(TURBIDITYPIN, INPUT);
pinMode(LEDPIN, OUTPUT);
// Initialize the LCD
lcd.init();
lcd.backlight();
lcd.print("System Initialized");
delay(2000);
lcd.clear();
Serial.println("Smart Water Quality Monitoring System Initialized");
}
void checkLimits(float temperature, float pH, float turbidity) {
bool alert = false;
lcd.clear();
lcd.setCursor(0, 0);
if (temperature < SAFE_TEMP_MIN || temperature > SAFE_TEMP_MAX) {
Serial.println("Alert: Temperature out of safe limits!");
lcd.print("Temp Alert!");
alert = true;
}
if (pH < SAFE_PH_MIN || pH > SAFE_PH_MAX) {
Serial.println("Alert: pH out of safe limits!");
lcd.print("pH Alert!");
alert = true;
}
if (turbidity > SAFE_TURBIDITY_MAX) {
Serial.println("Alert: Turbidity out of safe limits!");
lcd.print("Turb Alert!");
alert = true;
}
if (alert) {
digitalWrite(LEDPIN, HIGH); // Turn on LED
lcd.setCursor(0, 1);
lcd.print("Check System!");
} else {
digitalWrite(LEDPIN, LOW); // Turn off LED
lcd.setCursor(0, 0);
lcd.print("All OK!");
}
}
void loop() {
// Read temperature value (simulated using potentiometer)
int tempValueRaw = analogRead(TEMPSENSORPIN);
float temperature = map(tempValueRaw, 0, 4095, -40, 125); // conversion to temperature range
// Read pH value (simulated using potentiometer)
int pHValueRaw = analogRead(PHSENSORPIN);
float pHValue = map(pHValueRaw, 0, 4095, 0, 14); // conversion to pH range
// Read turbidity value (simulated using potentiometer)
int turbidityValue = analogRead(TURBIDITYPIN);
float turbidity = map(turbidityValue, 0, 4095, 0, 100); // conversion to turbidity percentage
// Print values to Serial Monitor
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" °C, pH Value: ");
Serial.print(pHValue);
Serial.print(", Turbidity Value: ");
Serial.println(turbidity);
// Check if values are within safe limits
checkLimits(temperature, pHValue, turbidity);
// Send data to ThingSpeak
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String url = String(serverName) + "?api_key=" + apiKey + "&field1=" + String(temperature) + "&field2=" + String(pHValue) + "&field3=" + String(turbidity);
http.begin(url);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println(httpResponseCode);
Serial.println(response);
Serial.println("sent successfully");
} else {
Serial.print("Error on sending GET: ");
Serial.println(httpResponseCode);
}
http.end();
}
delay(2000); // Wait for 2 seconds before next reading
}