#include <Wire.h>
#include <HTTPClient.h>
#include <WiFi.h>
// ThingSpeak parameters
const char *thingSpeakApiKey = "PKXBMVOS1FY4PITS";
const char *thingSpeakUrl = "http://api.thingspeak.com/update";
// Pin configuration for TDS and pH sensors
const int tdsSensorPin = A0; // Replace with the actual pin
const int phSensorPin = 5; // Replace with the actual pin
void setup() {
Serial.begin(115200);
Serial.print("Connecting to WiFi");
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
}
void loop() {
// Generate dummy data for the first 10 seconds
if (millis() < 10000) {
float tdsValue = random(0, 1000); // Simulating values between 0 and 1000
float phValue = random(0, 14); // Simulating values between 0 and 14
float tds = map(tdsValue, 0, 1000, 0, 1000);
float ph = map(phValue, 0, 14, 0, 14);
// Display sensor readings on the Serial Monitor
Serial.print(F("TDS: "));
Serial.print(tds);
Serial.print(F(", pH: "));
Serial.println(ph);
// Send data to ThingSpeak
sendDataToThingSpeak(tds, ph);
delay(1000); // Adjust the delay based on your requirements
} else {
// After 10 seconds, set the pH sensor to 6.4 and the TDS sensor to 120
float tds = 120.0;
float ph = 6.4;
// Display sensor readings on the Serial Monitor
Serial.print(F("TDS: "));
Serial.print(tds);
Serial.print(F(", pH: "));
Serial.println(ph);
// Send data to ThingSpeak
sendDataToThingSpeak(tds, ph);
// You may want to add additional logic or delay here, depending on your needs
}
}
void sendDataToThingSpeak(float tds, float ph) {
HTTPClient http;
// Construct the ThingSpeak URL with parameters
String url = String(thingSpeakUrl) + "?api_key=" + String(thingSpeakApiKey) +
"&field1=" + String(tds) + "&field2=" + String(ph);
Serial.println("Sending data to ThingSpeak...");
// Make the HTTP request
http.begin(url);
// Check the HTTP response code
int httpCode = http.GET();
if (httpCode > 0) {
Serial.printf("HTTP Response Code: %d\n", httpCode);
} else {
Serial.println("HTTP Request failed");
}
// Close the connection
http.end();
}