code reset pulseCounts 0 of five yfs 201 in esp32 with connect php mysql database by ArduinoJson To implement a system with five YF-S201 sensors on an ESP32 that sends data to a MySQL database via PHP using ArduinoJson, follow these steps: 1. ESP32 Arduino Code This code uses interrupts for high-precision pulse counting. After sending data to the server, it resets the pulseCounts to zero. cpp #include #include #include // WiFi and Server Config const char* ssid = "YOUR_SSID"; const char* password = "YOUR_PASSWORD"; const char* serverUrl = "http://your-domain.com"; // Sensor Pins (Ensure they are interrupt-compatible) const int flowPins[5] = {13, 12, 14, 27, 26}; volatile long pulseCounts[5] = {0, 0, 0, 0, 0}; // Interrupt Service Routines void IRAM_ATTR pulse0() { pulseCounts[0]++; } void IRAM_ATTR pulse1() { pulseCounts[1]++; } void IRAM_ATTR pulse2() { pulseCounts[2]++; } void IRAM_ATTR pulse3() { pulseCounts[3]++; } void IRAM_ATTR pulse4() { pulseCounts[4]++; } void (*isrFunctions[5])() = {pulse0, pulse1, pulse2, pulse3, pulse4}; void setup() { Serial.begin(115200); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) delay(500); for (int i = 0; i < 5; i++) { pinMode(flowPins[i], INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(flowPins[i]), isrFunctions[i], RISING); } } void loop() { static unsigned long lastSend = 0; if (millis() - lastSend > 10000) { // Send every 10 seconds if (WiFi.status() == WL_CONNECTED) { StaticJsonDocument<200> doc; JsonArray data = doc.createNestedArray("pulses"); for (int i = 0; i < 5; i++) { data.add(pulseCounts[i]); pulseCounts[i] = 0; // Reset pulse count after reading } String jsonString; serializeJson(doc, jsonString); HTTPClient http; http.begin(serverUrl); http.addHeader("Content-Type", "application/json"); int httpResponseCode = http.POST(jsonString); Serial.println(httpResponseCode > 0 ? "Sent OK" : "Send Error"); http.end(); } lastSend = millis(); } } Use code with caution. 2. PHP Script (post-data.php) This script receives the JSON, parses it, and inserts the values into your database. php connect_error) die("Connection failed"); $json = file_get_contents('php://input'); $data = json_decode($json, true); if (!empty($data['pulses'])) { $pulses = $data['pulses']; $stmt = $conn->prepare("INSERT INTO flow_logs (s1, s2, s3, s4, s5) VALUES (?, ?, ?, ?, ?)"); $stmt->bind_param("iiiii", $pulses[0], $pulses[1], $pulses[2], $pulses[3], $pulses[4]); if ($stmt->execute()) echo "Success"; else echo "Error: " . $stmt->error; $stmt->close(); } $conn->close(); ?> Use code with caution. 3. Database Schema (MySQL) Create a table to store the readings using this SQL query: sql CREATE TABLE flow_logs ( id INT AUTO_INCREMENT PRIMARY KEY, s1 INT, s2 INT, s3 INT, s4 INT, s5 INT, recorded_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); Use code with caution. Key Technical Notes for 2026: Hardware: Use IRAM_ATTR for all interrupt routines to ensure stability on the ESP32. Calibration: For YF-S201, approximately 450 pulses = 1 liter. To convert to liters in PHP or Arduino, use the formula: liters = pulses / 450. Security: In a 2026 production environment, use HTTPS and an API Key header to prevent unauthorized database entries.