#include <WiFi.h>
#include "DHTesp.h"
#include "ThingSpeak.h"
const int POTENTIOMETER_PIN = A0;
const int LED_PIN = 13;
const char* WIFI_NAME = "Wokwi-GUEST";
const char* WIFI_PASSWORD = "";
const int myChannelNumber = 2389209; // Replace with your ThingSpeak channel number
const char* myWriteAPIKey = "ANJJ3HK1VTV9DM1J"; // Replace with your ThingSpeak Write API Key
const char* server = "api.thingspeak.com";
WiFiClient client;
void setup() {
Serial.begin(9600);
pinMode(LED_PIN, OUTPUT);
WiFi.begin(WIFI_NAME, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
ThingSpeak.begin(client);
}
void loop() {
int sensorValue = analogRead(POTENTIOMETER_PIN);
float voltage = sensorValue * (5.0 / 1023.0); // Convert sensor value to voltage (assuming 5V Arduino)
ThingSpeak.setField(1, voltage);
// Map the voltage to the pH range
float pH = map(voltage, 0, 5, 6.5, 8.5); // Map the 0-5V range to pH 6.5-8.5
if (pH > 7.5) {
digitalWrite(LED_PIN, HIGH); // Turn on the LED if pH is greater than 7.5
} else {
digitalWrite(LED_PIN, LOW); // Turn off the LED if pH is less than or equal to 7.5
}
Serial.print("Voltage: ");
Serial.print(voltage, 2);
Serial.print("V, pH: ");
Serial.println(pH, 2);
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
delay(15000); // ThingSpeak update rate limit is 15 seconds
}