[OpenTRV-dev] water monitoring

Sat May 24 13:08:46 BST 2014

I tried to make a sketch that should count pulses...

pin numbers are not correct... i just want to see if my logic is correct... 
my water meter gives 1 pulse for each liter

the line:  rf12_control(0xC040); 
// set low-battery level to 2.2V i.s.o. 3.1V

is alo wrong, if not mistaken the RFM69 can go down to 1.8, but if its just 
a warning threshold then i guess 2.2 is a reasonble number to warn in good 

if powered from 5V it might never happen, but i have keept battery power in 
mind, just in case peoples meter are not near a mains socket

also we need to figure how RFM69CW should be connected to AVR... can we just 
copy of how OEM connected the RFM12 and use that?


#define RF_freq RF12_433MHZ                                             // 
Frequency of RF12B module can be RF12_433MHZ, RF12_868MHZ or RF12_915MHZ. 
You should use the one matching the module you have.
const int nodeID = 18;                                                  // 
emonTx temperature RFM12B node ID - should be unique on network
const int networkGroup = 210;                                           // 
emonTx RFM12B wireless network group - needs to be same as emonBase and 

#define RF69_COMPAT 0 // set to 1 to use RFM69CW
#include <JeeLib.h>   // make sure V12 (latest) is used if using RFM69CW
#include <avr/sleep.h>
ISR(WDT_vect) { Sleepy::watchdogEvent(); }                              // 
Attached JeeLib sleep function to Atmega328 watchdog -enables MCU to be put 
into sleep mode inbetween readings to reduce power consumption

typedef struct {
          int flow;
        int pulse;
      int battery;
} Payload;
Payload emontx;

const int LEDpin = 9; 
//LED connected to Pin 9. Used to flash when transmitting

// Pulse counting settings
long pulseCount = 0;                                                    // 
Number of pulses.
unsigned long pulseTime,lastTime;                                       // 
Used to measure flowrate.
double flow, elapsedl;                                                  // 
flow and liter
int ppl = 1;                                                            // 
pulses pr. liter.

long debouncing_time = 1000; //Debouncing Time in Milliseconds
volatile unsigned long last_micros;

void setup() {
  Serial.println("OpenTRV Pulse counting Example");
  Serial.print("Node: ");
  Serial.print(" Freq: ");
  if (RF_freq == RF12_433MHZ) Serial.print("433Mhz");
  if (RF_freq == RF12_868MHZ) Serial.print("868Mhz");
  if (RF_freq == RF12_915MHZ) Serial.print("915Mhz");
  Serial.print(" Network: ");

  rf12_initialize(nodeID, RF_freq, networkGroup); 
// initialize RFM12B
  rf12_control(0xC040);                                                 // 
set low-battery level to 2.2V i.s.o. 3.1V

  pinMode(LEDpin, OUTPUT);                                              // 
Setup indicator LED
  digitalWrite(LEDpin, LOW);

attachInterrupt(1, debounceInterrupt, RISING);                          // 
interrupt attached to pin3


void loop()

  emontx.pulse=pulseCount; pulseCount=0;
  // if ready to send + exit loop if it gets stuck as it seems too
  int i = 0; while (!rf12_canSend() && i<10) {rf12_recvDone(); i++;}
  rf12_sendStart(0, &emontx, sizeof emontx);
  // set the sync mode to 2 if the fuses are still the Arduino default
  // mode 3 (full powerdown) can only be used with 258 CK startup fuses
  digitalWrite(LEDpin, HIGH); delay(2); digitalWrite(LEDpin, LOW);      // 
flash LED

long readVcc() {
  long result;
  ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
  while (bit_is_set(ADCSRA,ADSC));
  result = ADCL;
  result |= ADCH<<8;
  result = 1126400L / result;
  return result;

void debounceInterrupt() {
  if((long)(micros() - last_micros) >= debouncing_time * 1000) {
    last_micros = micros();

void onPulse()
  lastTime = pulseTime;        //used to measure time between pulses.
  pulseTime = micros();
  emontx.flow = int((3600000000.0 / (pulseTime - lastTime))/ppl); 
//Calculate flowrate in Liters pr. hour

More information about the OpenTRV-dev mailing list