#include <WiFi.h>
#include <HTTPClient.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const int mychannelNUMber =2701952 ;
const char* serverName = "http://api.thingspeak.com/update";
String apiKey = "2XV2OX8VUQHWDTMP";
WiFiClient client ;
#define TEMPSENSORPIN 32
#define PHSENSORPIN 34
#define TURBIDITYPIN 35
#define LEDPIN 13
#define LCD_ADDR 0x27
LiquidCrystal_I2C lcd(LCD_ADDR, 16, 2);
const float SAFE_TEMP_MIN = 25;
const float SAFE_TEMP_MAX = 60;
const float SAFE_PH_MIN = 4;
const float SAFE_PH_MAX = 10;
const float SAFE_TURBIDITY_MAX = 80;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi...");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println();
Serial.println("Connected to WiFi");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
WiFi.mode(WIFI_STA);
pinMode(TEMPSENSORPIN, INPUT);
pinMode(PHSENSORPIN, INPUT);
pinMode(TURBIDITYPIN, INPUT);
pinMode(LEDPIN, OUTPUT);
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);
lcd.setCursor(0, 1);
lcd.print("Check System!");
} else {
digitalWrite(LEDPIN, LOW);
lcd.setCursor(0, 0);
lcd.print("All OK!");
}
}
void loop() {
int tempValueRaw = analogRead(TEMPSENSORPIN);
float temperature = map(tempValueRaw, 0, 4095, -40, 125);
int pHValueRaw = analogRead(PHSENSORPIN);
float pHValue = map(pHValueRaw, 0, 4095, 0, 14);
int turbidityValue = analogRead(TURBIDITYPIN);
float turbidity = map(turbidityValue, 0, 4095, 0, 100);
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" °C, pH Value: ");
Serial.print(pHValue);
Serial.print(", Turbidity Value: ");
Serial.println(turbidity);
checkLimits(temperature, pHValue, turbidity);
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);
}