13
13
#include < MySensor.h>
14
14
15
15
#define DIGITAL_INPUT_SENSOR 3 // The digital input you attached your sensor. (Only 2 and 3 generates interrupt!)
16
+ #define SENSOR_INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
17
+
16
18
#define PULSE_FACTOR 1000 // Nummber of blinks per m3 of your meter (One rotation/liter)
19
+
17
20
#define SLEEP_MODE false // flowvalue can only be reported when sleep mode is false.
18
- #define MAX_FLOW 40 // Max flow (l/min) value to report. This filetrs outliers.
19
- #define INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
20
- #define CHILD_ID 5 // Id of the sensor child
21
+
22
+ #define MAX_FLOW 40 // Max flow (l/min) value to report. This filters outliers.
23
+
24
+ #define CHILD_ID 1 // Id of the sensor child
25
+
21
26
unsigned long SEND_FREQUENCY = 20000 ; // Minimum time between send (in milliseconds). We don't want to spam the gateway.
22
27
23
28
MySensor gw;
24
-
29
+ MyMessage flowMsg (CHILD_ID,V_FLOW);
30
+ MyMessage volumeMsg (CHILD_ID,V_VOLUME);
31
+ MyMessage lastCounterMsg (CHILD_ID,V_VAR1);
32
+
25
33
double ppl = ((double )PULSE_FACTOR)/1000 ; // Pulses per liter
26
34
27
35
volatile unsigned long pulseCount = 0 ;
@@ -31,108 +39,119 @@ boolean pcReceived = false;
31
39
unsigned long oldPulseCount = 0 ;
32
40
unsigned long newBlink = 0 ;
33
41
double oldflow = 0 ;
34
- double volume;
35
- double oldvolume;
36
- unsigned long lastSend;
37
- unsigned long lastPulse;
38
- unsigned long currentTime;
39
- boolean metric;
40
- MyMessage flowMsg (CHILD_ID,V_FLOW);
41
- MyMessage volumeMsg (CHILD_ID,V_VOLUME);
42
- MyMessage pcMsg (CHILD_ID,V_VAR1);
43
-
42
+ double volume =0 ;
43
+ double oldvolume =0 ;
44
+ unsigned long lastSend =0 ;
45
+ unsigned long lastPulse =0 ;
44
46
45
47
void setup ()
46
48
{
47
49
gw.begin (incomingMessage);
48
50
49
51
// Send the sketch version information to the gateway and Controller
50
- gw.sendSketchInfo (" Water Meter" , " 1.0 " );
52
+ gw.sendSketchInfo (" Water Meter" , " 1.2 " );
51
53
52
54
// Register this device as Waterflow sensor
53
55
gw.present (CHILD_ID, S_WATER);
54
56
57
+ pulseCount = oldPulseCount = 0 ;
58
+
55
59
// Fetch last known pulse count value from gw
56
60
gw.request (CHILD_ID, V_VAR1);
57
61
58
- attachInterrupt (INTERRUPT, onPulse, RISING);
59
- lastSend = millis ();
62
+ lastSend = lastPulse = millis ();
63
+
64
+ attachInterrupt (SENSOR_INTERRUPT, onPulse, RISING);
60
65
}
61
66
62
67
63
68
void loop ()
64
69
{
65
70
gw.process ();
66
- currentTime = millis ();
71
+ unsigned long currentTime = millis ();
67
72
68
73
// Only send values at a maximum frequency or woken up from sleep
69
- bool sendTime = currentTime - lastSend > SEND_FREQUENCY;
70
- if (pcReceived && (SLEEP_MODE || sendTime)) {
71
- // New flow value has been calculated
74
+ if (SLEEP_MODE || (currentTime - lastSend > SEND_FREQUENCY))
75
+ {
76
+ lastSend=currentTime;
77
+
78
+ if (!pcReceived) {
79
+ // Last Pulsecount not yet received from controller, request it again
80
+ gw.request (CHILD_ID, V_VAR1);
81
+ return ;
82
+ }
83
+
72
84
if (!SLEEP_MODE && flow != oldflow) {
85
+ oldflow = flow;
86
+
87
+ Serial.print (" l/min:" );
88
+ Serial.println (flow);
89
+
73
90
// Check that we dont get unresonable large flow value.
74
91
// could hapen when long wraps or false interrupt triggered
75
92
if (flow<((unsigned long )MAX_FLOW)) {
76
93
gw.send (flowMsg.set (flow, 2 )); // Send flow value to gw
77
94
}
78
- // Serial.print("l/min:");
79
- // Serial.println(flow);
80
- oldflow = flow;
81
95
}
82
96
83
- // No Pulse count in 2min
97
+ // No Pulse count received in 2min
84
98
if (currentTime - lastPulse > 120000 ){
85
99
flow = 0 ;
86
100
}
87
-
88
-
101
+
89
102
// Pulse count has changed
90
103
if (pulseCount != oldPulseCount) {
91
- gw.send (pcMsg.set (pulseCount)); // Send volumevalue to gw VAR1
92
- double volume = ((double )pulseCount/((double )PULSE_FACTOR));
93
104
oldPulseCount = pulseCount;
105
+
106
+ Serial.print (" pulsecount:" );
107
+ Serial.println (pulseCount);
108
+
109
+ gw.send (lastCounterMsg.set (pulseCount)); // Send pulsecount value to gw in VAR1
110
+
111
+ double volume = ((double )pulseCount/((double )PULSE_FACTOR));
94
112
if (volume != oldvolume) {
95
- gw.send (volumeMsg.set (volume, 3 )); // Send volume value to gw
96
113
oldvolume = volume;
114
+
115
+ Serial.print (" volume:" );
116
+ Serial.println (volume, 3 );
117
+
118
+ gw.send (volumeMsg.set (volume, 3 )); // Send volume value to gw
97
119
}
98
120
}
99
- lastSend = currentTime;
100
- } else if (sendTime && !pcReceived) {
101
- // No count received. Try requesting it again
102
- gw.request (CHILD_ID, V_VAR1);
103
- lastSend=currentTime;
104
121
}
105
-
106
122
if (SLEEP_MODE) {
107
123
gw.sleep (SEND_FREQUENCY);
108
124
}
109
125
}
110
126
111
127
void incomingMessage (const MyMessage &message) {
112
- if (message.type ==V_VAR1) {
113
- pulseCount = oldPulseCount = message.getLong ();
128
+ if (message.type ==V_VAR1) {
129
+ unsigned long gwPulseCount=message.getULong ();
130
+ pulseCount += gwPulseCount;
114
131
Serial.print (" Received last pulse count from gw:" );
115
132
Serial.println (pulseCount);
116
133
pcReceived = true ;
117
134
}
118
135
}
119
136
120
-
121
137
void onPulse ()
122
- {
123
- if (!SLEEP_MODE) {
138
+ {
139
+ if (!SLEEP_MODE)
140
+ {
124
141
unsigned long newBlink = micros ();
125
142
unsigned long interval = newBlink-lastBlink;
126
- lastPulse = millis ();
127
- if (interval<500000L ) {
128
- // Sometimes we get interrupt on RISING, 500000 = 0.5sek debounce ( max 120 l/min)
129
- return ;
143
+
144
+ if (interval!=0 )
145
+ {
146
+ lastPulse = millis ();
147
+ if (interval<500000L ) {
148
+ // Sometimes we get interrupt on RISING, 500000 = 0.5sek debounce ( max 120 l/min)
149
+ return ;
150
+ }
151
+ flow = (60000000.0 /interval) / ppl;
130
152
}
131
- flow = (60000000.0 /interval) / ppl;
132
153
lastBlink = newBlink;
133
154
}
134
155
pulseCount++;
135
156
}
136
157
137
-
138
-
0 commit comments