Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Development
  3. Relay actuator + water meter pulse sensor sketch

Relay actuator + water meter pulse sensor sketch

Scheduled Pinned Locked Moved Development
7 Posts 4 Posters 4.1k Views 5 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    niccodemi
    wrote on last edited by
    #1

    I would like to combine relay actuator and water meter pulse sensor sketch . I merged the code from original examples, I can upload sketch but something must be wrong because only one device gets created in Vera. Gateway is version 1.5 and arduino 1.6.5

    #include <MySensor.h>  
    #include <SPI.h>
    #include <MySigningNone.h>
    #include <MyTransportNRF24.h>
    #include <MyTransportRFM69.h>
    #include <MyHwATMega328.h>
    
    #define DIGITAL_INPUT_SENSOR 5                  // The digital input you attached your sensor.  (Only 2 and 3 generates interrupt!)
    #define SENSOR_INTERRUPT DIGITAL_INPUT_SENSOR-2        // Usually the interrupt = pin -2 (on uno/nano anyway)
    
    #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
    
    #define RELAY_1  3  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define NUMBER_OF_RELAYS 1 // Total number of attached relays
    #define RELAY_ON 1  // GPIO value to write to turn on attached relay
    #define RELAY_OFF 0 // GPIO value to write to turn off attached relay
    
    unsigned long SEND_FREQUENCY = 30000;           // Minimum time between send (in milliseconds). We don't want to spam the gateway.
    
    MySensor gw;
    MyMessage flowMsg(CHILD_ID,V_FLOW);
    MyMessage volumeMsg(CHILD_ID,V_VOLUME);
    MyMessage lastCounterMsg(CHILD_ID,V_VAR1);
    MyTransportNRF24 radio(RF24_CE_PIN, RF24_CS_PIN, RF24_PA_LEVEL_GW);  
    MyHwATMega328 hw;
    
    double ppl = ((double)PULSE_FACTOR)/1000;        // Pulses per liter
    
    volatile unsigned long pulseCount = 0;   
    volatile unsigned long lastBlink = 0;
    volatile double flow = 0;  
    boolean 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()  
    {  
     gw.begin(incomingMessage); 
    
     // initialize our digital pins internal pullup resistor so one pulse switches from high to low (less distortion) 
     pinMode(DIGITAL_INPUT_SENSOR, INPUT_PULLUP);
     
     // Send the sketch version information to the gateway and Controller
     gw.sendSketchInfo("Watermeter-Relay", "1.0");
    
     // Register this device as Waterflow sensor
     gw.present(CHILD_ID, S_WATER);       
    
     pulseCount = oldPulseCount = 0;
    
     // Fetch last known pulse count value from gw
     gw.request(CHILD_ID, V_VAR1);
    
     lastSend = lastPulse = millis();
    
     attachInterrupt(SENSOR_INTERRUPT, onPulse, FALLING);
     // Fetch relay status
     for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
       // Register all sensors to gw (they will be created as child devices)
       gw.present(sensor, S_LIGHT);
       // Then set relay pins in output mode
       pinMode(pin, OUTPUT);   
       // Set relay to last known state (using eeprom storage) 
       digitalWrite(pin, gw.loadState(sensor)?RELAY_ON:RELAY_OFF);
     }
    }
    
    
    void loop()     
    { 
     gw.process();
     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
         gw.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)) {
           gw.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);
    
         gw.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);
           
           gw.send(volumeMsg.set(volume, 3));               // Send volume value to gw
         } 
       }
     }
     if (SLEEP_MODE) {
       gw.sleep(SEND_FREQUENCY);
     }
    }
    
    void incomingMessage(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;
      
     // We only expect one type of message from controller. But we better check anyway.
     if (message.type==V_LIGHT) {
        // Change relay state
        digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
        // Store state in eeprom
        gw.saveState(message.sensor, message.getBool());
        // Write some debug info
        Serial.print("Incoming change for sensor:");
        Serial.print(message.sensor);
        Serial.print(", New status: ");
        Serial.println(message.getBool());
      } 
     }
    }
    
    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++; 
    }
    
    SparkmanS 1 Reply Last reply
    0
    • N niccodemi

      I would like to combine relay actuator and water meter pulse sensor sketch . I merged the code from original examples, I can upload sketch but something must be wrong because only one device gets created in Vera. Gateway is version 1.5 and arduino 1.6.5

      #include <MySensor.h>  
      #include <SPI.h>
      #include <MySigningNone.h>
      #include <MyTransportNRF24.h>
      #include <MyTransportRFM69.h>
      #include <MyHwATMega328.h>
      
      #define DIGITAL_INPUT_SENSOR 5                  // The digital input you attached your sensor.  (Only 2 and 3 generates interrupt!)
      #define SENSOR_INTERRUPT DIGITAL_INPUT_SENSOR-2        // Usually the interrupt = pin -2 (on uno/nano anyway)
      
      #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
      
      #define RELAY_1  3  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
      #define NUMBER_OF_RELAYS 1 // Total number of attached relays
      #define RELAY_ON 1  // GPIO value to write to turn on attached relay
      #define RELAY_OFF 0 // GPIO value to write to turn off attached relay
      
      unsigned long SEND_FREQUENCY = 30000;           // Minimum time between send (in milliseconds). We don't want to spam the gateway.
      
      MySensor gw;
      MyMessage flowMsg(CHILD_ID,V_FLOW);
      MyMessage volumeMsg(CHILD_ID,V_VOLUME);
      MyMessage lastCounterMsg(CHILD_ID,V_VAR1);
      MyTransportNRF24 radio(RF24_CE_PIN, RF24_CS_PIN, RF24_PA_LEVEL_GW);  
      MyHwATMega328 hw;
      
      double ppl = ((double)PULSE_FACTOR)/1000;        // Pulses per liter
      
      volatile unsigned long pulseCount = 0;   
      volatile unsigned long lastBlink = 0;
      volatile double flow = 0;  
      boolean 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()  
      {  
       gw.begin(incomingMessage); 
      
       // initialize our digital pins internal pullup resistor so one pulse switches from high to low (less distortion) 
       pinMode(DIGITAL_INPUT_SENSOR, INPUT_PULLUP);
       
       // Send the sketch version information to the gateway and Controller
       gw.sendSketchInfo("Watermeter-Relay", "1.0");
      
       // Register this device as Waterflow sensor
       gw.present(CHILD_ID, S_WATER);       
      
       pulseCount = oldPulseCount = 0;
      
       // Fetch last known pulse count value from gw
       gw.request(CHILD_ID, V_VAR1);
      
       lastSend = lastPulse = millis();
      
       attachInterrupt(SENSOR_INTERRUPT, onPulse, FALLING);
       // Fetch relay status
       for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
         // Register all sensors to gw (they will be created as child devices)
         gw.present(sensor, S_LIGHT);
         // Then set relay pins in output mode
         pinMode(pin, OUTPUT);   
         // Set relay to last known state (using eeprom storage) 
         digitalWrite(pin, gw.loadState(sensor)?RELAY_ON:RELAY_OFF);
       }
      }
      
      
      void loop()     
      { 
       gw.process();
       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
           gw.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)) {
             gw.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);
      
           gw.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);
             
             gw.send(volumeMsg.set(volume, 3));               // Send volume value to gw
           } 
         }
       }
       if (SLEEP_MODE) {
         gw.sleep(SEND_FREQUENCY);
       }
      }
      
      void incomingMessage(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;
        
       // We only expect one type of message from controller. But we better check anyway.
       if (message.type==V_LIGHT) {
          // Change relay state
          digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
          // Store state in eeprom
          gw.saveState(message.sensor, message.getBool());
          // Write some debug info
          Serial.print("Incoming change for sensor:");
          Serial.print(message.sensor);
          Serial.print(", New status: ");
          Serial.println(message.getBool());
        } 
       }
      }
      
      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++; 
      }
      
      SparkmanS Offline
      SparkmanS Offline
      Sparkman
      Hero Member
      wrote on last edited by
      #2

      @niccodemi Looks like you are using 1 as the child id for both. It probably easiest to change the child id of the pulse sensor to 10 or something like that in case you add more relays in the future.

      Cheers
      Al

      1 Reply Last reply
      0
      • T Offline
        T Offline
        tbully
        wrote on last edited by
        #3

        I built an identical sketch. (Control a valve if water is flowing and shouldn't be - busted pipe, stuck sprinkler, etc)

        I'm posting to remind myself to send you my sketch when I have access to my IDE at home.

        1 Reply Last reply
        0
        • T Offline
          T Offline
          tbully
          wrote on last edited by
          #4

          Here's my code in case you're interested:

          //
          // 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.
          //
          // Sensor on pin 3
          
          #include <MySensor.h>
          #include <SPI.h>
          
          
          #define DIGITAL_INPUT_SENSOR 3                  // The digital input you attached your sensor.  (Only 2 and 3 generates interrupt!)
          //#define PULSE_FACTOR 12500                       // Nummber of blinks per m3 of your meter (One rotation/liter)
          #define PULSE_FACTOR 288000
          #define SLEEP_MODE false                        // flowvalue can only be reported when sleep mode is false.
          #define MAX_FLOW 25                             // Max flow (l/min) value to report. This filetrs outliers.
          #define INTERRUPT DIGITAL_INPUT_SENSOR-2        // Usually the interrupt = pin -2 (on uno/nano anyway)
          #define CHILD_ID 0                              // Id of the sensor child
          #define RELAY_1  4  // Arduino Digital I/O pin number for first relay
          #define NUMBER_OF_RELAYS 1 
          #define RELAY_ON 1
          #define RELAY_OFF 0
          unsigned long SEND_FREQUENCY = 10000;              // Minimum time between send (in miliseconds). We don't want to spam the gateway.
          
          MySensor gw;
          
          double ppl = ((double)PULSE_FACTOR)/1000;        // Pulses per liter
          
          volatile unsigned long pulseCount = 0;   
          volatile unsigned long lastBlink = 0;
          volatile double flow = 0;
          boolean pcReceived = false;
          unsigned long oldPulseCount = 0;
          unsigned long newBlink = 0;   
          double oldflow = 0;
          double volume;                     
          double oldvolume;
          unsigned long lastSend;
          unsigned long lastPulse;
          unsigned long currentTime;
          boolean metric;
          
          MyMessage flowMsg(CHILD_ID,V_FLOW);
          MyMessage volumeMsg(CHILD_ID,V_VOLUME);
          MyMessage pcMsg(CHILD_ID,V_VAR1);
          
          void setup()  
          {  
            //gw.begin(incomingMessage, AUTO, true);
            //gw.begin(incomingMessage, AUTO, false, AUTO, RF24_PA_LOW);
            gw.begin(incomingMessage, AUTO, false, AUTO);
            //  gw.send(pcMsg.set(0));
            //  gw.send(volumeMsg.set(0.000, 3));
            //Water meter setup
            //Serial.print("PPL:");
            //Serial.print(ppl);
          
            // Send the sketch version information to the gateway and Controller
            gw.sendSketchInfo("Water Meter and Valve", "1.0");
          
            // Register this device as Waterflow sensor
            gw.present(CHILD_ID, S_WATER);      
          
            // Fetch last known pulse count value from gw
            gw.request(CHILD_ID, V_VAR1);
            //pulseCount = oldPulseCount = 0;
          
            //Serial.print("Last pulse count from gw:");
            //Serial.println(pulseCount);
          
            attachInterrupt(INTERRUPT, onPulse, RISING);
            lastSend = millis();
          
            //RELAY SETUP
            for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
              // Register all sensors to gw (they will be created as child devices)
              gw.present(sensor, S_LIGHT);
              // Then set relay pins in output mode
              pinMode(pin, OUTPUT);   
              // Set relay to last known state (using eeprom storage) 
              digitalWrite(pin, gw.loadState(sensor)?RELAY_ON:RELAY_OFF);
            }
          
          }
          
          
          void loop()     
          { 
            gw.process();
            currentTime = millis();
            bool sendTime = currentTime - lastSend > SEND_FREQUENCY;
            if (pcReceived && (SLEEP_MODE || sendTime)) {
              // New flow value has been calculated  
              if (!SLEEP_MODE && flow != oldflow) {
                // Check that we dont get unresonable large flow value. 
                // could hapen when long wraps or false interrupt triggered
                if (flow<((unsigned long)MAX_FLOW)) {
                  gw.send(flowMsg.set(flow, 2));        // Send flow value to gw
                }
          
                //Serial.print("g/min:");
                // Serial.println(flow);
                oldflow = flow;
              }
          
              // No Pulse count in 2min 
              if(currentTime - lastPulse > 20000){
                flow = 0;
              } 
          
          
              // Pulse count has changed
              if (pulseCount != oldPulseCount) {
                gw.send(pcMsg.set(pulseCount));                  // Send  volumevalue to gw VAR1
                double volume = ((double)pulseCount/((double)PULSE_FACTOR)*264.172);
                //double volume = ((double)pulseCount/((double)PULSE_FACTOR));     
                oldPulseCount = pulseCount;
                if (volume != oldvolume) {
                  gw.send(volumeMsg.set(volume, 3));               // Send volume value to gw
                  oldvolume = volume;
                } 
              }
              lastSend = currentTime;
            } 
            else if (sendTime) {
              // No count received. Try requesting it again
              gw.request(CHILD_ID, V_VAR1);
              lastSend=currentTime;
            }
          
            if (SLEEP_MODE) {
              gw.sleep(SEND_FREQUENCY);
            } 	
          
          
          }
          
          
          void onPulse()     
          { 
            if (!SLEEP_MODE) {
              unsigned long newBlink = micros();   
              unsigned long interval = newBlink-lastBlink;
              lastPulse = millis();
              if (interval < 2080) {       // Sometimes we get interrupt on RISING,  500000 = 0.5sek debounce ( max 120 l/min)  WAS 2080
                return;   
              }
          
              flow = ((60000000.0 /interval) / ppl)*.264172;
              //flow = ((60000000.0 /interval) / ppl);
              // Serial.print("interval:");
              // Serial.println(interval);
              lastBlink = newBlink;
              // Serial.println(flow, 4);
            }
            pulseCount++;
          
          }
          
          void incomingMessage(const MyMessage &message) {
            if (message.type==V_VAR1) {  
              pulseCount = oldPulseCount = message.getLong();
              Serial.print("Received last pulse count from gw:");
              Serial.println(pulseCount);
              pcReceived = true;
            }
            if (message.type==V_LIGHT) {
              // Change relay state
              digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
              // Store state in eeprom
              gw.saveState(message.sensor, message.getBool());
              // Write some debug info
              Serial.print("Incoming change for sensor:");
              Serial.print(message.sensor);
              Serial.print(", New status: ");
              Serial.println(message.getBool());
            } 
          
          }
          
          N 1 Reply Last reply
          1
          • T tbully

            Here's my code in case you're interested:

            //
            // 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.
            //
            // Sensor on pin 3
            
            #include <MySensor.h>
            #include <SPI.h>
            
            
            #define DIGITAL_INPUT_SENSOR 3                  // The digital input you attached your sensor.  (Only 2 and 3 generates interrupt!)
            //#define PULSE_FACTOR 12500                       // Nummber of blinks per m3 of your meter (One rotation/liter)
            #define PULSE_FACTOR 288000
            #define SLEEP_MODE false                        // flowvalue can only be reported when sleep mode is false.
            #define MAX_FLOW 25                             // Max flow (l/min) value to report. This filetrs outliers.
            #define INTERRUPT DIGITAL_INPUT_SENSOR-2        // Usually the interrupt = pin -2 (on uno/nano anyway)
            #define CHILD_ID 0                              // Id of the sensor child
            #define RELAY_1  4  // Arduino Digital I/O pin number for first relay
            #define NUMBER_OF_RELAYS 1 
            #define RELAY_ON 1
            #define RELAY_OFF 0
            unsigned long SEND_FREQUENCY = 10000;              // Minimum time between send (in miliseconds). We don't want to spam the gateway.
            
            MySensor gw;
            
            double ppl = ((double)PULSE_FACTOR)/1000;        // Pulses per liter
            
            volatile unsigned long pulseCount = 0;   
            volatile unsigned long lastBlink = 0;
            volatile double flow = 0;
            boolean pcReceived = false;
            unsigned long oldPulseCount = 0;
            unsigned long newBlink = 0;   
            double oldflow = 0;
            double volume;                     
            double oldvolume;
            unsigned long lastSend;
            unsigned long lastPulse;
            unsigned long currentTime;
            boolean metric;
            
            MyMessage flowMsg(CHILD_ID,V_FLOW);
            MyMessage volumeMsg(CHILD_ID,V_VOLUME);
            MyMessage pcMsg(CHILD_ID,V_VAR1);
            
            void setup()  
            {  
              //gw.begin(incomingMessage, AUTO, true);
              //gw.begin(incomingMessage, AUTO, false, AUTO, RF24_PA_LOW);
              gw.begin(incomingMessage, AUTO, false, AUTO);
              //  gw.send(pcMsg.set(0));
              //  gw.send(volumeMsg.set(0.000, 3));
              //Water meter setup
              //Serial.print("PPL:");
              //Serial.print(ppl);
            
              // Send the sketch version information to the gateway and Controller
              gw.sendSketchInfo("Water Meter and Valve", "1.0");
            
              // Register this device as Waterflow sensor
              gw.present(CHILD_ID, S_WATER);      
            
              // Fetch last known pulse count value from gw
              gw.request(CHILD_ID, V_VAR1);
              //pulseCount = oldPulseCount = 0;
            
              //Serial.print("Last pulse count from gw:");
              //Serial.println(pulseCount);
            
              attachInterrupt(INTERRUPT, onPulse, RISING);
              lastSend = millis();
            
              //RELAY SETUP
              for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
                // Register all sensors to gw (they will be created as child devices)
                gw.present(sensor, S_LIGHT);
                // Then set relay pins in output mode
                pinMode(pin, OUTPUT);   
                // Set relay to last known state (using eeprom storage) 
                digitalWrite(pin, gw.loadState(sensor)?RELAY_ON:RELAY_OFF);
              }
            
            }
            
            
            void loop()     
            { 
              gw.process();
              currentTime = millis();
              bool sendTime = currentTime - lastSend > SEND_FREQUENCY;
              if (pcReceived && (SLEEP_MODE || sendTime)) {
                // New flow value has been calculated  
                if (!SLEEP_MODE && flow != oldflow) {
                  // Check that we dont get unresonable large flow value. 
                  // could hapen when long wraps or false interrupt triggered
                  if (flow<((unsigned long)MAX_FLOW)) {
                    gw.send(flowMsg.set(flow, 2));        // Send flow value to gw
                  }
            
                  //Serial.print("g/min:");
                  // Serial.println(flow);
                  oldflow = flow;
                }
            
                // No Pulse count in 2min 
                if(currentTime - lastPulse > 20000){
                  flow = 0;
                } 
            
            
                // Pulse count has changed
                if (pulseCount != oldPulseCount) {
                  gw.send(pcMsg.set(pulseCount));                  // Send  volumevalue to gw VAR1
                  double volume = ((double)pulseCount/((double)PULSE_FACTOR)*264.172);
                  //double volume = ((double)pulseCount/((double)PULSE_FACTOR));     
                  oldPulseCount = pulseCount;
                  if (volume != oldvolume) {
                    gw.send(volumeMsg.set(volume, 3));               // Send volume value to gw
                    oldvolume = volume;
                  } 
                }
                lastSend = currentTime;
              } 
              else if (sendTime) {
                // No count received. Try requesting it again
                gw.request(CHILD_ID, V_VAR1);
                lastSend=currentTime;
              }
            
              if (SLEEP_MODE) {
                gw.sleep(SEND_FREQUENCY);
              } 	
            
            
            }
            
            
            void onPulse()     
            { 
              if (!SLEEP_MODE) {
                unsigned long newBlink = micros();   
                unsigned long interval = newBlink-lastBlink;
                lastPulse = millis();
                if (interval < 2080) {       // Sometimes we get interrupt on RISING,  500000 = 0.5sek debounce ( max 120 l/min)  WAS 2080
                  return;   
                }
            
                flow = ((60000000.0 /interval) / ppl)*.264172;
                //flow = ((60000000.0 /interval) / ppl);
                // Serial.print("interval:");
                // Serial.println(interval);
                lastBlink = newBlink;
                // Serial.println(flow, 4);
              }
              pulseCount++;
            
            }
            
            void incomingMessage(const MyMessage &message) {
              if (message.type==V_VAR1) {  
                pulseCount = oldPulseCount = message.getLong();
                Serial.print("Received last pulse count from gw:");
                Serial.println(pulseCount);
                pcReceived = true;
              }
              if (message.type==V_LIGHT) {
                // Change relay state
                digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
                // Store state in eeprom
                gw.saveState(message.sensor, message.getBool());
                // Write some debug info
                Serial.print("Incoming change for sensor:");
                Serial.print(message.sensor);
                Serial.print(", New status: ");
                Serial.println(message.getBool());
              } 
            
            }
            
            N Offline
            N Offline
            niccodemi
            wrote on last edited by
            #5

            @tbully thanks, just what I was looking for.

            1 Reply Last reply
            0
            • T Offline
              T Offline
              thumper
              wrote on last edited by
              #6

              @tbully thank you for the code, can you hardware you are using. What are you using to monitor the flow of your pipes (flow meter or pulse monitor). Thanks in advance and sorry if you already mentioned this elsewhere.

              1 Reply Last reply
              0
              • T Offline
                T Offline
                tbully
                wrote on last edited by
                #7

                Hey there. Sorry for the delay. I never get notifications on this board for some reason.

                I'm using one of these:
                http://www.amazon.com/dp/B00EVKVM02/ref=pe_385040_30332190_TE_3p_dp_1

                And one of these for my sprinkler 3/4 line:
                http://www.amazon.com/dp/B00EVKOK6A/ref=pe_385040_30332190_TE_3p_dp_2
                http://www.amazon.com/gp/product/B009YVCMX4?psc=1&redirect=true&ref_=oh_aui_search_detailpage

                I couldn't find the 1" valve to go with the sensor but you get the idea. I'm just switching those with normal relays tied to the appropriate pins.

                Funny you should ask about this, however. I'm actually not overly thrilled with the flow sensors (especially the 1" model). It doesn't seem to detect low flow rates. Either my sketch isn't sensitive enough (maybe my debounce is screwed up) or it doesn't pulse for lower speeds.

                I'm considering another type of flow meter but 1) haven't found one yet 2) not looking forward to re-plumbing everything.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                29

                Online

                11.7k

                Users

                11.2k

                Topics

                113.1k

                Posts


                Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • MySensors
                • OpenHardware.io
                • Categories
                • Recent
                • Tags
                • Popular