[SOLVED] Problem w/ PulseCounting and/or Sync for my flow sensor monitoring

Hello all, my system goal and setup are as follow:
Goal: Monitor how much liquid is being poured out of my kegerator via Blynk, with the ability to re-sync values if board is disconnected as when I am away from home I want to turn the board off because I would obviously not be around to drink anything.

Setup: Flow sensor placed in the liquid line and connected to Arduino

My issue is with counting of the keg quantity. See below the results I am getting when I am using terminal print to debug, full code at the bottom of this post.

RESULTS:

Board On.
Using Gauge I set my Keg1 Quantity to 5
Pour some liquid terminal print results:
PulseCount: 311
totalGallons: 0.00
Keg1Quantity: 4.99
Turn board OFF
Turn board ON
PulseCount: 0
totalGallons: 0.00
Keg1Quantity: 0

Pour some more liquid
PulseCount: 27
totalGallons: 0.00
Keg1Quantity: 4.99

Turn board OFF
Turn board ON
PulseCount: 0
totalGallons: 0.00
Keg1Quantity: 0
Pour some more liquid
PulseCount: 27
totalGallons: 0.00
Keg1Quantity: 4.00

So to interpret those results, The pulseCounting seems to be working as the number of pulses did coordinate broadly with how much liquid I was pouring. The calculation of gallons used and its impact on quantity seemed accurate (32528 pulses = 1 gallon based on manufacturer documentation) so 300 pulses should be a .01 change in keg Quantity since it is tracked in gallons.

Issues are:
Everytime I reconnect it seems that the Sync is not working as everytime Sync runs it spits out all 0’s for the variables. However even though the sync isnt working when I go to pour again it picks up the right values which is confusing to me.

Final issue is what i showed last, when I turn board back on the kegQuantity jumps way down in a way that is not tied to pulses being counted.

This feels like a syncing issue or maybe in the backend it is adding all pulses and then syncs that large cumulative pulse number which causes a large jump down in quantity. I dont see where my sync code is not accurate though. I have had this large jump down in quantity happen even when board was not turned off and back on, so it seems like maybe my counting code is wrong.

Widget Setup:

Full Code:

#define BLYNK_DEBUG // Optional, this enables lots of prints
#define BLYNK_PRINT Serial
//All pin settings
#define tempHum_pin D2 //Humidity & top temp location
const int fans = D3;
byte sensorInterrupt1 = D5;
byte sensorPin1       = D5;
byte sensorInterrupt2 = D6;
byte sensorPin2       = D6;
byte sensorInterrupt3 = D7;
byte sensorPin3       = D7;

////Sensors Initialization
#include "DHT.h"
#define DHTTYPE DHT11   // DHT 11
DHT dht(tempHum_pin, DHTTYPE);
//#include <PlainTMP0x.h>
//PlainTMP0x TMP;
const uint32_t _interval = 2000;
int32_t _now, _lastTime;

//////////////////////BLYNK CODE
//#define BLYNK_PRINT Serial 
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
bool isFirstConnect = true;
#include <SimpleTimer.h>
SimpleTimer timer;
const char* ssid = "XXXX";
const char* password = "XXXX";
char auth[] = "XXXX";
WidgetTerminal terminal(V13);
//float bottomTemp;
float topTemp;
float humidity;
float emailTime;
float keg1Quantity;
static char viewKeg1[4];
float keg2Quantity;
static char viewKeg2[4];
float keg3Quantity;
static char viewKeg3[4];
float pulseCount1;
float totalGallons1;
float pulseCount2;
float totalGallons2;
float pulseCount3;
float totalGallons3;
int resetKegGal;
// This function will run every time Blynk connection is established
BLYNK_CONNECTED() {
  if (isFirstConnect) {
    //Blynk.syncAll();
    Blynk.syncVirtual(V0);
    Blynk.syncVirtual(V1);
    Blynk.syncVirtual(V2);
    terminal.println(keg1Quantity);
    terminal.flush();
    terminal.println(totalGallons1);
    terminal.flush();
    terminal.println(pulseCount1);
    terminal.flush();
    isFirstConnect = false;  
  }
}

//Reset Keg Volume
BLYNK_WRITE(V15) {
  switch (param.asInt())
  {
    case 1: // Item 1
      keg1Quantity = 5;
      Blynk.virtualWrite(V0, keg1Quantity);
      break;
    case 2: // Item 2
      keg2Quantity = 5;
      Blynk.virtualWrite(V1, keg2Quantity); 
      break;
    case 3: // Item 3
      keg3Quantity = 5;
      Blynk.virtualWrite(V2, keg3Quantity);
      break;
    case 4: // Item 1
      keg1Quantity = 2.5;
      Blynk.virtualWrite(V0, keg1Quantity);
      break;
    case 5: // Item 2
      keg2Quantity = 2.5;
      Blynk.virtualWrite(V1, keg2Quantity); 
      break;
    case 6: // Item 3
      keg3Quantity = 2.5;
      Blynk.virtualWrite(V2, keg3Quantity);
      break;
    case 7: // Item 1
      keg1Quantity = 1;
      Blynk.virtualWrite(V0, keg1Quantity);
      break;
    case 8: // Item 2
      keg2Quantity = 1;
      Blynk.virtualWrite(V1, keg2Quantity); 
      break;
    case 9: // Item 3
      keg3Quantity = 1;
      Blynk.virtualWrite(V2, keg3Quantity);
      break;
    case 10: // Item 1
      keg1Quantity = 2;
      Blynk.virtualWrite(V0, keg1Quantity);
      break;
    case 11: // Item 2
      keg2Quantity = 2;
      Blynk.virtualWrite(V1, keg2Quantity); 
      break;
    case 12: // Item 3
      keg3Quantity = 2;
      Blynk.virtualWrite(V2, keg3Quantity);
      break;
    case 13: // Item 1
      keg1Quantity = 3;
      Blynk.virtualWrite(V0, keg1Quantity);
      break;
    case 14: // Item 2
      keg2Quantity = 3;
      Blynk.virtualWrite(V1, keg2Quantity); 
      break;
    case 15: // Item 3
      keg3Quantity = 3;
      Blynk.virtualWrite(V2, keg3Quantity);
      break;
    case 16: // Item 1
      keg1Quantity = 4;
      Blynk.virtualWrite(V0, keg1Quantity);
      break;
    case 17: // Item 2
      keg2Quantity = 4;
      Blynk.virtualWrite(V1, keg2Quantity); 
      break;
    case 18: // Item 3
      keg3Quantity = 4;
      Blynk.virtualWrite(V2, keg3Quantity);
      break;
  }
}


// Get Keg Quantity Values From Server
BLYNK_WRITE(V0)
{
  keg1Quantity = param.asInt();
}

BLYNK_WRITE(V1)
{
  keg2Quantity = param.asInt();
}

BLYNK_WRITE(V2)
{
  keg3Quantity = param.asInt();
}

void sendValues() {
  Blynk.virtualWrite(V6, keg1Quantity);
  Blynk.virtualWrite(V7, keg2Quantity);
  Blynk.virtualWrite(V8, keg3Quantity);
  Blynk.virtualWrite(V9, topTemp);
}

void tempHum(){
  humidity = dht.readHumidity();
  topTemp = dht.readTemperature(true);
  Blynk.virtualWrite(V11,topTemp);
  Blynk.virtualWrite(V4,humidity);
  
  //do {
    //_now = millis();
  //} while ((_now - _lastTime) < _interval);
  //_lastTime = _now;
  //* run measurement */
  //bottomTemp = TMP.Temperature();
  //Blynk.virtualWrite(V3,bottomTemp);
 //if ((abs(bottomTemp - topTemp))> 4) { //controlling keezer fans
  //digitalWrite(fans,HIGH);
  //}
  //else {
  //digitalWrite(fans,LOW);
  //}
 
 if (humidity>90 && ((millis()-emailTime)>86400000)) {
  Blynk.email("phillipmurphy47@gmail.com", "Keezer Issue", "There is high humidity in the keezer, dry out condensation trap");
  emailTime = millis();
  }
}


void keg1(){

if (pulseCount1 >0){ 
    detachInterrupt(sensorInterrupt1);
    terminal.println(pulseCount1);
    terminal.flush();
    totalGallons1=(pulseCount1/32528);
    terminal.println(totalGallons1);
    terminal.flush();
    keg1Quantity = keg1Quantity - totalGallons1;
    terminal.println(keg1Quantity);
    terminal.flush();
    dtostrf(keg1Quantity,4, 2, viewKeg1);
    Blynk.virtualWrite(V0,viewKeg1);
    Serial.println(keg1Quantity);
    Serial.println(pulseCount1);
    pulseCount1 = 0;
    terminal.println(pulseCount1);
    attachInterrupt(sensorInterrupt1, pulseCounter1, FALLING);
    }
 }

void pulseCounter1(){
  pulseCount1++;
}

void keg2(){
if (pulseCount2 >0) {
    detachInterrupt(sensorInterrupt2);
    totalGallons2 = (pulseCount2/32528);
    keg2Quantity = keg2Quantity - totalGallons2;
    dtostrf(keg2Quantity,4, 2, viewKeg2);
    Blynk.virtualWrite(V1,viewKeg2);
    pulseCount2 = 0;
    attachInterrupt(sensorInterrupt2, pulseCounter2, FALLING);
    }
  }

void pulseCounter2(){
  pulseCount2++;
}

void keg3(){
if (pulseCount3 > 0){
    detachInterrupt(sensorInterrupt3);
    totalGallons3 = (pulseCount3/32528);
    keg3Quantity = keg3Quantity - totalGallons3;
    dtostrf(keg3Quantity,4, 2, viewKeg3);
    Blynk.virtualWrite(V2,viewKeg3);
    pulseCount3 = 0;
    attachInterrupt(sensorInterrupt3, pulseCounter3, FALLING);
    }
  }

void pulseCounter3(){
  pulseCount3++;
}

void myTimerEvent()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, millis() / 1000);
}


void setup() 
{
  Serial.begin(115200);
  Blynk.begin(auth,ssid,password);
  timer.setInterval(5000, tempHum);
  timer.setInterval(5000, keg1);
  timer.setInterval(5000, keg2);
  timer.setInterval(5000, keg3);
  timer.setInterval(5000, sendValues);
  timer.setInterval(500, myTimerEvent);
  //TMP.InitializeTMP(TMP_MOD_TMP04,&PORTD, PIND1 , TMP_UNI_FAHRENHEIT, 8);
  _lastTime = millis();
  dht.begin();
  pinMode(sensorPin1, INPUT);
  digitalWrite(sensorPin1, HIGH);
  attachInterrupt(sensorInterrupt1, pulseCounter1, FALLING);
  pinMode(sensorPin2, INPUT);
  digitalWrite(sensorPin2, HIGH);
  attachInterrupt(sensorInterrupt2, pulseCounter2, FALLING);
  pinMode(sensorPin3, INPUT);
  digitalWrite(sensorPin3, HIGH);
  attachInterrupt(sensorInterrupt3, pulseCounter3, FALLING);

}

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

There seems to be variable types mismatch in your sketch. Use param.asFloat() to convert string to float number.

Good point, I made the update but I am still getting the same issues of all 0 values when I sync, and large seemingly random drops in keg quantity.

This might be a cause. Try to send floats directly: Blynk.virtualWrite(V0, keg1Quantity)
This will make sure the value will be converted to float correctly when syncing from server.
And in the the app you can specify value formatting right in Gauge widget (e.g. /pin.##/).

p.s. I don’t think posting auth token here is a good idea.

That seemed to work, I will continue to try and trigger a problem but I havent run into one yet. Was the character type transition causing a syncing problem because it was expecting a float?

Anyway thanks a ton, I would probably not have noticed that one.

Which flow sensor are you using??
I have written instructables on similar topic it will be helpful to you

1 Like

This is the one I am using: http://www.alliedelec.com/gems-sensors-inc-173935-c/70461147/?mkwid=sKH2wmCMM&pcrid=30980760979&gclid=CjwKEAjw6e_IBRDvorfv2Ku79jMSJAAuiv9YC58RBqBwh0EtfnLSGpw64MnlNxbgtjuRChsvPOGQshoCCbjw_wcB

GEMS Sensor