Here's my code in case you're interested:
//
// Use this sensor to measure volume and flow of your house watermeter.
// You need to set the correct pulsefactor of your meter (pulses per m3).
// The sensor starts by fetching current volume reading from gateway (VAR 1).
// Reports both volume and flow back to gateway.
//
// Unfortunately millis() won't increment when the Arduino is in
// sleepmode. So we cannot make this sensor sleep if we also want
// to calculate/report flow.
//
// Sensor on pin 3
#include <MySensor.h>
#include <SPI.h>
#define DIGITAL_INPUT_SENSOR 3 // The digital input you attached your sensor. (Only 2 and 3 generates interrupt!)
//#define PULSE_FACTOR 12500 // Nummber of blinks per m3 of your meter (One rotation/liter)
#define PULSE_FACTOR 288000
#define SLEEP_MODE false // flowvalue can only be reported when sleep mode is false.
#define MAX_FLOW 25 // Max flow (l/min) value to report. This filetrs outliers.
#define INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
#define CHILD_ID 0 // Id of the sensor child
#define RELAY_1 4 // Arduino Digital I/O pin number for first relay
#define NUMBER_OF_RELAYS 1
#define RELAY_ON 1
#define RELAY_OFF 0
unsigned long SEND_FREQUENCY = 10000; // Minimum time between send (in miliseconds). We don't want to spam the gateway.
MySensor gw;
double ppl = ((double)PULSE_FACTOR)/1000; // Pulses per liter
volatile unsigned long pulseCount = 0;
volatile unsigned long lastBlink = 0;
volatile double flow = 0;
boolean pcReceived = false;
unsigned long oldPulseCount = 0;
unsigned long newBlink = 0;
double oldflow = 0;
double volume;
double oldvolume;
unsigned long lastSend;
unsigned long lastPulse;
unsigned long currentTime;
boolean metric;
MyMessage flowMsg(CHILD_ID,V_FLOW);
MyMessage volumeMsg(CHILD_ID,V_VOLUME);
MyMessage pcMsg(CHILD_ID,V_VAR1);
void setup()
{
//gw.begin(incomingMessage, AUTO, true);
//gw.begin(incomingMessage, AUTO, false, AUTO, RF24_PA_LOW);
gw.begin(incomingMessage, AUTO, false, AUTO);
// gw.send(pcMsg.set(0));
// gw.send(volumeMsg.set(0.000, 3));
//Water meter setup
//Serial.print("PPL:");
//Serial.print(ppl);
// Send the sketch version information to the gateway and Controller
gw.sendSketchInfo("Water Meter and Valve", "1.0");
// Register this device as Waterflow sensor
gw.present(CHILD_ID, S_WATER);
// Fetch last known pulse count value from gw
gw.request(CHILD_ID, V_VAR1);
//pulseCount = oldPulseCount = 0;
//Serial.print("Last pulse count from gw:");
//Serial.println(pulseCount);
attachInterrupt(INTERRUPT, onPulse, RISING);
lastSend = millis();
//RELAY SETUP
for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
// Register all sensors to gw (they will be created as child devices)
gw.present(sensor, S_LIGHT);
// Then set relay pins in output mode
pinMode(pin, OUTPUT);
// Set relay to last known state (using eeprom storage)
digitalWrite(pin, gw.loadState(sensor)?RELAY_ON:RELAY_OFF);
}
}
void loop()
{
gw.process();
currentTime = millis();
bool sendTime = currentTime - lastSend > SEND_FREQUENCY;
if (pcReceived && (SLEEP_MODE || sendTime)) {
// New flow value has been calculated
if (!SLEEP_MODE && flow != oldflow) {
// Check that we dont get unresonable large flow value.
// could hapen when long wraps or false interrupt triggered
if (flow<((unsigned long)MAX_FLOW)) {
gw.send(flowMsg.set(flow, 2)); // Send flow value to gw
}
//Serial.print("g/min:");
// Serial.println(flow);
oldflow = flow;
}
// No Pulse count in 2min
if(currentTime - lastPulse > 20000){
flow = 0;
}
// Pulse count has changed
if (pulseCount != oldPulseCount) {
gw.send(pcMsg.set(pulseCount)); // Send volumevalue to gw VAR1
double volume = ((double)pulseCount/((double)PULSE_FACTOR)*264.172);
//double volume = ((double)pulseCount/((double)PULSE_FACTOR));
oldPulseCount = pulseCount;
if (volume != oldvolume) {
gw.send(volumeMsg.set(volume, 3)); // Send volume value to gw
oldvolume = volume;
}
}
lastSend = currentTime;
}
else if (sendTime) {
// No count received. Try requesting it again
gw.request(CHILD_ID, V_VAR1);
lastSend=currentTime;
}
if (SLEEP_MODE) {
gw.sleep(SEND_FREQUENCY);
}
}
void onPulse()
{
if (!SLEEP_MODE) {
unsigned long newBlink = micros();
unsigned long interval = newBlink-lastBlink;
lastPulse = millis();
if (interval < 2080) { // Sometimes we get interrupt on RISING, 500000 = 0.5sek debounce ( max 120 l/min) WAS 2080
return;
}
flow = ((60000000.0 /interval) / ppl)*.264172;
//flow = ((60000000.0 /interval) / ppl);
// Serial.print("interval:");
// Serial.println(interval);
lastBlink = newBlink;
// Serial.println(flow, 4);
}
pulseCount++;
}
void incomingMessage(const MyMessage &message) {
if (message.type==V_VAR1) {
pulseCount = oldPulseCount = message.getLong();
Serial.print("Received last pulse count from gw:");
Serial.println(pulseCount);
pcReceived = true;
}
if (message.type==V_LIGHT) {
// Change relay state
digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
// Store state in eeprom
gw.saveState(message.sensor, message.getBool());
// Write some debug info
Serial.print("Incoming change for sensor:");
Serial.print(message.sensor);
Serial.print(", New status: ");
Serial.println(message.getBool());
}
}