2020 *
2121 * REVISION HISTORY
2222 * Version 1.0 - Henrik Ekblad
23+ * Version 1.1 - GizMoCuz
2324 *
2425 * DESCRIPTION
2526 * Use this sensor to measure volume and flow of your house watermeter.
3334 * http://www.mysensors.org/build/pulse_water
3435 */
3536
36- #include < SPI.h>
3737#include < MySensor.h>
38+ #include < SPI.h>
3839
3940#define DIGITAL_INPUT_SENSOR 3 // The digital input you attached your sensor. (Only 2 and 3 generates interrupt!)
4041#define SENSOR_INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
4748
4849#define CHILD_ID 1 // Id of the sensor child
4950
50- unsigned long SEND_FREQUENCY = 20000 ; // Minimum time between send (in milliseconds). We don't want to spam the gateway.
51+ unsigned long SEND_FREQUENCY = 30000 ; // Minimum time between send (in milliseconds). We don't want to spam the gateway.
5152
5253MySensor gw;
5354MyMessage flowMsg (CHILD_ID,V_FLOW);
@@ -72,8 +73,11 @@ void setup()
7273{
7374 gw.begin (incomingMessage);
7475
76+ // initialize our digital pins internal pullup resistor so one pulse switches from high to low (less distortion)
77+ pinMode (DIGITAL_INPUT_SENSOR, INPUT_PULLUP);
78+
7579 // Send the sketch version information to the gateway and Controller
76- gw.sendSketchInfo (" Water Meter" , " 1.2 " );
80+ gw.sendSketchInfo (" Water Meter" , " 1.1 " );
7781
7882 // Register this device as Waterflow sensor
7983 gw.present (CHILD_ID, S_WATER);
@@ -85,15 +89,15 @@ void setup()
8589
8690 lastSend = lastPulse = millis ();
8791
88- attachInterrupt (SENSOR_INTERRUPT, onPulse, RISING );
92+ attachInterrupt (SENSOR_INTERRUPT, onPulse, FALLING );
8993}
9094
9195
9296void loop ()
9397{
9498 gw.process ();
9599 unsigned long currentTime = millis ();
96-
100+
97101 // Only send values at a maximum frequency or woken up from sleep
98102 if (SLEEP_MODE || (currentTime - lastSend > SEND_FREQUENCY))
99103 {
@@ -124,7 +128,7 @@ void loop()
124128 }
125129
126130 // Pulse count has changed
127- if (pulseCount != oldPulseCount) {
131+ if (( pulseCount != oldPulseCount)||(!SLEEP_MODE) ) {
128132 oldPulseCount = pulseCount;
129133
130134 Serial.print (" pulsecount:" );
@@ -133,7 +137,7 @@ void loop()
133137 gw.send (lastCounterMsg.set (pulseCount)); // Send pulsecount value to gw in VAR1
134138
135139 double volume = ((double )pulseCount/((double )PULSE_FACTOR));
136- if (volume != oldvolume) {
140+ if (( volume != oldvolume)||(!SLEEP_MODE) ) {
137141 oldvolume = volume;
138142
139143 Serial.print (" volume:" );
@@ -152,6 +156,7 @@ void incomingMessage(const MyMessage &message) {
152156 if (message.type ==V_VAR1) {
153157 unsigned long gwPulseCount=message.getULong ();
154158 pulseCount += gwPulseCount;
159+ flow=oldflow=0 ;
155160 Serial.print (" Received last pulse count from gw:" );
156161 Serial.println (pulseCount);
157162 pcReceived = true ;
@@ -178,4 +183,3 @@ void onPulse()
178183 }
179184 pulseCount++;
180185}
181-
0 commit comments