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 Offline
    petewillP Offline
    petewill
    Admin
    wrote on last edited by
    #35

    @BulldogLowell Did you ever get around to updating the sketch to work with version 1.4.1? I'm finally getting around to making a rain gauge and I didn't want to recreate the wheel if you already made it and I missed it somewhere.

    Thanks,

    Pete

    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
      #36

      @petewill

      Sorry for the lag time, I've been on Easter vacation with the family!

      Yes, I did update it, but I don't have my laptop with me here. I'll post it when I return home, early next week.

      petewillP 1 Reply Last reply
      0
      • BulldogLowellB BulldogLowell

        @petewill

        Sorry for the lag time, I've been on Easter vacation with the family!

        Yes, I did update it, but I don't have my laptop with me here. I'll post it when I return home, early next week.

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

        @BulldogLowell Great, thanks! You are a way better coder than me and I was struggling to understand the code. I did learn quite a bit about interrupts when I was trying to figure it out though.

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

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

          @BulldogLowell Any luck finding the sketch? Sorry to bug you, I was just hoping to get this project completed soon so I can work on the irrigation controller next. It's almost time to start watering the lawn/plants... Thanks!

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

          BulldogLowellB 2 Replies Last reply
          0
          • petewillP petewill

            @BulldogLowell Any luck finding the sketch? Sorry to bug you, I was just hoping to get this project completed soon so I can work on the irrigation controller next. It's almost time to start watering the lawn/plants... Thanks!

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

            @petewill
            I am embarrassed to tell you that after updating my arduino software a while back, I cannot locate a compilable sketch!

            I have Time Machine, but still can't find it... and it is running on my rain guage!!!

            Here is a version that I have (which is titled RainGuage_V1.4.1.ino in my directory) which i found but cannot compile, it looks like a first save without edit?

            I need to completely update Arduino, the version and mySensors but please look at this:

            /*
             Arduino Tipping Bucket Rain Gauge
             
             June 15, 2014
             
             Version 1.00b   
             
             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 EEBPROM
             * 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 <EEPROM.h>  
            #include <RF24.h>
            #include <Sensor.h>
            //
            #define STATE_LOCATION 513 // stay away from EEPROM used with Sensor.h
            #define EEPROM_BUFFER 514  // location of the EEPROM circular buffer
            #define BUFFER_LENGTH 121
            //
            Sensor gw;
            boolean metric = false;
            //
            int eepromIndex;
            int tipSensorPin = 3;
            int ledPin = 5; //PWM capable required
            unsigned long dataMillis;
            unsigned long serialInterval = 10000UL;
            const unsigned long oneHour = 60000UL;
            unsigned long lastTipTime;
            unsigned long lastBucketInterval;
            unsigned long startMillis;
            int dayBuckets [5] [2] = {
             { V_VAR1, 24 },
             { V_VAR2, 48 },
             { V_VAR3, 72 },
             { V_VAR4, 96 },
             { V_VAR5, 120},
             };
            volatile byte rainBucket [120] ; // 5 days of data
            const unsigned long calibrateFactor = 1UL; //Calibration in milimeters of rain per single tip
            unsigned long rainRate = 0;
            float currentRain = 0;
            boolean wasTipped = false;
            boolean updateVera;
            int rainCount;
            volatile int tipBuffer = 0;
            byte rainWindow = 72;//default rain window in hours
            int rainSensorThreshold = 15;//default rain sensor sensitivity in mm
            byte hourCount = 24;
            byte state;
            byte oldState = -1;
            unsigned long ledPulseTime = 500UL;
            unsigned long pulseStart;
            //
            void setup()  
            { 
              Serial.begin(115200);
              gw.begin();
              //
              pinMode(tipSensorPin, OUTPUT);
              attachInterrupt (1, sensorTipped, CHANGE);
              pinMode(ledPin, OUTPUT);
              digitalWrite(ledPin, HIGH);
              gw.sendSketchInfo("Rain Guage", "1.00b"); 
              gw.sendSensorPresentation(1, S_RAIN);
              gw.sendSensorPresentation(2, S_MOTION);
              Serial.println(F("Sensor Presentation Complete"));
              pinMode(tipSensorPin, INPUT);
              //
              state = EEPROM.read(STATE_LOCATION);
              for (int i = 0; i < BUFFER_LENGTH; i++)
              {
                byte locator = EEPROM.read(EEPROM_BUFFER + i);
                if (locator == 0xFF)
                {
                  eepromIndex = EEPROM_BUFFER + i;
                  loadRainArray(eepromIndex);
                  Serial.println(eepromIndex);
                  break;
                }
              }
              //
              gw.sendVariable(2, V_TRIPPED, state);
              dataMillis = millis();
              startMillis = millis();
              lastTipTime = millis() - oneHour;
              // uncomment the following block the first time you include this device.  The delays
              // are necessary in order to create the five Variable buckets in the correct order.
              // Once you create them, you can comment out these lines and re-upload to your arduino
              //------------------------------------------------------
              /*
              for (int j = 0; j < 5; j++)
               {
               delay(2000);
               gw.sendVariable(1, dayBuckets[j] [0], 1);
               }
               delay(2000);
               gw.sendVariable(2,V_VAR1, 1);
               delay(2000);
               gw.sendVariable(2,V_VAR2, 1);
               */
              //------------------------------------------------------  
              rainWindow = atoi(gw.getStatus(2, V_VAR1));
              delay(2000);
              rainSensorThreshold = atoi(gw.getStatus(2, V_VAR2));
              gw.sendVariable(1, V_RAINRATE, 0);
              Serial.print(F("Radio Done"));  
              analogWrite(ledPin, 20);
            }
            //
            void loop()     
            { 
              unsigned long measure = 0; // Check to see if we need to show sensor tripped in this block
              for (int i = 0; i < rainWindow; i++)
              {
                measure = measure + rainBucket [i];
              }
              measure >= rainSensorThreshold ? state = 1: state = 0;
              if (millis() - pulseStart < ledPulseTime)
              {
                analogWrite(ledPin, 255);
              }
              else 
              {
                analogWrite(ledPin, 20);
              }
              if (state != oldState)
              {
                gw.sendVariable(2, V_TRIPPED, state);
                EEPROM.write(STATE_LOCATION, state);
                oldState = state;
              }
              //
              if (millis() - lastTipTime >= oneHour)// timeout for rain rate
              {
                if (rainRate != 0)
                {
                  rainRate = 0;
                  gw.sendVariable(1, V_RAINRATE, rainRate);
                }
              }
              if (updateVera)
              {
                gw.sendVariable(1, V_RAINRATE, rainRate);
                updateVera = false;
              }
              //
              if ( (millis() - dataMillis) >= serialInterval)//Comment back in this block to enable Serial prints
              {
                for (int i = 24; i <= 120; i=i+24)
                {
                  updateSerialData(i);
                }
                dataMillis = millis();
              }
              //
              if (tipBuffer > 0)
              {
                Serial.println(F("Sensor Tipped"));
                rainBucket [0] ++;
                pulseStart = millis();
                if (rainBucket [0] > 253) rainBucket[0] = 253; // odd occurance but prevent overflow
                int dayTotal = 0;
                for (int i = 0; i < 24; i++)
                {
                  dayTotal = dayTotal + rainBucket [i];
                }
                gw.sendVariable(1, V_RAIN, dayTotal);
                unsigned long tipDelay = millis() - lastTipTime;
                if (tipDelay <= oneHour) 
                {
                  rainRate = ((oneHour) / tipDelay);
                  gw.sendVariable(1, V_RAINRATE, rainRate);
                  Serial.print(F("RainRate= "));
                  Serial.println(rainRate);
                }
                lastTipTime = millis();
                updateVera = true;
                tipBuffer--;
              }
              if ( (millis()- startMillis) >= oneHour)
              {
                Serial.println("one hour elapsed");
                //EEPROM write last value
                EEPROM.write(eepromIndex, rainBucket[0]);
                eepromIndex++;
                if (eepromIndex > EEPROM_BUFFER + BUFFER_LENGTH) eepromIndex = EEPROM_BUFFER;
                Serial.println(eepromIndex);
                EEPROM.write(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.sendVariable(1, V_RAIN, tipCounter(24));// send 24hr tips
                startMillis = millis();
                for (int j = 0; j < 5; j++)
                {
                  int total = 0;
                  for (int i = 0; i < dayBuckets[j][1]; i++)
                  {
                    total = total + rainBucket [i];
                  }
                  gw.sendVariable( 1, dayBuckets[j] [0], total);
                }
                hourCount++;
                if (hourCount >=3)//8 times daily update the Sensor variables
                {
                  rainWindow = atoi(gw.getStatus(2, V_VAR1));
                  if (rainWindow < 6) 
                  {
                    rainWindow = 6;
                    gw.sendVariable( 2, V_VAR1, rainWindow);
                  }
                  if (rainWindow > 120) 
                  {
                    rainWindow = 120;
                    gw.sendVariable( 2, V_VAR2, rainWindow);
                  }
                  rainSensorThreshold = atoi(gw.getStatus(2, V_VAR2));
                  delay(2000);
                  //
                  if (rainSensorThreshold < 1) 
                  {
                    rainSensorThreshold = 1;
                    gw.sendVariable( 2, V_VAR2, rainSensorThreshold);
                  }
                  if (rainSensorThreshold > 1000) 
                  {
                    rainSensorThreshold = 1000;
                    gw.sendVariable( 2, V_VAR2, rainSensorThreshold);
                  }
                  //
                  hourCount = 0;
                }
              }
            }
            //
            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;
            }
            //
            unsigned long tipCounter(int x)
            {
              int tipCount = 0;
              for ( int i = 0; i < x; i++)
              {
                tipCount = tipCount + rainBucket [i];
              }
              return tipCount;
            }
            //
            void updateSerialData(int x)
            {
              Serial.print(F("Tips last "));
              Serial.print(x);
              Serial.print(F("hours: "));
              int 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.println(value);
                 if (value < EEPROM_BUFFER) 
                   {
                     value = EEPROM_BUFFER + BUFFER_LENGTH;
                   }
                 byte rainValue = EEPROM.read(value);
                 Serial.println(rainValue);
                 if (rainValue < 255)
                 {
                   rainBucket[i] = rainValue;
                 }
                 else
                 {
                   rainBucket [i] = 0;
                 }
               }
            }
            
            
            
            

            @petewill

            I will convert this this weekend.

            1 Reply Last reply
            0
            • petewillP petewill

              @BulldogLowell Any luck finding the sketch? Sorry to bug you, I was just hoping to get this project completed soon so I can work on the irrigation controller next. It's almost time to start watering the lawn/plants... Thanks!

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

              @petewill

              Her is an untested go at it, and a very down and dirty update... I will test it probably Wednesday night, when I am back home. Let me know if you can!

              /*
               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>
              #include <EEPROM.h>
              
              #define RADIO_ID 99
              
              #define STATE_LOCATION 513 // stay away from EEPROM used with Sensor.h
              #define EEPROM_BUFFER 514  // location of the EEPROM circular buffer
              #define BUFFER_LENGTH 121  // buffer plus the current hour
              //
              MySensor gw;
              //
              MyMessage msgRainRate(1, V_RAINRATE);
              MyMessage msgRain(1, V_RAIN);
              MyMessage msgRainVAR1(1, V_VAR1);
              MyMessage msgRainVAR2(1, V_VAR2);
              MyMessage msgRainVAR3(1, V_VAR3);
              MyMessage msgRainVAR4(1, V_VAR4);
              MyMessage msgRainVAR5(1, V_VAR5);
              
              MyMessage msgTripped(2, S_MOTION);
              MyMessage msgTrippedVar1(2, V_VAR1);
              MyMessage msgTrippedVar2(2, V_VAR2);
              //
              boolean metric = false;
              //
              int eepromIndex;
              int tipSensorPin = 3;
              int ledPin = 5; //PWM capable required
              unsigned long dataMillis;
              unsigned long serialInterval = 10000UL;
              const unsigned long oneHour = 60000UL;
              unsigned long lastTipTime;
              unsigned long lastBucketInterval;
              unsigned long startMillis;
              int dayBuckets [5] [2] = {
                { V_VAR1, 24 },
                { V_VAR2, 48 },
                { V_VAR3, 72 },
                { V_VAR4, 96 },
                { V_VAR5, 120},
              };
              volatile byte rainBucket [120] ; // 5 days of data
              const unsigned long calibrateFactor = 1UL; //Calibration in milimeters of rain per single tip
              unsigned long rainRate = 0;
              float currentRain = 0;
              boolean wasTipped = false;
              boolean updateVera;
              int rainCount;
              volatile int tipBuffer = 0;
              byte rainWindow = 72;         //default rain window in hours
              int rainSensorThreshold = 15; //default rain sensor sensitivity in mm
              byte hourCount = 24;
              byte state;
              byte oldState = -1;
              //
              void setup()
              {
                Serial.begin(115200);
                gw.begin(getVariables, RADIO_ID);
                //
                pinMode(tipSensorPin, OUTPUT);
                attachInterrupt (1, sensorTipped, CHANGE);
                pinMode(ledPin, OUTPUT);
                digitalWrite(ledPin, HIGH);
                gw.sendSketchInfo("Rain Guage", "1.01b");
                gw.present(1, S_RAIN);
                gw.present(2, S_MOTION);
                Serial.println(F("Sensor Presentation Complete"));
                pinMode(tipSensorPin, INPUT);
                //
                state = EEPROM.read(STATE_LOCATION);    //retreive prior state from EEPROM
                gw.send(msgTripped.set(state), false);  //send state to Vera
                delay(250);                             // recharge the capacitor
                //
                boolean isDataOnEeprom = false;
                for (int i = 0; i < BUFFER_LENGTH; i++)
                {
                  byte locator = EEPROM.read(EEPROM_BUFFER + i);
                  if (locator == 0xFF)  // found the EEPROM circular buffer index
                  {
                    eepromIndex = EEPROM_BUFFER + i;
                    loadRainArray(eepromIndex);
                    isDataOnEeprom = true;
                    Serial.println(eepromIndex);
                    break;
                  }
                }
                //
                if (!isDataOnEeprom)
                {
                  // load all zeroes to EEPROM?
                  // I gotta check this out!!!
                }
                //
                // reset the timers
                dataMillis = millis();
                startMillis = millis();
                lastTipTime = millis() - oneHour;
                gw.request(2, V_VAR1);
                delay(250);
                gw.request(2, V_VAR2);
                delay(250);
                Serial.print(F("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
                //
                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];
                }
                state = (measure >= rainSensorThreshold);
                if (state != oldState)
                {
                  gw.send(msgTripped.set(state), false);
                  delay(250);
                  EEPROM.write(STATE_LOCATION, 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);
                  }
                }
                // if tipped,send updates to VERA
                //
                if (updateVera)
                {
                  gw.send(msgRainRate.set(rainRate));
                  delay(250);
                  updateVera = false;
                }
                /*
                if ( (millis() - dataMillis) >= serialInterval)//Comment back in this block to enable Serial prints
                {
                  for (int i = 24; i <= 120; i = i + 24)
                  {
                    updateSerialData(i);
                  }
                  dataMillis = millis();
                }
                */
                if (tipBuffer > 0)
                {
                  Serial.println(F("Sensor Tipped"));
                  rainBucket [0] ++;
                  //
                  if (rainBucket [0] > 253) rainBucket[0] = 253; // odd occurance but prevent overflow
                  int dayTotal = 0;
                  for (int i = 0; i < 24; i++)
                  {
                    dayTotal = dayTotal + rainBucket [i];
                  }
                  gw.send(msgRain.set(dayTotal));
                  delay(250);
                  unsigned long tipDelay = millis() - lastTipTime;
                  if (tipDelay <= oneHour)
                  {
                    rainRate = ((oneHour) / tipDelay);
                    gw.send(msgRainRate.set(rainRate));
                    Serial.print(F("RainRate= "));
                    Serial.println(rainRate);
                  }
                  lastTipTime = millis();
                  updateVera = true;
                  tipBuffer--;
                }
                if (millis() - startMillis >= oneHour)
                {
                  Serial.println("one hour elapsed");
                  //EEPROM write last value
                  EEPROM.write(eepromIndex, rainBucket[0]);
                  eepromIndex++;
                  if (eepromIndex > EEPROM_BUFFER + BUFFER_LENGTH) eepromIndex = EEPROM_BUFFER;
                  Serial.println(eepromIndex);
                  EEPROM.write(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)));// 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;
              }
              //
              int tipCounter(int hours)
              {
                int 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: "));
                int 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.println(value);
                  if (value < EEPROM_BUFFER)
                  {
                    value = EEPROM_BUFFER + BUFFER_LENGTH;
                  }
                  byte rainValue = EEPROM.read(value);
                  Serial.println(rainValue);
                  if (rainValue < 255)
                  {
                    rainBucket[i] = rainValue;
                  }
                  else
                  {
                    rainBucket [i] = 0;
                  }
                }
              }
              
              void transmitRainData(void)
              {
                int rainUpdateTotal = 0;
                for (int i = 0; i < 24; i++)
                {
                  rainUpdateTotal += rainBucket[i];
                }
                gw.send(msgRainVAR1.set(rainUpdateTotal));
                delay(250);
                for (int i = 24; i < 48; i++)
                {
                  rainUpdateTotal += rainBucket[i];
                }
                gw.send(msgRainVAR2.set(rainUpdateTotal));
                delay(250);
                for (int i = 48; i < 72; i++)
                {
                  rainUpdateTotal += rainBucket[i];
                }
                gw.send(msgRainVAR3.set(rainUpdateTotal));
                delay(250);
                for (int i = 72; i < 96; i++)
                {
                  rainUpdateTotal += rainBucket[i];
                }
                gw.send(msgRainVAR4.set(rainUpdateTotal));
                delay(250);
                for (int i = 96; i < 120; i++)
                {
                  rainUpdateTotal += rainBucket[i];
                }
                gw.send(msgRainVAR5.set(rainUpdateTotal));
                delay(250);
              }
              
              void getVariables(const MyMessage &message)
              {
                if (message.sensor == 1)
                {
                  // nothing to do here
                }
                else if (message.sensor == 2)
                {
                  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();
                }
              }
              
              
              
              1 Reply Last reply
              0
              • petewillP Offline
                petewillP Offline
                petewill
                Admin
                wrote on last edited by
                #41

                @BulldogLowell Awesome, thank you very much! I know what it's like to lose code that way. I have done the same thing myself...

                Yes, I will test this. I'll do my best to get it tested tonight. I'm combining it with some other sensors (light and temp/humidity) so once I get everything merged I'll be able to upload it.

                I'll let you know what I find.

                Thanks again!

                Pete

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

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

                  @BulldogLowell

                  Ok, I made the necessary changes for my application and I had a couple of questions along the way.

                  • I have never worked with eeprom so I don't pretend to know anything about it (it's now on my list of things to research though). Anyway, I noticed above hek recommended using the "saveState()/loadState() function in the MySensors library". Is that necessary in your code or are we ok as is? It seems to be functioning ok for me when I compile it so maybe not...

                  • I would like to trigger my rain sensor as soon as the first bucket has tipped (I have scenes that tell me to close the windows etc. when it's raining). Is changing the rainSensorThreshold value to 1 an appropriate way to do that or should I create another child node for my purposes? Maybe I should use rainRate in my PLEG instead?

                  • My tipping bucket was pulled from an existing rain gauge and according to my calculations (which may be off) each bucket is .7mm of rain. I don't know much about unsigned long variables but I'm wondering if I need to change "const unsigned long calibrateFactor" to a float or something?

                  Thanks for all your help!!

                  Pete

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

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

                    Jim,

                    I uploaded the code but it didn't seem to work for me. Do I need to upload some additional files to Vera? When I added the nodes a lot of my devices stopped displaying correctly (not just the newly added ones). They look like this:
                    upload-f009544f-e06e-4001-abba-63de772a744f

                    When I deleted the rain gauge nodes everything came back:
                    upload-2a04de4c-b861-4e84-892d-2ae37591ad0e

                    As I was looking at the code I came up with some additional questions:

                    • There is a dayBuckets array but it doesn't seem to do anything in the code?

                    • I also couldn't find "const unsigned long calibrateFactor" used anywhere.

                    I fully admit I'm not a very good with code and I could be missing something. I was just trying to find how the variables were working together and being calculated.

                    Thanks for all your help with this! I could not do it without you.

                    Pete

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

                    BulldogLowellB 1 Reply Last reply
                    0
                    • korttomaK Offline
                      korttomaK Offline
                      korttoma
                      Hero Member
                      wrote on last edited by
                      #44

                      @petewill The reason your MySensor plugin brakes when you add the rain gauge is that the device_files are missing. I don't know if there are any device files for the rain gauge since they are not included in the github branch for the vera plugin.

                      You could check under your rain gauge which device_file it is trying to use maybe you can search for it online and hope it exists.
                      Worst case is that you need to create them your self.

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

                        yeah, you need the implemention files:

                        D_RainSensor1.xml:

                        <?xml version="1.0"?>
                        <root xmlns="urn:schemas-upnp-org:device-1-0">
                          <specVersion>
                            <major>1</major>
                            <minor>0</minor>
                          </specVersion>
                          <device>
                            <deviceType>urn:schemas-micasaverde-com:device:RainSensor:1</deviceType>
                            <Category_Num>12</Category_Num>
                        	<staticJson>D_RainSensor1.json</staticJson>
                            <serviceList>
                              <service>
                                <serviceType>urn:schemas-upnp-org:service:RainSensor:1</serviceType>
                                <serviceId>urn:upnp-org:serviceId:RainSensor1</serviceId>
                                <SCPDURL>S_RainSensor1.xml</SCPDURL>
                              </service>
                              <service>
                                <serviceType>urn:schemas-micasaverde-com:service:HaDevice:1</serviceType>
                                <serviceId>urn:micasaverde-com:serviceId:HaDevice1</serviceId>
                                <SCPDURL>S_HaDevice1.xml</SCPDURL>
                              </service>
                            </serviceList>
                          </device>
                        </root>
                        

                        and

                        D_RainSensor1.json

                        {
                            "flashicon": "icons/Humidity_Sensor.swf",
                            "imgIconBody": "",
                            "imgIconDimmable": "",
                            "imgIconTurnable": "",
                            "imgIconMin": "",
                            "imgIconMax": "",
                            "halloIconsDir": "pics\/hallo",
                            "inScene": "0",
                            "DisplayStatus": {},
                            "doc_url": {
                                "doc_language": 1,
                                "doc_manual": 1,
                                "doc_version": 1,
                                "doc_platform": 0,
                                "doc_page": "sensors" 
                            },
                        	
                            "Tabs": [
                                {
                                    "Label": {
                                        "lang_tag": "tabname_control",
                                        "text": "Information" 
                                    },
                                    "Position": "0",
                                    "TabType": "flash",
                                    "SceneGroup": [
                                        {
                                            "id": "1",
                                            "top": "1.5",
                                            "left": "0",
                                            "x": "2",
                                            "y": "2"
                                        }
                                    ],
                                    "ControlGroup": [
                                        {
                                            "id": "1",
                                            "scenegroup": "1",
                                            "type": "info"
                                        },
                                        {
                                            "id": "2",
                                            "scenegroup": "1",
                                            "type": "info"
                                        }
                                    ],
                                    "Control": [
                                      {
                                        "ControlGroup":"1",
                                        "ControlPair": "1",
                                        "ControlHeader": "1",
                                        "top": "1",
                                        "left": "0",
                                        "x": "1.25",
                                        "ControlType": "label",
                                        "Label": {
                                            "lang_tag": "rain_rate",
                                            "text": "Rate of Rain" 
                                        },
                                        "Display": {
                                            "Top": 20,
                                            "Left": 50,
                                            "Width": 75,
                                            "Height": 20 
                                        } 
                                      },             
                                      {
                                        "ControlGroup":"1",
                                        "ControlPair": "1",
                                        "ControlHeader": "1",
                                        "top": "1",
                                        "left": "1.25",
                                        "x": "0.75",
                           		     "ControlType": "variable",
                                        "Display": {
                                            "Service": "urn:upnp-org:serviceId:RainSensor1",
                                            "Variable": "CurrentRain",
                                            "Top": 20,
                                            "Left": 150,
                                            "Width": 25,
                                            "Height": 20 
                                        } 
                                      },
                                      {
                                        "ControlType": "label",
                                        "Label": {
                                            "lang_tag": "total_rain_unit",
                                            "text": "mm/hr" 
                                        },
                                        "Display": {
                                            "Top": 20,
                                            "Left": 200,
                                            "Width": 25,
                                            "Height": 20 
                                        } 
                                      },
                                      {
                                        "ControlGroup":"2",
                                        "ControlPair": "2",
                                        "ControlHeader": "1",
                                        "top": "0",
                                        "left": "0",
                                        "x": "1.25",
                                        "ControlType": "label",
                                        "Label": {
                                            "lang_tag": "total_rain",
                                            "text": "24hrs Total:",
                                        },
                                        "Display": {
                                            "Top": 55,
                                            "Left": 50,
                                            "Width": 75,
                                            "Height": 20 
                                        } 
                                      },             
                                      {
                                        "ControlGroup":"2",
                                        "ControlPair": "2",
                                        "ControlHeader": "1",
                                        "top": "0",
                                        "left": "1.25",
                                        "x": "0.75",
                           		     "ControlType": "variable",
                                        "Display": {
                                            "Service": "urn:upnp-org:serviceId:RainSensor1",
                                            "Variable": "CurrentTRain",
                                            "Top": 55,
                                            "Left": 150,
                                            "Width": 25,
                                            "Height": 20 
                                        } 
                                      },
                                      {
                                        "ControlType": "label",
                                        "Label": {
                                            "lang_tag": "rain_rate_unit",
                                            "text": "mm" 
                                        },
                                        "Display": {
                                            "Top": 55,
                                            "Left": 200,
                                            "Width": 25,
                                            "Height": 20 
                                        } 
                                      },
                                      {
                                        "ControlGroup":"3",
                                        "ControlPair": "3",
                                        "ControlHeader": "1",
                                        "top": "0",
                                        "left": "0",
                                        "x": "1.25",
                                        "ControlType": "label",
                                        "Label": {
                                            "lang_tag": "rain_forty",
                                            "text": "48hrs Total:" 
                                        },
                                        "Display": {
                                            "Top": 80,
                                            "Left": 50,
                                            "Width": 75,
                                            "Height": 20 
                                        } 
                                      },             
                                      {
                                        "ControlGroup":"3",
                                        "ControlPair": "3",
                                        "ControlHeader": "1",
                                        "top": "0",
                                        "left": "1.25",
                                        "x": "0.75",
                           		     "ControlType": "variable",
                                        "Display": {
                                            "Service": "urn:upnp-org:serviceId:VContainer1",
                                            "Variable": "Variable2",
                                            "Top": 80,
                                            "Left": 150,
                                            "Width": 25,
                                            "Height": 20 
                                        } 
                                      },
                                      {
                                        "ControlType": "label",
                                        "Label": {
                                            "lang_tag": "rain_unit",
                                            "text": "mm" 
                                        },
                                        "Display": {
                                            "Top": 80,
                                            "Left": 200,
                                            "Width": 25,
                                            "Height": 20 
                                        } 
                                      },
                                      {
                                        "ControlGroup":"4",
                                        "ControlPair": "4",
                                        "ControlHeader": "1",
                                        "top": "0",
                                        "left": "0",
                                        "x": "1.25",
                                        "ControlType": "label",
                                        "Label": {
                                            "lang_tag": "rain_seventy",
                                            "text": "72hrs Total:" 
                                        },
                                        "Display": {
                                            "Top": 105,
                                            "Left": 50,
                                            "Width": 75,
                                            "Height": 20 
                                        } 
                                      },             
                                      {
                                        "ControlGroup":"4",
                                        "ControlPair": "4",
                                        "ControlHeader": "1",
                                        "top": "0",
                                        "left": "1.25",
                                        "x": "0.75",
                           		     "ControlType": "variable",
                                        "Display": {
                                            "Service": "urn:upnp-org:serviceId:VContainer1",
                                            "Variable": "Variable3",
                                            "Top": 105,
                                            "Left": 150,
                                            "Width": 25,
                                            "Height": 20 
                                        } 
                                      },
                                      {
                                        "ControlType": "label",
                                        "Label": {
                                            "lang_tag": "rain_unit1",
                                            "text": "mm" 
                                        },
                                        "Display": {
                                            "Top": 105,
                                            "Left": 200,
                                            "Width": 25,
                                            "Height": 20 
                                        } 
                                      },
                                      {
                                        "ControlGroup":"5",
                                        "ControlPair": "5",
                                        "ControlHeader": "1",
                                        "top": "0",
                                        "left": "0",
                                        "x": "1.25",
                                        "ControlType": "label",
                                        "Label": {
                                            "lang_tag": "rain_forty",
                                            "text": "96hrs Total:" 
                                        },
                                        "Display": {
                                            "Top": 130,
                                            "Left": 50,
                                            "Width": 75,
                                            "Height": 20 
                                        } 
                                      },             
                                      {
                                        "ControlGroup":"5",
                                        "ControlPair": "5",
                                        "ControlHeader": "1",
                                        "top": "0",
                                        "left": "1.25",
                                        "x": "0.75",
                           		     "ControlType": "variable",
                                        "Display": {
                                            "Service": "urn:upnp-org:serviceId:VContainer1",
                                            "Variable": "Variable4",
                                            "Top": 130,
                                            "Left": 150,
                                            "Width": 25,
                                            "Height": 20 
                                        } 
                                      },
                                      {
                                        "ControlType": "label",
                                        "Label": {
                                            "lang_tag": "rain_unit2",
                                            "text": "mm" 
                                        },
                                        "Display": {
                                            "Top": 130,
                                            "Left": 200,
                                            "Width": 25,
                                            "Height": 20 
                                        } 
                                      },
                                      {
                                        "ControlGroup":"6",
                                        "ControlPair": "6",
                                        "ControlHeader": "1",
                                        "top": "0",
                                        "left": "0",
                                        "x": "1.25",
                                        "ControlType": "label",
                                        "Label": {
                                            "lang_tag": "rain_hundred",
                                            "text": "120hrs Total:" 
                                        },
                                        "Display": {
                                            "Top": 155,
                                            "Left": 50,
                                            "Width": 75,
                                            "Height": 20 
                                        } 
                                      },             
                                      {
                                        "ControlGroup":"6",
                                        "ControlPair": "6",
                                        "ControlHeader": "1",
                                        "top": "0",
                                        "left": "1.25",
                                        "x": "0.75",
                           		     "ControlType": "variable",
                                        "Display": {
                                            "Service": "urn:upnp-org:serviceId:VContainer1",
                                            "Variable": "Variable5",
                                            "Top": 155,
                                            "Left": 150,
                                            "Width": 25,
                                            "Height": 20 
                                        } 
                                      },
                                      {
                                        "ControlType": "label",
                                        "Label": {
                                            "lang_tag": "rain_unit3",
                                            "text": "mm" 
                                        },
                                        "Display": {
                                            "Top": 155,
                                            "Left": 200,
                                            "Width": 25,
                                            "Height": 20 
                                        } 
                                      }
                                  ]
                                 },
                                {
                                    "Label": {
                                        "lang_tag": "advanced",
                                        "text": "Advanced" 
                                    },
                                    "Position": "1",
                                    "TabType": "javascript",
                                    "ScriptName": "shared.js",
                                    "Function": "advanced_device" 
                                },
                                {
                                    "Label": {
                                        "lang_tag": "logs",
                                        "text": "Logs"
                                    },
                                    "Position": "2",
                                    "TabType": "javascript",
                                    "ScriptName": "shared.js",
                                    "Function": "device_logs"
                                },
                                {
                                    "Label": {
                                        "lang_tag": "notifications",
                                        "text": "Notifications"
                                    },
                                    "Position": "3",
                                    "TabType": "javascript",
                                    "ScriptName": "shared.js",
                                    "Function": "device_notifications"
                                }
                            ],
                            "eventList2": [
                                {
                                    "id": 1,
                                    "label": {
                                        "lang_tag": "total_rain_goes_above",
                                        "text": "Total rain goes above"
                                    },
                                    "serviceId": "urn:upnp-org:serviceId:RainSensor1",
                                    "norepeat": "1",
                                    "argumentList": [
                                        {
                                            "id": 1,
                                            "dataType": "r8",
                                            "name": "CurrentTRain",
                                            "comparisson": ">",
                                            "prefix": {
                                                "lang_tag": "total_rain",
                                                "text": "Total rain"
                                            },
                                            "suffix": {
                                                "lang_tag": "total_rain_unit",
                                                "text": "mm"
                                            },
                                            "HumanFriendlyText": {
                                                "lang_tag": "hft_total_rain_goes_above",
                                                "text": "Total rain for _DEVICE_NAME_ goes above _ARGUMENT_VALUE_"
                                            }
                                        }
                                    ]
                                },
                                {
                                    "id": 2,
                                    "label": {
                                        "lang_tag": "total_rain_goes_below",
                                        "text": "Total rain goes below"
                                    },
                                    "serviceId": "urn:upnp-org:serviceId:RainSensor1",
                                    "norepeat": "1",
                                    "argumentList": [
                                        {
                                            "id": 1,
                                            "dataType": "r8",
                                            "name": "CurrentTRain",
                                            "comparisson": "<",
                                            "prefix": {
                                                "lang_tag": "total_rain",
                                                "text": "Total rain"
                                            },
                                            "suffix": {
                                                "lang_tag": "total_rain_unit",
                                                "text": "mm"
                                            },
                                            "HumanFriendlyText": {
                                                "lang_tag": "hft_total_rain_goes_below",
                                                "text": "Total rain for _DEVICE_NAME_ goes below _ARGUMENT_VALUE_"
                                            }
                                        }
                                    ]
                                },
                                {
                                    "id": 3,
                                    "label": {
                                        "lang_tag": "rain_rate_goes_above",
                                        "text": "Current rain goes above"
                                    },
                                    "serviceId": "urn:upnp-org:serviceId:RainSensor1",
                                    "norepeat": "1",
                                    "argumentList": [
                                        {
                                            "id": 1,
                                            "dataType": "r8",
                                            "name": "CurrentRain",
                                            "comparisson": ">",
                                            "prefix": {
                                                "lang_tag": "rain_rate",
                                                "text": "Current rain"
                                            },
                                            "suffix": {
                                                "lang_tag": "rain_rate_unit",
                                                "text": "mm/hr"
                                            },
                                            "HumanFriendlyText": {
                                                "lang_tag": "hft_rain_rate_goes_above",
                                                "text": "Current rain for _DEVICE_NAME_ goes above _ARGUMENT_VALUE_"
                                            }
                                        }
                                    ]
                                },
                                {
                                    "id": 4,
                                    "label": {
                                        "lang_tag": "rain_rate_goes_below",
                                        "text": "Current rain goes below"
                                    },
                                    "serviceId": "urn:upnp-org:serviceId:RainSensor1",
                                    "norepeat": "1",
                                    "argumentList": [
                                        {
                                            "id": 1,
                                            "dataType": "r8",
                                            "name": "CurrentRain",
                                            "comparisson": "<",
                                            "prefix": {
                                                "lang_tag": "rain_rate",
                                                "text": "Current rain"
                                            },
                                            "suffix": {
                                                "lang_tag": "rain_rate_unit",
                                                "text": "mm/hr"
                                            },
                                            "HumanFriendlyText": {
                                                "lang_tag": "hft_rain_rate_goes_below",
                                                "text": "Current rain for _DEVICE_NAME_ goes below _ARGUMENT_VALUE_"
                                            }
                                        }
                                    ]
                                },
                                {
                                    "id": 5,
                                    "label": {
                                        "lang_tag": "battery_level_goes_below",
                                        "text": "Battery level goes below"
                                    },
                                    "serviceId": "urn:micasaverde-com:serviceId:HaDevice1",
                                    "argumentList": [
                                        {
                                            "id": 1,
                                            "prefix": {
                                                "lang_tag": "Level",
                                                "text": "Level"
                                            },
                                            "dataType": "i4",
                                            "name": "BatteryLevel",
                                            "comparisson": "<",
                                            "suffix": {
                                                "lang_tag": "percent_sign",
                                                "text": "%"
                                            },
                                            "HumanFriendlyText": {
                                                "lang_tag": "hft_battery_level_goes_below",
                                                "text": "Battery level for _DEVICE_NAME_ go below _ARGUMENT_VALUE_%"
                                            }
                                        }
                                    ]
                                }
                            ],
                            "DeviceType": "urn:schemas-micasaverde-com:device:RainSensor:1"
                        }
                        

                        S_Rainsensor1.xml:

                        <?xml version="1.0"?>
                        <scpd xmlns="urn:schemas-upnp-org:service-1-0">
                          <specVersion>
                            <major>1</major>
                            <minor>0</minor>
                          </specVersion>
                          <serviceStateTable>
                            <stateVariable>
                              <name>CurrentTRain</name>
                              <sendEventsAttribute>yes</sendEventsAttribute>
                              <dataType>r8</dataType>
                              <shortCode>total_rain</shortCode>
                            </stateVariable>
                            <stateVariable>
                              <name>CurrentRain</name>
                              <sendEventsAttribute>yes</sendEventsAttribute>
                              <dataType>r8</dataType>
                              <shortCode>rain_rate</shortCode>
                            </stateVariable>
                          </serviceStateTable>
                          <actionList>
                            <action>
                              <name>GetCurrentTRain</name>
                              <argumentList>
                                <argument>
                                  <name>RainValue</name>
                                  <direction>out</direction>
                                  <relatedStateVariable>CurrentTRain</relatedStateVariable>
                                </argument>
                              </argumentList>
                            </action>
                        	<action>
                              <name>GetCurrentRain</name>
                              <argumentList>
                                <argument>
                                  <name>RainValue</name>
                                  <direction>out</direction>
                                  <relatedStateVariable>CurrentRain</relatedStateVariable>
                                </argument>
                              </argumentList>
                            </action>
                          </actionList>
                        </scpd>
                        
                        
                        1 Reply Last reply
                        0
                        • petewillP petewill

                          Jim,

                          I uploaded the code but it didn't seem to work for me. Do I need to upload some additional files to Vera? When I added the nodes a lot of my devices stopped displaying correctly (not just the newly added ones). They look like this:
                          upload-f009544f-e06e-4001-abba-63de772a744f

                          When I deleted the rain gauge nodes everything came back:
                          upload-2a04de4c-b861-4e84-892d-2ae37591ad0e

                          As I was looking at the code I came up with some additional questions:

                          • There is a dayBuckets array but it doesn't seem to do anything in the code?

                          • I also couldn't find "const unsigned long calibrateFactor" used anywhere.

                          I fully admit I'm not a very good with code and I could be missing something. I was just trying to find how the variables were working together and being calculated.

                          Thanks for all your help with this! I could not do it without you.

                          Pete

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

                          @petewill said:

                          As I was looking at the code I came up with some additional questions:

                          There is a dayBuckets array but it doesn't seem to do anything in the code?

                          I also couldn't find "const unsigned long calibrateFactor" used anywhere.

                          dayBuckets[] was used in the old code and since I designed 1mm of rain per tip, I never had to implement calibration.

                          1 Reply Last reply
                          0
                          • petewillP petewill

                            @BulldogLowell

                            Ok, I made the necessary changes for my application and I had a couple of questions along the way.

                            • I have never worked with eeprom so I don't pretend to know anything about it (it's now on my list of things to research though). Anyway, I noticed above hek recommended using the "saveState()/loadState() function in the MySensors library". Is that necessary in your code or are we ok as is? It seems to be functioning ok for me when I compile it so maybe not...

                            • I would like to trigger my rain sensor as soon as the first bucket has tipped (I have scenes that tell me to close the windows etc. when it's raining). Is changing the rainSensorThreshold value to 1 an appropriate way to do that or should I create another child node for my purposes? Maybe I should use rainRate in my PLEG instead?

                            • My tipping bucket was pulled from an existing rain gauge and according to my calculations (which may be off) each bucket is .7mm of rain. I don't know much about unsigned long variables but I'm wondering if I need to change "const unsigned long calibrateFactor" to a float or something?

                            Thanks for all your help!!

                            Pete

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

                            @petewill said:

                            I have never worked with eeprom so I don't pretend to know anything about it (it's now on my list of things to research though). Anyway, I noticed above hek recommended using the "saveState()/loadState() function in the MySensors library". Is that necessary in your code or are we ok as is? It seems to be functioning ok for me when I compile it so maybe not...

                            I developed the code before hek integrated EEPROM in his libraries. This uses a circular buffer, so it would take me some time to reprogram for hek's library, so I just kept using the arduino EEPROM.h library I used to develop the original code.

                            @petewill said:

                            changing the rainSensorThreshold value to 1 an appropriate way to do that

                            yes, I hardcoded boundaries, which you will will allow a minimum of 1 (one)

                            @petewill said:

                            My tipping bucket was pulled from an existing rain gauge and according to my calculations (which may be off) each bucket is .7mm of rain. I don't know much about unsigned long variables but I'm wondering if I need to change "const unsigned long calibrateFactor" to a float or something?

                            Sorry, I never put a bucket calibration into my code, I lucked out with the 1mm per tip working.

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

                              @BulldogLowell Thanks for the responses! Ok, I suspected I was missing some implementation files. Thanks for them. Did you create them from scratch? If so, nice!

                              I will take a look at the code to see if I can implement the calebrateFactor. I will most likely be back with more questions. :) Thanks again.

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

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

                                @BulldogLowell
                                I have been doing lots of research and working to understand the code so it will work with the calibrateFactor and I have another question... Why is the "tipSensorPin" set to OUTPUT then INPUT? Does that have to do with the interrupt?

                                pinMode(tipSensorPin, OUTPUT);
                                attachInterrupt (1, sensorTipped, CHANGE); //* should this be RISING instead??
                                pinMode(ledPin, OUTPUT);
                                digitalWrite(ledPin, HIGH);
                                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"));
                                pinMode(tipSensorPin, INPUT);
                                

                                Thanks again for your help! I'll post the full code back here when I have it working in case anyone else wants to use a different size tipping bucket.

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

                                BulldogLowellB 1 Reply Last reply
                                0
                                • petewillP petewill

                                  @BulldogLowell
                                  I have been doing lots of research and working to understand the code so it will work with the calibrateFactor and I have another question... Why is the "tipSensorPin" set to OUTPUT then INPUT? Does that have to do with the interrupt?

                                  pinMode(tipSensorPin, OUTPUT);
                                  attachInterrupt (1, sensorTipped, CHANGE); //* should this be RISING instead??
                                  pinMode(ledPin, OUTPUT);
                                  digitalWrite(ledPin, HIGH);
                                  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"));
                                  pinMode(tipSensorPin, INPUT);
                                  

                                  Thanks again for your help! I'll post the full code back here when I have it working in case anyone else wants to use a different size tipping bucket.

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

                                  @petewill said:

                                  Why is the "tipSensorPin" set to OUTPUT then INPUT? Does that have to do with the interrupt?

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

                                  funny how the brain works, I never noticed that...

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

                                    @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();
                                      }
                                    }
                                    

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

                                    BulldogLowellB 2 Replies Last reply
                                    1
                                    • hekH Offline
                                      hekH Offline
                                      hek
                                      Admin
                                      wrote on last edited by
                                      #52

                                      This starts to look really good. Maybe we should lift this into the examples on github?

                                      Just wish someone could sell the tipping bucket hardware. :)

                                      1 Reply Last reply
                                      0
                                      • 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
                                        #53

                                        @petewill

                                        Pete, let me look it all over again and see if we can get the calibration working. I think the EEPROM part can be updated for @hek 's addition into the mySensors library.

                                        @hek maybe we can do a 100% printed design for the collector/funnel and mechanism. and offer that on github as well.

                                        hekH petewillP 2 Replies Last reply
                                        0
                                        • BulldogLowellB BulldogLowell

                                          @petewill

                                          Pete, let me look it all over again and see if we can get the calibration working. I think the EEPROM part can be updated for @hek 's addition into the mySensors library.

                                          @hek maybe we can do a 100% printed design for the collector/funnel and mechanism. and offer that on github as well.

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

                                          @BulldogLowell said:

                                          @hek maybe we can do a 100% printed design for the collector/funnel and mechanism. and offer that on github as well.

                                          Good idea. We could probably just place the stl-file in the folder together with ino-file. Let me know when you feel ready.

                                          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