#define BLYNK_TEMPLATE_ID "TMPL6mxC5NXaI"
#define BLYNK_TEMPLATE_NAME "pH Turbidity and Temperature test"
#define BLYNK_AUTH_TOKEN "ilXvtY4_XtX5Up-7nGrvSsqHpOHhv596" // Replace with your Blynk Auth Token
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <HTTPClient.h>
// WiFi credentials
const char* ssid = "Wokwi-GUEST"; // Replace with your WiFi SSID
const char* password = ""; // Replace with your WiFi Password
// Google Sheets Webhook URL
const char* googleSheetsURL = "https://script.google.com/macros/s/AKfycbyQu475Wg16k8hg2MUXqfQktx8fuOkMvI6u35jDRg6rtabYk2g8kpnt341dyu73kYQFbw/exec";
// Sensor Pins
const int tempSensorPin = 34; // Example GPIO pin for temperature
const int humidSensorPin = 35; // Example GPIO pin for humidity
const int lightSensorPin = 32; // Example GPIO pin for light
// Timer for sending data
BlynkTimer timer;
// Function to send data to Google Sheets
void sendToGoogleSheets(float temp, float humid, float light) {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String url = String(googleSheetsURL) +
"?temperature=" + String(temp) +
"&humidity=" + String(humid) +
"&light=" + String(light);
http.begin(url);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
Serial.println("Data sent to Google Sheets successfully.");
} else {
Serial.println("Error sending data to Google Sheets.");
}
http.end();
} else {
Serial.println("WiFi not connected.");
}
}
// Function to read sensors and send data to Blynk & Google Sheets
void sendData() {
float temperature = analogRead(tempSensorPin) * (3.3 / 4095.0) * 100.0; // Simulated temperature
float humidity = analogRead(humidSensorPin) * (3.3 / 4095.0) * 100.0; // Simulated humidity
float light = analogRead(lightSensorPin) * (3.3 / 4095.0) * 100.0; // Simulated light intensity
// Send data to Blynk
Blynk.virtualWrite(V0, temperature);
Blynk.virtualWrite(V1, humidity);
Blynk.virtualWrite(V2, light);
// Send data to Google Sheets
sendToGoogleSheets(temperature, humidity, light);
Serial.println("Data sent successfully:");
Serial.printf("Temperature: %.2f, Humidity: %.2f, Light: %.2f\n", temperature, humidity, light);
}
void setup() {
Serial.begin(115200);
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi connected!");
// Initialize Blynk
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, password);
// Set up a timer to send data every 10 seconds
timer.setInterval(10000L, sendData);
}
void loop() {
Blynk.run();
timer.run();
}