Gas Meter



  • Hi,

    I'm working on gas meter using In-Z61 reed sensor - it's dedicated for gas meters.

    0_1502394827409_Przechwytywanie0.PNG

    My node is build based on https://www.mysensors.org/hardware/ac-dc-ssd-relay
    My controller is Domo: v3.8258
    My gateway is Arduino UNO shield

    Data is send over to Domo as designed
    One full rotare of gas meter 00.000 [m3] -> 00.010 [m3] is one impuls triggered by reed sensor.

    This is working - after one rotare i got value send to Domo:

    0_1502394260818_Przechwytywanie.PNG

    The issue is that after some while the counter still increments - no actual meter movement is done
    Below is debug - pulse was 100 and was changed to 101.

    pulsecount:100
    TSP:MSG:SEND 8-8-0-0 s=1,c=1,t=24,pt=5,l=4,sg=0,ft=0,st=ok:100
    volume:0.100
    TSP:MSG:SEND 8-8-0-0 s=1,c=1,t=35,pt=7,l=5,sg=0,ft=0,st=ok:0.100
    TSP:MSG:READ 7-7-255 s=255,c=3,t=7,pt=0,l=0,sg=0:
    TSP:MSG:BC
    TSP:MSG:READ 7-7-255 s=255,c=3,t=7,pt=0,l=0,sg=0:
    TSP:MSG:BC
    l/min:0.03
    TSP:MSG:SEND 8-8-0-0 s=1,c=1,t=34,pt=7,l=5,sg=0,ft=0,st=ok:0.03
    pulsecount:101
    TSP:MSG:SEND 8-8-0-0 s=1,c=1,t=24,pt=5,l=4,sg=0,ft=0,st=ok:101
    volume:0.101
    TSP:MSG:SEND 8-8-0-0 s=1,c=1,t=35,pt=7,l=5,sg=0,ft=0,st=ok:0.101
    TSP:MSG:READ 7-7-255 s=255,c=3,t=7,pt=0,l=0,sg=0:
    TSP:MSG:BC
    pulsecount:101
    

    As a sketch I use modified version of PulseWaterMeter - https://www.mysensors.org/build/pulse_water

    I changed presentation:

     present(CHILD_ID, S_GAS);
    

    and added node temp sensor:

    present(CHILD_DSB_ID, S_TEMP);
    

    It looks like some kind of bouncing effect, but have no idea why counter increments itself.

    Help me out to make the code more clear and free of errors.

    code below:

    /**
     * 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.
     *
     *******************************
     *
     * REVISION HISTORY
     * Version 1.0 - Henrik Ekblad
     * Version 1.1 - GizMoCuz
     *
     * DESCRIPTION
     * Use this sensor to measure volume and flow of your house watermeter.
     * You need to set the correct pulsefactor of your meter (pulses per m3).
     * The sensor starts by fetching current volume reading from gateway (VAR 1).
     * Reports both volume and flow back to gateway.
     *
     * Unfortunately millis() won't increment when the Arduino is in
     * sleepmode. So we cannot make this sensor sleep if we also want
     * to calculate/report flow.
     * http://www.mysensors.org/build/pulse_water
     */
    
    // Enable debug prints to serial monitor
    #define MY_DEBUG
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_NRF5_ESB
    //#define MY_RADIO_RFM69
    //#define MY_RADIO_RFM95
    
    #include <MySensors.h>
    #include <SPI.h>
    #include <Bounce2.h>
    #include <OneWire.h>
    #include <DallasTemperature.h>
    
    #define CHILD_DSB_ID 13
    #define ONE_WIRE_BUS 8
    
    #define DIGITAL_INPUT_SENSOR 3                  // The digital input you attached your sensor.  (Only 2 and 3 generates interrupt!)
    
    #define PULSE_FACTOR 1000                       // Nummber of blinks per m3 of your meter (One rotation/liter)
    
    #define SLEEP_MODE false                        // flowvalue can only be reported when sleep mode is false.
    
    #define MAX_FLOW 40                             // Max flow (l/min) value to report. This filters outliers.
    
    #define CHILD_ID 1                              // Id of the sensor child
    
    unsigned long SEND_FREQUENCY =
        3000;           // Minimum time between send (in milliseconds). We don't want to spam the gateway.
    
    Bounce debouncer = Bounce();
    int oldValue = 0;
    bool state;
    Bounce debouncer2 = Bounce();
    int oldValue2 = 0;
    bool state2;
    
    MyMessage flowMsg(CHILD_ID,V_FLOW);
    MyMessage volumeMsg(CHILD_ID,V_VOLUME);
    MyMessage lastCounterMsg(CHILD_ID,V_VAR1);
    MyMessage msgTemp(CHILD_DSB_ID, V_TEMP);
    
    OneWire oneWire(ONE_WIRE_BUS);
    DallasTemperature sensors(&oneWire); // Pass the oneWire reference to Dallas Temperature. 
    
    double ppl = ((double)PULSE_FACTOR)/1000;        // Pulses per liter
    
    volatile unsigned long pulseCount = 0;
    volatile unsigned long lastBlink = 0;
    volatile double flow = 0;
    bool pcReceived = false;
    unsigned long oldPulseCount = 0;
    unsigned long newBlink = 0;
    double oldflow = 0;
    double volume =0;
    double oldvolume =0;
    unsigned long lastSend =0;
    unsigned long lastPulse =0;
    
    void setup()
    {
        // initialize our digital pins internal pullup resistor so one pulse switches from high to low (less distortion)
        pinMode(DIGITAL_INPUT_SENSOR, INPUT_PULLUP);
    
        pulseCount = oldPulseCount = 0;
    
        // Fetch last known pulse count value from gw
        request(CHILD_ID, V_VAR1);
    
        lastSend = lastPulse = millis();
    
        attachInterrupt(digitalPinToInterrupt(DIGITAL_INPUT_SENSOR), onPulse, FALLING);
    }
    
    void presentation()
    {
        // Send the sketch version information to the gateway and Controller
        sendSketchInfo("Gas Meter", "1.1");
    
        // Register this device as Waterflow sensor
        present(CHILD_ID, S_GAS);
        present(CHILD_DSB_ID, S_TEMP);
    }
    
    void loop()
    {
        static float prevTemp = 0;
        unsigned long currentTime = millis();
    
        // Only send values at a maximum frequency or woken up from sleep
        if (SLEEP_MODE || (currentTime - lastSend > SEND_FREQUENCY)) {
            lastSend=currentTime;
    
            if (!pcReceived) {
                //Last Pulsecount not yet received from controller, request it again
                request(CHILD_ID, V_VAR1);
                return;
            }
    
            if (!SLEEP_MODE && flow != oldflow) {
                oldflow = flow;
    
                Serial.print("l/min:");
                Serial.println(flow);
    
                // Check that we dont get unresonable large flow value.
                // could hapen when long wraps or false interrupt triggered
                if (flow<((unsigned long)MAX_FLOW)) {
                    send(flowMsg.set(flow, 2));                   // Send flow value to gw
                }
            }
    
            // No Pulse count received in 2min
            if(currentTime - lastPulse > 120000) {
                flow = 0;
            }
    
            // Pulse count has changed
            if ((pulseCount != oldPulseCount)||(!SLEEP_MODE)) {
                oldPulseCount = pulseCount;
    
                Serial.print("pulsecount:");
                Serial.println(pulseCount);
    
                send(lastCounterMsg.set(pulseCount));                  // Send  pulsecount value to gw in VAR1
    
                double volume = ((double)pulseCount/((double)PULSE_FACTOR));
                if ((volume != oldvolume)||(!SLEEP_MODE)) {
                    oldvolume = volume;
    
                    Serial.print("volume:");
                    Serial.println(volume, 3);
    
                    send(volumeMsg.set(volume, 3));               // Send volume value to gw
                }
            }
     // Fetch temperatures from Dallas sensors
        sensors.requestTemperatures();
        // Fetch and round temperature to one decimal
        float temperature = static_cast<float>(static_cast<int>(sensors.getTempCByIndex(0) * 10.f)) / 10.f;
    
        if (temperature != -127.00f && temperature != 85.00f && prevTemp != temperature) {
            // Send in the new temperature
            send(msgTemp.set(temperature, 1));
            Serial.print("Sent temperature: ");
            Serial.println(temperature);
            prevTemp = temperature;
        }
         }
        if (SLEEP_MODE) {
            sleep(SEND_FREQUENCY);
        }
        
    }
    
    void receive(const MyMessage &message)
    {
        if (message.type==V_VAR1) {
            unsigned long gwPulseCount=message.getULong();
            pulseCount += gwPulseCount;
            flow=oldflow=0;
            Serial.print("Received last pulse count from gw:");
            Serial.println(pulseCount);
            pcReceived = true;
        }
    }
    
    void onPulse()
    {
        if (!SLEEP_MODE) {
            unsigned long newBlink = micros();
            unsigned long interval = newBlink-lastBlink;
    
            if (interval!=0) {
                lastPulse = millis();
                if (interval<500000L) {
                    // Sometimes we get interrupt on RISING,  500000 = 0.5sek debounce ( max 120 l/min)
                    return;
                }
                flow = (60000000.0 /interval) / ppl;
            }
            lastBlink = newBlink;
        }
        pulseCount++;
    }
    

Log in to reply
 

Suggested Topics

  • 3
  • 2
  • 6
  • 1
  • 6

0
Online

11.4k
Users

11.1k
Topics

112.7k
Posts