This Arduino code reads the water level sensor and controls a pump based on the sensor state. It also sends the sensor and pump states to a server.
// Libraries included
#include <WiFi.h>
#include <HTTPClient.h>
// Global Variables
String serverName = "https://7b1bd7318-bc4d-4740-a62d-d7195741dcee-00-2biv5sob6tgpp.spock.replit.dev/bebedero.php";
int sensorLevel = 34; // Pin where the level sensor is connected
int pumpPin = 5; // Pin where the pump is connected
void setup() {
Serial.begin(115200);
pinMode(sensorLevel, INPUT);
pinMode(pumpPin, OUTPUT);
WiFi.begin("frank", "franlopez");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected.");
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(serverName);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
int sensorState = digitalRead(sensorLevel);
int pumpState = (sensorState == HIGH) ? HIGH : LOW;
digitalWrite(pumpPin, pumpState);
String httpRequestData = "sensor=" + String(sensorState) + "&pump=" + String(pumpState);
int httpResponseCode = http.POST(httpRequestData);
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println(httpResponseCode);
Serial.println(response);
} else {
Serial.print("Error on sending POST: ");
Serial.println(httpResponseCode);
}
http.end();
}
delay(1000);
}