[OpenTRV-dev] water monitoring

Bo Herrmannsen EMAIL ADDRESS HIDDEN
Sat May 24 13:50:23 BST 2014


hmm... of course our OpenTRV board can be used without mods...

but then i looked at the sketch'es from OEM and the one i made and it came 
to mind that the OEM ones rely on the RFM being connected to the right 
pins... as they are not defined in the sketch i started to look in the 
jeelib files and found this in RF69_avr.h

#else // ATmega168, ATmega328, etc.

// #define RFM_IRQ     2
#define SS_DDR      DDRB
#define SS_PORT     PORTB
#define SS_BIT      2     // for PORTB: 2 = d.10, 1 = d.9, 0 = d.8

#define SPI_SS      2     // PB0
#define SPI_MOSI    3     // PB1
#define SPI_MISO    4     // PB2
#define SPI_SCK     5     // PB3

static void spiConfigPins () {
    SS_PORT |= _BV(SS_BIT);
    SS_DDR |= _BV(SS_BIT);
    PORTB |= _BV(SPI_SS);
    DDRB |= _BV(SPI_SS) | _BV(SPI_MOSI) | _BV(SPI_SCK);
}


But the SCK line on OEM boards are not connected to PB3.... where do i find 
where it expects the what pin on the RFM69CW are connected to pins on the 
AVR... ??



-----Oprindelig meddelelse----- 
From: Bo Herrmannsen
Sent: Saturday, May 24, 2014 2:08 PM
To: Closed list for developer discussions
Subject: water monitoring

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