Pulse water meter

Hi to everyone. I am over here in Ethiopia (an adjustment coming from Canada) for a while and trying to make my life more predictable with Blynk :slight_smile: We intermittently get water into our water tanks and have to make it stretch till the next time the water comes in. I would like to be notified when the water comes in so I can go make sure our top tank is full as well as the two bottom tanks. I would also like to know how many liters came in to see if we will make it till the next time water comes. I found a water meter that has a magnet but no reed switch. Gingerly, taking it apart I placed a copper wire inside the face and using a stretched out spring from a ballpoint pen I managed a reed switch. Now using an interrupt I managed to get it to notify and count but it continues to count and notify (every 5s) as long as the reed is closed. Probably something basic but nonetheless I am still missing something.

Equipment- NodeMCU

Here’s my code

#define BLYNK_PRINT Serial
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

int isButtonPressed;
int counterLiters = 0;

void notifyOnButtonPress()
{
  // Invert state, since button is "Active LOW"
  int isButtonPressed = !digitalRead(D2);

    // Note:
    //   We allow 1 notification per 5 seconds for now.
    Blynk.notify("The Gate bell");
    counterLiters ++ ;
    Blynk.virtualWrite(V4,counterLiters);    
}
void setup()
{
  Blynk.begin(auth, ssid, pass);
  
  // Setup notification button on pin 2
  pinMode(D2, INPUT_PULLUP);
  // Attach pin 2 interrupt to our handler
  attachInterrupt(digitalPinToInterrupt(D2), notifyOnButtonPress, CHANGE);
  Blynk.virtualWrite(V4,counterLiters);
  
}

void loop()
{
  Blynk.run();
}

LOL Noticed it still says “The Gate bell” in notification. We live in a gated compound here so have a button to ring my gate bell on Blynk and am toying with a relay to notify me everytime someone rings my bell. As well I have that NodeMCU tracking inside temp and humidity on a DHT11.

Generally you would use coded routines and functions to set up a “flag” variable that, with an if() statement, allows the initial notification and disallows any further notifications until either a timeout timer or other deciding factor resets the flag

If you’re using an interrupt then your routine that is called when the interrupt is triggered doesn’t then need to check the state of D2.
Also, you probably need the interrupt set to either RISING or FALLING (depending on whether your reed switch is normally open or normally closed) rather than set to CHANGE.

So, the way it should work is that each time the magnet moves past the reed switch it creates one pulse. This will call your pulse counting routine and increment your counter. Whether this equates to one litre per pulse will be something you need to check by experimentation, and tweak your code accordingly.

To get your Blynk notification once water start flowing, then as @Gunner says you’ll normally set a flag (maybe when the first pulse is detected, or maybe after the pulse count equals a certain number - say 10 - to rule out false alarms). I’d probably then either have a Blynk Switch widget that allows you to manually reset the alert flag once the water flow has stopped, or have some sort of timeout timer that is triggered by your count pulse routine. If no pulses are received after say 1 minute then the code will assume that the water flow has stopped and reset the flag, so that you’ll then get a new alert next time the water starts flowing.

If you can get hold of some ultrasonic sensors, you’ll see quite a few projects on here where people are using these to measure the height of the water in a tank and calculate the remaining volume of water, which might be a handy addition.

Pete.

Thanks for the explanation Pete, will try the rising/falling. I will have to look into the Flag thing. Don’t understand that yet… Newbie

Declare a global Boolean variable (it’s global if you put it at the top of your code) called maybe “NotificationSent” and set it to false when you declare it.
In your function that counts the pulses from your meter, check if NotificationSent is true using an if statement. If it’s false then send your notification and then set NotificationSent to true.
When the flow stops, you’ll want to set your NotificationSent flag back to false again, so you’ll be notified next time the water starts flowing.
How/where you do this will depend on how you structure your code. I discussed earlier the use of a timeout timer, that triggers when no pulses have been received for x amount of time. If you went for this option, then the function that’s called when your timeout timer expires is where you’d set your flag back to false.

Pete.

Ok thanks for the help so far. To get any accuracy out of my homemade reed switches I added another one and wait for both to go true before incrementing and then immediately changing both to low. Now I need to work on the notification. As well I will need to clear the counter every 24hrs or divided the flow per hour or something like that…

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

bool d1Trig = false; //Bool to store interupt for D1
bool d2Trig = false; //Bool to store interupt for D2
int counterLiters = 0;

WidgetLCD lcd (V0);

void d1Trigopp()
{
  d1Trig = true; //On interupt set D1 bool to true
  lcd.print(0,0, "d1 true" ); 
  lcd.print(0,1, counterLiters);  
}

void d2Trigopp()
{
  d2Trig = true; //On interupt set set D2 bool to true
  lcd.print(0,0, "d2 true" );
  if(d1Trig == true && d2Trig == true) //Check both variables
    {
    lcd.clear();
    counterLiters ++; // increment counter
    lcd.print(0,0, "counter" );
    Blynk.virtualWrite(V4,counterLiters);
    d1Trig = false; //immediately set values to false so exits loop
    d2Trig = false;
    }
}

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass); 
  pinMode(D1, INPUT_PULLUP);
  pinMode(D2, INPUT_PULLUP);
  digitalWrite(D4,HIGH);
  attachInterrupt(digitalPinToInterrupt(D1), d1Trigopp, FALLING);
  attachInterrupt(digitalPinToInterrupt(D2), d2Trigopp, FALLING);
  Blynk.virtualWrite(V4,counterLiters); 
  lcd.clear();
  lcd.print(0,0, "setup" );  
}

void loop()
{
  Blynk.run();
}

I have a project for my watering system at my home to detect leaks when the flow rate goes above a certain threshold. I use this water meter. Are you using something similar? And If your meter has a magnet built in, why not use a hall sensor for measuring pulses?

I count the number of pulses every say 5 seconds and then calculate the average flow rate over that 5 second period. If it’s higher than my set threshold, it sends me a text that I have a leak and also turns off the solenoid supplying the water.

You can also count total pulses (if you know the pulses per gal or liter) to get the total gallons. As you suggest, you’ll need to determine when to reset that total gallons in variable. I do this to get total water usage per month. When the month changes it sets the variable back to zero.

For your project… if you start detecting pulses then water is flowing so send out a notification. Set a flag that the notification was sent so it won’t keep sending them while the water continues to flow. Then when you stop seeing pulses for some determined period of time (see Blynk Timer) set the notification sent flag back to false for the next time.

This is what I have to work with out here. Shipping anything here on courier ends up being a couple hundred USD.

1 Like

I think that your reed switch may have been suffering some contact bounce, randomly giving multiple interrupt triggers for some activations. There are ways to fix this in your code, but I guess that adding a second sensor does the same thing.

This is quite a bit different to the requirement you described in your first post. Resetting your counter to zero after 24 hours can be done, but is quite a bit more work, and I’m not sure that it actually gives you any benefits.
As your coding skills are fairly basic at the moment, it would be worth putting more thought into what you actually need from the system before starting to try coding it.

Clearly you need an alert to tell you when the flow has started. You may also want an alert to tell you when the flow has stopped. That could include info that the duration was x minutes/hours and that the quantity of water passing through your meter was y litres.

You could also think about using a Superchart widget to plot the water flow over time. This would involve sending data to the Superchart on a regular basis (say every minute), whether water is flowing or not.

So, my advice would be to plan out what you actually need in terms of functionality before you do any more. This could be a ‘must have’ and ‘would like’ list, but keep it fairly simple at this stage. For each piece of functionality, ask yourself why you need it, and can you live without it.
Also ask yourself what happens if the water starts flowing when you’re not there, and how you want to handle a situation where you’re away while several water flow cycles occur.

Pete.

Basic is the word! This and other applications I have messed with the millis() timing but I can’t get it to work with blynk. If you feel like it you could draft a denounce sketch and I will study it and see if I can get a grip on how it works together with blynk timer.

I don’t use Blynk in the same way as most people, so I don’t write much Blynk specific code.
However, take a look at the code in this post:

The CheckButtonState has my standard debounce functionality in it and works with Blynk. It’s commented in a way that should make it fairly easy to follow.

Pete.

Thanks for the reply I’ll reference the link.