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. My Project
  3. Rain Guage

Rain Guage

Scheduled Pinned Locked Moved My Project
128 Posts 23 Posters 110.3k Views 21 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.
  • petewillP petewill

    @BulldogLowell said:

    it looks a lot like a bonehead error in the code!!

    Ok, thanks. I make those constantly...

    I have been testing the code for the last couple of days and I think this is working but another set of eyes would be good. I added functionality to set the "calibrateFactor" to a decimal. I also updated the save/load EEPROM to work with the MySensors standard. It seems to be working in my testing but I'm no programmer.

    I also have two questions:

    1. Is it necessary to save/load "state" to/from EEPROM? It seems that from my testing the state is set when "measure" is calculated in this line: state = (measure >= rainSensorThreshold);. It seems that measure is evaluated when the sensor first loads and rainBucket [] is loaded from EEPROM. Hopefully that makes sense what I'm asking.

    2. I'm still trying to understand interrupts but I'm wondering if changing to "FALLING" would be more appropriate? Maybe it doesn't matter but I thought I'd check. Here is the line I'm referring to: attachInterrupt (1, sensorTipped, FALLING);

    Here is the code if you wanted to take a look:

     /*
     Arduino Tipping Bucket Rain Gauge
     
     April 26, 2015
    
     Version 1.01b for MySensors version 1.4.1
    
     Arduino Tipping Bucket Rain Gauge
    
     Utilizing a tipping bucket sensor, your Vera home automation controller and the MySensors.org
     gateway you can measure and sense local rain.  This sketch will create two devices on your
     Vera controller.  One will display your total precipitation for the last 24, 48, 72, 96 and 120
     hours.  The other, a sensor that changes state if there is recent rain (up to last 120 hours)
     above a threshold.  Both these settings are user definable.
    
     This sketch features the following:
    
     * Allows you to set the rain threshold in mm
     * Allows you to determine the interval window up to 120 hours.
     * Displays the last 24, 48, 72, 96 and 120 hours total rain in Variable1 through Variable5
       of the Rain Sensor device
     * Configuration changes to Sensor device updated every 3 hours.
     * SHould run on any Arduino
     * Will retain Tripped/Not Tripped status and data in a power outage, saving small ammount
       of data to EEPROM (Circular Buffer to maximize life of EEPROM)
     * There is a unique setup requirement necessary in order to properly present the Vera device
       variables.  The details are outlined in the sketch below.
     * LED status indicator
    
     by BulldogLowell@gmail.com for free public use
    
     */
    #include <SPI.h>
    #include <MySensor.h>
    
    //*No longer need the EEPROM.h?
    //#include <EEPROM.h>
    
    #define NODE_ID 24
    #define SN "Rain Gauge"
    #define SV "1.01b"
    
    #define CHILD_ID_RAIN 3 
    #define CHILD_ID_TRIPPED_RAIN 4
    
    #define STATE_LOCATION 0 // location to save state to EEPROM
    #define EEPROM_BUFFER 1  // location of the EEPROM circular buffer
    #define BUFFER_LENGTH 121  // buffer plus the current hour
    //
    MySensor gw;
    //
    MyMessage msgRainRate(CHILD_ID_RAIN, V_RAINRATE);
    MyMessage msgRain(CHILD_ID_RAIN, V_RAIN);
    MyMessage msgRainVAR1(CHILD_ID_RAIN, V_VAR1);
    MyMessage msgRainVAR2(CHILD_ID_RAIN, V_VAR2);
    MyMessage msgRainVAR3(CHILD_ID_RAIN, V_VAR3);
    MyMessage msgRainVAR4(CHILD_ID_RAIN, V_VAR4);
    MyMessage msgRainVAR5(CHILD_ID_RAIN, V_VAR5);
    
    MyMessage msgTripped(CHILD_ID_TRIPPED_RAIN, V_TRIPPED);
    MyMessage msgTrippedVar1(CHILD_ID_TRIPPED_RAIN, V_VAR1);
    MyMessage msgTrippedVar2(CHILD_ID_TRIPPED_RAIN, V_VAR2);
    //
    boolean metric = false;
    //
    int eepromIndex;
    int tipSensorPin = 3; //Do not change (needed for interrupt)
    int ledPin = 5; //PWM capable required
    unsigned long dataMillis;
    unsigned long serialInterval = 10000UL;
    const unsigned long oneHour = 3600000UL;
    unsigned long lastTipTime;
    unsigned long lastBucketInterval;
    unsigned long startMillis;
    
    float rainBucket [120] ; // 5 days of data  //*120 hours = 5 days
    
    float calibrateFactor = .7; //Calibration in milimeters of rain per single tip.  Note: Limit to one decimal place or data may be truncated when saving to eeprom. 
    
    float rainRate = 0;
    volatile int tipBuffer = 0;
    byte rainWindow = 72;         //default rain window in hours
    int rainSensorThreshold = 10; //default rain sensor sensitivity in mm.  Will be overwritten with msgTrippedVar2. 
    byte state = 0;
    byte oldState = -1;
    //
    void setup()
    {
      gw.begin(getVariables, NODE_ID);
    
      pinMode(tipSensorPin, INPUT);
    
    //  attachInterrupt (1, sensorTipped, CHANGE); //* should this be FALLING instead??
      attachInterrupt (1, sensorTipped, FALLING);
      pinMode(ledPin, OUTPUT);
      digitalWrite(ledPin, HIGH);
      digitalWrite(tipSensorPin, HIGH); //ADDED. Activate internal pull-up 
      gw.sendSketchInfo(SN, SV);
      gw.present(CHILD_ID_RAIN, S_RAIN);
      gw.present(CHILD_ID_TRIPPED_RAIN, S_MOTION);
    //  Serial.println(F("Sensor Presentation Complete"));
      
    
      state = gw.loadState(STATE_LOCATION); //retreive prior state from EEPROM
    //  Serial.print("Tripped State (from EEPROM): ");
    //  Serial.println(state);
      
      gw.send(msgTripped.set(state==1?"1":"0"));
      delay(250);                             // recharge the capacitor
      //
      boolean isDataOnEeprom = false;
      for (int i = 0; i < BUFFER_LENGTH; i++)
      {
    
        byte locator = gw.loadState(EEPROM_BUFFER + i); //New code
    
        if (locator == 0xFF)  // found the EEPROM circular buffer index
        {
          eepromIndex = EEPROM_BUFFER + i;
          //Now that we have the buffer index let's populate the rainBucket with data from eeprom
          loadRainArray(eepromIndex); 
          isDataOnEeprom = true;
    //      Serial.print("EEPROM Index ");
    //      Serial.println(eepromIndex);
          break;
        }
      }
    
      // reset the timers
      dataMillis = millis();
      startMillis = millis();
      lastTipTime = millis() - oneHour;  //will this work if millis() starts a 0??
      gw.request(CHILD_ID_TRIPPED_RAIN, V_VAR1); //Get rainWindow from controller (Vera)
      delay(250);
      gw.request(CHILD_ID_TRIPPED_RAIN, V_VAR2); //Get rainSensorThreshold from controller (Vera)
      delay(250);
    //  Serial.println("Radio Done");
    //  analogWrite(ledPin, 20);
    
    }
    //
    void loop()
    {
      gw.process();
      pulseLED();
    
      //
      // let's constantly check to see if the rain in the past rainWindow hours is greater than rainSensorThreshold
      //
    
      float measure = 0; // Check to see if we need to show sensor tripped in this block
    
      for (int i = 0; i < rainWindow; i++)
      {
        measure += rainBucket [i];
    //    Serial.print("measure value (total rainBucket within rainWindow): ");
    //    Serial.println(measure);
      }
      state = (measure >= rainSensorThreshold);
    
      if (state != oldState)
      {
        gw.send(msgTripped.set(state==1?"1":"0"));
        delay(250);
        gw.saveState(STATE_LOCATION, state); //New Code
    //    Serial.print("State Changed. Tripped State: ");
    //    Serial.println(state);
        
        oldState = state;
      }
      //
      // Now lets reset the rainRate to zero if no tips in the last hour
      //
      if (millis() - lastTipTime >= oneHour)// timeout for rain rate
      {
        if (rainRate != 0)
        {
          rainRate = 0;
          gw.send(msgRainRate.set(0));
          delay(250);
        }
      }
    
    ////////////////////////////
    ////Comment back in this block to enable Serial prints
    //  
    //  if ( (millis() - dataMillis) >= serialInterval)
    //  {
    //    for (int i = 24; i <= 120; i = i + 24)
    //    {
    //      updateSerialData(i);
    //    }
    //    dataMillis = millis();
    //  }
    //  
    ////////////////////////////
      
      
      if (tipBuffer > 0)
      {
    //     Serial.println("Sensor Tipped");
        
    
        //******Added calibrateFactor calculations here to account for calibrateFactor being different than 1
        rainBucket [0] += calibrateFactor;
    //    Serial.print("rainBucket [0] value: ");
    //    Serial.println(rainBucket [0]);
        
        if (rainBucket [0] * calibrateFactor > 253) rainBucket[0] = 253; // odd occurance but prevent overflow}}
    
        float dayTotal = 0;
        for (int i = 0; i < 24; i++)
        {
          dayTotal = dayTotal + rainBucket [i];
        }
        
    //    Serial.print("dayTotal value: ");
    //    Serial.println(dayTotal);
        
        gw.send(msgRain.set(dayTotal,1));
        delay(250);
        unsigned long tipDelay = millis() - lastTipTime;
        if (tipDelay <= oneHour)
        {
    
          rainRate = ((oneHour) / tipDelay) * calibrateFactor; //Is my math/logic correct here??
      
          gw.send(msgRainRate.set(rainRate, 1));
    
    //      Serial.print("RainRate= ");
    //      Serial.println(rainRate);
        }
    
        //If this is the first trip in an hour send .1
        else
        {
          gw.send(msgRainRate.set(0.1, 1));
        }
        lastTipTime = millis();
        tipBuffer--;
      }
      if (millis() - startMillis >= oneHour)
      {
    //    Serial.println("One hour elapsed.");
        //EEPROM write last value
        //Converting rainBucket to byte.  Note: limited to one decimal place.
        //Can this math be simplified?? 
        float convertRainBucket = rainBucket[0] * 10;
        if (convertRainBucket  > 253) convertRainBucket = 253; // odd occurance but prevent overflow
        byte eepromRainBucket = (byte)convertRainBucket;
        gw.saveState(eepromIndex, eepromRainBucket);
    
        eepromIndex++;
        if (eepromIndex > EEPROM_BUFFER + BUFFER_LENGTH) eepromIndex = EEPROM_BUFFER;
    //    Serial.print("Writing to EEPROM.  Index: ");
    //    Serial.println(eepromIndex);
        gw.saveState(eepromIndex, 0xFF);
        
        for (int i = BUFFER_LENGTH - 1; i >= 0; i--)//cascade an hour of values
        {
          rainBucket [i + 1] = rainBucket [i];
        }
        rainBucket[0] = 0;
        gw.send(msgRain.set(tipCounter(24),1));// send 24hr tips
        delay(250);
        transmitRainData(); // send all of the 5 buckets of data to controller
        startMillis = millis();
      }
    }
    //
    void sensorTipped()
    {
      static unsigned long last_interrupt_time = 0;
      unsigned long interrupt_time = millis();
      if (interrupt_time - last_interrupt_time > 200)
      {
        tipBuffer++;
      }
      last_interrupt_time = interrupt_time;
    }
    //
    float tipCounter(int hours)
    {
      float tipCount = 0;
      for ( int i = 0; i < hours; i++)
      {
        tipCount = tipCount + rainBucket [i];
      }
      return tipCount;
    }
    //
    void updateSerialData(int x)
    {
      Serial.print(F("Tips last "));
      Serial.print(x);
      Serial.print(F(" hours: "));
      float tipCount = 0;
      for (int i = 0; i < x; i++)
      {
        tipCount = tipCount + rainBucket [i];
      }
      Serial.println(tipCount);
    }
    void loadRainArray(int value)
    {
      for (int i = 0; i < BUFFER_LENGTH - 1; i++)
      {
        value--;
        Serial.print("EEPROM location: ");
        Serial.println(value);
        if (value < EEPROM_BUFFER)
        {
          value = EEPROM_BUFFER + BUFFER_LENGTH;
        }
        float rainValue = gw.loadState(value);
    
        if (rainValue < 255)
        {
          //Convert back to decimal value
          float decimalRainValue = rainValue/10;
          rainBucket[i] = decimalRainValue;
        }
        else
        {
          rainBucket [i] = 0;
        }
    //    Serial.print("rainBucket[ value: ");
    //    Serial.print(i);
    //    Serial.print("] value: ");
    //    Serial.println(rainBucket[i]);
      }
    }
    
    void transmitRainData(void)
    {
      float rainUpdateTotal = 0;
      for (int i = 0; i < 24; i++)
      {
        rainUpdateTotal += rainBucket[i];
      }
      gw.send(msgRainVAR1.set(rainUpdateTotal,1));
      delay(250);
      for (int i = 24; i < 48; i++)
      {
        rainUpdateTotal += rainBucket[i];
      }
      gw.send(msgRainVAR2.set(rainUpdateTotal,1));
      delay(250);
      for (int i = 48; i < 72; i++)
      {
        rainUpdateTotal += rainBucket[i];
      }
      gw.send(msgRainVAR3.set(rainUpdateTotal,1));
    
      delay(250);
      for (int i = 72; i < 96; i++)
      {
        rainUpdateTotal += rainBucket[i];
      }
      gw.send(msgRainVAR4.set(rainUpdateTotal,1));
    
      delay(250);
      for (int i = 96; i < 120; i++)
      {
        rainUpdateTotal += rainBucket[i];
      }
      gw.send(msgRainVAR5.set(rainUpdateTotal,1));
      delay(250);
    }
    
    void getVariables(const MyMessage &message)
    {
      if (message.sensor == CHILD_ID_RAIN)
      {
        // nothing to do here
      }
      else if (message.sensor == CHILD_ID_TRIPPED_RAIN)
      {
        if (message.type == V_VAR1)
        {
          rainWindow = atoi(message.data);
          if (rainWindow > 120)
          {
            rainWindow = 120;
          }
          else if (rainWindow < 6)
          {
            rainWindow = 6;
          }
          if (rainWindow != atoi(message.data))   // if I changed the value back inside the boundries, push that number back to Vera
          {
            gw.send(msgTrippedVar1.set(rainWindow));
          }
        }
        else if (message.type == V_VAR2)
        {
          rainSensorThreshold = atoi(message.data);
          if (rainSensorThreshold > 1000)
          {
            rainSensorThreshold = 1000;
          }
          else if (rainSensorThreshold < 1)
          {
            rainSensorThreshold = 1;
          }
          if (rainSensorThreshold != atoi(message.data))  // if I changed the value back inside the boundries, push that number back to Vera
          {
            gw.send(msgTrippedVar2.set(rainSensorThreshold));
          }
        }
      }
    }
    void pulseLED(void)
    {
      static boolean ledState = true;
      static unsigned long pulseStart = millis();
      if (millis() - pulseStart < 500UL)
      {
        digitalWrite(ledPin, !ledState);
        pulseStart = millis();
      }
    }
    
    BulldogLowellB Offline
    BulldogLowellB Offline
    BulldogLowell
    Contest Winner
    wrote on last edited by BulldogLowell
    #57

    @petewill said:

    I have been testing the code for the last couple of days and I think this is working but another set of eyes would be good. I added functionality to set the "calibrateFactor" to a decimal. I also updated the save/load EEPROM to work with the MySensors standard. It seems to be working in my testing but I'm no programmer.

    Pete,

    I had a whack of combining your desire to use floats, and my desire not to use floats!

    Essentially we are storing in hundredths of a mm or hundredths of an inch, and dividing the values down when we transmit to Vera.

    you set the calibration in hundredths of a unit per tip here:

    #define CALIBRATE_FACTOR 5 // e.g. 5 is .05mm (or 5 hundredths of an inch if imperial) per tip
    

    Take a look at this and let me know if you like it, note it is untested and I still have to test the circular buffer storing ints instead of bytes.

    /*
     Arduino Tipping Bucket Rain Gauge
    
     April 26, 2015
    
     Version 1.02b for MySensors version 1.4.1
    
     Arduino Tipping Bucket Rain Gauge
    
     Utilizing a tipping bucket sensor, your Vera home automation controller and the MySensors.org
     gateway you can measure and sense local rain.  This sketch will create two devices on your
     Vera controller.  One will display your total precipitation for the last 24, 48, 72, 96 and 120
     hours.  The other, a sensor that changes state if there is recent rain (up to last 120 hours)
     above a threshold.  Both these settings are user definable.
    
     This sketch features the following:
    
     * Allows you to set the rain threshold in mm
     * Allows you to determine the interval window up to 120 hours.
     * Displays the last 24, 48, 72, 96 and 120 hours total rain in Variable1 through Variable5
       of the Rain Sensor device
     * Configuration changes to Sensor device updated every hour
     * SHould run on any Arduino
     * Will retain Tripped/Not Tripped status and data in a power outage, saving small ammount
       of data to EEPROM (Circular Buffer to maximize life of EEPROM)
     * There is a unique setup requirement necessary in order to properly present the Vera device
       variables.  The details are outlined in the sketch below.
     * LED status indicator
    
     by @BulldogLowell and @PeteWill for free public use
    
     */
    #include <SPI.h>
    #include <MySensor.h>
    #include <math.h>
    
    #define NODE_ID 24
    #define SKETCH_NAME "Rain Gauge"
    #define SKETCH_VERSION "1.02b"
    
    #define DEBUG_ON  // comment out this line to disable serial debug
    
    #define CHILD_ID_RAIN 3
    #define CHILD_ID_TRIPPED_RAIN 4
    
    #define STATE_LOCATION 0 // location to save state to EEPROM
    #define EEPROM_BUFFER 1  // location of the EEPROM circular buffer
    #define BUFFER_LENGTH 121  // buffer plus the current hour
    
    #define CALIBRATE_FACTOR 100 // e.g. 5 is .05mm (or 5 hundredths of an inch if imperial) per tip
    
    #ifdef  DEBUG_ON
    #define DEBUG_PRINT(x)   Serial.print(x)
    #define DEBUG_PRINTLN(x) Serial.println(x)
    #define SERIAL_START(x)  Serial.begin(x)
    #else
    #define DEBUG_PRINT(x)
    #define DEBUG_PRINTLN(x)
    #define SERIAL_START(x)
    #endif
    
    //
    MySensor gw;
    //
    MyMessage msgRainRate(CHILD_ID_RAIN, V_RAINRATE);
    MyMessage msgRain(CHILD_ID_RAIN, V_RAIN);
    MyMessage msgRainVAR1(CHILD_ID_RAIN, V_VAR1);
    MyMessage msgRainVAR2(CHILD_ID_RAIN, V_VAR2);
    MyMessage msgRainVAR3(CHILD_ID_RAIN, V_VAR3);
    MyMessage msgRainVAR4(CHILD_ID_RAIN, V_VAR4);
    MyMessage msgRainVAR5(CHILD_ID_RAIN, V_VAR5);
    //
    MyMessage msgTripped(CHILD_ID_TRIPPED_RAIN, V_TRIPPED);
    MyMessage msgTrippedVar1(CHILD_ID_TRIPPED_RAIN, V_VAR1);
    MyMessage msgTrippedVar2(CHILD_ID_TRIPPED_RAIN, V_VAR2);
    //
    boolean metric = true;
    //
    int eepromIndex;
    int tipSensorPin = 3; // Must be interrupt capable pin
    int ledPin = 5; // PWM capable pin required
    unsigned long dataMillis;
    unsigned long serialInterval = 10000UL;
    const unsigned long oneHour = 3600000UL;
    unsigned long lastTipTime;
    unsigned long lastBucketInterval;
    unsigned long startMillis;
    
    unsigned int rainBucket [120] ; /* 120 hours = 5 days of data */
    unsigned int rainRate = 0;
    volatile int tipBuffer = 0;
    byte rainWindow = 72;         //default rain window in hours
    int rainSensorThreshold = 50; //default rain sensor sensitivity in hundredths.  Will be overwritten with msgTrippedVar2.
    byte state = 0;
    byte oldState = -1;
    //
    void setup()
    {
      SERIAL_START(115200);
      gw.begin(getVariables, NODE_ID);
      pinMode(tipSensorPin, INPUT_PULLUP);
      attachInterrupt (1, sensorTipped, FALLING);  // depending on location of the hall effect sensor may need CHANGE
      pinMode(ledPin, OUTPUT);
      digitalWrite(ledPin, HIGH);
      gw.sendSketchInfo(SKETCH_NAME, SKETCH_VERSION);
      gw.present(CHILD_ID_RAIN, S_RAIN);
      gw.present(CHILD_ID_TRIPPED_RAIN, S_MOTION);
      DEBUG_PRINTLN(F("Sensor Presentation Complete"));
      state = gw.loadState(STATE_LOCATION); //retreive prior state from EEPROM
      DEBUG_PRINT(F("Previous Tripped State (from EEPROM): "));
      DEBUG_PRINTLN(state ? "Tripped" : "Not Tripped");
      //
      gw.send(msgTripped.set(state));
      delay(250);  // recharge the capacitor
      //
      boolean isDataOnEeprom = false;
      for (int i = 0; i < BUFFER_LENGTH; i++)
      {
        byte locator = gw.loadState(EEPROM_BUFFER + 2 * i); //<<<<<<<<<<<
        if (locator == 0xFF)  // found the EEPROM circular buffer index
        {
          eepromIndex = EEPROM_BUFFER + 2 * i;
          //Now that we have the buffer index let's populate the rainBucket with data from eeprom
          loadRainArray(eepromIndex);
          isDataOnEeprom = true;
          DEBUG_PRINT("EEPROM Index ");
          DEBUG_PRINTLN(eepromIndex);
          isDataOnEeprom = true;
          break;
        }
      }
      if (!isDataOnEeprom) // Added for the first time it is run on a new arduino
      {
        eepromIndex = 1;
        gw.saveState(eepromIndex, 0xFF);
        gw.saveState(eepromIndex + 1, 0x00);
      }
      // reset the timers
      dataMillis = millis();
      startMillis = millis();
      lastTipTime = millis() - oneHour;
      gw.request(CHILD_ID_TRIPPED_RAIN, V_VAR1); //Get rainWindow from controller (Vera)
      delay(250);
      gw.request(CHILD_ID_TRIPPED_RAIN, V_VAR2); //Get rainSensorThreshold from controller (Vera)
      delay(250);
      DEBUG_PRINTLN(F("Radio Setup Complete!"));
    }
    //
    void loop()
    {
      gw.process();
      if (state)
      {
        prettyFade();  // breath if tripped
      }
      else
      {
        slowFlash();   // blink if not tripped
      }
      //
      // let's constantly check to see if the rain in the past rainWindow hours is greater than rainSensorThreshold
      //
      int measure = 0; // Check to see if we need to show sensor tripped in this block
      for (int i = 0; i < rainWindow; i++)
      {
        measure += rainBucket [i];
        DEBUG_PRINT(F("measure value (total rainBucket within rainWindow): "));
        DEBUG_PRINTLN(measure);
      }
      //
      state = (measure >= rainSensorThreshold);
      if (state != oldState)
      {
        gw.send(msgTripped.set(state));
        delay(250);
        gw.saveState(STATE_LOCATION, state); //New Code
        DEBUG_PRINT(F("State Changed. Tripped State: "));
        DEBUG_PRINTLN(state);
        oldState = state;
      }
      //
      // Now lets reset the rainRate to zero if no tips in the last hour
      //
      if (millis() - lastTipTime >= oneHour)// timeout for rain rate
      {
        if (rainRate != 0)
        {
          rainRate = 0;
          gw.send(msgRainRate.set(0));
          delay(250);
        }
      }
    
    #ifdef DEBUG_ON
      if ( (millis() - dataMillis) >= serialInterval)
      {
        for (int i = 24; i <= 120; i = i + 24)
        {
          updateSerialData(i);
        }
        dataMillis = millis();
      }
    #endif
      //
      if (tipBuffer > 0)
      {
        DEBUG_PRINTLN(F("Sensor Tipped"));
        DEBUG_PRINT(F("rainBucket [0] value: "));
        DEBUG_PRINTLN(rainBucket [0]);
        //
        int dayTotal = 0;
        for (int i = 0; i < 24; i++)
        {
          dayTotal = dayTotal + rainBucket [i];
        }
        //
        DEBUG_PRINT(F("dayTotal value: "));
        DEBUG_PRINTLN(dayTotal);
        gw.send(msgRain.set(dayTotal, 1));
        delay(250);
        //
        unsigned long tipDelay = millis() - lastTipTime;
        if (tipDelay <= oneHour)
        {
          rainRate = ((oneHour) / tipDelay);
          gw.send(msgRainRate.set(rainRate, 1));
          DEBUG_PRINT(F("RainRate= "));
          DEBUG_PRINTLN(rainRate);
        }
        //
        //If this is the first trip in an hour send .1
        //
        else
        {
          gw.send(msgRainRate.set( (float) CALIBRATE_FACTOR / 100.0, 1));//<<<<<<<<<<<<<<calibrate?
        }
        lastTipTime = millis();
        tipBuffer--;
      }
      //
      if (millis() - startMillis >= oneHour)
      {
        DEBUG_PRINTLN(F("One hour elapsed."));
        //EEPROM write last value
        gw.saveState(eepromIndex, highByte(rainBucket[0]));
        gw.saveState(eepromIndex + 1, lowByte(rainBucket[0]));
        eepromIndex++;
        if (eepromIndex > EEPROM_BUFFER + BUFFER_LENGTH) eepromIndex = EEPROM_BUFFER;
        DEBUG_PRINT(F("Writing to EEPROM.  Index: "));
        DEBUG_PRINTLN(eepromIndex);
        gw.saveState(eepromIndex, 0xFF);
        gw.saveState(eepromIndex + 1, 0x00);
        //
        for (int i = BUFFER_LENGTH - 1; i >= 0; i--)//cascade an hour of values
        {
          rainBucket [i + 1] = rainBucket [i];
        }
        rainBucket[0] = 0;
        gw.send(msgRain.set(tipCounter(24), 1)); // send 24hr tips
        delay(250);
        transmitRainData(); // send all of the 5 buckets of data to controller
        delay(250);
        gw.request(CHILD_ID_TRIPPED_RAIN, V_VAR1);
        delay(250);
        gw.request(CHILD_ID_TRIPPED_RAIN, V_VAR2);
        startMillis = millis();
      }
    }
    //
    void sensorTipped()
    {
      static unsigned long last_interrupt_time = 0;
      unsigned long interrupt_time = millis();
      if (interrupt_time - last_interrupt_time > 200)
      {
        rainBucket[0] += CALIBRATE_FACTOR; // adds CALIBRATE_FACTOR hundredths of unit each tip
      }
      last_interrupt_time = interrupt_time;
    }
    //
    float tipCounter(int hours)
    {
      float tipCount = 0;
      for ( int i = 0; i < hours; i++)
      {
        tipCount = tipCount + rainBucket [i];
      }
      return tipCount;
    }
    //
    void updateSerialData(int x)
    {
      DEBUG_PRINT(F("Tips last "));
      DEBUG_PRINT(x);
      DEBUG_PRINTLN(F(" hours: "));
      int tipCount = 0;
      for (int i = 0; i < x; i++)
      {
        tipCount = tipCount + rainBucket [i];
      }
      DEBUG_PRINTLN(tipCount);
    }
    void loadRainArray(int value)
    {
      for (int i = 0; i < BUFFER_LENGTH - 1; i++)
      {
        value--;
        DEBUG_PRINT("EEPROM location: ");
        DEBUG_PRINTLN(value);
        if (value < EEPROM_BUFFER)
        {
          value = EEPROM_BUFFER + BUFFER_LENGTH;
        }
        byte rainValueHigh = gw.loadState(value);
        byte rainValueLow = gw.loadState(value + 1);
        int rainValue = (rainValueHigh << 8) & rainValueLow;
        rainBucket[i] = rainValue;
        //
        DEBUG_PRINT(F("rainBucket[ value: "));
        DEBUG_PRINT(i);
        DEBUG_PRINT(F("] value: "));
        DEBUG_PRINTLN(rainBucket[i]);
      }
    }
    
    void transmitRainData(void)
    {
      int rainUpdateTotal = 0;
      for (int i = 0; i < 24; i++)
      {
        rainUpdateTotal += rainBucket[i];
      }
      gw.send(msgRainVAR1.set(( float) rainUpdateTotal / 100.0 , 1));
      delay(250);
      for (int i = 24; i < 48; i++)
      {
        rainUpdateTotal += rainBucket[i];
      }
      gw.send(msgRainVAR2.set( (float) rainUpdateTotal / 100.0 , 1));
      delay(250);
      for (int i = 48; i < 72; i++)
      {
        rainUpdateTotal += rainBucket[i];
      }
      gw.send(msgRainVAR3.set( (float) rainUpdateTotal / 100.0 , 1));
      delay(250);
      for (int i = 72; i < 96; i++)
      {
        rainUpdateTotal += rainBucket[i];
      }
      gw.send(msgRainVAR4.set( (float) rainUpdateTotal / 100.0 , 1));
      delay(250);
      for (int i = 96; i < 120; i++)
      {
        rainUpdateTotal += rainBucket[i];
      }
      gw.send(msgRainVAR5.set( (float) rainUpdateTotal / 100.0 , 1));
      delay(250);
    }
    
    void getVariables(const MyMessage &message)
    {
      if (message.sensor == CHILD_ID_RAIN)
      {
        // nothing to do here
      }
      else if (message.sensor == CHILD_ID_TRIPPED_RAIN)
      {
        if (message.type == V_VAR1)
        {
          rainWindow = atoi(message.data);
          if (rainWindow > 120)
          {
            rainWindow = 120;
          }
          else if (rainWindow < 1)
          {
            rainWindow = 1;
          }
          if (rainWindow != atoi(message.data))   // if I changed the value back inside the boundries, push that number back to Vera
          {
            gw.send(msgTrippedVar1.set(rainWindow));
          }
        }
        else if (message.type == V_VAR2)
        {
          rainSensorThreshold = atoi(message.data);
          if (rainSensorThreshold > 10000)
          {
            rainSensorThreshold = 10000;
          }
          else if (rainSensorThreshold < 1)
          {
            rainSensorThreshold = 1;
          }
          if (rainSensorThreshold != atoi(message.data))  // if I changed the value back inside the boundries, push that number back to Vera
          {
            gw.send(msgTrippedVar2.set(rainSensorThreshold));
          }
        }
      }
    }
    //
    void prettyFade(void)
    {
      float val = (exp(sin(millis() / 2000.0 * PI)) - 0.36787944) * 108.0;
      analogWrite(ledPin, val);
    }
    
    void slowFlash(void)
    {
      static boolean ledState = true;
      static unsigned long pulseStart = millis();
      if (millis() - pulseStart < 100UL)
      {
        digitalWrite(ledPin, !ledState);
        pulseStart = millis();
      }
    }
    

    EDIT, modified some serial.print()s that needed editing for the debug toggle

    1 Reply Last reply
    0
    • petewillP Offline
      petewillP Offline
      petewill
      Admin
      wrote on last edited by
      #58

      @BulldogLowell
      Thanks for the comments and sorry for the delayed response. It was a busy weekend and I didn't get much time in front of the computer.

      I had a whack of combining your desire to use floats, and my desire not to use floats!

      Haha! I only used floats because I don't know any better. I'm curious, why the dislike of floats?

      Once you get it done, let's take @hek 's idea and put it into github.

      Is this something you would like me to do since you made it way better :) I'm happy to do it but I don't want to overstep. I consider this your handy work that I'm just happy to use.

      Take a look at this and let me know if you like it, note it is untested and I still have to test the circular buffer storing ints instead of bytes.

      Great! I will take a look some time this week. I mounted my gauge on the house today because we were supposed to get some rain and I wanted to see how it worked in real life. Unfortunately we didn't get any... Hopefully we will get some in the next couple of days before I pull it off to do some more testing.

      we also need to create image files for the Sensor, but for now, if you want to do the tutorial, I say go for it!

      I was thinking of going through how I made my gauge by using an existing rain gauge. I'd go over how to do the calculations for the tipping bucket calibration, uploading the files to Vera and how I added the MySensors stuff to the existing gauge. I will definitely mention the 3d printing option as well

      Thanks for your help!.

      My "How To" home automation video channel: https://www.youtube.com/channel/UCq_Evyh5PQALx4m4CQuxqkA

      BulldogLowellB 1 Reply Last reply
      0
      • petewillP petewill

        @BulldogLowell
        Thanks for the comments and sorry for the delayed response. It was a busy weekend and I didn't get much time in front of the computer.

        I had a whack of combining your desire to use floats, and my desire not to use floats!

        Haha! I only used floats because I don't know any better. I'm curious, why the dislike of floats?

        Once you get it done, let's take @hek 's idea and put it into github.

        Is this something you would like me to do since you made it way better :) I'm happy to do it but I don't want to overstep. I consider this your handy work that I'm just happy to use.

        Take a look at this and let me know if you like it, note it is untested and I still have to test the circular buffer storing ints instead of bytes.

        Great! I will take a look some time this week. I mounted my gauge on the house today because we were supposed to get some rain and I wanted to see how it worked in real life. Unfortunately we didn't get any... Hopefully we will get some in the next couple of days before I pull it off to do some more testing.

        we also need to create image files for the Sensor, but for now, if you want to do the tutorial, I say go for it!

        I was thinking of going through how I made my gauge by using an existing rain gauge. I'd go over how to do the calculations for the tipping bucket calibration, uploading the files to Vera and how I added the MySensors stuff to the existing gauge. I will definitely mention the 3d printing option as well

        Thanks for your help!.

        BulldogLowellB Offline
        BulldogLowellB Offline
        BulldogLowell
        Contest Winner
        wrote on last edited by
        #59

        @petewill said:

        why the dislike of floats?

        their four-byte size on arduino make for a huge eeprom storage buffer that we really don't need, we will never have negative rain and we'd never be interested in precision below 0.01 inches or milimeters.

        @petewill said:

        I mounted my gauge on the house today

        terrific, glad to see it make its way into the real world!

        petewillP 1 Reply Last reply
        0
        • BulldogLowellB BulldogLowell

          @petewill said:

          why the dislike of floats?

          their four-byte size on arduino make for a huge eeprom storage buffer that we really don't need, we will never have negative rain and we'd never be interested in precision below 0.01 inches or milimeters.

          @petewill said:

          I mounted my gauge on the house today

          terrific, glad to see it make its way into the real world!

          petewillP Offline
          petewillP Offline
          petewill
          Admin
          wrote on last edited by
          #60

          @BulldogLowell said:

          their four-byte size on arduino make for a huge eeprom storage buffer that we really don't need

          Good to know! I still have a lot to learn about all this stuff. It's definitely fun.

          terrific, glad to see it make its way into the real world!

          Yes, me too! I'm happy to report it's working. It rained 6.4mm last night. SO COOL!

          My "How To" home automation video channel: https://www.youtube.com/channel/UCq_Evyh5PQALx4m4CQuxqkA

          BulldogLowellB 1 Reply Last reply
          2
          • petewillP petewill

            @BulldogLowell said:

            their four-byte size on arduino make for a huge eeprom storage buffer that we really don't need

            Good to know! I still have a lot to learn about all this stuff. It's definitely fun.

            terrific, glad to see it make its way into the real world!

            Yes, me too! I'm happy to report it's working. It rained 6.4mm last night. SO COOL!

            BulldogLowellB Offline
            BulldogLowellB Offline
            BulldogLowell
            Contest Winner
            wrote on last edited by
            #61

            @petewill

            :thumbsup:

            big thumbs up!

            1 Reply Last reply
            0
            • BulldogLowellB Offline
              BulldogLowellB Offline
              BulldogLowell
              Contest Winner
              wrote on last edited by
              #62

              how about adding windspeed to the device?

              hekH 1 Reply Last reply
              0
              • BulldogLowellB BulldogLowell

                how about adding windspeed to the device?

                hekH Offline
                hekH Offline
                hek
                Admin
                wrote on last edited by
                #63

                @BulldogLowell said:

                how about adding windspeed to the device?

                Cool device. Haven't seen that one before.

                1 Reply Last reply
                0
                • petewillP Offline
                  petewillP Offline
                  petewill
                  Admin
                  wrote on last edited by
                  #64

                  @BulldogLowell Cool! Do you think it detects wind from all directions? Would this be better than the traditional spinning cup design?

                  My "How To" home automation video channel: https://www.youtube.com/channel/UCq_Evyh5PQALx4m4CQuxqkA

                  1 Reply Last reply
                  0
                  • BulldogLowellB Offline
                    BulldogLowellB Offline
                    BulldogLowell
                    Contest Winner
                    wrote on last edited by BulldogLowell
                    #65

                    @hek
                    @petewill

                    A little progress on the device...

                    Screen Shot 2015-05-09 at 11.07.41 PM.png

                    Screen Shot 2015-05-09 at 11.17.11 PM.png

                    Screen Shot 2015-05-09 at 11.07.28 PM.png

                    Screen Shot 2015-05-09 at 11.11.29 PM.png

                    BulldogLowellB 1 Reply Last reply
                    1
                    • BulldogLowellB BulldogLowell

                      @hek
                      @petewill

                      A little progress on the device...

                      Screen Shot 2015-05-09 at 11.07.41 PM.png

                      Screen Shot 2015-05-09 at 11.17.11 PM.png

                      Screen Shot 2015-05-09 at 11.07.28 PM.png

                      Screen Shot 2015-05-09 at 11.11.29 PM.png

                      BulldogLowellB Offline
                      BulldogLowellB Offline
                      BulldogLowell
                      Contest Winner
                      wrote on last edited by BulldogLowell
                      #66

                      electronics box:

                      Screen Shot 2015-05-10 at 10.08.29 AM.png

                      Screen Shot 2015-05-10 at 10.09.57 AM.png

                      1 Reply Last reply
                      0
                      • FrancoisF Offline
                        FrancoisF Offline
                        Francois
                        wrote on last edited by Francois
                        #67

                        @BulldogLowell this looks great once you complete it I will print it. Only problem our raining session is over till Dec. But I can start the build so long :).

                        1 Reply Last reply
                        1
                        • hekH Offline
                          hekH Offline
                          hek
                          Admin
                          wrote on last edited by
                          #68

                          Nice. But I'm a bit worried about water finding its way into the electronics. Would it be possible to make a cavity for the electronics box in the bottom plate?

                          1431266959242-screen-shot-2015-05-10-at-10.08.29-am.png

                          BulldogLowellB 1 Reply Last reply
                          0
                          • hekH hek

                            Nice. But I'm a bit worried about water finding its way into the electronics. Would it be possible to make a cavity for the electronics box in the bottom plate?

                            1431266959242-screen-shot-2015-05-10-at-10.08.29-am.png

                            BulldogLowellB Offline
                            BulldogLowellB Offline
                            BulldogLowell
                            Contest Winner
                            wrote on last edited by BulldogLowell
                            #69

                            @hek
                            recessing the electronics into the bottom would be tough in this design. I was taking advantage on the two parts to use the smooth plate of the printer, and then make a gasket or even use silicone. lemme think about it. I could add a couple of deflectors on the bottom plate to glue on around where the weep holes are...

                            PS enough room for 2 C sized cells

                            bottom view:

                            Screen Shot 2015-05-10 at 11.32.50 AM.png

                            Screen Shot 2015-05-10 at 11.44.44 AM.png

                            1 Reply Last reply
                            1
                            • petewillP Offline
                              petewillP Offline
                              petewill
                              Admin
                              wrote on last edited by
                              #70

                              @BulldogLowell Awesome!

                              I kind of wish I didn't have mine built already :)

                              I may have missed it but did you have any mounting holes (for the side of a building or pole) What is your vision for that?

                              It would also be cool to see some place to add a DHT22 in there. On mine I added one of those as well as a BH1750 for light. I'm not sure how long that will hold up though.

                              My "How To" home automation video channel: https://www.youtube.com/channel/UCq_Evyh5PQALx4m4CQuxqkA

                              BulldogLowellB 2 Replies Last reply
                              0
                              • petewillP petewill

                                @BulldogLowell Awesome!

                                I kind of wish I didn't have mine built already :)

                                I may have missed it but did you have any mounting holes (for the side of a building or pole) What is your vision for that?

                                It would also be cool to see some place to add a DHT22 in there. On mine I added one of those as well as a BH1750 for light. I'm not sure how long that will hold up though.

                                BulldogLowellB Offline
                                BulldogLowellB Offline
                                BulldogLowell
                                Contest Winner
                                wrote on last edited by
                                #71

                                @petewill said:

                                It would also be cool to see some place to add a DHT22 in there. On mine I added one of those as well as a BH1750 for light. I'm not sure how long that will hold up though.

                                I'll try to fit that in, if I can, but I'm really focusing on sealing up the electronics. A skirt around the electronics housing may be enough...

                                I was thinking of a female 'flange-like' option for the unit to sit at the top of some galvanized pipe. It could be mounted that way on the side of a house, a fence or just a pole (back of your basketball backboard!). So, the yellow-brown part will be of two options to print.

                                hekH 1 Reply Last reply
                                0
                                • BulldogLowellB BulldogLowell

                                  @petewill said:

                                  It would also be cool to see some place to add a DHT22 in there. On mine I added one of those as well as a BH1750 for light. I'm not sure how long that will hold up though.

                                  I'll try to fit that in, if I can, but I'm really focusing on sealing up the electronics. A skirt around the electronics housing may be enough...

                                  I was thinking of a female 'flange-like' option for the unit to sit at the top of some galvanized pipe. It could be mounted that way on the side of a house, a fence or just a pole (back of your basketball backboard!). So, the yellow-brown part will be of two options to print.

                                  hekH Offline
                                  hekH Offline
                                  hek
                                  Admin
                                  wrote on last edited by
                                  #72

                                  @BulldogLowell said:

                                  I was taking advantage on the two parts to use the smooth plate of the printer

                                  Of course, didn't think that long.

                                  1 Reply Last reply
                                  0
                                  • petewillP petewill

                                    @BulldogLowell Awesome!

                                    I kind of wish I didn't have mine built already :)

                                    I may have missed it but did you have any mounting holes (for the side of a building or pole) What is your vision for that?

                                    It would also be cool to see some place to add a DHT22 in there. On mine I added one of those as well as a BH1750 for light. I'm not sure how long that will hold up though.

                                    BulldogLowellB Offline
                                    BulldogLowellB Offline
                                    BulldogLowell
                                    Contest Winner
                                    wrote on last edited by
                                    #73

                                    @petewill

                                    Screen Shot 2015-05-10 at 12.49.56 PM.png

                                    1 Reply Last reply
                                    0
                                    • A Offline
                                      A Offline
                                      Andreas Maurer
                                      wrote on last edited by
                                      #74

                                      If you add a little edge around the holes, the water cannt flow into the electronic box.

                                      1 Reply Last reply
                                      0
                                      • petewillP Offline
                                        petewillP Offline
                                        petewill
                                        Admin
                                        wrote on last edited by
                                        #75

                                        @BulldogLowell Awesome. I would love to get a 3d printer. Someday.

                                        My "How To" home automation video channel: https://www.youtube.com/channel/UCq_Evyh5PQALx4m4CQuxqkA

                                        1 Reply Last reply
                                        0
                                        • BulldogLowellB Offline
                                          BulldogLowellB Offline
                                          BulldogLowell
                                          Contest Winner
                                          wrote on last edited by
                                          #76

                                          @petewill

                                          As it happens, I am sort of turning the code on its head for this update. Do you want to use accumulated rain over X days as the trigger?

                                          I was thinking about storing the time on EEPROM and retrieving the stored rain values from VERA for each of the last 5 days after a power-up/restart. This saves from having to create too much EEPROM management, plus we can observe how much time has passed in the event of a power-off. That is, if the unit starts up, we test EEPROM to see what was the last day of rain recorded. If it hasn't been more than one, then we retrieve the values from VERA that were previously sent up. Sound OK to you?

                                          I would save an epoch timestamp once each day at midnight when we total today's rain and cascade the values back a day.

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


                                          13

                                          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