#include <WiFi.h>
#include <HTTPClient.h>
// WiFi credentials for Wokwi
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
String UID = "U71463b22a98d92c20fd04052510e6e89";
// Google Form endpoint
const String FORM_ENDPOINT = "https://docs.google.com/forms/d/e/"
"1FAIpQLSdfGlCv0nPVg1fvyQi177cgja9mgNdhMRqnH9Bm8hVGUwWvhA/"
"formResponse?usp=pp_url&"
"entry.683095985={UID}&"
"entry.1513483625={distance}&"
"entry.1153056995={water_level}&"
"submit=Submit";
WiFiClient client;
unsigned long myChannelNumber = 2254066;
const char * myWriteAPIKey = "DTV09I0TYO6QGNGZ";
int statusCode;
// Setup the sensor pins
const int trigPin = 26; // Trigger pin
const int echoPin = 27; // Echo pin
void setup() {
// Start Serial Monitor
Serial.begin(115200);
// WiFi setup
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
// Wait until connected to Wi-Fi
Serial.print("Connecting to WiFi...");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected to WiFi!");
// Setup sensor pins
pinMode(trigPin, OUTPUT); // Trigger pin
pinMode(echoPin, INPUT); // Echo pin
}
long getDistance() {
// Send a pulse to trigger the sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Measure the duration of the pulse on the Echo pin
long duration = pulseIn(echoPin, HIGH);
// Calculate distance in cm (speed of sound is 0.034 cm/us)
long distance = (duration * 0.034) / 2;
return distance;
}
void loop() {
// Get the measured distance from the sensor
long distance = getDistance();
// Simulate the water level (random between 0 and 20 cm)
float simulated_water_level = 15; // Simulated water level between 0 and 20 cm
// Calculate the distance between the sensor and water surface
float water_distance = 25 - simulated_water_level; // The distance is the height of the pipe minus the water level
// Prepare the URL with formatted parameters
String url = FORM_ENDPOINT;
url.replace("{UID}", UID);
url.replace("{distance}", String(water_distance));
url.replace("{water_level}", String(simulated_water_level));
// Send data to Google Form using HTTP GET request
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(url); // Specify the URL
int httpResponseCode = http.GET(); // Send GET request
// Print response
if (httpResponseCode == 200) {
Serial.println("Data sent successfully!");
} else {
Serial.print("Error sending data. HTTP code: ");
Serial.println(httpResponseCode);
}
http.end(); // Free resources
}
// Toggle LED to indicate action
digitalWrite(32, HIGH);
delay(1000);
digitalWrite(32, LOW);
// Delay for 10 seconds
delay(10000);
}