#define BLYNK_TEMPLATE_ID "TMPL6mxC5NXaI"
#define BLYNK_TEMPLATE_NAME "pH Turbidity and Temperature test"
#define BLYNK_AUTH_TOKEN "ilXvtY4_XtX5Up-7nGrvSsqHpOHhv596"
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <HTTPClient.h>
#define ONE_WIRE_BUS 27
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);
float tempCL = 0;
float tempFH = 0;
char auth[] = "ilXvtY4_XtX5Up-7nGrvSsqHpOHhv596"; // Your Blynk authentication token
char ssid[] = "Wokwi-GUEST"; // Your WiFi network SSID
char pass[] = ""; // Your WiFi network password
// Google Sheets Web App URL
const char* googleSheetsURL = "https://script.google.com/macros/s/AKfycbyEOajxe9HK1JH6RGL6fQOlhL7gxfpNnuQ04Hxa_ogrSEDblMSphn0is_Od2giSZd5XUA/exec";
void setup() {
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
sensors.begin(); // Initialize the Dallas temperature sensors
Serial.println("Setup complete");
}
void sendToGoogleSheets(float ph, float turbidity, float tempC, float tempF) {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String url = String(googleSheetsURL) +
"?ph=" + String(ph) +
"&turbidity=" + String(turbidity) +
"&tempC=" + String(tempC) +
"&tempF=" + String(tempF);
http.begin(url); // Specify the URL
int httpResponseCode = http.GET(); // Send the GET request
if (httpResponseCode > 0) {
Serial.println("Data sent to Google Sheets successfully.");
} else {
Serial.printf("Error sending data: %d\n", httpResponseCode);
}
http.end(); // End the HTTP connection
} else {
Serial.println("WiFi not connected. Data not sent.");
}
}
void loop() {
char buffer[8] = ""; // Create a character buffer to store received data
// Check if there's enough data available to read
if (Serial.available() >= 8) {
Serial.readBytesUntil('\n', buffer, 8); // Read the incoming data until newline character
// Create two separate character buffers for each floating-point number
char buffer1[5] = ""; // Buffer for the first floating-point number
char buffer2[4] = ""; // Buffer for the second floating-point number
// Copy the first 4 characters to buffer1 and the last 3 characters to buffer2
strncpy(buffer1, buffer, 4);
strncpy(buffer2, buffer + 4, 3);
// Null-terminate the character arrays
buffer1[4] = '\0';
buffer2[3] = '\0';
// Convert the character buffers to floating-point numbers
float ph_value = atof(buffer1);
float turbidity_value = atof(buffer2);
// Print the converted floating-point numbers
Serial.print("pH value: ");
Serial.println(ph_value);
Serial.print("Turbidity value: ");
Serial.println(turbidity_value);
// Send the values to Blynk
Blynk.virtualWrite(V0, ph_value);
Blynk.virtualWrite(V1, turbidity_value);
// Request temperature from sensors
sensors.requestTemperatures(); // Request temperatures from all devices on the bus
tempCL = sensors.getTempCByIndex(0); // Get Celsius temperature
tempFH = sensors.getTempFByIndex(0); // Get Fahrenheit temperature
Serial.print("Celsius temperature: ");
Serial.println(tempCL);
Serial.print("Fahrenheit temperature: ");
Serial.println(tempFH);
// Send temperature data to Blynk
Blynk.virtualWrite(V2, tempCL);
Blynk.virtualWrite(V3, tempFH);
// Send data to Google Sheets
sendToGoogleSheets(ph_value, turbidity_value, tempCL, tempFH);
}
Blynk.run(); // Run Blynk loop
}