#include <WiFi.h>
#include <ThingSpeak.h>
#include <HTTPClient.h>

// Declare global variables
WiFiClient client;

// Replace these with your actual values
const char *ssid = "Wokwi-GUEST";
const char *password = "";
const char *apiKey = "V46OKHYC2R3560KV";
const long channelID = 2334764; // Replace with your ThingSpeak channel ID

const char *iftttKey = "TS3S0Fz_u45OAq0mp_G3L";
const char *eventName = "pHRate";

// Declare sensor pins
const int Tur_PIN = 34; // Turbidity
#define PH_PIN 35         // pH
#define DHT_PIN 33        // Temperature

// Declare warning LEDs
const int pHWarningAxit = 2; // Red LED
const int pHWarningbazo = 18; // Green LED
const int TurWarning = 4;    // Orange LED for Turbidity
const int TempWarning = 5;   // Yellow LED for Temperature

float turbidity;
float phValue;
float temp;

void setup() {
  Serial.begin(9600);
    pinMode(pHWarningAxit, OUTPUT);
      pinMode(pHWarningbazo, OUTPUT);
        pinMode(TurWarning, OUTPUT);
          pinMode(TempWarning, OUTPUT);
            Serial.print("Connecting to WiFi");
              WiFi.begin(ssid, password);
                while (WiFi.status() != WL_CONNECTED) {
                    delay(100);
                        Serial.print(".");
                          }
                            Serial.println(" Connected!");
                            }

                            void Turbidity() {
                              int Tur = analogRead(Tur_PIN);
                                turbidity = map(Tur, 0, 4095, 0, 100);
                                  Serial.print("Turbidity: ");
                                    Serial.println(turbidity);

                                      delay(100);
                                        if (turbidity < 20) {
                                            Serial.println("Low Turbidity");
                                                digitalWrite(TurWarning, LOW);
                                                  } else if (turbidity >= 20 && turbidity < 50) {
                                                      Serial.println("Moderate Turbidity");
                                                          digitalWrite(TurWarning, HIGH);
                                                            } else if (turbidity >= 50) {
                                                                Serial.println("High Turbidity");
                                                                    digitalWrite(TurWarning, HIGH);
                                                                      }
                                                                      }

                                                                      void Temperature() {
                                                                        int Temp = analogRead(DHT_PIN);
                                                                          temp = map(Temp, 0, 4095, 0, 100);
                                                                            Serial.print("Temperature: ");
                                                                              Serial.println(temp);

                                                                                delay(100);
                                                                                  if (temp < 20) {
                                                                                      Serial.println("Low Temperature");
                                                                                          digitalWrite(TempWarning, LOW);
                                                                                            } else if (temp >= 20 && temp < 50) {
                                                                                                Serial.println("Moderate Temperature");
                                                                                                    digitalWrite(TempWarning, HIGH);
                                                                                                      } else if (temp >= 50) {
                                                                                                          Serial.println("High Temperature");
                                                                                                              digitalWrite(TempWarning, HIGH);
                                                                                                                }
                                                                                                                }

                                                                                                                void pHRate() {
                                                                                                                  int phR = analogRead(PH_PIN);
                                                                                                                    phValue = map(phR, 0, 4095, 0, 14);
                                                                                                                      Serial.print("pH Value: ");
                                                                                                                        Serial.println(phValue);
                                                                                                                          delay(100);
                                                                                                                            if (phValue >= 6.5 && phValue <= 8.5) {
                                                                                                                                Serial.println("Optimal pH");
                                                                                                                                    digitalWrite(pHWarningAxit, LOW);
                                                                                                                                        digitalWrite(pHWarningbazo, LOW);
                                                                                                                                          } else if (phValue < 6.5) {
                                                                                                                                              Serial.println("Low pH");
                                                                                                                                                  digitalWrite(pHWarningAxit, HIGH);
                                                                                                                                                      digitalWrite(pHWarningbazo, LOW);
                                                                                                                                                        } else if (phValue > 8.5) {
                                                                                                                                                            Serial.println("High pH");
                                                                                                                                                                digitalWrite(pHWarningbazo, HIGH);
                                                                                                                                                                    digitalWrite(pHWarningAxit, LOW);
                                                                                                                                                                      }
                                                                                                                                                                      }

                                                                                                                                                                      void loop() {
                                                                                                                                                                        Turbidity();
                                                                                                                                                                          pHRate();
                                                                                                                                                                            Temperature();

                                                                                                                                                                              // Send data to ThingSpeak
                                                                                                                                                                                ThingSpeak.writeField(2334762, 1, phValue, "6HFMKFL3PR3J035A");
                                                                                                                                                                                  ThingSpeak.writeField(2334761, 2, temp, "CWCDIKMRECMSZXAJ");
                                                                                                                                                                                    ThingSpeak.writeField(2334763, 3, turbidity, "95VFP9DLWPKDKOWU");

                                                                                                                                                                                      // Send an HTTP request to IFTTT
                                                                                                                                                                                        if (phValue < 6.5 || phValue > 8.5) {
                                                                                                                                                                                            String url = "http://maker.ifttt.com/trigger/pHRate/with/key/TS3S0Fz_u45OAq0mp_G3L";

                                                                                                                                                                                                Serial.println("Connecting to IFTTT: " + url);

                                                                                                                                                                                                    HTTPClient http;
                                                                                                                                                                                                        http.begin(url);
                                                                                                                                                                                                            int httpResponseCode = http.GET();

                                                                                                                                                                                                                if (httpResponseCode > 0) {
                                                                                                                                                                                                                      Serial.println("HTTP " + String(httpResponseCode));
                                                                                                                                                                                                                          } else {
                                                                                                                                                                                                                                Serial.println("Error code: " + String(httpResponseCode));
                                                                                                                                                                                                                                    }

                                                                                                                                                                                                                                        http.end();
                                                                                                                                                                                                                                          }

                                                                                                                                                                                                                                          }

                                                                                                                                                                                                                                          void sendIFTTTEvent() {
                                                                                                                                                                                                                                            HTTPClient http;

                                                                                                                                                                                                                                              String url = "http://maker.ifttt.com/trigger/";
                                                                                                                                                                                                                                                url += eventName;
                                                                                                                                                                                                                                                  url += "/with/key/";
                                                                                                                                                                                                                                                    url += iftttKey;

                                                                                                                                                                                                                                                      Serial.print("Connecting to IFTTT: ");
                                                                                                                                                                                                                                                        Serial.println(url);

                                                                                                                                                                                                                                                          http.begin(url);
                                                                                                                                                                                                                                                            int httpResponseCode = http.GET();

                                                                                                                                                                                                                                                              if (httpResponseCode > 0) {
                                                                                                                                                                                                                                                                  Serial.print("HTTP ");
                                                                                                                                                                                                                                                                      Serial.println(httpResponseCode);
                                                                                                                                                                                                                                                                        } else {
                                                                                                                                                                                                                                                                            Serial.print("Error code: ");
                                                                                                                                                                                                                                                                                Serial.println(httpResponseCode);
                                                                                                                                                                                                                                                                                    Serial.println(":-(");
                                                                                                                                                                                                                                                                                      }

                                                                                                                                                                                                                                                                                        http.end();
                                                                                                                                                                                                                                                                                        }