Domotiocz + Rain gauge


  • Hardware Contributor

    Hi!

    Has anyone a sketch with a tipping bucket posting to domoticz?
    I have tried MySensors tippingbucket and also Water pulse meter sketch but cant figure it out...

    Thanx
    Andreas


  • Hardware Contributor

    This is my rainsensor/tipping bucket for MySensors that works with Domoticz.

    It uses 2xAA as power source, a Pro Mini 3.3v with a 0.9 to 3.3 booster.
    The bucket has a magnet which triggeras a reed switch which triggers an interrupt pin.
    I also use battery sensing with voltage divider.

    // Enable debug prints to serial monitor
    #define MY_DEBUG 
    #define MY_RADIO_NRF24
    #define MY_NODE_ID 7
    
    #include <SPI.h>
    #include <MySensor.h>  
    
    // Running this in Domoticz stable version 2.5 will not work - upgrade to beta.
    
    #define DIGITAL_INPUT_SENSOR 3                  // The reed switch you attached.  (Only 2 and 3 generates interrupt!)
    #define INTERRUPT DIGITAL_INPUT_SENSOR-2        // Usually the interrupt = pin -2 (on uno/nano anyway)
    
    #define CHILD_ID 1                              // Id of the sensor child
    #define SKETCH_NAME "Rain Gauge"                // Change to a fancy name you like
    #define SKETCH_VERSION "1.1"                    // Your version
    
    unsigned long SLEEP_TIME = 18*60000;            // Sleep time (in milliseconds).
    //unsigned long SLEEP_TIME = 20000;             // use this instead for debug
    
    float hwRainVolume = 0;                         // Current rainvolume calculated in hardware.
    int hwPulseCounter = 0;                         // Pulsecount recieved from GW
    float fullCounter = 0;                           // Counts when to send counter
    float bucketSize = 0.5;                           // Bucketsize mm, needs to be 1, 0.5, 0.25, 0.2 or 0.1
    boolean pcReceived = false;                     // If we have recieved the pulscount from GW or not 
    boolean reedState;                              // Current state the reedswitch is in
    boolean oldReedState;                           // Old state (last state) of the reedswitch
    unsigned long lastSend =0;                      // Time we last tried to fetch counter.
    
    MyMessage volumeMsg(CHILD_ID,V_RAIN);
    MyMessage lastCounterMsg(CHILD_ID,V_VAR1);
    
    //=========================
    // BATTERY VOLTAGE DIVIDER SETUP
    // 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)/470e3)*1.1 = Vmax = 3.44 Volts
    // 3.44/1023 = Volts per bit = 0.003363075
    #define VBAT_PER_BITS 0.003363075  
    #define VMIN 1.9                                  //  Vmin (radio Min Volt)=1.9V (564v)
    #define VMAX 3.0                                  //  Vmax = (2xAA bat)=3.0V (892v)
    int batteryPcnt = 0;                              // Calc value for battery %
    int batLoop = 0;                                  // Loop to help calc average
    int batArray[3];                                  // Array to store value for average calc.
    int BATTERY_SENSE_PIN = A0;                       // select the input pin for the battery sense point
    //=========================
    
    void presentation() {
    
      // Send the Sketch Version Information to the Gateway
      sendSketchInfo(SKETCH_NAME, SKETCH_VERSION);
    
      // Register this device as Rain sensor (will not show in Domoticz until first value arrives)
      present(CHILD_ID, S_RAIN);       
    }
    
    
    void setup()  
    {  
      // use the 1.1 V internal reference
      analogReference(INTERNAL);             // For battery sensing
      
      pinMode(DIGITAL_INPUT_SENSOR, INPUT_PULLUP);  // sets the reed sensor digital pin as input
    
      reedState = digitalRead(DIGITAL_INPUT_SENSOR); // Read what state the reedswitch is in
      oldReedState = reedState; // Set startup position for reedswitch 
      
      Serial.println("Startup completed");
    }
    
    void loop()     
    { 
    unsigned long currentTime = millis();
    
        //See if we have the counter/pulse from Domoticz - and ask for it if we dont.
        if (!pcReceived && (currentTime - lastSend > 5000)) {      
          request(CHILD_ID, V_VAR1);
          lastSend=currentTime;
          return;
        }
        if (!pcReceived) {
          return;
        }
        
    //Read if the bucket tipped over
    reedState = digitalRead(DIGITAL_INPUT_SENSOR);
    boolean tipped = oldReedState != reedState; 
    
        //BUCKET TIPS!
        if (tipped==true) {
        Serial.println("The bucket has tipped over...");
        oldReedState = reedState;
        hwRainVolume = hwRainVolume + bucketSize;
        send(volumeMsg.set((float)hwRainVolume,1));
        wait(1000);
        fullCounter = fullCounter + bucketSize;
    
          //Count so we send the counter for every 1mm
          if(fullCounter >= 1){
          hwPulseCounter++;
          send(lastCounterMsg.set(hwPulseCounter));
          wait(1000);
          fullCounter = 0;
          }
          
        }
        
        if (tipped==false) {
    
         //No bucket tipped over last sleep-period, check battery then...
         batM(); 
        }
    
    lastSend=currentTime;
    sleep(INTERRUPT, CHANGE, SLEEP_TIME); 
    //The interupt can be CHANGE or FALLING depending on how you wired the hardware.
    }
    
    //Read if we have a incoming message.
    void receive(const MyMessage &msg) {
        if (msg.type==V_VAR1) {
        hwPulseCounter = msg.getULong();
        hwRainVolume = hwPulseCounter;
        pcReceived = true;
        Serial.print("Received last pulse count from gw: ");
        Serial.println(hwPulseCounter);   
        }
    }
    
    void batM(){  //The battery calculations
      
       delay(500);
       // Battery monitoring reading
       int sensorValue = analogRead(BATTERY_SENSE_PIN);    
       delay(500);
       
       // Calculate the battery in %
       float Vbat  = sensorValue * VBAT_PER_BITS;
       int batteryPcnt = static_cast<int>(((Vbat-VMIN)/(VMAX-VMIN))*100.);
       Serial.print("Battery percent: "); Serial.print(batteryPcnt); Serial.println(" %");  
       
       // Add it to array so we get an average of 3 (3x20min)
       batArray[batLoop] = batteryPcnt;
      
       if (batLoop > 1) {  
         batteryPcnt = (batArray[0] + batArray[1] + batArray[2]);
         batteryPcnt = batteryPcnt / 3;
     
       if (batteryPcnt > 100) {
         batteryPcnt=100;
     }
     
         Serial.print("Battery Average (Send): "); Serial.print(batteryPcnt); Serial.println(" %");
           sendBatteryLevel(batteryPcnt);
           batLoop = 0;
           
         //Sends 1 per hour as a heartbeat.
         send(volumeMsg.set((float)hwRainVolume,1));   
         send(lastCounterMsg.set(hwPulseCounter));
         }
         else 
         {
         batLoop++;
         }
    }
    

  • Hardware Contributor

    This is great!
    1 year - times fly by, but my rainsensor is going strong!

    I just wanted to celebrate 1 year for my Rainsensor on batteries, reporting every hour (or less when it rains). Still going strong and 81% battery left! Except for some range issues in the beginning you can see its working as it should:

    Also It will be my (almost) 1000th post so thank you all for making MySensors such a great community. I just love every minute here!

    0_1474871808445_1.jpg



  • @sundberg84 Wow, that is impressive! What type of batteries are you using?

    I'm waiting for parts to come in to start building some battery powered and external sensors.


  • Hardware Contributor

    @mrwomble - 2xAA



  • Thats impressive, out of interest are you doing anything with this data in domotiocz?


  • Hardware Contributor

    @Qu3Uk nope, just monitoring. I could probably use it for watering if I made some actuators... but not that garden interested.


  • MySensors Evangelist

    @sundberg84 , very nice. I would say.. I want that as well ;-)
    any suggestions for a shopping and modification list?
    As I don't have a 3D printer to print the thing myself.


  • Hardware Contributor


  • Contest Winner

    @sundberg84 Just playing with your sketch, because it's easier for my to understand than the one one the build page. I'm curious about how domoticz handles these values. I don't see you reset the hwRainVolume variable. So does this mean that this is an ever increasing value that must be send to Domoticz?


  • Hardware Contributor

    @TheoL - Correct.
    There is a countervalue and Domoticz calculates the value from midnight each night.


  • Contest Winner

    @sundberg84 Thanx for your fast reply. So what happens when the float resets to 0?


  • Contest Winner

    @sincze Hello my friend, just curious how many sensors you've got hooked up at the moment?


  • Hardware Contributor

    @TheoL - Just to add what we discussed in chat for future questions - If float would hit the maximum value (which is hugh so lets hope its not a problem) im not sure what will happen...


  • Contest Winner

    @sundberg84 It's also not a concern of the MySensors node. This should be handled by Domoticz. I really like your sketch, it works like a charm. I modified it to my needs so that I can run the node without the sleep. Still need to think of an hourly heart beat. But almost there.


  • Hardware Contributor

    @TheoL - If you modify the sketch with a heartbeat() please post it :) Im interested as well!


  • Contest Winner

    @AWI do you happen to know if the pulse counter in Domoticz is a 1mm pulse counter? Or is it a real tip pulse counter? Right know where checking whenever the rain fall exceeds 1mm and send a pulse ++ in that case. Meaning we miss some pulse counts when we reset the node.


  • Hero Member

    @TheoL My rain meter counts in 1 mm resolution but I am sending actual total (in mm) from the start of sensor and keep totals in EEPROM.


  • Contest Winner

    @AWI (Glad that your back, hope you're doing allright) So, if I understand your setup correctly you don't send the pulse count to Domoticz? I kind of like it, because that way I can reset the rain sensor many times and still keep the rain fall log in sync, without using the EPROM.

    And my rain sensor did reset a couple of times. I had to put a diode (Dutch name is blus diode) between the two wires coming out from the rain gauge to prevent this.


  • Hero Member

    @TheoL (i'm fine, thank you) That is what i'm doing. The only thing what I need to do after a reset is delete the miscalculation in Domoticz (easy). My sensor (oregon) uses a 'reed' switch (no induction). I MySensoriz'ed it without losing the original circuitry and function. (so in parallel).

    0_1475422212074_upload-82d166bf-a999-4d76-b3e3-8c010fa5f427


Log in to reply
 

Looks like your connection to MySensors Forum was lost, please wait while we try to reconnect.