48 #define DIGITAL_INPUT_SENSOR 3 // The digital input you attached your sensor. (Only 2 and 3 generates interrupt!)
50 #define PULSE_FACTOR 1000 // Number of blinks per m3 of your meter (One rotation/liter)
52 #define SLEEP_MODE false // flowvalue can only be reported when sleep mode is false.
54 #define MAX_FLOW 40 // Max flow (l/min) value to report. This filters outliers.
56 #define CHILD_ID 1 // Id of the sensor child
58 uint32_t SEND_FREQUENCY =
63 MyMessage lastCounterMsg(CHILD_ID,V_VAR1);
65 double ppl = ((double)PULSE_FACTOR)/1000;
67 volatile uint32_t pulseCount = 0;
68 volatile uint32_t lastBlink = 0;
69 volatile double flow = 0;
70 bool pcReceived =
false;
71 uint32_t oldPulseCount = 0;
75 uint32_t lastPulse =0;
77 #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
78 #define IRQ_HANDLER_ATTR ICACHE_RAM_ATTR
80 #define IRQ_HANDLER_ATTR
86 uint32_t newBlink = micros();
87 uint32_t interval = newBlink-lastBlink;
91 if (interval<500000L) {
95 flow = (60000000.0 /interval) / ppl;
105 pinMode(DIGITAL_INPUT_SENSOR, INPUT_PULLUP);
107 pulseCount = oldPulseCount = 0;
112 lastSend = lastPulse = millis();
114 attachInterrupt(digitalPinToInterrupt(DIGITAL_INPUT_SENSOR), onPulse, FALLING);
128 uint32_t currentTime = millis();
131 if (SLEEP_MODE || (currentTime - lastSend > SEND_FREQUENCY)) {
132 lastSend=currentTime;
140 if (!SLEEP_MODE && flow != oldflow) {
143 Serial.print(
"l/min:");
144 Serial.println(flow);
148 if (flow<((uint32_t)MAX_FLOW)) {
154 if(currentTime - lastPulse > 120000) {
159 if ((pulseCount != oldPulseCount)||(!SLEEP_MODE)) {
160 oldPulseCount = pulseCount;
162 Serial.print(
"pulsecount:");
163 Serial.println(pulseCount);
165 send(lastCounterMsg.
set(pulseCount));
167 double volume = ((double)pulseCount/((
double)PULSE_FACTOR));
168 if ((volume != oldvolume)||(!SLEEP_MODE)) {
171 Serial.print(
"volume:");
172 Serial.println(volume, 3);
174 send(volumeMsg.
set(volume, 3));
179 sleep(SEND_FREQUENCY,
false);
185 if (message.
getType()==V_VAR1) {
186 uint32_t gwPulseCount=message.
getULong();
187 pulseCount += gwPulseCount;
189 Serial.print(
"Received last pulse count from gw:");
190 Serial.println(pulseCount);