49 #define DIGITAL_INPUT_SENSOR 3 // The digital input you attached your sensor. (Only 2 and 3 generates interrupt!)
51 #define PULSE_FACTOR 1000 // Number of blinks per m3 of your meter (One rotation/liter)
53 #define SLEEP_MODE false // flowvalue can only be reported when sleep mode is false.
55 #define MAX_FLOW 40 // Max flow (l/min) value to report. This filters outliers.
57 #define CHILD_ID 1 // Id of the sensor child
59 uint32_t SEND_FREQUENCY =
64 MyMessage lastCounterMsg(CHILD_ID,V_VAR1);
66 double ppl = ((double)PULSE_FACTOR)/1000;
68 volatile uint32_t pulseCount = 0;
69 volatile uint32_t lastBlink = 0;
70 volatile double flow = 0;
71 bool pcReceived =
false;
72 uint32_t oldPulseCount = 0;
76 uint32_t lastPulse =0;
78 #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
79 #define IRQ_HANDLER_ATTR ICACHE_RAM_ATTR
81 #define IRQ_HANDLER_ATTR
87 uint32_t newBlink = micros();
88 uint32_t interval = newBlink-lastBlink;
92 if (interval<500000L) {
96 flow = (60000000.0 /interval) / ppl;
106 pinMode(DIGITAL_INPUT_SENSOR, INPUT_PULLUP);
108 pulseCount = oldPulseCount = 0;
113 lastSend = lastPulse = millis();
115 attachInterrupt(digitalPinToInterrupt(DIGITAL_INPUT_SENSOR), onPulse, FALLING);
129 uint32_t currentTime = millis();
132 if (SLEEP_MODE || (currentTime - lastSend > SEND_FREQUENCY)) {
133 lastSend=currentTime;
141 if (!SLEEP_MODE && flow != oldflow) {
144 Serial.print(
"l/min:");
145 Serial.println(flow);
149 if (flow<((uint32_t)MAX_FLOW)) {
155 if(currentTime - lastPulse > 120000) {
160 if ((pulseCount != oldPulseCount)||(!SLEEP_MODE)) {
161 oldPulseCount = pulseCount;
163 Serial.print(
"pulsecount:");
164 Serial.println(pulseCount);
166 send(lastCounterMsg.
set(pulseCount));
168 double volume = ((double)pulseCount/((
double)PULSE_FACTOR));
169 if ((volume != oldvolume)||(!SLEEP_MODE)) {
172 Serial.print(
"volume:");
173 Serial.println(volume, 3);
175 send(volumeMsg.
set(volume, 3));
180 sleep(SEND_FREQUENCY,
false);
186 if (message.
getType()==V_VAR1) {
187 uint32_t gwPulseCount=message.
getULong();
188 pulseCount += gwPulseCount;
190 Serial.print(
"Received last pulse count from gw:");
191 Serial.println(pulseCount);