#include // For esp_restart() // ... esp_restart(); // This resets everything! --------------------------------------------------- code how to reset iram_attr about 0 in esp32 yfs 201 with connect php mysql database by ArduinoJson To reset an IRAM_ATTR pulse counter on an ESP32 and send the data to a PHP/MySQL database, follow this implementation. As of 2026, using ArduinoJson and IRAM_ATTR for Interrupt Service Routines (ISRs) remains the standard for reliable real-time sensor monitoring. 1. ESP32 Arduino Code This code counts pulses from the YF-S201 sensor, calculates flow, sends it via a JSON POST request, and then resets the counter to zero. cpp #include #include #include const char* ssid = "YOUR_WIFI_SSID"; const char* password = "YOUR_WIFI_PASSWORD"; const char* serverUrl = "http://your-domain.com"; const int flowSensorPin = 4; // GPIO 4 (D4) volatile int pulseCount = 0; // Must be volatile // IRAM_ATTR places the function in RAM for speed and reliability void IRAM_ATTR pulseCounter() { pulseCount++; } void setup() { Serial.begin(115200); WiFi.begin(ssid, password); pinMode(flowSensorPin, INPUT_PULLUP); // Attach interrupt to pin attachInterrupt(digitalPinToInterrupt(flowSensorPin), pulseCounter, RISING); } void loop() { static unsigned long lastTime = 0; if (millis() - lastTime > 10000) { // Send data every 10 seconds lastTime = millis(); // 1. Read and "Reset" the counter safely noInterrupts(); // Stop interrupts briefly to prevent data corruption int finalCount = pulseCount; pulseCount = 0; // Reset counter to 0 interrupts(); // 2. Prepare JSON with ArduinoJson StaticJsonDocument<200> doc; doc["sensor"] = "YFS201"; doc["pulses"] = finalCount; doc["flow_rate"] = finalCount / 7.5; // Calibration factor for YF-S201 String jsonString; serializeJson(doc, jsonString); // 3. Send to PHP/MySQL if (WiFi.status() == WL_CONNECTED) { HTTPClient http; http.begin(serverUrl); http.addHeader("Content-Type", "application/json"); int httpResponseCode = http.POST(jsonString); Serial.print("HTTP Response: "); Serial.println(httpResponseCode); http.end(); } } } Use code with caution. 2. PHP Script (post_data.php) This script receives the JSON, parses it, and inserts it into your MySQL database. php connect_error) { die("Connection failed: " . $conn->connect_error); } // Get JSON data from POST $json = file_get_contents('php://input'); $data = json_decode($json, true); if ($data) { $sensor = $data['sensor']; $pulses = $data['pulses']; $flow_rate = $data['flow_rate']; $sql = "INSERT INTO flow_readings (sensor_name, pulses, flow_rate) VALUES ('$sensor', '$pulses', '$flow_rate')"; if ($conn->query($sql) === TRUE) { echo "Success"; } else { echo "Error: " . $sql . "
" . $conn->error; } } $conn->close(); ?> Use code with caution. 3. MySQL Table Structure Run this SQL command in your database (e.g., via phpMyAdmin) to create the necessary table. sql CREATE TABLE flow_readings ( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, sensor_name VARCHAR(30) NOT NULL, pulses INT(10), flow_rate FLOAT, reading_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ); Use code with caution. Critical Implementation Notes for 2026: IRAM_ATTR Importance: Without this attribute, the ESP32 may crash because the ISR might try to run from Flash memory while the flash is busy writing other data. Volatile Variables: Always declare the pulse counter as volatile to ensure the compiler reads it directly from RAM rather than a register. Data Integrity: Use noInterrupts() and interrupts() when resetting the counter to 0 to prevent a pulse from arriving mid-calculation.