code reset volume and pulseCounts 0 of five yfs 401 in esp32 with connect php mysql database by ArduinoJson This solution involves an ESP32 reading five YF-S401 flow sensors and sending the data to a MySQL database via a PHP script. After a successful database update, the ESP32 resets the pulseCounts and volume to zero. 1. ESP32 Arduino Code This code uses ArduinoJson to package data and HTTPClient to send it. 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 Configuration const int numSensors = 5; const int flowPins[numSensors] = {4, 5, 18, 19, 21}; // ESP32 GPIOs volatile long pulseCounts[numSensors] = {0, 0, 0, 0, 0}; float volumes[numSensors] = {0, 0, 0, 0, 0}; const float calibrationFactor = 5880.0; // Pulses per Liter for YF-S401 // Interrupt functions for each sensor void IRAM_ATTR pulseCounter0() { pulseCounts[0]++; } void IRAM_ATTR pulseCounter1() { pulseCounts[1]++; } void IRAM_ATTR pulseCounter2() { pulseCounts[2]++; } void IRAM_ATTR pulseCounter3() { pulseCounts[3]++; } void IRAM_ATTR pulseCounter4() { pulseCounts[4]++; } void setup() { Serial.begin(115200); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) delay(500); void (*interrupts[])() = {pulseCounter0, pulseCounter1, pulseCounter2, pulseCounter3, pulseCounter4}; for (int i = 0; i < numSensors; i++) { pinMode(flowPins[i], INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(flowPins[i]), interrupts[i], FALLING); } } void loop() { static unsigned long lastSend = 0; if (millis() - lastSend > 10000) { // Send every 10 seconds sendData(); lastSend = millis(); } } void sendData() { if (WiFi.status() == WL_CONNECTED) { StaticJsonDocument<300> doc; JsonArray data = doc.createNestedArray("sensors"); for (int i = 0; i < numSensors; i++) { volumes[i] = (float)pulseCounts[i] / calibrationFactor; // Calculate Volume JsonObject obj = data.createNestedObject(); obj["id"] = i + 1; obj["pulses"] = pulseCounts[i]; obj["volume"] = volumes[i]; } String jsonString; serializeJson(doc, jsonString); HTTPClient http; http.begin(serverUrl); http.addHeader("Content-Type", "application/json"); int httpResponseCode = http.POST(jsonString); if (httpResponseCode > 0) { Serial.println("Data sent successfully. Resetting counters..."); // RESET: Set all counts and volumes back to 0 after success for (int i = 0; i < numSensors; i++) { pulseCounts[i] = 0; volumes[i] = 0.0; } } http.end(); } } Use code with caution. 2. PHP Script (update_flow.php) This script receives the JSON and updates your MySQL database. php connect_error) die("Connection failed: " . $conn->connect_error); $json = file_get_contents('php://input'); $data = json_decode($json, true); if (!empty($data['sensors'])) { foreach ($data['sensors'] as $sensor) { $id = $sensor['id']; $pulses = $sensor['pulses']; $volume = $sensor['volume']; // Update total or log reading $sql = "UPDATE flow_readings SET last_pulses=$pulses, total_volume=total_volume+$volume WHERE sensor_id=$id"; $conn->query($sql); } echo "Success"; } $conn->close(); ?> Use code with caution. 3. Database Schema Create a table to store the sensor data: sql CREATE TABLE flow_readings ( sensor_id INT PRIMARY KEY, last_pulses INT DEFAULT 0, total_volume FLOAT DEFAULT 0.0, last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ); -- Initialize for 5 sensors INSERT INTO flow_readings (sensor_id) VALUES (1), (2), (3), (4), (5); Use code with caution. Key Considerations Calibration: For the YF-S401, 1 Liter is approximately 5880 pulses. Interrupts: Using IRAM_ATTR ensures the interrupt service routines (ISR) are stored in the ESP32's fast internal RAM. Resets: The reset logic in the ESP32 code only triggers if httpResponseCode > 0, ensuring no data is lost if the server is down.