//importing libraries
#include <WiFi.h>
#include "DHTesp.h"
#include "ThingSpeak.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
//wifi and thinkspeak connection
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const long channelID = 2390794;
const char* writeAPIKey = "9XG1VN5NVTXZNLN9";
// Sensor and Display Settings
const int DHTPin = 15;
const int pHpin = 35;
const int turbidityPin = 34; // Change this to your turbidity sensor pin
const float defaultPH = 7.0;
const float calibration_value = 21.34 - 0.7;
// LCD settings
const int i2c_addr = 0x27;
const int lcdColumns = 20;
const int lcdRows = 4;
LiquidCrystal_I2C lcd(i2c_addr, lcdColumns, lcdRows);
WiFiClient client;
DHTesp dhtSensor;
void setup() {
Serial.begin(115200);
initializeSensors();
initializeLCD();
connectToWiFi();
ThingSpeak.begin(client);
}
void loop() {
TempAndHumidity newValues = dhtSensor.getTempAndHumidity();
float pH = readPH();
pH = constrain(pH, 1.0, 14.0); // Constrain pH value between 1 and 14
int turbidityValue = analogRead(turbidityPin);
float turbidity = map(turbidityValue, 0, 750, 100, 0);
turbidity = constrain(turbidity, 0, 5.0); // Constrain turbidity value below 5 NTU
displayDataOnLCD(newValues, pH, turbidity);
delay(20000); // Delay for 20 seconds
sendDataToThingSpeak(newValues.temperature, newValues.humidity, pH, turbidity);
}
void initializeSensors() {
dhtSensor.setup(DHTPin, DHTesp::DHT22);
}
void initializeLCD() {
lcd.init();
lcd.backlight();
}
void connectToWiFi() {
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
}
float readPH() {
int pHval = analogRead(pHpin);
float pH;
if (pHval < 10) {
pH = defaultPH;
Serial.println("pH sensor not found");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("pH: Sensor Not");
lcd.setCursor(0, 1);
lcd.print("Connected");
} else {
float voltage = pHval * (3.3 / 4096.0);
pH = -5.70 * voltage + calibration_value;
pH = constrain(pH, 1.0, 14.0); // Constrain pH value between 1 and 14
Serial.println("pH Sensor Connected");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("pH: ");
lcd.print(pH, 2);
}
return pH;
}
void displayDataOnLCD(TempAndHumidity newValues, float pH, float turbidity) {
lcd.setCursor(0, 1);
lcd.print("Temp:");
lcd.print(newValues.temperature, 1);
lcd.print("C Hum:");
lcd.print(newValues.humidity, 1);
lcd.setCursor(0, 0);
lcd.print("pH:");
lcd.print(pH, 1);
lcd.print(" Turb:");
lcd.print(turbidity, 1);
}
void sendDataToThingSpeak(float temperature, float humidity, float pH, float turbidity) {
if (WiFi.status() == WL_CONNECTED) {
ThingSpeak.setField(1, temperature);
ThingSpeak.setField(2, humidity);
ThingSpeak.setField(3, pH);
ThingSpeak.setField(4, turbidity);
int x = ThingSpeak.writeFields(channelID, writeAPIKey);
if (x == 200) {
Serial.println("Data sent to ThingSpeak");
} else {
Serial.println("Error sending data to ThingSpeak");
Serial.println(x);
}
} else {
Serial.println("WiFi not connected. Data not sent.");
}
}