Navigation

    • Register
    • Login
    • OpenHardware.io
    • Categories
    • Recent
    • Tags
    • Popular
    1. Home
    2. bart59
    • Profile
    • Following
    • Followers
    • Topics
    • Posts
    • Best
    • Groups

    bart59

    @bart59

    8
    Reputation
    10
    Posts
    518
    Profile views
    2
    Followers
    0
    Following
    Joined Last Online

    bart59 Follow

    Best posts made by bart59

    • RE: 💬 Water Meter Pulse Sensor

      I wanted to operate my water pulse meter on batteries and also get the water flow. The original design had the following issues with that:

      • Incorrect flow calc: micros() was used to calculate the flow, however micros() wraps every 70 minutes which looks like a huge flow (which is then discarded in code)
      • Volume calc: millis() wraps every 50 days which is not handled correctly either
      • Too much current for battery use: The IR LED of the TCRT5000 is always on and the LM393 comparator is also taking a few mA's
      • Could not report flow in sleep mode because millis() does not increment on sleep - need to do this based on calculation of total sleep time. We now simply calculate the number of pulses per minute and deduct the flow
      • I also had issued with the data transport reliability, so I added error counters (which show up on the Gateway as distance sensors)
      • I also wanted to provide a measurement counter to the gateway (that counts up each time a message is sent)
      • The sensor will reboot itself when too many errors occur

      So I modified the circuit of the IR sensor:

      • Assumption that the wheel of the water meter turns slowly (takes at least a few seconds to turn around)
      • We will wake up every 500 millisecond to turn on the IR LED connected to PIN 8. Pin 8 also powers the photo transistor that measures the reflection
      • I removed the power from the opamp circuit that is linked to the photo transistor
      • The voltage from the photo transistor is then read using an analog read on A1. Based on a threshold value we will deduct if the mirror on the water meter is in view
      • Pin 7 is connected to a learning switch which will turn the device in a specific mode and the min/max values on A1 are used to calculate the value of the threshold (which is then stored in the EEPROM)
      • After 30 seconds in learning mode, the new threshold is established and the LED on Pin 6 will show the actual on/off mirror signals, so you can see the pulses are correctly counted
      • switch back the DIP switch on Pin 7 to bring back normal mode
      • The circuit also contains the battery voltage sensor circuit (I am using a 1.5V battery and step up circuit). So the resistors used are 470k from + pole of battery to the A0 input and 1 M ohm from A0 to ground
      
      /**
      
       * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
       * Copyright (C) 2013-2015 Sensnology AB
       * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
       *
       * Documentation: http://www.mysensors.org
       * Support Forum: http://forum.mysensors.org
       *
       * This program is free software; you can redistribute it and/or
       * modify it under the terms of the GNU General Public License
       * version 2 as published by the Free Software Foundation.
       *
       *******************************
       *
       * REVISION HISTORY
       * Version 1.0 - Henrik Ekblad
       * Version 1.1 - GizMoCuz
       * Version 1.2 - changed BM: using low power separate circuit for infra red on pin 8 + analog A1
       * 
       * ISSUES WITH ORIGINAL CODE
       * Incorrect flow calc: micros() was used to calculate the flow, however micros() is wraps every 70 minutes which looks like a huge flow (which is discarded)
       * Volume calc: millis() wraps every 50 days which is not handled correctly
       * Too much current for battery use: The IR LED of the TCRT5000 is always on and the LM393 comparator is also taking a few mA's
       * Could not report flow in sleep mode because millis() does not increment on sleep - need to do this based on calculation of total sleep time
       * 
       * MODIFIED CIRCUIT IR SENSOR
       * Assumption that the wheel of the water meter turns slowly (takes at least a few seconds to turn around)
       * We will wake up every second to turn on the IR LED (connected to PIN 8). Pin 8 also powers the photo transistor that measures the reflection
       * The voltage from the photo transistor is then read using an analog read on A1. Based on a treshold value we will deduct if the mirror is in view
       * Pin 7 is connected to a learning switch which will turn the device in continous mode and the min/max values on A1 are used to recalc the treshold
       * during a 30 second period. After this period the new treshold is established and the LED on Pin 6 will show the actual on/off mirror signals
       *
       * http://www.mysensors.org/build/pulse_water
       */
      
      // BOARD: PRO MINI 3.3V/ 8Mhz ATMEGA328 8Mhz
      
      // Enable debug prints to serial monitor
      #define MY_DEBUG 
      
      // Enable and select radio type attached
      #define MY_RADIO_NRF24
      //#define MY_RADIO_RFM69
      
      #define MY_NODE_ID 10                 // hard code the node number
      #include <SPI.h>
      #include <MySensors.h>  
      
      #define SENSOR_POWER 8                // pin that will provide power to IR LED + sense circuit
      #define IR_SENSE_PIN  A1              // input for IR voltage
      #define BATTERY_SENSE_PIN  A0         // select the input pin for the battery sense point
      #define LEARN_SWITCH_PIN 7            // switch (SW1 on battery module) to turn on learning mode (low==on)
      #define LEARN_LED_PIN 6               // LED feedback during learning mode (LED on battery module)
      #define LEARN_TIME 30                 // number of seconds we will keep learn loop
      
      #define PULSE_FACTOR 1000             // Nummber of blinks per m3 of your meter (One rotation/1 liter)
      #define MAX_FLOW 80                   // Max flow (l/min) value to report. This filters outliers.
      #define CHILD_ID 1                    // Id of the sensor child (contains 3 subs: V_FLOW, V_VOLUME, VAR1)
      #define CHILD_PINGID 2                // ID of ping counter
      #define CHILD_ERRID 3                 // ID of error counter
      
      #define CHECK_FREQUENCY 500           // time in milliseconds between loop (where we check the sensor) - 500ms   
      #define MIN_SEND_FREQ 60              // Minimum time between send (in multiplies of CHECK_FREQUENCY). We don't want to spam the gateway (30 seconds)
      #define MAX_SEND_FREQ 1200            // Maximum time between send (in multiplies of CHECK_FREQUENCY). We need to show we are alive (600 sec/10 min)
      #define IR_ON_SETTLE 2                // number of milliseconds after we turned on the IR LED and we assume the receive signal is stable (in ms)
      #define EE_TRESHOLD 10                // config addresses 0 + 1 used for treshold from learning (loadState() returns only uint8 value)
      #define TRESHOLD_MARGIN 3             // additional margin before we actually see a one or zero
      #define RESETMIN 5                    // number of cycle times (either 30 sec of 10 min) we consistently need to have transmission errors before we perform hard reset
      
      MyMessage volumeMsg(CHILD_ID,V_VOLUME); // display volume and flow on the same CHILD_ID
      MyMessage flowMsg(CHILD_ID,V_FLOW); // flow
      MyMessage lastCounterMsg(CHILD_ID,V_VAR1);
      MyMessage pingMsg(CHILD_PINGID,V_DISTANCE); // use distance to keep track of changing value
      MyMessage errMsg(CHILD_ERRID,V_DISTANCE); // use distance to keep track of changing value
      
      
      double ppl = ((double)PULSE_FACTOR / 1000.0);    // Pulses per liter
      unsigned int oldBatteryPcnt = 0;          // check if changed
      unsigned int minsendcnt = MIN_SEND_FREQ;  // counter for keeping minimum intervals between sending
      unsigned int maxsendcnt = MAX_SEND_FREQ;  // counter for keeping maximum intervals between sending 
      unsigned int treshold = 512;              // threshold value when to swap on/off for pulse
      unsigned long pulseCount = 0;             // total volume of this pulse meter (value stored/received on gateway on pcReceived)
      unsigned long oldPulseCount = 0;          // to see if we have received something
      boolean pcReceived = false;               // received volume from prior reboot
      boolean onoff = false;                    // sensor value above/below treshold 
      unsigned int intervalcnt = 0;             // number of cycles between last period (for flow calculation)
      double flow = 0;                          // maintain flow
      double oldflow = 0;                       // keep prior flow (only send on change)
      unsigned int learntime=LEARN_TIME*2;      // timer for learning period
      unsigned int learnlow = 1023;             // lowest value found during learning
      unsigned int learnhigh = 0;               // highest value found during learning
      boolean learnsaved = false;               // have saved learned value
      unsigned long pingcnt = 0;
      unsigned long errcnt = 0;                 // error count
      unsigned int errcnt2 = 0;                 // error counter set to 0 when sending is ok
      
      void(* resetFunc) (void) = 0;//declare reset function at address 0 (for rebooting the Arduino)
      
      void setup() {    
        // make sure a few vars have the right init value after software reboot
        pingcnt = 0;
        pcReceived = false;
        pulseCount = oldPulseCount = 0;
        // setup hardware
        pinMode(SENSOR_POWER, OUTPUT); 
        digitalWrite(SENSOR_POWER, LOW);
        pinMode(LEARN_SWITCH_PIN, INPUT_PULLUP);
        pinMode(LEARN_LED_PIN, INPUT);      // default is input because this pin also has SW2 of battery block
      
        // Fetch last known pulse count value from gateway
        request(CHILD_ID, V_VAR1);
      
        // Fetch threshold value from EE prom
        treshold = readEeprom(EE_TRESHOLD);
        if (treshold<30 || treshold>1000) treshold = 512;   // wrong value in EEprom, take default
        Serial.print("Treshold: ");
        Serial.println(treshold);
              
        // use the 1.1 V internal reference for the battery and IR sensor
      #if defined(__AVR_ATmega2560__)
         analogReference(INTERNAL1V1);
      #else
         analogReference(INTERNAL);
      #endif
        analogRead(IR_SENSE_PIN); // settle analogreference value
        wait(CHECK_FREQUENCY); // wait a bit
      }
      
      void presentation()  {
        // Send the sketch version information to the gateway and Controller
        sendSketchInfo("Water Meter", "1.2");
      
        // Register this device as Waterflow sensor
        present(CHILD_ID, S_WATER);      
        present(CHILD_PINGID, S_DISTANCE); 
        present(CHILD_ERRID, S_DISTANCE);
      }
      
      void loop() {
        if (digitalRead(LEARN_SWITCH_PIN)==LOW) {
          pinMode(LEARN_LED_PIN, OUTPUT);
          digitalWrite(SENSOR_POWER, HIGH);
          intervalcnt = 0;
          learn_loop();
        } else {
          learntime=LEARN_TIME*2;
          learnlow = 1023;
          learnhigh = 0;
          pinMode(LEARN_LED_PIN, INPUT);
          normal_loop();
        }
      }
      
      void learn_loop() {
        // will run into this loop as long as we are learning
        wait(500);
        unsigned int sensorValue = analogRead(IR_SENSE_PIN);
        Serial.print("IR: ");
        Serial.print(sensorValue);
        if (learntime>0) {
          // still learning
          learntime--;
          learnsaved = false;    
          digitalWrite(LEARN_LED_PIN, !digitalRead(LEARN_LED_PIN));  // blink led
          if (sensorValue < learnlow) {
            learnlow = sensorValue;
            Serial.println(" Lowest");
          } else if (sensorValue > learnhigh) {
            learnhigh = sensorValue;
            Serial.println(" Highest");
          } else Serial.println();
        } else {
          if (!learnsaved) {
            treshold = (learnhigh + learnlow)/2;
            Serial.print("Treshold: ");
            Serial.println(treshold);
            storeEeprom(EE_TRESHOLD, treshold);
          }
          learnsaved = true;
          // just display using LED
          digitalWrite(LEARN_LED_PIN, sensorValue>treshold);
          Serial.println((sensorValue>treshold ? " on" : " off"));
        }
      }
      
      void normal_loop() { 
        unsigned long start_loop = millis();    // to allow adjusting wait time
        intervalcnt++;
        // we start doing a measurement
        digitalWrite(SENSOR_POWER, HIGH);
        wait(IR_ON_SETTLE); 
        unsigned int sensorValue = analogRead(IR_SENSE_PIN);
        digitalWrite(SENSOR_POWER, LOW); 
        #ifdef MY_DEBUG_DETAIL
        Serial.print("IR: ");
        Serial.println(sensorValue);
        #endif
        boolean nowvalue = onoff;
        if (onoff && (sensorValue<treshold-TRESHOLD_MARGIN)) nowvalue = false;
        if (!onoff && (sensorValue>treshold+TRESHOLD_MARGIN)) nowvalue = true;
        if (nowvalue != onoff) {
          // we have a pulse, only count on upwards pulse
          onoff = nowvalue;
          if (onoff) {
            pulseCount++;
            #ifdef MY_DEBUG
            Serial.print("p: ");
            Serial.println(pulseCount);
            #endif
          }
        }
      
      // Only send values at a maximum frequency or woken up from sleep
        if (minsendcnt>0) minsendcnt--;
        if (maxsendcnt>0) maxsendcnt--;
        // send minimum interval when we have pulse changes or if we had some flow the prior time or send on timeout
        if ((minsendcnt==0 && (pulseCount != oldPulseCount)) || (minsendcnt==0 && oldflow != 0) || maxsendcnt==0) {
          if (!pcReceived) {   //Last Pulsecount not yet received from controller, request it again
            Serial.print("Re-request var1 ..");
            request(CHILD_ID, V_VAR1);
      // Prevent flooding the gateway with re-requests,,, wait at least 1000 ms for gateway (cannot be sleep or smartSleep
            wait(2*CHECK_FREQUENCY); 
            return;
          }
          minsendcnt = MIN_SEND_FREQ;
          maxsendcnt = MAX_SEND_FREQ;
          pingcnt++;
      
          sensorValue = analogRead(BATTERY_SENSE_PIN);
          int batteryPcnt = sensorValue / 10;
          // 1M, 470K divider across battery and using internal ADC ref of 1.1V
          // Sense point is bypassed with 0.1 uF cap to reduce noise at that point
          // ((1e6+470e3)/1e6)*1.1 = Vmax = 1.67 Volts
          // 1.67/1023 = Volts per bit = 0.00158065
      
          Serial.print("Battery %: ");
          Serial.println(batteryPcnt);
      
          if (oldBatteryPcnt != batteryPcnt) {
            sendBatteryLevel(batteryPcnt);
            oldBatteryPcnt = batteryPcnt;
          }
          double volume = ((double)pulseCount/((double)PULSE_FACTOR));      
          flow = ((double) (pulseCount-oldPulseCount)) * (60000.0 / ((double) intervalcnt*(double) CHECK_FREQUENCY)) / ppl;  // flow in liter/min
      
          #ifdef MY_DEBUG
          Serial.print("pulsecount:");
          Serial.println(pulseCount);
          Serial.print("volume:");
          Serial.println(volume, 3);
          Serial.print("l/min:");
          Serial.println(flow);
          #endif
             
          bool b = send(lastCounterMsg.set(pulseCount));  // Send  pulsecount value to gw in VAR1
          if (b) errcnt2=0; else { errcnt++; errcnt2++; }
          b = send(volumeMsg.set(volume, 3));               // Send volume (set function 2nd argument is resolution)
          if (b) errcnt2=0; else { errcnt++; errcnt2++; }
          b = send(flowMsg.set(flow, 2));                   // Send flow value to gw
          if (b) errcnt2=0; else { errcnt++; errcnt2++; }
          b = send(pingMsg.set(pingcnt));                   // ensure at least this var has a different value
          if (b) errcnt2=0; else { errcnt++; errcnt2++; }
          b = send(errMsg.set(errcnt2+((float) errcnt2/100),2));    // ensure we always send error count
          if (b) errcnt2=0; else { errcnt++; errcnt2++; }
          oldPulseCount = pulseCount;
          intervalcnt = 0;
          oldflow = flow; 
          if (errcnt2>= (5*RESETMIN)) {
            Serial.println("Reset");
            wait(300);
            resetFunc(); //call reset to reboot the Arduino
          }
        }
      // calculate how long it took to process all of this. then go to sleep for the remaining period
        unsigned long end_loop = millis();
        if (end_loop - start_loop < CHECK_FREQUENCY)
          sleep(CHECK_FREQUENCY - (end_loop > start_loop ? end_loop - start_loop : 0));
      }
      
      void receive(const MyMessage &message) {
        if (message.type==V_VAR1) {
          unsigned long gwPulseCount=message.getULong();
          pulseCount += gwPulseCount;
          oldPulseCount += gwPulseCount;
          flow=oldflow=0;
          Serial.print("Received last pulse count from gw:");
          Serial.println(pulseCount);
          pcReceived = true;
        }
      }
      
      
      void storeEeprom(int pos, int value) {
          // function for saving the values to the internal EEPROM
          // value = the value to be stored (as int)
          // pos = the first byte position to store the value in
          // only two bytes can be stored with this function (max 32.767)
          saveState(pos, ((unsigned int)value >> 8 ));
          pos++;
          saveState(pos, (value & 0xff));
      }
      
      int readEeprom(int pos) {
          // function for reading the values from the internal EEPROM
          // pos = the first byte position to read the value from 
          int hiByte;
          int loByte;
          hiByte = loadState(pos) << 8;
          pos++;
          loByte = loadState(pos);
          return (hiByte | loByte);
      }
      
      posted in Announcements
      bart59
      bart59
    • RE: 💬 Water Meter Pulse Sensor

      @mfalkvidd - I did measure the average current consumption at the time, but I not remember the exact value. I believe it was well below the 0.5 mA. The sensor has been up and running on the same single 1.5V AA battery for 30 days now and the batt percentage still shows 93%.

      posted in Announcements
      bart59
      bart59
    • RE: 💬 Water Meter Pulse Sensor

      @dpcreel. I would be surprised the Arduino could not handle the HMC5883L magnetometer from Sparkfun. The I2C protocol is natively supported and you only need to analyse numbers coming from the 3-Axis to decide when the wheels inside the gas meter turn. I think the position of the sensor will be very critical, but the code should not be too complex. Arduino's are typically limited in handling much data (there is only 2000 bytes of RAM in the ATmega328), but your code should not need much RAM space. The program size can be 32 KB which should be enough.

      If you want to go for the PI, there are I2C libraries out there you could use and I would then bypass the Mysensors gateway alltogether and connect an ethernet cable to the PI and use the MQTT protocol to talk to your home controller.

      regards
      Bart

      posted in Announcements
      bart59
      bart59
    • RE: 💬 Water Meter Pulse Sensor

      Hi Aram

      The original code from the mysensors site does not handle your situation very well (indeed because the switch staying on or off for a long time). You can use my code (see my post further up this discussion). In my case I have a pulse every 1 liter.

      In your case you only have 1 pulse every 10 liters, which means you have to take a much longer period to calculate the flow correctly. Basically you have to set MIN_SEND_FREQ to a higher value. The flow is calculated based on the number of pulses in a given time period. Example: with 6 l/min you have to calculate this value only once every 5 minutes (=30 liter = 3 pulses from your Siemens meter) instead of every 30 seconds as I do. So if MIN_SEND_FREQ = 600 (every 5 minutes) your flow is calculated as:

      flow = ((double) (pulseCount-oldPulseCount)) * (60000.0 / ((double) intervalcnt*(double) CHECK_FREQUENCY)) / ppl;

      In the example above (pulseCount-oldPulseCount) = 3 pulses
      ppl = 0.1
      intervalcnt = MIN_SEND_FREQ = 600
      CHECK_FREQUENCY = 500
      ==> flow is 6 l/min

      regards

      Bart

      posted in Announcements
      bart59
      bart59
    • RE: 💬 Water Meter Pulse Sensor

      @mrc-core
      The spurious interrups are caused by the open line. The Arduino has high impedance inputs and anything from a WiFi signal or the signal from a close by circuit can cause the value to swing between 0 and 3 Volt, triggering your input. See https://www.arduino.cc/en/Tutorial/DigitalPins

      On your high value in the graph: Domoticz water sensors require the sensor to post the total water volume. This means that the sensor needs to "know" the total of volumes from all the past measurements. In fact all the (wrong) past pulses you generated with the open input are stored by the sensor. There are 2 ways this storage mechanism can be realized in Arduino code:

      (1) keep the value in the Arduino EEPROM and
      (2) use a build in feature of Domoticz to store values on behalf of sensors.

      Mysesnsors uses method 2 as method (1) has 2 disadvantages: The code needs to continuously write to the EEPROM, because you never know when the Arduino will be rebooted. The EEPROM on the Arduino chip does not allow you to write to it that often (max 100,000 times or so) and the EEPROM is cleared when you update the software. The sketch used by MySensors uses method (2) by sending the pulsecount to the gateway using send(lastCounterMsg.set(pulseCount)) statement at the same time the volume is sent to Domoticz with send(volumeMsg.set(volume, 3)); At each reboot the sensor requests the value of VAR1 from the gateway using request(CHILD_ID, V_VAR1); The sensor does not send any data to the gateway until the gateway has received the counter past value. The receive() function is called by the Mysensor library when data comes back from the gateway. There the pulseCount += gwPulseCount statement adds the value to the pulseCount.

      This means the sensor will not be able to "forget" the pulses that came from the period your digital input was not connected.

      There are 3 ways to fix this problem:

      • you can delete your sensor at the Domoticz side (throwing away all history): First power down your Arduino sensor, the go into Domoticz screen and remove the sensor from the Utility screen and then remove the sensor from the Setup->Hardware->MysensorsGW->Setup->Click on your water meter-> delete all Children-> delete the water meter sensor itself. Then power up your Arduino and you will see the sensor show up again.
      • as above: remove your sensor from the Utilities tab and change the code to give your sensor another new ID. At the top of your code you can put a statement like
      #define MY_NODE_ID 10
      

      This will be the ID number that shows in the hardware setup screen in Domoticz for your sensor. If you omit this code MySensors will create a new (unique) code for you. However in my system I prefer to allocate the numbers myself to each sensor. So a quick way to create a brand new sensor history in Domoticz is to simply change the MY_NODE_ID number (change from 10 to 11).

      • change yuor Arduino code to force a 0 in the VAR1 holding variable for your sensor. You can do that by adding a statement send(lastCounterMsg.set(0)); inside your loop() function (best place is here):
      // Only send values at a maximum frequency or woken up from sleep
         if (SLEEP_MODE || (currentTime - lastSend > SEND_FREQUENCY)) {
             lastSend=currentTime;
      
            send(lastCounterMsg.set(0));  // <--- keep here shortly and remove later
      
             if (!pcReceived) {
                 //Last Pulsecount not yet received from controller, request it again
                 request(CHILD_ID, V_VAR1);
                 return;
             }
      

      You will compile and upload this code to your Arduino and let it run for a minute or so, then remove the line and upload the orginal code. You should now be able to remove the data point in Domoticz (by using ctrl-mouseclick on the graphic item)

      Hope this helps

      Bart

      posted in Announcements
      bart59
      bart59

    Latest posts made by bart59

    • RE: 💬 Water Meter Pulse Sensor

      @smilvert I am afraid your water meter does not work. On many other watermeters the black spinning wheel is somewhat bigger and has a small mirror attached. The light from the IR LED is reflected by the mirror and the IR sensor picks up the reflection as a pulse every rotation (in your case 0.5 L). I suggest you buy another water meter that has a sensor build in and provides you with a direct signal. In my home I have separate water meters for each water consumer (such shower, hot water, kitchen, etc.).

      I am using a Honeywell C7195A2001B which has a hall sensor that pulses at 7 Hz per liter/min, so you will get 420 pulses per liter. These meters are typically used in boiler or central heating systems that also can provide hot water for your shower. You need to modify the source code a bit to handle the high pulse frequency, but the nice thing is that the measurement is very accurate.

      regards

      Bart

      posted in Announcements
      bart59
      bart59
    • RE: 💬 Water Meter Pulse Sensor

      @mrc-core You are right. This is the wrong type of water meter. I have been using 2 other brands watermeters: the Honeywell C7195x2001B (see http://ecc.emea.honeywell.com/downloads/EN2R9029.PDF) and the Caleffi series 316 (http://www.arbo.it/images/techsheets/316405.pdf). You local central heating installation guy may have them as they are used in boiler systems of central heating for the warm water supply. They are also sold on eBay. Both work more or less the same and can be operated on 5V. The number of pulses per liter are much higher (something like 500 pulses per liter), so they are more accurate. You will need to modify your code to work with this. Below the code of my sensor (which also measures multiple temperatures at the same Arduino chip):

      /**
       * The MySensors Arduino library handles the wireless radio link and protocol
       * between your home built sensors/actuators and HA controller of choice.
       * The sensors forms a self healing radio network with optional repeaters. Each
       * repeater and gateway builds a routing tables in EEPROM which keeps track of the
       * network topology allowing messages to be routed to nodes.
       *
       * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
       * Copyright (C) 2013-2015 Sensnology AB
       * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
       *
       * Documentation: http://www.mysensors.org
       * Support Forum: http://forum.mysensors.org
       *
       * This program is free software; you can redistribute it and/or
       * modify it under the terms of the GNU General Public License
       * version 2 as published by the Free Software Foundation.
       * 
       * (c) 2017 Bart M - last edits: 15 May 2017
       * 
       * Note: using timer0 to generate a second interrupt for our 1 ms counters. timer0 is still also  used for delay()
       *
       * Functions WTW Flow and Temperature
       * 
       * WTW flow
       *  - Flow of shower cold water
       *  - Flow of boiler cold water entry
       *  
       * Temperature 
       * - Temperature of outgoing water (Dallas with cable)
       * - Temperature of shower head (Dallas)
       * 
       * Flow meter using Honeywell C7195A2001B which has a hall sensor that pulses at 7 Hz per liter/min
       * we count only upgoing pulses, so in our case we get: 
       *   7 pulses per second at 1 l/min
       *   420 pulses per liter
       *   420000 pulses per m3
       * Flow meter Caleffi 316 which has a hall sensor that pulses at 8.8 Hz per liter/min (according to specs)
       * In reality the frequency is about 30% lower in my case, so we have to correct this with a factor of 1.3.
       *   8.8 pulses per second at 1 l/min
       *   528 pulses per liter
       *   528000 pulses per m3
       *   528000/1.3 = 406154
       * 
       * If we use 250 m3 per year we come to 105 x 10^6 pulses/year
       *`this is stored in an unsigned long that can hold 4295 x 10^6 -> volume overflow after 41 years
       *
       *****************************************************************************************************  */
      
      // BOARD: PRO MINI 5V V/ 16Mhz ATMEGA328 16Mhz
      
      // type of flow meter
      #define CALEFFI
      // #define HONEYWELL
      
      // Enable debug prints to serial monitor
      #define MY_DEBUG 1
      
      // Enable and select radio type attached
      #define MY_RADIO_NRF24
      // No repeater 
      
      // node ID's
      #define MY_NODE_ID 27             // start naming my own nodes at number (set in comments if you want automatic)
      #define FLOW_ID 1                 // flow meter 1 (start here if more)
      #define NFLOWS 2                  // number of flow meters  
      #define TEMP_ID 3                 // Temperature (start here if there are more linked)
      
      #include <SPI.h>
      #include <MySensors.h>  
      #include <DallasTemperature.h>
      #include <OneWire.h>
      
      // PIN connections where the flow meters are connected. We have 2 flow meters, so our array has 2 entries
      uint8_t FlowPins[NFLOWS] = {2, 3};
      #define TEMP_PIN 4                // temp sensor connected to pin4
      
      #ifdef CALEFFI
      // #define PULSE_FACTOR 528000        // Number of blinks per m3 of your meter Caleffi (from data sheet)
      #define PULSE_FACTOR 406154        // Nummber of blinks per m3 of your meter Caleffi (measured in my installation)
      #else
      #define PULSE_FACTOR 420000        // Nummber of blinks per m3 of your meter Honeywell
      #endif
      
      // configs
      #define MAXFLOWERROR 50             // maximum number of l/min that we accept
      
      // delay times
      #define CHECK_FREQUENCY 1000         // time in milliseconds between loop (where we check the sensor) - 1000ms
      #define MIN_SEND_FREQ 10             // Minimum time between send (in multiplies of CHECK_FREQUENCY). We don't want to spam the gateway (10 seconds)
      #define MIN_FLOW_SEND_FREQ 20        // Minimum time between send (in multiplies of CHECK_FREQUENCY). We don't want to spam the gateway (30 seconds)
      #define MAX_SEND_FREQ 600            // Maximum time between send (in multiplies of CHECK_FREQUENCY). We need to show we are alive (600 sec/10 min)
      
      // one wire config
      #define ONE_WIRE_BUS TEMP_PIN
      #define MAX_ATTACHED_DS18B20 8
      
      // Motion message types
      MyMessage volumeMsg(FLOW_ID,V_VOLUME);
      MyMessage flowMsg(FLOW_ID,V_FLOW);
      MyMessage lastCounterMsg(FLOW_ID,V_VAR1);
      MyMessage tempmsg(TEMP_ID, V_TEMP);
      
      OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
      DallasTemperature sensors(&oneWire); // Pass the oneWire reference to Dallas Temperature. 
      float lastTemperature[MAX_ATTACHED_DS18B20];
      volatile unsigned int numSensors = 0;
      
      double ppl = ((double)PULSE_FACTOR / 1000.0);    // Pulses per liter
      boolean pcReceived[NFLOWS];                      // received volume from prior reboot
      double oldflow[NFLOWS];                          // keep prior flow (only send on change)
      unsigned long oldflow_cnt[NFLOWS];               // only send when changed
      
      
      // updated in ISR
      volatile unsigned int mcnt = CHECK_FREQUENCY;   // decremented in ISR at 1000Hz. Cycles at one per second to update send counters
      volatile unsigned int tempsendcnt[MAX_ATTACHED_DS18B20];  
      volatile unsigned int maxtempsendcnt[MAX_ATTACHED_DS18B20];
      volatile unsigned int flowsendcnt[NFLOWS];
      volatile unsigned int maxflowsendcnt[NFLOWS];
      volatile bool flow_status[NFLOWS];              // status of flow (on or off cycle)
      volatile unsigned long flow_cnt[NFLOWS];        // counter volume for each flow sensor
      volatile unsigned int intervalcnt[NFLOWS];      // keep track of number of milliseconds since we had previous flow info
      
      void before() { 
        for (uint8_t i=0; i<NFLOWS; i++) {
          pinMode(FlowPins[i], INPUT);
        }
        // Startup up the OneWire library
        sensors.begin();
        for (uint8_t i=0; i<NFLOWS; i++) {
          oldflow[i] = 0;
          flow_status[i] = false;
          flow_cnt[i] = 0;
          oldflow_cnt[i] = 0;
          pcReceived[i] = false;
          flowsendcnt[i] = MIN_FLOW_SEND_FREQ;
          maxflowsendcnt[i] = MAX_SEND_FREQ;
          intervalcnt[i] = 0;
        }
        for (uint8_t i=0; i<MAX_ATTACHED_DS18B20; i++) { 
           lastTemperature[i]=0;
           tempsendcnt[i] = 0;
           maxtempsendcnt[i] = MAX_SEND_FREQ;
        }
      }
      
      void setup() {
         Serial.println("setup()");
         // Timer0 is already used for millis() - we'll just interrupt somewhere
         // in the middle and call the TIMER0_COMPA_vect interrupt
         OCR0A = 0xAF;
         TIMSK0 |= _BV(OCIE0A);
         sensors.setWaitForConversion(true);
         // Fetch last known pulse count value from gw
         for (uint8_t i=0; i<NFLOWS; i++) {
           request(FLOW_ID+i, V_VAR1);
         }
      }
      
      void presentation()  {
        // Send the sketch version information to the gateway and Controller
        sendSketchInfo("WTW flow sensor", "1.2");
      
        // Register all sensors to gw (they will be created as child devices)
        numSensors = sensors.getDeviceCount();
        Serial.print("# temp sensors: ");
        Serial.println(numSensors);
        DeviceAddress add;
        for (uint8_t i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) { 
           present(TEMP_ID+i, S_TEMP);
           Serial.print(i);
           Serial.print("=");
           sensors.getAddress(add, i);
           printAddress(add);
           Serial.println();
        }
        for (uint8_t i=0; i<NFLOWS; i++) {
          present(FLOW_ID+i, S_WATER);
        }
      }
      
      // function to print a device address of a Dallas temp sensor
      void printAddress(DeviceAddress deviceAddress) {
        for (uint8_t i = 0; i < 8; i++) {
          // zero pad the address if necessary
          if (deviceAddress[i] < 16) Serial.print("0");
          Serial.print(deviceAddress[i], HEX);
        }
      }
      
      
      void loop() {
        // we come here every 1000 ms (defined in CHECK_FREQUENCY)
        
        // now handle temperature
        if (numSensors>0) {
          sensors.requestTemperatures();
          for (uint8_t i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {
            // Fetch and round temperature to one decimal
            float temperature = static_cast<float>(static_cast<int> (sensors.getTempCByIndex(i) * 10.)) / 10.;
        
            // Only send data if temperature has changed and no error
            if (((lastTemperature[i] != temperature && tempsendcnt[i]==0) || maxtempsendcnt[i]==0) && temperature != -127.00 && temperature != 85.00) {
              // Send in the new temperature
              send(tempmsg.setSensor(TEMP_ID+i).set(temperature,1));
              // Save new temperature for next compare
              lastTemperature[i]=temperature;
              tempsendcnt[i] = MIN_SEND_FREQ;
              maxtempsendcnt[i] = MAX_SEND_FREQ;
            }
          }
          for (uint8_t i=0; i<NFLOWS; i++) {
             if ((flowsendcnt[i]==0 && (oldflow_cnt[i] != flow_cnt[i])) || (flowsendcnt[i]==0 && oldflow[i] != 0) || maxflowsendcnt[i]==0) {
                if (!pcReceived[i]) {   //Last Pulsecount not yet received from controller, request it again
                   Serial.print("Re-request var1 for sensor ");
                   Serial.println(FLOW_ID+i);
                   request(FLOW_ID+i, V_VAR1);
                   wait(2*CHECK_FREQUENCY); // wait at least 1000 ms for gateway (cannot be sleep or smartSleep)
                   return;
                }
                flowsendcnt[i] = MIN_FLOW_SEND_FREQ;
                maxflowsendcnt[i] = MAX_SEND_FREQ;
                double volume = ((double)flow_cnt[i]/((double)PULSE_FACTOR));      
                double flow = (((double) (flow_cnt[i]-oldflow_cnt[i])) * ((double) 60000.0 / ((double) intervalcnt[i]))) / ppl;  // flow in liter/min
      
                Serial.print("Flow meter:");
                Serial.println(FLOW_ID+i);
                Serial.print("pulsecount:");
                Serial.println(flow_cnt[i]);
                Serial.print("volume:");
                Serial.println(volume, 3);
                Serial.print("l/min:");
                Serial.println(flow);
                intervalcnt[i] = 0;
                oldflow[i] = flow; 
                oldflow_cnt[i] = flow_cnt[i];
                send(lastCounterMsg.setSensor(FLOW_ID+i).set(flow_cnt[i]));     // Send  pulsecount value to gw in VAR1
                send(volumeMsg.setSensor(FLOW_ID+i).set(volume, 3));            // Send volume (set function 2nd argument is resolution)
                if (flow<MAXFLOWERROR) send(flowMsg.setSensor(FLOW_ID+i).set(flow, 2));                // Send flow value to gw
             }
          }
        }
      
        // Serial.print(end_loop-start_loop);
        wait(CHECK_FREQUENCY);
      }
      
      // Receive data from gateway
      void receive(const MyMessage &message) {
        for (uint8_t i=0; i<NFLOWS; i++) {
           if (message.type==V_VAR1 && message.sensor==FLOW_ID+i) {
              unsigned long gwPulseCount=message.getULong();
              flow_cnt[i] += gwPulseCount;
              oldflow_cnt[i] += gwPulseCount;
              oldflow[i] = 0;
              Serial.print("Received last pulse count for ");
              Serial.print(FLOW_ID+i);
              Serial.print(" from gw:");
              Serial.println(gwPulseCount);
              pcReceived[i] = true;
           }
        }
      }
      
      
      // Interrupt on timer0 - called as part of timer0 - already running at 1ms intervals
      SIGNAL(TIMER0_COMPA_vect) {
        if (mcnt>0) mcnt--;
        for (uint8_t i=0; i<NFLOWS; i++) {
           if (mcnt==0) {
              if (flowsendcnt[i]>0) flowsendcnt[i]--;
              if (maxflowsendcnt[i]>0) maxflowsendcnt[i]--;
           }
           intervalcnt[i]++;
           bool val = digitalRead(FlowPins[i]);
           if (val != flow_status[i]) {
              flow_status[i] = val;
              if (!val) flow_cnt[i]++;      // we increment counter on down flank
           }
        }
        if (mcnt==0) {
           mcnt = CHECK_FREQUENCY;
           for (uint8_t i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) { 
              if (tempsendcnt[i]>0) tempsendcnt[i]--;
              if (maxtempsendcnt[i]>0) maxtempsendcnt[i]--;
           }
        }
      }
      

      Good luck

      Bart

      posted in Announcements
      bart59
      bart59
    • RE: 💬 Water Meter Pulse Sensor

      @mrc-core The NACK may be bad transmission. I typically connect a 5 cm piece of wire to the antenna of the send/receive module to increase the reception of data

      posted in Announcements
      bart59
      bart59
    • RE: 💬 Water Meter Pulse Sensor

      @mrc-core Just to make sure: are you connecting/disconnecting the data pin to your sensor when the Arduino is powered off and are you sure the connection is solid? If you connect things with power on you may accidently trigger a pulse a 100 times.... which will be sent to the gateway 20 secs later. Always power off stuff first and use a solid connection (soldering is always better) when making modifications. How did you power the hall sensor? Should be from the 3.3V VCC connector of the Arduino and not from the higher voltage or something separate. Can you upload a photo of your setup to allow us to see how you connect things?

      Bart

      posted in Announcements
      bart59
      bart59
    • RE: 💬 Water Meter Pulse Sensor

      @mrc-core
      The spurious interrups are caused by the open line. The Arduino has high impedance inputs and anything from a WiFi signal or the signal from a close by circuit can cause the value to swing between 0 and 3 Volt, triggering your input. See https://www.arduino.cc/en/Tutorial/DigitalPins

      On your high value in the graph: Domoticz water sensors require the sensor to post the total water volume. This means that the sensor needs to "know" the total of volumes from all the past measurements. In fact all the (wrong) past pulses you generated with the open input are stored by the sensor. There are 2 ways this storage mechanism can be realized in Arduino code:

      (1) keep the value in the Arduino EEPROM and
      (2) use a build in feature of Domoticz to store values on behalf of sensors.

      Mysesnsors uses method 2 as method (1) has 2 disadvantages: The code needs to continuously write to the EEPROM, because you never know when the Arduino will be rebooted. The EEPROM on the Arduino chip does not allow you to write to it that often (max 100,000 times or so) and the EEPROM is cleared when you update the software. The sketch used by MySensors uses method (2) by sending the pulsecount to the gateway using send(lastCounterMsg.set(pulseCount)) statement at the same time the volume is sent to Domoticz with send(volumeMsg.set(volume, 3)); At each reboot the sensor requests the value of VAR1 from the gateway using request(CHILD_ID, V_VAR1); The sensor does not send any data to the gateway until the gateway has received the counter past value. The receive() function is called by the Mysensor library when data comes back from the gateway. There the pulseCount += gwPulseCount statement adds the value to the pulseCount.

      This means the sensor will not be able to "forget" the pulses that came from the period your digital input was not connected.

      There are 3 ways to fix this problem:

      • you can delete your sensor at the Domoticz side (throwing away all history): First power down your Arduino sensor, the go into Domoticz screen and remove the sensor from the Utility screen and then remove the sensor from the Setup->Hardware->MysensorsGW->Setup->Click on your water meter-> delete all Children-> delete the water meter sensor itself. Then power up your Arduino and you will see the sensor show up again.
      • as above: remove your sensor from the Utilities tab and change the code to give your sensor another new ID. At the top of your code you can put a statement like
      #define MY_NODE_ID 10
      

      This will be the ID number that shows in the hardware setup screen in Domoticz for your sensor. If you omit this code MySensors will create a new (unique) code for you. However in my system I prefer to allocate the numbers myself to each sensor. So a quick way to create a brand new sensor history in Domoticz is to simply change the MY_NODE_ID number (change from 10 to 11).

      • change yuor Arduino code to force a 0 in the VAR1 holding variable for your sensor. You can do that by adding a statement send(lastCounterMsg.set(0)); inside your loop() function (best place is here):
      // Only send values at a maximum frequency or woken up from sleep
         if (SLEEP_MODE || (currentTime - lastSend > SEND_FREQUENCY)) {
             lastSend=currentTime;
      
            send(lastCounterMsg.set(0));  // <--- keep here shortly and remove later
      
             if (!pcReceived) {
                 //Last Pulsecount not yet received from controller, request it again
                 request(CHILD_ID, V_VAR1);
                 return;
             }
      

      You will compile and upload this code to your Arduino and let it run for a minute or so, then remove the line and upload the orginal code. You should now be able to remove the data point in Domoticz (by using ctrl-mouseclick on the graphic item)

      Hope this helps

      Bart

      posted in Announcements
      bart59
      bart59
    • RE: 💬 Water Meter Pulse Sensor

      My code basically measures the time between two upward pulses. You can modify the code to also count the downward pulses. The net effect is that you will get a count every 5 liter (on average), but if there is no flow, the first pulse will always be off by 1-4 liter because you do not know how far the rotation is completed.

      On NAMUR: you can actually use my code here too: I use analog input A1 to measure the voltage on the infra red sensor (which varies between 0 and 1.1 Volt). During the learn mode (set with a seaprate DIP switch) the code measures the input voltage for a period of 30 seconds while you turn open the water tap (you may want to increase the timing in your case) and then calculates the average between the lowest and highest voltage as the currect point there is a 1 or 0 coming from the pump (in my case it is an IR LED that is reflecting from a mirror into a photo sensor and the position of the mirror may change - resulting in different voltages).

      regards
      Bart

      posted in Announcements
      bart59
      bart59
    • RE: 💬 Water Meter Pulse Sensor

      Hi Aram

      The original code from the mysensors site does not handle your situation very well (indeed because the switch staying on or off for a long time). You can use my code (see my post further up this discussion). In my case I have a pulse every 1 liter.

      In your case you only have 1 pulse every 10 liters, which means you have to take a much longer period to calculate the flow correctly. Basically you have to set MIN_SEND_FREQ to a higher value. The flow is calculated based on the number of pulses in a given time period. Example: with 6 l/min you have to calculate this value only once every 5 minutes (=30 liter = 3 pulses from your Siemens meter) instead of every 30 seconds as I do. So if MIN_SEND_FREQ = 600 (every 5 minutes) your flow is calculated as:

      flow = ((double) (pulseCount-oldPulseCount)) * (60000.0 / ((double) intervalcnt*(double) CHECK_FREQUENCY)) / ppl;

      In the example above (pulseCount-oldPulseCount) = 3 pulses
      ppl = 0.1
      intervalcnt = MIN_SEND_FREQ = 600
      CHECK_FREQUENCY = 500
      ==> flow is 6 l/min

      regards

      Bart

      posted in Announcements
      bart59
      bart59
    • RE: 💬 Water Meter Pulse Sensor

      @dpcreel. I would be surprised the Arduino could not handle the HMC5883L magnetometer from Sparkfun. The I2C protocol is natively supported and you only need to analyse numbers coming from the 3-Axis to decide when the wheels inside the gas meter turn. I think the position of the sensor will be very critical, but the code should not be too complex. Arduino's are typically limited in handling much data (there is only 2000 bytes of RAM in the ATmega328), but your code should not need much RAM space. The program size can be 32 KB which should be enough.

      If you want to go for the PI, there are I2C libraries out there you could use and I would then bypass the Mysensors gateway alltogether and connect an ethernet cable to the PI and use the MQTT protocol to talk to your home controller.

      regards
      Bart

      posted in Announcements
      bart59
      bart59
    • RE: 💬 Water Meter Pulse Sensor

      @mfalkvidd - I did measure the average current consumption at the time, but I not remember the exact value. I believe it was well below the 0.5 mA. The sensor has been up and running on the same single 1.5V AA battery for 30 days now and the batt percentage still shows 93%.

      posted in Announcements
      bart59
      bart59
    • RE: 💬 Water Meter Pulse Sensor

      I wanted to operate my water pulse meter on batteries and also get the water flow. The original design had the following issues with that:

      • Incorrect flow calc: micros() was used to calculate the flow, however micros() wraps every 70 minutes which looks like a huge flow (which is then discarded in code)
      • Volume calc: millis() wraps every 50 days which is not handled correctly either
      • Too much current for battery use: The IR LED of the TCRT5000 is always on and the LM393 comparator is also taking a few mA's
      • Could not report flow in sleep mode because millis() does not increment on sleep - need to do this based on calculation of total sleep time. We now simply calculate the number of pulses per minute and deduct the flow
      • I also had issued with the data transport reliability, so I added error counters (which show up on the Gateway as distance sensors)
      • I also wanted to provide a measurement counter to the gateway (that counts up each time a message is sent)
      • The sensor will reboot itself when too many errors occur

      So I modified the circuit of the IR sensor:

      • Assumption that the wheel of the water meter turns slowly (takes at least a few seconds to turn around)
      • We will wake up every 500 millisecond to turn on the IR LED connected to PIN 8. Pin 8 also powers the photo transistor that measures the reflection
      • I removed the power from the opamp circuit that is linked to the photo transistor
      • The voltage from the photo transistor is then read using an analog read on A1. Based on a threshold value we will deduct if the mirror on the water meter is in view
      • Pin 7 is connected to a learning switch which will turn the device in a specific mode and the min/max values on A1 are used to calculate the value of the threshold (which is then stored in the EEPROM)
      • After 30 seconds in learning mode, the new threshold is established and the LED on Pin 6 will show the actual on/off mirror signals, so you can see the pulses are correctly counted
      • switch back the DIP switch on Pin 7 to bring back normal mode
      • The circuit also contains the battery voltage sensor circuit (I am using a 1.5V battery and step up circuit). So the resistors used are 470k from + pole of battery to the A0 input and 1 M ohm from A0 to ground
      
      /**
      
       * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
       * Copyright (C) 2013-2015 Sensnology AB
       * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
       *
       * Documentation: http://www.mysensors.org
       * Support Forum: http://forum.mysensors.org
       *
       * This program is free software; you can redistribute it and/or
       * modify it under the terms of the GNU General Public License
       * version 2 as published by the Free Software Foundation.
       *
       *******************************
       *
       * REVISION HISTORY
       * Version 1.0 - Henrik Ekblad
       * Version 1.1 - GizMoCuz
       * Version 1.2 - changed BM: using low power separate circuit for infra red on pin 8 + analog A1
       * 
       * ISSUES WITH ORIGINAL CODE
       * Incorrect flow calc: micros() was used to calculate the flow, however micros() is wraps every 70 minutes which looks like a huge flow (which is discarded)
       * Volume calc: millis() wraps every 50 days which is not handled correctly
       * Too much current for battery use: The IR LED of the TCRT5000 is always on and the LM393 comparator is also taking a few mA's
       * Could not report flow in sleep mode because millis() does not increment on sleep - need to do this based on calculation of total sleep time
       * 
       * MODIFIED CIRCUIT IR SENSOR
       * Assumption that the wheel of the water meter turns slowly (takes at least a few seconds to turn around)
       * We will wake up every second to turn on the IR LED (connected to PIN 8). Pin 8 also powers the photo transistor that measures the reflection
       * The voltage from the photo transistor is then read using an analog read on A1. Based on a treshold value we will deduct if the mirror is in view
       * Pin 7 is connected to a learning switch which will turn the device in continous mode and the min/max values on A1 are used to recalc the treshold
       * during a 30 second period. After this period the new treshold is established and the LED on Pin 6 will show the actual on/off mirror signals
       *
       * http://www.mysensors.org/build/pulse_water
       */
      
      // BOARD: PRO MINI 3.3V/ 8Mhz ATMEGA328 8Mhz
      
      // Enable debug prints to serial monitor
      #define MY_DEBUG 
      
      // Enable and select radio type attached
      #define MY_RADIO_NRF24
      //#define MY_RADIO_RFM69
      
      #define MY_NODE_ID 10                 // hard code the node number
      #include <SPI.h>
      #include <MySensors.h>  
      
      #define SENSOR_POWER 8                // pin that will provide power to IR LED + sense circuit
      #define IR_SENSE_PIN  A1              // input for IR voltage
      #define BATTERY_SENSE_PIN  A0         // select the input pin for the battery sense point
      #define LEARN_SWITCH_PIN 7            // switch (SW1 on battery module) to turn on learning mode (low==on)
      #define LEARN_LED_PIN 6               // LED feedback during learning mode (LED on battery module)
      #define LEARN_TIME 30                 // number of seconds we will keep learn loop
      
      #define PULSE_FACTOR 1000             // Nummber of blinks per m3 of your meter (One rotation/1 liter)
      #define MAX_FLOW 80                   // Max flow (l/min) value to report. This filters outliers.
      #define CHILD_ID 1                    // Id of the sensor child (contains 3 subs: V_FLOW, V_VOLUME, VAR1)
      #define CHILD_PINGID 2                // ID of ping counter
      #define CHILD_ERRID 3                 // ID of error counter
      
      #define CHECK_FREQUENCY 500           // time in milliseconds between loop (where we check the sensor) - 500ms   
      #define MIN_SEND_FREQ 60              // Minimum time between send (in multiplies of CHECK_FREQUENCY). We don't want to spam the gateway (30 seconds)
      #define MAX_SEND_FREQ 1200            // Maximum time between send (in multiplies of CHECK_FREQUENCY). We need to show we are alive (600 sec/10 min)
      #define IR_ON_SETTLE 2                // number of milliseconds after we turned on the IR LED and we assume the receive signal is stable (in ms)
      #define EE_TRESHOLD 10                // config addresses 0 + 1 used for treshold from learning (loadState() returns only uint8 value)
      #define TRESHOLD_MARGIN 3             // additional margin before we actually see a one or zero
      #define RESETMIN 5                    // number of cycle times (either 30 sec of 10 min) we consistently need to have transmission errors before we perform hard reset
      
      MyMessage volumeMsg(CHILD_ID,V_VOLUME); // display volume and flow on the same CHILD_ID
      MyMessage flowMsg(CHILD_ID,V_FLOW); // flow
      MyMessage lastCounterMsg(CHILD_ID,V_VAR1);
      MyMessage pingMsg(CHILD_PINGID,V_DISTANCE); // use distance to keep track of changing value
      MyMessage errMsg(CHILD_ERRID,V_DISTANCE); // use distance to keep track of changing value
      
      
      double ppl = ((double)PULSE_FACTOR / 1000.0);    // Pulses per liter
      unsigned int oldBatteryPcnt = 0;          // check if changed
      unsigned int minsendcnt = MIN_SEND_FREQ;  // counter for keeping minimum intervals between sending
      unsigned int maxsendcnt = MAX_SEND_FREQ;  // counter for keeping maximum intervals between sending 
      unsigned int treshold = 512;              // threshold value when to swap on/off for pulse
      unsigned long pulseCount = 0;             // total volume of this pulse meter (value stored/received on gateway on pcReceived)
      unsigned long oldPulseCount = 0;          // to see if we have received something
      boolean pcReceived = false;               // received volume from prior reboot
      boolean onoff = false;                    // sensor value above/below treshold 
      unsigned int intervalcnt = 0;             // number of cycles between last period (for flow calculation)
      double flow = 0;                          // maintain flow
      double oldflow = 0;                       // keep prior flow (only send on change)
      unsigned int learntime=LEARN_TIME*2;      // timer for learning period
      unsigned int learnlow = 1023;             // lowest value found during learning
      unsigned int learnhigh = 0;               // highest value found during learning
      boolean learnsaved = false;               // have saved learned value
      unsigned long pingcnt = 0;
      unsigned long errcnt = 0;                 // error count
      unsigned int errcnt2 = 0;                 // error counter set to 0 when sending is ok
      
      void(* resetFunc) (void) = 0;//declare reset function at address 0 (for rebooting the Arduino)
      
      void setup() {    
        // make sure a few vars have the right init value after software reboot
        pingcnt = 0;
        pcReceived = false;
        pulseCount = oldPulseCount = 0;
        // setup hardware
        pinMode(SENSOR_POWER, OUTPUT); 
        digitalWrite(SENSOR_POWER, LOW);
        pinMode(LEARN_SWITCH_PIN, INPUT_PULLUP);
        pinMode(LEARN_LED_PIN, INPUT);      // default is input because this pin also has SW2 of battery block
      
        // Fetch last known pulse count value from gateway
        request(CHILD_ID, V_VAR1);
      
        // Fetch threshold value from EE prom
        treshold = readEeprom(EE_TRESHOLD);
        if (treshold<30 || treshold>1000) treshold = 512;   // wrong value in EEprom, take default
        Serial.print("Treshold: ");
        Serial.println(treshold);
              
        // use the 1.1 V internal reference for the battery and IR sensor
      #if defined(__AVR_ATmega2560__)
         analogReference(INTERNAL1V1);
      #else
         analogReference(INTERNAL);
      #endif
        analogRead(IR_SENSE_PIN); // settle analogreference value
        wait(CHECK_FREQUENCY); // wait a bit
      }
      
      void presentation()  {
        // Send the sketch version information to the gateway and Controller
        sendSketchInfo("Water Meter", "1.2");
      
        // Register this device as Waterflow sensor
        present(CHILD_ID, S_WATER);      
        present(CHILD_PINGID, S_DISTANCE); 
        present(CHILD_ERRID, S_DISTANCE);
      }
      
      void loop() {
        if (digitalRead(LEARN_SWITCH_PIN)==LOW) {
          pinMode(LEARN_LED_PIN, OUTPUT);
          digitalWrite(SENSOR_POWER, HIGH);
          intervalcnt = 0;
          learn_loop();
        } else {
          learntime=LEARN_TIME*2;
          learnlow = 1023;
          learnhigh = 0;
          pinMode(LEARN_LED_PIN, INPUT);
          normal_loop();
        }
      }
      
      void learn_loop() {
        // will run into this loop as long as we are learning
        wait(500);
        unsigned int sensorValue = analogRead(IR_SENSE_PIN);
        Serial.print("IR: ");
        Serial.print(sensorValue);
        if (learntime>0) {
          // still learning
          learntime--;
          learnsaved = false;    
          digitalWrite(LEARN_LED_PIN, !digitalRead(LEARN_LED_PIN));  // blink led
          if (sensorValue < learnlow) {
            learnlow = sensorValue;
            Serial.println(" Lowest");
          } else if (sensorValue > learnhigh) {
            learnhigh = sensorValue;
            Serial.println(" Highest");
          } else Serial.println();
        } else {
          if (!learnsaved) {
            treshold = (learnhigh + learnlow)/2;
            Serial.print("Treshold: ");
            Serial.println(treshold);
            storeEeprom(EE_TRESHOLD, treshold);
          }
          learnsaved = true;
          // just display using LED
          digitalWrite(LEARN_LED_PIN, sensorValue>treshold);
          Serial.println((sensorValue>treshold ? " on" : " off"));
        }
      }
      
      void normal_loop() { 
        unsigned long start_loop = millis();    // to allow adjusting wait time
        intervalcnt++;
        // we start doing a measurement
        digitalWrite(SENSOR_POWER, HIGH);
        wait(IR_ON_SETTLE); 
        unsigned int sensorValue = analogRead(IR_SENSE_PIN);
        digitalWrite(SENSOR_POWER, LOW); 
        #ifdef MY_DEBUG_DETAIL
        Serial.print("IR: ");
        Serial.println(sensorValue);
        #endif
        boolean nowvalue = onoff;
        if (onoff && (sensorValue<treshold-TRESHOLD_MARGIN)) nowvalue = false;
        if (!onoff && (sensorValue>treshold+TRESHOLD_MARGIN)) nowvalue = true;
        if (nowvalue != onoff) {
          // we have a pulse, only count on upwards pulse
          onoff = nowvalue;
          if (onoff) {
            pulseCount++;
            #ifdef MY_DEBUG
            Serial.print("p: ");
            Serial.println(pulseCount);
            #endif
          }
        }
      
      // Only send values at a maximum frequency or woken up from sleep
        if (minsendcnt>0) minsendcnt--;
        if (maxsendcnt>0) maxsendcnt--;
        // send minimum interval when we have pulse changes or if we had some flow the prior time or send on timeout
        if ((minsendcnt==0 && (pulseCount != oldPulseCount)) || (minsendcnt==0 && oldflow != 0) || maxsendcnt==0) {
          if (!pcReceived) {   //Last Pulsecount not yet received from controller, request it again
            Serial.print("Re-request var1 ..");
            request(CHILD_ID, V_VAR1);
      // Prevent flooding the gateway with re-requests,,, wait at least 1000 ms for gateway (cannot be sleep or smartSleep
            wait(2*CHECK_FREQUENCY); 
            return;
          }
          minsendcnt = MIN_SEND_FREQ;
          maxsendcnt = MAX_SEND_FREQ;
          pingcnt++;
      
          sensorValue = analogRead(BATTERY_SENSE_PIN);
          int batteryPcnt = sensorValue / 10;
          // 1M, 470K divider across battery and using internal ADC ref of 1.1V
          // Sense point is bypassed with 0.1 uF cap to reduce noise at that point
          // ((1e6+470e3)/1e6)*1.1 = Vmax = 1.67 Volts
          // 1.67/1023 = Volts per bit = 0.00158065
      
          Serial.print("Battery %: ");
          Serial.println(batteryPcnt);
      
          if (oldBatteryPcnt != batteryPcnt) {
            sendBatteryLevel(batteryPcnt);
            oldBatteryPcnt = batteryPcnt;
          }
          double volume = ((double)pulseCount/((double)PULSE_FACTOR));      
          flow = ((double) (pulseCount-oldPulseCount)) * (60000.0 / ((double) intervalcnt*(double) CHECK_FREQUENCY)) / ppl;  // flow in liter/min
      
          #ifdef MY_DEBUG
          Serial.print("pulsecount:");
          Serial.println(pulseCount);
          Serial.print("volume:");
          Serial.println(volume, 3);
          Serial.print("l/min:");
          Serial.println(flow);
          #endif
             
          bool b = send(lastCounterMsg.set(pulseCount));  // Send  pulsecount value to gw in VAR1
          if (b) errcnt2=0; else { errcnt++; errcnt2++; }
          b = send(volumeMsg.set(volume, 3));               // Send volume (set function 2nd argument is resolution)
          if (b) errcnt2=0; else { errcnt++; errcnt2++; }
          b = send(flowMsg.set(flow, 2));                   // Send flow value to gw
          if (b) errcnt2=0; else { errcnt++; errcnt2++; }
          b = send(pingMsg.set(pingcnt));                   // ensure at least this var has a different value
          if (b) errcnt2=0; else { errcnt++; errcnt2++; }
          b = send(errMsg.set(errcnt2+((float) errcnt2/100),2));    // ensure we always send error count
          if (b) errcnt2=0; else { errcnt++; errcnt2++; }
          oldPulseCount = pulseCount;
          intervalcnt = 0;
          oldflow = flow; 
          if (errcnt2>= (5*RESETMIN)) {
            Serial.println("Reset");
            wait(300);
            resetFunc(); //call reset to reboot the Arduino
          }
        }
      // calculate how long it took to process all of this. then go to sleep for the remaining period
        unsigned long end_loop = millis();
        if (end_loop - start_loop < CHECK_FREQUENCY)
          sleep(CHECK_FREQUENCY - (end_loop > start_loop ? end_loop - start_loop : 0));
      }
      
      void receive(const MyMessage &message) {
        if (message.type==V_VAR1) {
          unsigned long gwPulseCount=message.getULong();
          pulseCount += gwPulseCount;
          oldPulseCount += gwPulseCount;
          flow=oldflow=0;
          Serial.print("Received last pulse count from gw:");
          Serial.println(pulseCount);
          pcReceived = true;
        }
      }
      
      
      void storeEeprom(int pos, int value) {
          // function for saving the values to the internal EEPROM
          // value = the value to be stored (as int)
          // pos = the first byte position to store the value in
          // only two bytes can be stored with this function (max 32.767)
          saveState(pos, ((unsigned int)value >> 8 ));
          pos++;
          saveState(pos, (value & 0xff));
      }
      
      int readEeprom(int pos) {
          // function for reading the values from the internal EEPROM
          // pos = the first byte position to read the value from 
          int hiByte;
          int loByte;
          hiByte = loadState(pos) << 8;
          pos++;
          loByte = loadState(pos);
          return (hiByte | loByte);
      }
      
      posted in Announcements
      bart59
      bart59