Water Flow Sensor วัดอัตราการไหลของน้ำ

สมาชิกท่านไหน พอมี code เซนเซอร์วัดอัตราการไหลของน้ำที่ใช้กับ esp8266 บ้างไหมคับ

YF-S201C Water Flow Sensor

ตัวนี้คับ

ตัวที่มีใช้ไฟกี่โวลต์ครับ

มองไม่ชัด ถ่ายป้ายชัดๆ มาดูหน่อย นี้ตัวอย่างสำหรับ 9V

int Pulses = 3;          //Arduino digital pin 3 is declared as Pulses
int led    = 7; //Led pin
volatile int pulsecount; //Variable to count number of pulses  
volatile int i=0;                 //Variable for led status 

void setup() {
  //Interrupt pin 3 declared as input and pull up resitor is enabled
  pinMode(Pulses, INPUT_PULLUP); 
  //Led as output
  pinMode(led, OUTPUT);
  //Interrupt is attached to pin 3
  //ISR(interuupt service routine) of interrupt is CountPulses
  //Interrupt is activated on HIGH to LOW transition 
  attachInterrupt(digitalPinToInterrupt(Pulses), CountPulses, FALLING);    
  Serial.begin(9600);  //Starting serial monitor
}
void loop() { 
  pulsecount = 0;  //Start counting from 0 again
  i=0;             //Reset led toggling variable
  interrupts();    //Enables interrupt on arduino pin 3
  delay (1000);    //Wait 1 second 
  noInterrupts();  //Disable the interrupt

 //One second is over now and we have the number of pulses in variable 
 //'pulsecount'

 //Calculating the water flow rate in Milli Liters per minute
  double flowRate;    
  flowRate = (pulsecount * 2.22 * 60); 
  Serial.print("Flow rate=");       
  Serial.print(flowRate);   //Print milli liters per minute on serial monitor 
  Serial.println("mL/minute");     
}
 
void CountPulses()
{
    pulsecount++; //Increment the variable on every pulse
    //On every pulse toggle the state of led.
    if(i%2==0)
    digitalWrite(led,HIGH);
    else
    digitalWrite(led,LOW);
    i++;
}

สำหรับ ESP8266

YF-S201C Water Flow Sensor + esp8266

Code Example

#include <Arduino.h>
#include <EEPROM.h>
#define USE_SERIAL Serial
#include <ESP8266WiFi.h>

  // Variable init
  const int  buttonPin = D2; // variable for D2 pin
  int contagem = 0;   // variable to store the “rise ups” from the flowmeter pulses
  int litros = 0;
  char litros_string[10]="0";
  int addr = 0; //endereço eeprom

  //SSID and PASSWORD for the AP (swap the XXXXX for real ssid and password )
  const char* ssid = "XXXXX";
  const char* password = "XXXXX";



  //Interrupt function, so that the counting of pulse “rise ups” dont interfere with the rest of the code  (attachInterrupt)
  void pin_ISR()
  {   
      contagem++;
  }

void setup() {
   // Serial Comunication init
   Serial.begin(115200);
   delay(10);

   // EEPROM access init
   EEPROM.begin(1);
   litros=EEPROM.read(addr);


   // Initialization of the variable “buttonPin” as INPUT (D2 pin)
   pinMode(buttonPin, INPUT);
    
   // Wifi connection init
   Serial.println();
   Serial.print("A iniciar ligação...");
   Serial.println();
   WiFi.begin(ssid, password);

  //Waiting for the connection to be established
   Serial.print("Waiting for the connection..."); 
   while (WiFi.status() != WL_CONNECTED)
    {
      delay(2000);
      Serial.print(".");

      if(WiFi.status() == WL_CONNECTED)
      {
        Serial.println();
        Serial.printf("Connect to the SSID: %s",ssid);
      }
    }

  // Attach an interrupt to the ISR vector
  attachInterrupt(digitalPinToInterrupt(buttonPin), pin_ISR, RISING);

  Serial.println();
  Serial.print("Waiting for client....");
  Serial.println();
}


void loop() {
  
 

  if(contagem > 440 )
  {
      litros++;
      Serial.println();
      Serial.print("Litros: ");
      Serial.print(litros);

      //Write the new litres value to the EEPROM and put “contagem” variable to zero
      EEPROM.write(addr, litros);
      EEPROM.commit();
      contagem = 0;
      
  }//stop counting

  delay(500);
}