what about something like this for the pump?
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
// Wi-Fi credentials
const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";
// Pump controller port
unsigned int localPort = 3333;
// Pump relay pin
const int pumpRelayPin = D1;
WiFiUDP udp;
// Keep track of active requests
bool sensorRequests[3] = {false, false, false};
void setup() {
Serial.begin(115200);
connectToWiFi();
udp.begin(localPort);
pinMode(pumpRelayPin, OUTPUT);
digitalWrite(pumpRelayPin, LOW); // Initially turn off pump
}
void loop() {
int packetSize = udp.parsePacket();
if (packetSize) {
char packet[2];
udp.read(packet, 2);
int sensorId = packet[0] - '0';
bool requestStatus = packet[1] == '1';
if (requestStatus) {
handleRequest(sensorId);
} else {
handleRequestRemoval(sensorId);
}
}
}
void handleRequest(int sensorId) {
sensorRequests[sensorId] = true;
if (needsWater()) {
// Turn on pump
digitalWrite(pumpRelayPin, HIGH);
}
}
void handleRequestRemoval(int sensorId) {
sensorRequests[sensorId] = false;
if (!needsWater()) {
// Turn off pump
digitalWrite(pumpRelayPin, LOW);
}
}
bool needsWater() {
return sensorRequests[0] || sensorRequests[1] || sensorRequests[2];
}
void connectToWiFi() {
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}