Here, real quick is my implementation of two liquid glow meters on an MQTT enabled ESP. I really was thinking about what in my house to instrument, but it always seems that I want everything to be automated, but it’s hard to decide what you’d do with the data once you put so much effort into getting it. I figured I’d instrument the hot and cold water to look for leaks, and to watch my usage. I may only need a tiny hot water heater, or I may be running it out fast. Also, charting the time on of the hot faucet may give an indication to how much water is wasted staying cold in the pex. Anyway, my data could be used to size upgrades or energy saving things for the house, and the whole thing cost less than $20 in quantity one, so why not.
I decided to go with a plastic flow meter I got from an ebay seller whose cart I was filling up to get free shipping (see, I use the fluff I pad with). They happen to be the same as the plastic flowmeters adafruit stock, so that’s nice. The sensor inside is essentially a waterwheel and hall-effect sensor that counts rotations. The sensor says it operates from 5 to 18v, but I would rather it run at 3.3. I elected to just run it at 5 because the sensor may be less responsive, or accurate if it even did anything at 3.3v. The output is in pulses and the pulses are naturally 5 volts, which is a problem for me. The best thing about uni-directional sensors is that the voltage divider is just a pair of resistors. Put simply, the voltage drop across the resistors is going to be 5 volts, but you can pick where along the drop from 5 to 0 you want and get a lower voltage signal out. This is what I did to take the inputs from both flow meters (12K and 22K resistors were used to good effect, I have heard problems with different values). The total bill of materials for this project is 4 resistors, 2 sensors, and one NodeMCU (which can be changed to an esp-01 when it is done being developed). I’d say that’s pretty cheap for network logging flow meters.
This time the code is relatively simple, but with one quirk: we’re using interrupts. The code that adafruit provided uses the hardware registers on the arduino to set the interrupts but we use a simpler method, with the ESP you can simply say attachInterrupt(pin#, function, type). In that statement the pin# is which pin should be used for sensing, function is the function that will be executed when the interrupt is detected, and type is whether it is on a low-to-high transition, high-to-low, or either. With that, a local counter variable, and some math to convert to liters you can see just how many liters have flowed past the meter since you last checked. This is a rather basic implementation to show the concept with just 2 MQTT commands to read the flow out, in a final version I would add an RTC to send updates periodically, another command to zero the count, and auto-zeroing on perhaps a daily basis after the data has been sent and acknowledged as recorded.
As should be expected the code is here.
The rest of this series can be gotten through from the home page here.