#include <WiFi.h>
#include <HTTPClient.h>
#include <DHT.h>
#include <DHT_U.h>
// Define pins
#define DHTPIN 15 // GPIO 15 for DHT22 data
#define DHTTYPE DHT22
#define POT1_PIN 32 // GPIO 32 for Potentiometer 1 (Water condition)
#define POT2_PIN 33 // GPIO 33 for Potentiometer 2 (Water pH)
#define MOISTURE_PIN 34 // GPIO 34 for Potentiometer (Soil Moisture)
#define LED_PIN 26 // GPIO 26 for LED (Water Pump)
// WiFi credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// ThingSpeak settings
const char* server = "http://api.thingspeak.com";
const char* apiKey = "NWTWYKWS6RRQDFYU";
const unsigned long channelID = 2594761;
// Initialize DHT sensor
DHT dht(DHTPIN, DHTTYPE);
// Fish species and their ranges
const float tempMin = 25.0;
const float tempMax = 30.0;
const int turbidityMin = 10;
const int turbidityMax = 30;
const int phMin = 6;
const int phMax = 9;
const int waterLevelMin = 50;
const int waterLevelMax = 80;
void setup() {
// Start serial communication
Serial.begin(115200);
// Initialize DHT sensor
dht.begin();
// Initialize LED pin
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
// Give some time for the sensor to stabilize
delay(2000);
// Connect to WiFi
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi...");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(" connected");
}
void loop() {
// Read DHT22 sensor
float temperatureC = dht.readTemperature();
// Check if reading is valid
if (isnan(temperatureC)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Read potentiometer 1 (for water condition)
int potValue1 = analogRead(POT1_PIN);
float waterConditionValue = map(potValue1, 0, 4095, turbidityMin, turbidityMax);
// Read potentiometer 2 (for water pH)
int potValue2 = analogRead(POT2_PIN);
float waterPHValue = map(potValue2, 0, 4095, phMin, phMax);
// Simulate water level
float waterLevelValue = map(potValue1, 0, 4095, waterLevelMin, waterLevelMax);
// Read soil moisture potentiometer
int soilMoistureValue = analogRead(MOISTURE_PIN);
float soilMoisturePercent = map(soilMoistureValue, 0, 4095, 0, 100);
// Control LED based on soil moisture
if (soilMoisturePercent < 30) { // Adjust this threshold as needed
digitalWrite(LED_PIN, HIGH); // Turn on LED (pump)
} else {
digitalWrite(LED_PIN, LOW); // Turn off LED (pump)
}
// Print actual values to serial monitor
Serial.print("Temperature: ");
Serial.print(temperatureC);
Serial.println(" °C");
Serial.print("Water condition value: ");
Serial.print(waterConditionValue);
Serial.println("%");
Serial.print("Water pH value: ");
Serial.println(waterPHValue);
Serial.print("Water level value: ");
Serial.print(waterLevelValue);
Serial.println("%");
Serial.print("Soil Moisture: ");
Serial.print(soilMoisturePercent);
Serial.println("%");
// Send data to ThingSpeak
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String url = String(server) + "/update?api_key=" + apiKey +
"&field1=" + temperatureC +
"&field2=" + waterConditionValue +
"&field3=" + waterPHValue +
"&field4=" + waterLevelValue +
"&field5=" + soilMoisturePercent;
http.begin(url);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
} else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
http.end();
} else {
Serial.println("WiFi Disconnected");
}
// Wait 15 seconds between measurements
delay(15000); // Minimum allowed by ThingSpeak
}