[OpenTRV-dev] its alive....

Bo Herrmannsen EMAIL ADDRESS HIDDEN
Sat May 18 17:45:50 BST 2013


almost got the water meter now...

just need to figure how many pulses pr liter water and how to make it reset
to 0 after each time it has reported in a value...

complete sketch in case attachments are not allowed:



/*
  emonTX water meter Example

  Part of the openenergymonitor.org project
  Licence: GNU GPL V3

  Authors: Glyn Hudson, Trystan Lea
  Builds upon JeeLabs RF12 library and Arduino

  THIS SKETCH REQUIRES:

  Libraries in the standard arduino libraries folder:
    - JeeLib        https://github.com/jcw/jeelib

*/

#define freq RF12_868MHZ                                                //
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 = 2;                                                  //
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

const int time_between_readings= 5000;

#include <JeeLib.h>                                                     //
Download JeeLib: http://github.com/jcw/jeelib
#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 water; int battery; } Payload;
Payload emontx;

long pulseCount = 0;
unsigned long pulseTime,lastTime; // Used to measure time between pulses
double water;
int ppl = 1;                     // pulses per liter - figured by filling a
10 liter bucket and divide by 10.


void setup() {
  Serial.begin(9600);
  Serial.println("emonTX water meter example");
  Serial.println("OpenEnergyMonitor.org");
  Serial.print("Node: ");
  Serial.print(nodeID);
  Serial.print(" Freq: ");
  if (freq == RF12_433MHZ) Serial.print("433Mhz");
  if (freq == RF12_868MHZ) Serial.print("868Mhz");
  if (freq == RF12_915MHZ) Serial.print("915Mhz");
  Serial.print(" Network: ");
  Serial.println(networkGroup);

  // pulse detection interrupt (emontx pulse channel - IRQ0 D3)
  attachInterrupt(1, onPulse, FALLING);

  rf12_initialize(nodeID, 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);

}

void loop()
{
  emontx.water=water;
  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);
  Sleepy::loseSomeTime(time_between_readings);


  Serial.println(pulseCount * ppl);  // watt hour elapsed


}

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;
}

// The interrupt routine - runs each time a falling edge of a pulse is
detected
void onPulse()
{
  lastTime = pulseTime;
  pulseTime = micros();
  pulseCount++;                                               // count
pulse
  water = int((3600000000.0 / (pulseTime - lastTime))/ppl);  // calculate
power
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.opentrv.org.uk/pipermail/opentrv-dev/attachments/20130518/47ff1378/attachment-0001.html>


More information about the OpenTRV-dev mailing list