[OpenTRV-dev] water monitoring

Bo Herrmannsen EMAIL ADDRESS HIDDEN
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 
time...

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?


Sketch:
-------------

#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 
emonGLCD

#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.begin(9600);
  Serial.println("OpenTRV Pulse counting Example");
  Serial.println("opentrv.org.uk");
  Serial.print("Node: ");
  Serial.print(nodeID);
  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: ");
  Serial.println(networkGroup);



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


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


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

}

void loop()
{

  emontx.pulse=pulseCount; pulseCount=0;
  emontx.battery=readVcc();
  rf12_sleep(RF12_WAKEUP);
  // 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
  rf12_sendWait(2);
  rf12_sleep(RF12_SLEEP);
  digitalWrite(LEDpin, HIGH); delay(2); digitalWrite(LEDpin, LOW);      // 
flash LED
  Sleepy::loseSomeTime;
}

long readVcc() {
  long result;
  ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
  delay(2);
  ADCSRA |= _BV(ADSC);
  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) {
    onPulse();
    last_micros = micros();
  }
}

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



More information about the OpenTRV-dev mailing list