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.
  • mrc-coreM mrc-core

    At the rain gauge i have an arduino nano conneting PIN "d3" to TX2 over the rain gauge circuit, the arduino is powered at pins VIN and GND.
    The rain gauge gets its power from the 3.3V over the arduino.

    im posting two images from the rain gauge circuit:
    IMG_1
    IMG_2

    Starting to believe i had connect someting rong like for example had switched TX1 and TX2 at the rain gauge

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

    @mrc-core

    No PULLUP or PULLDOWN resistor?

    1 Reply Last reply
    0
    • mrc-coreM Offline
      mrc-coreM Offline
      mrc-core
      wrote on last edited by
      #102

      no i cant find any pullup or pulldown resistor.
      You can see the images from de interior of the rain gauge hi have.

      1 Reply Last reply
      0
      • mrc-coreM Offline
        mrc-coreM Offline
        mrc-core
        wrote on last edited by
        #103

        Which resistor do i pu between the 5v and the D3 pin ???

        BulldogLowellB 1 Reply Last reply
        0
        • mrc-coreM mrc-core

          Which resistor do i pu between the 5v and the D3 pin ???

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

          @mrc-core

          if you don't pull up/down the signal, you may get floating voltages and spurious interrupts occurring.

          try to use the internal pullup

          pinMode(yourInterruptPin, INPUT_PULLUP);
          

          if that does not work try an external 10kOhm resistor to pull it up/down for your switch.

          1 Reply Last reply
          0
          • mrc-coreM Offline
            mrc-coreM Offline
            mrc-core
            wrote on last edited by
            #105

            Going to put the 10kOhm resistor. the internall pullup did not work.
            One question. the resistor i put it between de 5v and the D3 pin

            BulldogLowellB 1 Reply Last reply
            0
            • mrc-coreM mrc-core

              Going to put the 10kOhm resistor. the internall pullup did not work.
              One question. the resistor i put it between de 5v and the D3 pin

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

              @mrc-core

              to PULL UP, yes.

              1 Reply Last reply
              0
              • mrc-coreM Offline
                mrc-coreM Offline
                mrc-core
                wrote on last edited by mrc-core
                #107

                Thanks going to try it now. And today is a good day since its raining.

                No luck... now i'm getting only "0"

                What i have done until now:
                Put a resistor 10kOhm between the GND and D3 Pin from arduino and connect it to TX2 over rthe rain gauge.
                And connected the 5V from arduino to the VCC over the rain gauge.

                But i'm only getting 0 ...

                send: 4-4-0-0 s=3,c=1,t=28,pt=7,l=5,sg=0,st=fail:-0.2
                read: 5-5-255 s=255,c=3,t=7,pt=0,l=0,sg=0:
                send: 4-4-0-0 s=4,c=1,t=16,pt=1,l=1,sg=0,st=ok:0
                New Sensor State... Sensor: Not Tripped
                read: 3-3-0 s=0,c=1,t=38,pt=7,l=5,sg=0:4.748
                read: 0-0-4 s=4,c=2,t=24,pt=0,l=1,sg=0:1
                read: 5-5-255 s=255,c=3,t=7,pt=0,l=0,sg=0:
                read: 5-5-255 s=255,c=3,t=7,pt=0,l=0,sg=0:

                BulldogLowellB 1 Reply Last reply
                0
                • mrc-coreM mrc-core

                  Thanks going to try it now. And today is a good day since its raining.

                  No luck... now i'm getting only "0"

                  What i have done until now:
                  Put a resistor 10kOhm between the GND and D3 Pin from arduino and connect it to TX2 over rthe rain gauge.
                  And connected the 5V from arduino to the VCC over the rain gauge.

                  But i'm only getting 0 ...

                  send: 4-4-0-0 s=3,c=1,t=28,pt=7,l=5,sg=0,st=fail:-0.2
                  read: 5-5-255 s=255,c=3,t=7,pt=0,l=0,sg=0:
                  send: 4-4-0-0 s=4,c=1,t=16,pt=1,l=1,sg=0,st=ok:0
                  New Sensor State... Sensor: Not Tripped
                  read: 3-3-0 s=0,c=1,t=38,pt=7,l=5,sg=0:4.748
                  read: 0-0-4 s=4,c=2,t=24,pt=0,l=1,sg=0:1
                  read: 5-5-255 s=255,c=3,t=7,pt=0,l=0,sg=0:
                  read: 5-5-255 s=255,c=3,t=7,pt=0,l=0,sg=0:

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

                  @mrc-core

                  I guess I'd need to see a schematic or a good photo.

                  can you generate a simple interrupt with a pushbutton and a resistor?

                  1 Reply Last reply
                  0
                  • mrc-coreM Offline
                    mrc-coreM Offline
                    mrc-core
                    wrote on last edited by
                    #109

                    Hi. yesterday i disassembled the rain gauge circuit removed from it the reed switch and created a new circuit.
                    I have connnected the reed switch to GND and PIN3 whit 10kOhm resistor.
                    I'll post an image tonight.

                    Up until now the values i'me getting from this new circuit is:
                    rain gauge log

                    Values i'm getting from serial port:
                    Rain last 24 hours:
                    1.80
                    Rain last 48 hours:
                    12481.94
                    Rain last 72 hours:
                    28210.34
                    Rain last 96 hours:
                    43938.74
                    Rain last 120 hours:
                    59667.14
                    read and drop: 6-6-255 s=255,c=3,t=7,pt=0,l=0,sg=0:
                    read and drop: 6-6-255 s=255,c=3,t=7,pt=0,l=0,sg=0:
                    read and drop: 5-5-255 s=255,c=3,t=7,pt=0,l=0,sg=0:
                    read and drop: 6-6-255 s=255,c=3,t=7,pt=0,l=0,sg=0:

                    Seeing the graphic and this values it seems the problem i was having is fixed...
                    But tonight i'm going do spread some water over the bucket to see if i get any values and not just 0

                    1 Reply Last reply
                    1
                    • Ivan ZI Offline
                      Ivan ZI Offline
                      Ivan Z
                      Hardware Contributor
                      wrote on last edited by
                      #110

                      MI-SOL Rain Guage has CALIBRATE_FACTOR = 36

                      1 Reply Last reply
                      0
                      • Ivan ZI Offline
                        Ivan ZI Offline
                        Ivan Z
                        Hardware Contributor
                        wrote on last edited by
                        #111

                        Sensor manufacturer has sent information about the sensor. 0.3 mm to 1 tick

                        petewillP 1 Reply Last reply
                        0
                        • Ivan ZI Ivan Z

                          Sensor manufacturer has sent information about the sensor. 0.3 mm to 1 tick

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

                          @Ivan-Z :thumbsup: Thanks for the info.

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

                          1 Reply Last reply
                          0
                          • petewillP petewill

                            @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

                            TheekshanaT Offline
                            TheekshanaT Offline
                            Theekshana
                            wrote on last edited by
                            #113

                            Hey guys,
                            Sorry if this is a stupid question but how does the Arduino interact with the tipping bucket to detect rain?
                            Thanks!

                            petewillP 1 Reply Last reply
                            0
                            • TheekshanaT Theekshana

                              Hey guys,
                              Sorry if this is a stupid question but how does the Arduino interact with the tipping bucket to detect rain?
                              Thanks!

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

                              @Theekshana There is a magnet on the tipping bucket and a reed switch that detects the magnet each time it passes by. So each time the bucket tips it will trigger to the Arduino and it can count from there.

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

                              1 Reply Last reply
                              0
                              • N Offline
                                N Offline
                                niccodemi
                                wrote on last edited by
                                #115

                                Has anyone converted this sketch to Mysensors 2.0 already? I've tried but received following error when compiling:

                                Rain-gauge.ino: In function 'void setup()':
                                Rain-gauge.ino:151:28: error: too many arguments to function 'void requestTime()'
                                In file included from D:\Programs\Arduino\Sketchbook\libraries\libraries\MySensors/MySensors.h:293:0,
                                             from Rain-gauge.ino:71:
                                D:\Programs\Arduino\Sketchbook\libraries\libraries\MySensors/core/MySensorsCore.cpp:296:6: note: declared here
                                 void requestTime() {
                                   ^
                                Rain-gauge.ino: In function 'void loop()':
                                Rain-gauge.ino:323:28: error: too many arguments to function 'void requestTime()'
                                In file included from D:\Programs\Arduino\Sketchbook\libraries\libraries\MySensors/MySensors.h:293:0,
                                             from Rain-gauge.ino:71:
                                D:\Programs\Arduino\Sketchbook\libraries\libraries\MySensors/core/MySensorsCore.cpp:296:6: note: declared here
                                 void requestTime() {
                                  ^
                                too many arguments to function 'void requestTime()'
                                

                                Modified sketch below:

                                /*
                                 Arduino Tipping Bucket Rain Gauge
                                
                                 April 26, 2015
                                
                                 Version 1.4.1 alpha
                                
                                 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 5 days.  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.
                                
                                 There is a build overview video here: https://youtu.be/1eMfKQaLROo
                                
                                 This sketch features the following:
                                
                                 * Allows you to set the rain threshold in mm
                                 * Allows you to determine the tripped indicator window up to 120 hours.
                                 * Displays the last 5 days of rain in Variable1 through Variable5
                                   of the Rain Sensor device
                                 * Configuration changes to Sensor device updated every hour
                                 * Should run on any Arduino
                                 * Will retain Tripped/Not Tripped status and data in a power interruption, saving small amount
                                   of data to EEPROM (Circular Buffer to maximize life of EEPROM)
                                 * LED status indicator
                                 * Optional Temp/Humidity (DHT-22 or DHT-11) and Light LUX (BH1750) sensors. To use, uncomment
                                   #define DHT_ON  and/or #define LUX_ON
                                 * Optionally send total accumulation of each day's rainfall or send only individual days rainfall totals.
                                   Comment out #define USE_DAILY to display individual daily rainfall.
                                
                                 by @BulldogLowell and @PeteWill for free public use
                                
                                 */
                                #include <SPI.h>
                                #include <math.h>
                                #include <Time.h>
                                
                                //#define NODE_ID AUTO //or AUTO to let controller assign
                                #define SKETCH_NAME "Rain Gauge"
                                #define SKETCH_VERSION "1.4.1a"
                                #define MY_RADIO_NRF24
                                
                                #define DWELL_TIME 125  // this allows for radio to come back to power after a transmission, ideally 0 
                                
                                //#define DEBUG_ON  // comment out this line to disable serial debug
                                //#define DHT_ON // uncomment out this line to enable DHT sensor
                                //#define LUX_ON // uncomment out this line to enable BH1750 sensor
                                //#define USE_DAILY // displays each time segment as an accumulation of prior periods inclusive.  Comment out to display individual daily rainfall totals in the variables sent to your controller.
                                
                                #define CALIBRATE_FACTOR 60 // amount of rain per rain bucket tip e.g. 5 is .05mm
                                #define DHT_LUX_DELAY 300000  //Delay in milliseconds that the DHT and LUX sensors will wait before sending data
                                
                                #define CHILD_ID_RAIN_LOG 3  // Keeps track of accumulated rainfall
                                #define CHILD_ID_TRIPPED_INDICATOR 4  // Indicates Tripped when rain detected
                                #define EEPROM_BUFFER_LOCATION 0  // location of the EEPROM circular buffer
                                #define E_BUFFER_LENGTH 240
                                #define RAIN_BUCKET_SIZE 120
                                
                                #ifdef  DEBUG_ON
                                  #define DEBUG_PRINT(x)   Serial.print(x)
                                  #define DEBUG_PRINTLN(x) Serial.println(x)
                                  #define SERIAL_START(x)  Serial.begin(x)
                                  #else
                                  #define DEBUG_PRINT(x)
                                  #define DEBUG_PRINTLN(x)
                                  #define SERIAL_START(x)
                                #endif
                                #include <MySensors.h>
                                //
                                MyMessage msgRainRate(CHILD_ID_RAIN_LOG, V_RAINRATE);
                                MyMessage msgRain(CHILD_ID_RAIN_LOG, V_RAIN);
                                //
                                MyMessage msgRainVAR1(CHILD_ID_RAIN_LOG, V_VAR1);
                                MyMessage msgRainVAR2(CHILD_ID_RAIN_LOG, V_VAR2);
                                MyMessage msgRainVAR3(CHILD_ID_RAIN_LOG, V_VAR3);
                                MyMessage msgRainVAR4(CHILD_ID_RAIN_LOG, V_VAR4);
                                MyMessage msgRainVAR5(CHILD_ID_RAIN_LOG, V_VAR5);
                                //
                                MyMessage msgTripped(CHILD_ID_TRIPPED_INDICATOR, V_TRIPPED);
                                MyMessage msgTrippedVar1(CHILD_ID_TRIPPED_INDICATOR, V_VAR1);
                                MyMessage msgTrippedVar2(CHILD_ID_TRIPPED_INDICATOR, V_VAR2);
                                //
                                #ifdef DHT_ON
                                  #include <DHT.h>
                                  #define CHILD_ID_HUM 0
                                  #define CHILD_ID_TEMP 1
                                  #define HUMIDITY_SENSOR_DIGITAL_PIN 8
                                  DHT dht;
                                  float lastTemp;
                                  float lastHum;
                                  boolean metric = true;
                                  MyMessage msgHum(CHILD_ID_HUM, V_HUM);
                                  MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
                                #endif
                                //
                                #ifdef LUX_ON
                                  //BH1750 is connected to SCL (analog input A5) and SDA (analog input A4)
                                  #include <BH1750.h>
                                  #include <Wire.h>
                                  #define CHILD_ID_LIGHT 2
                                  BH1750 lightSensor;
                                  MyMessage msg(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
                                  unsigned int lastlux;
                                  byte heartbeat = 10; //Used to send the light lux to gateway as soon as the device is restarted and after the DHT_LUX_DELAY has happened 10 times
                                #endif
                                unsigned long sensorPreviousMillis;
                                int eepromIndex;
                                int tipSensorPin = 3; // Pin the tipping bucket is connected to. Must be interrupt capable pin
                                int ledPin = 5; // Pin the LED is connected to.  PWM capable pin required
                                unsigned long dataMillis;
                                unsigned long serialInterval = 600000UL;
                                const unsigned long oneHour = 3600000UL;
                                unsigned long lastTipTime;
                                unsigned long lastRainTime; //Used for rainRate calculation
                                unsigned int rainBucket [RAIN_BUCKET_SIZE] ; /* 24 hours x 5 Days = 120 hours */
                                unsigned int rainRate = 0;
                                byte rainWindow = 72;         //default rain window in hours.  Will be overwritten with msgTrippedVar1.
                                volatile int wasTippedBuffer = 0;
                                int rainSensorThreshold = 50; //default rain sensor sensitivity in hundredths.  Will be overwritten with msgTrippedVar2.
                                byte state = 0;
                                byte oldState = -1;
                                int lastRainRate = 0;
                                int lastMeasure = 0;
                                boolean gotTime = false;
                                byte lastHour;
                                byte currentHour;
                                //
                                void setup()
                                {
                                  SERIAL_START(115200);
                                  //
                                  // Set up the IO
                                  pinMode(tipSensorPin, INPUT_PULLUP);
                                  attachInterrupt (1, sensorTipped, FALLING);  // depending on location of the hall effect sensor may need CHANGE
                                  pinMode(ledPin, OUTPUT);
                                  digitalWrite(ledPin, HIGH);
                                  //
                                  //Let's get the controller talking to the Arduino
                                  //begin(getVariables, NODE_ID);
                                
                                  //
                                  //Sync time with the server, this will be called hourly in order to keep time from creeping with the crystal
                                  //
                                  unsigned long functionTimeout = millis();
                                  while (timeStatus() == timeNotSet && millis() - functionTimeout < 30000UL)
                                  {
                                //    process();
                                    requestTime(receiveTime);
                                    DEBUG_PRINTLN(F("Getting Time"));
                                    wait(1000); // call once per second
                                    DEBUG_PRINTLN(F("."));
                                  }
                                  currentHour = hour();
                                  lastHour = hour();
                                  //
                                  //retrieve from EEPROM stored values on a power cycle.
                                  //
                                  boolean isDataOnEeprom = false;
                                  for (int i = 0; i < E_BUFFER_LENGTH; i++)
                                  {
                                    byte locator = loadState(EEPROM_BUFFER_LOCATION + i);
                                    if (locator == 0xFE)  // found the EEPROM circular buffer index
                                    {
                                      eepromIndex = EEPROM_BUFFER_LOCATION + i;
                                      DEBUG_PRINT(F("EEPROM Index "));
                                      DEBUG_PRINTLN(eepromIndex);
                                      //Now that we have the buffer index let's populate the rainBucket[] with data from eeprom
                                      loadRainArray(eepromIndex);
                                      isDataOnEeprom = true;
                                      break;
                                    }
                                  }
                                  //
                                  if (!isDataOnEeprom) // Added for the first time it is run on a new Arduino
                                  {
                                    DEBUG_PRINTLN(F("I didn't find valid EEPROM Index, so I'm writing one to location 0"));
                                    eepromIndex = EEPROM_BUFFER_LOCATION;
                                    saveState(eepromIndex, 0xFE);
                                    saveState(eepromIndex + 1, 0xFE);
                                    //then I will clear out any bad data
                                    for (int i = 2; i <= E_BUFFER_LENGTH; i++)
                                    {
                                      saveState(i, 0x00);
                                    }
                                  }
                                  dataMillis = millis();
                                  lastTipTime = millis() - oneHour; //why is this -oneHour?? Doesn't millis() start at 0 when first powered on?
                                  //
                                  request(CHILD_ID_TRIPPED_INDICATOR, V_VAR1);
                                  wait(DWELL_TIME);
                                  request(CHILD_ID_TRIPPED_INDICATOR, V_VAR2);
                                  wait(DWELL_TIME);
                                  //
                                #ifdef DHT_ON
                                  dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN);
                                  // Register all sensors to gw (they will be created as child devices)
                                  present(CHILD_ID_HUM, S_HUM);
                                  wait(DWELL_TIME);
                                  present(CHILD_ID_TEMP, S_TEMP);
                                  wait(DWELL_TIME);
                                  metric = getConfig().isMetric;
                                  .wait(DWELL_TIME);
                                #endif
                                  //
                                #ifdef LUX_ON
                                  present(CHILD_ID_LIGHT, S_LIGHT_LEVEL);
                                  wait(DWELL_TIME);
                                  lightSensor.begin();
                                #endif
                                  //
                                  DEBUG_PRINTLN(F("Radio Setup Complete!"));
                                  transmitRainData();
                                }
                                void presentation()
                                {
                                  sendSketchInfo(SKETCH_NAME, SKETCH_VERSION);
                                  wait(DWELL_TIME);
                                  present(CHILD_ID_RAIN_LOG, S_RAIN);
                                  wait(DWELL_TIME);
                                  present(CHILD_ID_TRIPPED_INDICATOR, S_MOTION);
                                  wait(DWELL_TIME);
                                  DEBUG_PRINTLN(F("Sensor Presentation Complete"));
                                }
                                //
                                void loop()
                                {
                                //  process();
                                  if (state)
                                  {
                                    prettyFade();  // breathe if tripped
                                  }
                                  else
                                  {
                                    slowFlash();   // blink if not tripped
                                  }
                                #ifdef DEBUG_ON  // Serial Debug Block
                                  if ( (millis() - dataMillis) >= serialInterval)
                                  {
                                    for (int i = 24; i <= 120; i = i + 24)
                                    {
                                      updateSerialData(i);
                                    }
                                    dataMillis = millis();
                                  }
                                #endif
                                  //
                                  // 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];
                                    if (measure != lastMeasure)
                                    {
                                      //      DEBUG_PRINT(F("measure value (total rainBucket within rainWindow): "));
                                      //      DEBUG_PRINTLN(measure);
                                      lastMeasure = measure;
                                    }
                                  }
                                  //
                                  state = (measure >= (rainSensorThreshold * 100));
                                  if (state != oldState)
                                  {
                                    send(msgTripped.set(state));
                                    wait(DWELL_TIME);
                                    DEBUG_PRINT(F("New Sensor State... Sensor: "));
                                    DEBUG_PRINTLN(state ? "Tripped" : "Not Tripped");
                                    oldState = state;
                                  }
                                  //
                                  unsigned long tipDelay = millis() - lastRainTime;
                                  if (wasTippedBuffer) // if was tipped, then update the 24hour total and transmit to Vera
                                  {
                                    DEBUG_PRINTLN(F("Sensor Tipped"));
                                    DEBUG_PRINT(F("rainBucket [0] value: "));
                                    DEBUG_PRINTLN(rainBucket [0]);
                                    send(msgRain.set((float)rainTotal(currentHour) / 100, 1)); //Calculate the total rain for the day
                                    wait(DWELL_TIME);
                                    wasTippedBuffer--;
                                    rainRate = ((oneHour) / tipDelay);
                                    if (rainRate != lastRainRate)
                                    {
                                      send(msgRainRate.set(rainRate, 1));
                                      wait(DWELL_TIME);
                                      DEBUG_PRINT(F("RainRate= "));
                                      DEBUG_PRINTLN(rainRate);
                                      lastRainRate = rainRate;
                                    }
                                    lastRainTime = lastTipTime;
                                  }
                                  //
                                  currentHour = hour();
                                  if (currentHour != lastHour)
                                  {
                                    DEBUG_PRINTLN(F("One hour elapsed."));
                                    send(msgRain.set((float)rainTotal(currentHour) / 100, 1)); // send today's rainfall
                                    wait(DWELL_TIME);
                                    saveState(eepromIndex, highByte(rainBucket[0]));
                                    saveState(eepromIndex + 1, lowByte(rainBucket[0]));
                                    DEBUG_PRINT(F("Saving rainBucket[0] to eeprom. rainBucket[0] = "));
                                    DEBUG_PRINTLN(rainBucket[0]);
                                    for (int i = RAIN_BUCKET_SIZE - 1; i >= 0; i--)//cascade an hour of values back into the array
                                    {
                                      rainBucket [i + 1] = rainBucket [i];
                                    }
                                    request(CHILD_ID_TRIPPED_INDICATOR, V_VAR1);
                                    wait(DWELL_TIME);
                                    request(CHILD_ID_TRIPPED_INDICATOR, V_VAR2);
                                    wait(DWELL_TIME);
                                    rainBucket[0] = 0;
                                    eepromIndex = eepromIndex + 2;
                                    if (eepromIndex > EEPROM_BUFFER_LOCATION + E_BUFFER_LENGTH)
                                    {
                                      eepromIndex = EEPROM_BUFFER_LOCATION;
                                    }
                                    DEBUG_PRINT(F("Writing to EEPROM.  Index: "));
                                    DEBUG_PRINTLN(eepromIndex);
                                    saveState(eepromIndex, 0xFE);
                                    saveState(eepromIndex + 1, 0xFE);
                                    requestTime(receiveTime); // sync the time every hour
                                    wait(DWELL_TIME);
                                    transmitRainData();
                                    rainRate = 0;
                                    send(msgRainRate.set(rainRate, 1));
                                    wait(DWELL_TIME);
                                    DEBUG_PRINTLN(F("Sending rainRate is 0 to controller"));
                                    lastHour = currentHour;
                                  }
                                  if (millis() - sensorPreviousMillis > DHT_LUX_DELAY)
                                  {
                                    #ifdef DHT_ON  //DHT Code
                                      doDHT();
                                    #endif
                                    #ifdef LUX_ON
                                      doLUX();
                                    #endif
                                    sensorPreviousMillis = millis();
                                  }
                                }
                                //
                                #ifdef DHT_ON
                                void doDHT(void)
                                {
                                  float temperature = dht.getTemperature();
                                    if (isnan(temperature)) 
                                    {
                                      DEBUG_PRINTLN(F("Failed reading temperature from DHT"));
                                    } else if (temperature != lastTemp) 
                                    {
                                      lastTemp = temperature;
                                      if (!metric) 
                                      {
                                        temperature = dht.toFahrenheit(temperature);
                                      }
                                      gw.send(msgTemp.set(temperature, 1));
                                      gw.wait(DWELL_TIME);
                                      DEBUG_PRINT(F("Temperature is: "));
                                      DEBUG_PRINTLN(temperature);
                                    }
                                    float humidity = dht.getHumidity();;
                                    if (isnan(humidity)) 
                                    {
                                      DEBUG_PRINTLN(F("Failed reading humidity from DHT"));
                                    } else if (humidity != lastHum) 
                                    {
                                      lastHum = humidity;
                                      gw.send(msgHum.set(humidity, 1));
                                      gw.wait(DWELL_TIME);
                                      DEBUG_PRINT(F("Humidity is: "));
                                      DEBUG_PRINTLN(humidity);
                                    }
                                }
                                #endif
                                //
                                #ifdef LUX_ON
                                void doLUX(void)
                                {
                                  unsigned int lux = lightSensor.readLightLevel();// Get Lux value
                                  DEBUG_PRINT(F("Current LUX Level: "));
                                  DEBUG_PRINTLN(lux);
                                  heartbeat++;
                                  if (lux != lastlux || heartbeat > 10) 
                                  {
                                    gw.send(msg.set(lux));
                                    lastlux = lux;
                                  }
                                  if (heartbeat > 10) 
                                  {
                                    heartbeat = 0;
                                  }
                                }
                                #endif
                                //
                                void sensorTipped()
                                {
                                  unsigned long thisTipTime = millis();
                                  if (thisTipTime - lastTipTime > 100UL)
                                  {
                                    rainBucket[0] += CALIBRATE_FACTOR; // adds CALIBRATE_FACTOR hundredths of unit each tip
                                    wasTippedBuffer++;
                                  }
                                  lastTipTime = thisTipTime;
                                }
                                //
                                int rainTotal(int hours)
                                {
                                  int total = 0;
                                  for ( int i = 0; i <= hours; i++)
                                  {
                                    total += rainBucket [i];
                                  }
                                  return total;
                                }
                                
                                void updateSerialData(int x)
                                {
                                  DEBUG_PRINT(F("Rain last "));
                                  DEBUG_PRINT(x);
                                  DEBUG_PRINTLN(F(" hours: "));
                                  float tipCount = 0;
                                  for (int i = 0; i < x; i++)
                                  {
                                    tipCount = tipCount + rainBucket [i];
                                  }
                                  tipCount = tipCount / 100;
                                  DEBUG_PRINTLN(tipCount);
                                }
                                
                                void loadRainArray(int value) // retrieve stored rain array from EEPROM on powerup
                                {
                                  for (int i = 0; i < RAIN_BUCKET_SIZE; i++)
                                  {
                                    value = value - 2;
                                    if (value < EEPROM_BUFFER_LOCATION)
                                    {
                                      value = EEPROM_BUFFER_LOCATION + E_BUFFER_LENGTH;
                                    }
                                    DEBUG_PRINT(F("EEPROM location: "));
                                    DEBUG_PRINTLN(value);
                                    byte rainValueHigh = loadState(value);
                                    byte rainValueLow = loadState(value + 1);
                                    unsigned int rainValue = rainValueHigh << 8;
                                    rainValue |= rainValueLow;
                                    rainBucket[i + 1] = rainValue;
                                    //
                                    DEBUG_PRINT(F("rainBucket[ value: "));
                                    DEBUG_PRINT(i + 1);
                                    DEBUG_PRINT(F("] value: "));
                                    DEBUG_PRINTLN(rainBucket[i + 1]);
                                  }
                                }
                                
                                void transmitRainData(void)
                                {
                                  DEBUG_PRINT(F("In transmitRainData. currentHour = "));
                                  DEBUG_PRINTLN(currentHour);
                                  int rainUpdateTotal = 0;
                                  for (int i = currentHour; i >= 0; i--)
                                  {
                                    rainUpdateTotal += rainBucket[i];
                                    DEBUG_PRINT(F("Adding rainBucket["));
                                    DEBUG_PRINT(i);
                                    DEBUG_PRINTLN(F("] to rainUpdateTotal."));
                                  }
                                  DEBUG_PRINT(F("TX Day 1: rainUpdateTotal = "));
                                  DEBUG_PRINTLN((float)rainUpdateTotal / 100.0);
                                  send(msgRainVAR1.set((float)rainUpdateTotal / 100.0, 1)); //Send current day rain totals (resets at midnight)
                                  wait(DWELL_TIME);
                                #ifdef USE_DAILY
                                  rainUpdateTotal = 0;
                                #endif
                                  for (int i = currentHour + 24; i > currentHour; i--)
                                  {
                                    rainUpdateTotal += rainBucket[i];
                                    DEBUG_PRINT(F("Adding rainBucket["));
                                    DEBUG_PRINT(i);
                                    DEBUG_PRINTLN(F("] to rainUpdateTotal."));
                                  }
                                  DEBUG_PRINT(F("TX Day 2: rainUpdateTotal = "));
                                  DEBUG_PRINTLN((float)rainUpdateTotal / 100.0);
                                  send(msgRainVAR2.set((float)rainUpdateTotal / 100.0, 1));
                                  wait(DWELL_TIME);
                                #ifdef USE_DAILY
                                  rainUpdateTotal = 0;
                                #endif
                                  for (int i = currentHour + 48; i > currentHour + 24; i--)
                                  {
                                    rainUpdateTotal += rainBucket[i];
                                    DEBUG_PRINT(F("Adding rainBucket["));
                                    DEBUG_PRINT(i);
                                    DEBUG_PRINTLN(F("] to rainUpdateTotal."));
                                  }
                                  DEBUG_PRINT(F("TX Day 3: rainUpdateTotal = "));
                                  DEBUG_PRINTLN((float)rainUpdateTotal / 100.0);
                                  send(msgRainVAR3.set((float)rainUpdateTotal / 100.0, 1));
                                  wait(DWELL_TIME);
                                #ifdef USE_DAILY
                                  rainUpdateTotal = 0;
                                #endif
                                  for (int i = currentHour + 72; i > currentHour + 48; i--)
                                  {
                                    rainUpdateTotal += rainBucket[i];
                                    DEBUG_PRINT(F("Adding rainBucket["));
                                    DEBUG_PRINT(i);
                                    DEBUG_PRINTLN(F("] to rainUpdateTotal."));
                                  }
                                  DEBUG_PRINT(F("TX Day 4: rainUpdateTotal = "));
                                  DEBUG_PRINTLN((float)rainUpdateTotal / 100.0);
                                  send(msgRainVAR4.set((float)rainUpdateTotal / 100.0, 1));
                                  wait(DWELL_TIME);
                                #ifdef USE_DAILY
                                  rainUpdateTotal = 0;
                                #endif
                                  for (int i = currentHour + 96; i > currentHour + 72; i--)
                                  {
                                    rainUpdateTotal += rainBucket[i];
                                    DEBUG_PRINT(F("Adding rainBucket["));
                                    DEBUG_PRINT(i);
                                    DEBUG_PRINTLN(F("] to rainUpdateTotal."));
                                  }
                                  DEBUG_PRINT(F("TX Day 5: rainUpdateTotal = "));
                                  DEBUG_PRINTLN((float)rainUpdateTotal / 100.0);
                                  send(msgRainVAR5.set((float)rainUpdateTotal / 100.0, 1));
                                  wait(DWELL_TIME);
                                }
                                
                                void getVariables(const MyMessage &message)
                                {
                                  if (message.sensor == CHILD_ID_RAIN_LOG)
                                  {
                                    // nothing to do here
                                  }
                                  else if (message.sensor == CHILD_ID_TRIPPED_INDICATOR)
                                  {
                                    if (message.type == V_VAR1)
                                    {
                                      rainWindow = atoi(message.data);
                                      if (rainWindow > 120)
                                      {
                                        rainWindow = 120;
                                      }
                                      else if (rainWindow < 1)
                                      {
                                        rainWindow = 1;
                                      }
                                      if (rainWindow != atoi(message.data))   // if I changed the value back inside the boundries, push that number back to Vera
                                      {
                                        send(msgTrippedVar1.set(rainWindow));
                                      }
                                    }
                                    else if (message.type == V_VAR2)
                                    {
                                      rainSensorThreshold = atoi(message.data);
                                      if (rainSensorThreshold > 10000)
                                      {
                                        rainSensorThreshold = 10000;
                                      }
                                      else if (rainSensorThreshold < 1)
                                      {
                                        rainSensorThreshold = 1;
                                      }
                                      if (rainSensorThreshold != atoi(message.data))  // if I changed the value back inside the boundries, push that number back to Vera
                                      {
                                        send(msgTrippedVar2.set(rainSensorThreshold));
                                      }
                                    }
                                  }
                                }
                                
                                void prettyFade(void)
                                {
                                  float val = (exp(sin(millis() / 2000.0 * PI)) - 0.36787944) * 108.0;
                                  analogWrite(ledPin, val);
                                }
                                
                                void slowFlash(void)
                                {
                                  static boolean ledState = true;
                                  static unsigned long pulseStart = millis();
                                  if (millis() - pulseStart < 100UL)
                                  {
                                    digitalWrite(ledPin, !ledState);
                                    pulseStart = millis();
                                  }
                                }
                                
                                void receiveTime(unsigned long time)
                                {
                                  DEBUG_PRINTLN(F("Time received..."));
                                  setTime(time);
                                  char theTime[6];
                                  sprintf(theTime, "%d:%2d", hour(), minute());
                                  DEBUG_PRINTLN(theTime);
                                }
                                
                                AWIA 1 Reply Last reply
                                0
                                • N niccodemi

                                  Has anyone converted this sketch to Mysensors 2.0 already? I've tried but received following error when compiling:

                                  Rain-gauge.ino: In function 'void setup()':
                                  Rain-gauge.ino:151:28: error: too many arguments to function 'void requestTime()'
                                  In file included from D:\Programs\Arduino\Sketchbook\libraries\libraries\MySensors/MySensors.h:293:0,
                                               from Rain-gauge.ino:71:
                                  D:\Programs\Arduino\Sketchbook\libraries\libraries\MySensors/core/MySensorsCore.cpp:296:6: note: declared here
                                   void requestTime() {
                                     ^
                                  Rain-gauge.ino: In function 'void loop()':
                                  Rain-gauge.ino:323:28: error: too many arguments to function 'void requestTime()'
                                  In file included from D:\Programs\Arduino\Sketchbook\libraries\libraries\MySensors/MySensors.h:293:0,
                                               from Rain-gauge.ino:71:
                                  D:\Programs\Arduino\Sketchbook\libraries\libraries\MySensors/core/MySensorsCore.cpp:296:6: note: declared here
                                   void requestTime() {
                                    ^
                                  too many arguments to function 'void requestTime()'
                                  

                                  Modified sketch below:

                                  /*
                                   Arduino Tipping Bucket Rain Gauge
                                  
                                   April 26, 2015
                                  
                                   Version 1.4.1 alpha
                                  
                                   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 5 days.  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.
                                  
                                   There is a build overview video here: https://youtu.be/1eMfKQaLROo
                                  
                                   This sketch features the following:
                                  
                                   * Allows you to set the rain threshold in mm
                                   * Allows you to determine the tripped indicator window up to 120 hours.
                                   * Displays the last 5 days of rain in Variable1 through Variable5
                                     of the Rain Sensor device
                                   * Configuration changes to Sensor device updated every hour
                                   * Should run on any Arduino
                                   * Will retain Tripped/Not Tripped status and data in a power interruption, saving small amount
                                     of data to EEPROM (Circular Buffer to maximize life of EEPROM)
                                   * LED status indicator
                                   * Optional Temp/Humidity (DHT-22 or DHT-11) and Light LUX (BH1750) sensors. To use, uncomment
                                     #define DHT_ON  and/or #define LUX_ON
                                   * Optionally send total accumulation of each day's rainfall or send only individual days rainfall totals.
                                     Comment out #define USE_DAILY to display individual daily rainfall.
                                  
                                   by @BulldogLowell and @PeteWill for free public use
                                  
                                   */
                                  #include <SPI.h>
                                  #include <math.h>
                                  #include <Time.h>
                                  
                                  //#define NODE_ID AUTO //or AUTO to let controller assign
                                  #define SKETCH_NAME "Rain Gauge"
                                  #define SKETCH_VERSION "1.4.1a"
                                  #define MY_RADIO_NRF24
                                  
                                  #define DWELL_TIME 125  // this allows for radio to come back to power after a transmission, ideally 0 
                                  
                                  //#define DEBUG_ON  // comment out this line to disable serial debug
                                  //#define DHT_ON // uncomment out this line to enable DHT sensor
                                  //#define LUX_ON // uncomment out this line to enable BH1750 sensor
                                  //#define USE_DAILY // displays each time segment as an accumulation of prior periods inclusive.  Comment out to display individual daily rainfall totals in the variables sent to your controller.
                                  
                                  #define CALIBRATE_FACTOR 60 // amount of rain per rain bucket tip e.g. 5 is .05mm
                                  #define DHT_LUX_DELAY 300000  //Delay in milliseconds that the DHT and LUX sensors will wait before sending data
                                  
                                  #define CHILD_ID_RAIN_LOG 3  // Keeps track of accumulated rainfall
                                  #define CHILD_ID_TRIPPED_INDICATOR 4  // Indicates Tripped when rain detected
                                  #define EEPROM_BUFFER_LOCATION 0  // location of the EEPROM circular buffer
                                  #define E_BUFFER_LENGTH 240
                                  #define RAIN_BUCKET_SIZE 120
                                  
                                  #ifdef  DEBUG_ON
                                    #define DEBUG_PRINT(x)   Serial.print(x)
                                    #define DEBUG_PRINTLN(x) Serial.println(x)
                                    #define SERIAL_START(x)  Serial.begin(x)
                                    #else
                                    #define DEBUG_PRINT(x)
                                    #define DEBUG_PRINTLN(x)
                                    #define SERIAL_START(x)
                                  #endif
                                  #include <MySensors.h>
                                  //
                                  MyMessage msgRainRate(CHILD_ID_RAIN_LOG, V_RAINRATE);
                                  MyMessage msgRain(CHILD_ID_RAIN_LOG, V_RAIN);
                                  //
                                  MyMessage msgRainVAR1(CHILD_ID_RAIN_LOG, V_VAR1);
                                  MyMessage msgRainVAR2(CHILD_ID_RAIN_LOG, V_VAR2);
                                  MyMessage msgRainVAR3(CHILD_ID_RAIN_LOG, V_VAR3);
                                  MyMessage msgRainVAR4(CHILD_ID_RAIN_LOG, V_VAR4);
                                  MyMessage msgRainVAR5(CHILD_ID_RAIN_LOG, V_VAR5);
                                  //
                                  MyMessage msgTripped(CHILD_ID_TRIPPED_INDICATOR, V_TRIPPED);
                                  MyMessage msgTrippedVar1(CHILD_ID_TRIPPED_INDICATOR, V_VAR1);
                                  MyMessage msgTrippedVar2(CHILD_ID_TRIPPED_INDICATOR, V_VAR2);
                                  //
                                  #ifdef DHT_ON
                                    #include <DHT.h>
                                    #define CHILD_ID_HUM 0
                                    #define CHILD_ID_TEMP 1
                                    #define HUMIDITY_SENSOR_DIGITAL_PIN 8
                                    DHT dht;
                                    float lastTemp;
                                    float lastHum;
                                    boolean metric = true;
                                    MyMessage msgHum(CHILD_ID_HUM, V_HUM);
                                    MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
                                  #endif
                                  //
                                  #ifdef LUX_ON
                                    //BH1750 is connected to SCL (analog input A5) and SDA (analog input A4)
                                    #include <BH1750.h>
                                    #include <Wire.h>
                                    #define CHILD_ID_LIGHT 2
                                    BH1750 lightSensor;
                                    MyMessage msg(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
                                    unsigned int lastlux;
                                    byte heartbeat = 10; //Used to send the light lux to gateway as soon as the device is restarted and after the DHT_LUX_DELAY has happened 10 times
                                  #endif
                                  unsigned long sensorPreviousMillis;
                                  int eepromIndex;
                                  int tipSensorPin = 3; // Pin the tipping bucket is connected to. Must be interrupt capable pin
                                  int ledPin = 5; // Pin the LED is connected to.  PWM capable pin required
                                  unsigned long dataMillis;
                                  unsigned long serialInterval = 600000UL;
                                  const unsigned long oneHour = 3600000UL;
                                  unsigned long lastTipTime;
                                  unsigned long lastRainTime; //Used for rainRate calculation
                                  unsigned int rainBucket [RAIN_BUCKET_SIZE] ; /* 24 hours x 5 Days = 120 hours */
                                  unsigned int rainRate = 0;
                                  byte rainWindow = 72;         //default rain window in hours.  Will be overwritten with msgTrippedVar1.
                                  volatile int wasTippedBuffer = 0;
                                  int rainSensorThreshold = 50; //default rain sensor sensitivity in hundredths.  Will be overwritten with msgTrippedVar2.
                                  byte state = 0;
                                  byte oldState = -1;
                                  int lastRainRate = 0;
                                  int lastMeasure = 0;
                                  boolean gotTime = false;
                                  byte lastHour;
                                  byte currentHour;
                                  //
                                  void setup()
                                  {
                                    SERIAL_START(115200);
                                    //
                                    // Set up the IO
                                    pinMode(tipSensorPin, INPUT_PULLUP);
                                    attachInterrupt (1, sensorTipped, FALLING);  // depending on location of the hall effect sensor may need CHANGE
                                    pinMode(ledPin, OUTPUT);
                                    digitalWrite(ledPin, HIGH);
                                    //
                                    //Let's get the controller talking to the Arduino
                                    //begin(getVariables, NODE_ID);
                                  
                                    //
                                    //Sync time with the server, this will be called hourly in order to keep time from creeping with the crystal
                                    //
                                    unsigned long functionTimeout = millis();
                                    while (timeStatus() == timeNotSet && millis() - functionTimeout < 30000UL)
                                    {
                                  //    process();
                                      requestTime(receiveTime);
                                      DEBUG_PRINTLN(F("Getting Time"));
                                      wait(1000); // call once per second
                                      DEBUG_PRINTLN(F("."));
                                    }
                                    currentHour = hour();
                                    lastHour = hour();
                                    //
                                    //retrieve from EEPROM stored values on a power cycle.
                                    //
                                    boolean isDataOnEeprom = false;
                                    for (int i = 0; i < E_BUFFER_LENGTH; i++)
                                    {
                                      byte locator = loadState(EEPROM_BUFFER_LOCATION + i);
                                      if (locator == 0xFE)  // found the EEPROM circular buffer index
                                      {
                                        eepromIndex = EEPROM_BUFFER_LOCATION + i;
                                        DEBUG_PRINT(F("EEPROM Index "));
                                        DEBUG_PRINTLN(eepromIndex);
                                        //Now that we have the buffer index let's populate the rainBucket[] with data from eeprom
                                        loadRainArray(eepromIndex);
                                        isDataOnEeprom = true;
                                        break;
                                      }
                                    }
                                    //
                                    if (!isDataOnEeprom) // Added for the first time it is run on a new Arduino
                                    {
                                      DEBUG_PRINTLN(F("I didn't find valid EEPROM Index, so I'm writing one to location 0"));
                                      eepromIndex = EEPROM_BUFFER_LOCATION;
                                      saveState(eepromIndex, 0xFE);
                                      saveState(eepromIndex + 1, 0xFE);
                                      //then I will clear out any bad data
                                      for (int i = 2; i <= E_BUFFER_LENGTH; i++)
                                      {
                                        saveState(i, 0x00);
                                      }
                                    }
                                    dataMillis = millis();
                                    lastTipTime = millis() - oneHour; //why is this -oneHour?? Doesn't millis() start at 0 when first powered on?
                                    //
                                    request(CHILD_ID_TRIPPED_INDICATOR, V_VAR1);
                                    wait(DWELL_TIME);
                                    request(CHILD_ID_TRIPPED_INDICATOR, V_VAR2);
                                    wait(DWELL_TIME);
                                    //
                                  #ifdef DHT_ON
                                    dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN);
                                    // Register all sensors to gw (they will be created as child devices)
                                    present(CHILD_ID_HUM, S_HUM);
                                    wait(DWELL_TIME);
                                    present(CHILD_ID_TEMP, S_TEMP);
                                    wait(DWELL_TIME);
                                    metric = getConfig().isMetric;
                                    .wait(DWELL_TIME);
                                  #endif
                                    //
                                  #ifdef LUX_ON
                                    present(CHILD_ID_LIGHT, S_LIGHT_LEVEL);
                                    wait(DWELL_TIME);
                                    lightSensor.begin();
                                  #endif
                                    //
                                    DEBUG_PRINTLN(F("Radio Setup Complete!"));
                                    transmitRainData();
                                  }
                                  void presentation()
                                  {
                                    sendSketchInfo(SKETCH_NAME, SKETCH_VERSION);
                                    wait(DWELL_TIME);
                                    present(CHILD_ID_RAIN_LOG, S_RAIN);
                                    wait(DWELL_TIME);
                                    present(CHILD_ID_TRIPPED_INDICATOR, S_MOTION);
                                    wait(DWELL_TIME);
                                    DEBUG_PRINTLN(F("Sensor Presentation Complete"));
                                  }
                                  //
                                  void loop()
                                  {
                                  //  process();
                                    if (state)
                                    {
                                      prettyFade();  // breathe if tripped
                                    }
                                    else
                                    {
                                      slowFlash();   // blink if not tripped
                                    }
                                  #ifdef DEBUG_ON  // Serial Debug Block
                                    if ( (millis() - dataMillis) >= serialInterval)
                                    {
                                      for (int i = 24; i <= 120; i = i + 24)
                                      {
                                        updateSerialData(i);
                                      }
                                      dataMillis = millis();
                                    }
                                  #endif
                                    //
                                    // 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];
                                      if (measure != lastMeasure)
                                      {
                                        //      DEBUG_PRINT(F("measure value (total rainBucket within rainWindow): "));
                                        //      DEBUG_PRINTLN(measure);
                                        lastMeasure = measure;
                                      }
                                    }
                                    //
                                    state = (measure >= (rainSensorThreshold * 100));
                                    if (state != oldState)
                                    {
                                      send(msgTripped.set(state));
                                      wait(DWELL_TIME);
                                      DEBUG_PRINT(F("New Sensor State... Sensor: "));
                                      DEBUG_PRINTLN(state ? "Tripped" : "Not Tripped");
                                      oldState = state;
                                    }
                                    //
                                    unsigned long tipDelay = millis() - lastRainTime;
                                    if (wasTippedBuffer) // if was tipped, then update the 24hour total and transmit to Vera
                                    {
                                      DEBUG_PRINTLN(F("Sensor Tipped"));
                                      DEBUG_PRINT(F("rainBucket [0] value: "));
                                      DEBUG_PRINTLN(rainBucket [0]);
                                      send(msgRain.set((float)rainTotal(currentHour) / 100, 1)); //Calculate the total rain for the day
                                      wait(DWELL_TIME);
                                      wasTippedBuffer--;
                                      rainRate = ((oneHour) / tipDelay);
                                      if (rainRate != lastRainRate)
                                      {
                                        send(msgRainRate.set(rainRate, 1));
                                        wait(DWELL_TIME);
                                        DEBUG_PRINT(F("RainRate= "));
                                        DEBUG_PRINTLN(rainRate);
                                        lastRainRate = rainRate;
                                      }
                                      lastRainTime = lastTipTime;
                                    }
                                    //
                                    currentHour = hour();
                                    if (currentHour != lastHour)
                                    {
                                      DEBUG_PRINTLN(F("One hour elapsed."));
                                      send(msgRain.set((float)rainTotal(currentHour) / 100, 1)); // send today's rainfall
                                      wait(DWELL_TIME);
                                      saveState(eepromIndex, highByte(rainBucket[0]));
                                      saveState(eepromIndex + 1, lowByte(rainBucket[0]));
                                      DEBUG_PRINT(F("Saving rainBucket[0] to eeprom. rainBucket[0] = "));
                                      DEBUG_PRINTLN(rainBucket[0]);
                                      for (int i = RAIN_BUCKET_SIZE - 1; i >= 0; i--)//cascade an hour of values back into the array
                                      {
                                        rainBucket [i + 1] = rainBucket [i];
                                      }
                                      request(CHILD_ID_TRIPPED_INDICATOR, V_VAR1);
                                      wait(DWELL_TIME);
                                      request(CHILD_ID_TRIPPED_INDICATOR, V_VAR2);
                                      wait(DWELL_TIME);
                                      rainBucket[0] = 0;
                                      eepromIndex = eepromIndex + 2;
                                      if (eepromIndex > EEPROM_BUFFER_LOCATION + E_BUFFER_LENGTH)
                                      {
                                        eepromIndex = EEPROM_BUFFER_LOCATION;
                                      }
                                      DEBUG_PRINT(F("Writing to EEPROM.  Index: "));
                                      DEBUG_PRINTLN(eepromIndex);
                                      saveState(eepromIndex, 0xFE);
                                      saveState(eepromIndex + 1, 0xFE);
                                      requestTime(receiveTime); // sync the time every hour
                                      wait(DWELL_TIME);
                                      transmitRainData();
                                      rainRate = 0;
                                      send(msgRainRate.set(rainRate, 1));
                                      wait(DWELL_TIME);
                                      DEBUG_PRINTLN(F("Sending rainRate is 0 to controller"));
                                      lastHour = currentHour;
                                    }
                                    if (millis() - sensorPreviousMillis > DHT_LUX_DELAY)
                                    {
                                      #ifdef DHT_ON  //DHT Code
                                        doDHT();
                                      #endif
                                      #ifdef LUX_ON
                                        doLUX();
                                      #endif
                                      sensorPreviousMillis = millis();
                                    }
                                  }
                                  //
                                  #ifdef DHT_ON
                                  void doDHT(void)
                                  {
                                    float temperature = dht.getTemperature();
                                      if (isnan(temperature)) 
                                      {
                                        DEBUG_PRINTLN(F("Failed reading temperature from DHT"));
                                      } else if (temperature != lastTemp) 
                                      {
                                        lastTemp = temperature;
                                        if (!metric) 
                                        {
                                          temperature = dht.toFahrenheit(temperature);
                                        }
                                        gw.send(msgTemp.set(temperature, 1));
                                        gw.wait(DWELL_TIME);
                                        DEBUG_PRINT(F("Temperature is: "));
                                        DEBUG_PRINTLN(temperature);
                                      }
                                      float humidity = dht.getHumidity();;
                                      if (isnan(humidity)) 
                                      {
                                        DEBUG_PRINTLN(F("Failed reading humidity from DHT"));
                                      } else if (humidity != lastHum) 
                                      {
                                        lastHum = humidity;
                                        gw.send(msgHum.set(humidity, 1));
                                        gw.wait(DWELL_TIME);
                                        DEBUG_PRINT(F("Humidity is: "));
                                        DEBUG_PRINTLN(humidity);
                                      }
                                  }
                                  #endif
                                  //
                                  #ifdef LUX_ON
                                  void doLUX(void)
                                  {
                                    unsigned int lux = lightSensor.readLightLevel();// Get Lux value
                                    DEBUG_PRINT(F("Current LUX Level: "));
                                    DEBUG_PRINTLN(lux);
                                    heartbeat++;
                                    if (lux != lastlux || heartbeat > 10) 
                                    {
                                      gw.send(msg.set(lux));
                                      lastlux = lux;
                                    }
                                    if (heartbeat > 10) 
                                    {
                                      heartbeat = 0;
                                    }
                                  }
                                  #endif
                                  //
                                  void sensorTipped()
                                  {
                                    unsigned long thisTipTime = millis();
                                    if (thisTipTime - lastTipTime > 100UL)
                                    {
                                      rainBucket[0] += CALIBRATE_FACTOR; // adds CALIBRATE_FACTOR hundredths of unit each tip
                                      wasTippedBuffer++;
                                    }
                                    lastTipTime = thisTipTime;
                                  }
                                  //
                                  int rainTotal(int hours)
                                  {
                                    int total = 0;
                                    for ( int i = 0; i <= hours; i++)
                                    {
                                      total += rainBucket [i];
                                    }
                                    return total;
                                  }
                                  
                                  void updateSerialData(int x)
                                  {
                                    DEBUG_PRINT(F("Rain last "));
                                    DEBUG_PRINT(x);
                                    DEBUG_PRINTLN(F(" hours: "));
                                    float tipCount = 0;
                                    for (int i = 0; i < x; i++)
                                    {
                                      tipCount = tipCount + rainBucket [i];
                                    }
                                    tipCount = tipCount / 100;
                                    DEBUG_PRINTLN(tipCount);
                                  }
                                  
                                  void loadRainArray(int value) // retrieve stored rain array from EEPROM on powerup
                                  {
                                    for (int i = 0; i < RAIN_BUCKET_SIZE; i++)
                                    {
                                      value = value - 2;
                                      if (value < EEPROM_BUFFER_LOCATION)
                                      {
                                        value = EEPROM_BUFFER_LOCATION + E_BUFFER_LENGTH;
                                      }
                                      DEBUG_PRINT(F("EEPROM location: "));
                                      DEBUG_PRINTLN(value);
                                      byte rainValueHigh = loadState(value);
                                      byte rainValueLow = loadState(value + 1);
                                      unsigned int rainValue = rainValueHigh << 8;
                                      rainValue |= rainValueLow;
                                      rainBucket[i + 1] = rainValue;
                                      //
                                      DEBUG_PRINT(F("rainBucket[ value: "));
                                      DEBUG_PRINT(i + 1);
                                      DEBUG_PRINT(F("] value: "));
                                      DEBUG_PRINTLN(rainBucket[i + 1]);
                                    }
                                  }
                                  
                                  void transmitRainData(void)
                                  {
                                    DEBUG_PRINT(F("In transmitRainData. currentHour = "));
                                    DEBUG_PRINTLN(currentHour);
                                    int rainUpdateTotal = 0;
                                    for (int i = currentHour; i >= 0; i--)
                                    {
                                      rainUpdateTotal += rainBucket[i];
                                      DEBUG_PRINT(F("Adding rainBucket["));
                                      DEBUG_PRINT(i);
                                      DEBUG_PRINTLN(F("] to rainUpdateTotal."));
                                    }
                                    DEBUG_PRINT(F("TX Day 1: rainUpdateTotal = "));
                                    DEBUG_PRINTLN((float)rainUpdateTotal / 100.0);
                                    send(msgRainVAR1.set((float)rainUpdateTotal / 100.0, 1)); //Send current day rain totals (resets at midnight)
                                    wait(DWELL_TIME);
                                  #ifdef USE_DAILY
                                    rainUpdateTotal = 0;
                                  #endif
                                    for (int i = currentHour + 24; i > currentHour; i--)
                                    {
                                      rainUpdateTotal += rainBucket[i];
                                      DEBUG_PRINT(F("Adding rainBucket["));
                                      DEBUG_PRINT(i);
                                      DEBUG_PRINTLN(F("] to rainUpdateTotal."));
                                    }
                                    DEBUG_PRINT(F("TX Day 2: rainUpdateTotal = "));
                                    DEBUG_PRINTLN((float)rainUpdateTotal / 100.0);
                                    send(msgRainVAR2.set((float)rainUpdateTotal / 100.0, 1));
                                    wait(DWELL_TIME);
                                  #ifdef USE_DAILY
                                    rainUpdateTotal = 0;
                                  #endif
                                    for (int i = currentHour + 48; i > currentHour + 24; i--)
                                    {
                                      rainUpdateTotal += rainBucket[i];
                                      DEBUG_PRINT(F("Adding rainBucket["));
                                      DEBUG_PRINT(i);
                                      DEBUG_PRINTLN(F("] to rainUpdateTotal."));
                                    }
                                    DEBUG_PRINT(F("TX Day 3: rainUpdateTotal = "));
                                    DEBUG_PRINTLN((float)rainUpdateTotal / 100.0);
                                    send(msgRainVAR3.set((float)rainUpdateTotal / 100.0, 1));
                                    wait(DWELL_TIME);
                                  #ifdef USE_DAILY
                                    rainUpdateTotal = 0;
                                  #endif
                                    for (int i = currentHour + 72; i > currentHour + 48; i--)
                                    {
                                      rainUpdateTotal += rainBucket[i];
                                      DEBUG_PRINT(F("Adding rainBucket["));
                                      DEBUG_PRINT(i);
                                      DEBUG_PRINTLN(F("] to rainUpdateTotal."));
                                    }
                                    DEBUG_PRINT(F("TX Day 4: rainUpdateTotal = "));
                                    DEBUG_PRINTLN((float)rainUpdateTotal / 100.0);
                                    send(msgRainVAR4.set((float)rainUpdateTotal / 100.0, 1));
                                    wait(DWELL_TIME);
                                  #ifdef USE_DAILY
                                    rainUpdateTotal = 0;
                                  #endif
                                    for (int i = currentHour + 96; i > currentHour + 72; i--)
                                    {
                                      rainUpdateTotal += rainBucket[i];
                                      DEBUG_PRINT(F("Adding rainBucket["));
                                      DEBUG_PRINT(i);
                                      DEBUG_PRINTLN(F("] to rainUpdateTotal."));
                                    }
                                    DEBUG_PRINT(F("TX Day 5: rainUpdateTotal = "));
                                    DEBUG_PRINTLN((float)rainUpdateTotal / 100.0);
                                    send(msgRainVAR5.set((float)rainUpdateTotal / 100.0, 1));
                                    wait(DWELL_TIME);
                                  }
                                  
                                  void getVariables(const MyMessage &message)
                                  {
                                    if (message.sensor == CHILD_ID_RAIN_LOG)
                                    {
                                      // nothing to do here
                                    }
                                    else if (message.sensor == CHILD_ID_TRIPPED_INDICATOR)
                                    {
                                      if (message.type == V_VAR1)
                                      {
                                        rainWindow = atoi(message.data);
                                        if (rainWindow > 120)
                                        {
                                          rainWindow = 120;
                                        }
                                        else if (rainWindow < 1)
                                        {
                                          rainWindow = 1;
                                        }
                                        if (rainWindow != atoi(message.data))   // if I changed the value back inside the boundries, push that number back to Vera
                                        {
                                          send(msgTrippedVar1.set(rainWindow));
                                        }
                                      }
                                      else if (message.type == V_VAR2)
                                      {
                                        rainSensorThreshold = atoi(message.data);
                                        if (rainSensorThreshold > 10000)
                                        {
                                          rainSensorThreshold = 10000;
                                        }
                                        else if (rainSensorThreshold < 1)
                                        {
                                          rainSensorThreshold = 1;
                                        }
                                        if (rainSensorThreshold != atoi(message.data))  // if I changed the value back inside the boundries, push that number back to Vera
                                        {
                                          send(msgTrippedVar2.set(rainSensorThreshold));
                                        }
                                      }
                                    }
                                  }
                                  
                                  void prettyFade(void)
                                  {
                                    float val = (exp(sin(millis() / 2000.0 * PI)) - 0.36787944) * 108.0;
                                    analogWrite(ledPin, val);
                                  }
                                  
                                  void slowFlash(void)
                                  {
                                    static boolean ledState = true;
                                    static unsigned long pulseStart = millis();
                                    if (millis() - pulseStart < 100UL)
                                    {
                                      digitalWrite(ledPin, !ledState);
                                      pulseStart = millis();
                                    }
                                  }
                                  
                                  void receiveTime(unsigned long time)
                                  {
                                    DEBUG_PRINTLN(F("Time received..."));
                                    setTime(time);
                                    char theTime[6];
                                    sprintf(theTime, "%d:%2d", hour(), minute());
                                    DEBUG_PRINTLN(theTime);
                                  }
                                  
                                  AWIA Offline
                                  AWIA Offline
                                  AWI
                                  Hero Member
                                  wrote on last edited by
                                  #116

                                  @niccodemi syntax for time functions is:

                                  requestTime();						// Request latest time from controller
                                  
                                  // This is called when a new time value was received
                                  void receiveTime(unsigned long controllerTime) {
                                  }
                                  
                                  N 1 Reply Last reply
                                  0
                                  • AWIA AWI

                                    @niccodemi syntax for time functions is:

                                    requestTime();						// Request latest time from controller
                                    
                                    // This is called when a new time value was received
                                    void receiveTime(unsigned long controllerTime) {
                                    }
                                    
                                    N Offline
                                    N Offline
                                    niccodemi
                                    wrote on last edited by
                                    #117

                                    @AWI thank you. Sketch compiles now but after uploading it still doesn't work - serial monitor is blank (debug on).

                                    1 Reply Last reply
                                    0
                                    • mrc-coreM Offline
                                      mrc-coreM Offline
                                      mrc-core
                                      wrote on last edited by mrc-core
                                      #118

                                      Using the new code for version 2.0.0 i'm getting this errors:

                                      C:\Users\marco\Documents\Arduino\Nova pasta\RainGauge\RainGauge.ino: In function 'void setup()':

                                      RainGauge:145: error: 'timeStatus' was not declared in this scope

                                      while (timeStatus() == timeNotSet && millis() - functionTimeout < 30000UL)

                                      RainGauge:145: error: 'timeNotSet' was not declared in this scope

                                      while (timeStatus() == timeNotSet && millis() - functionTimeout < 30000UL)

                                      RainGauge:152: error: 'hour' was not declared in this scope

                                      currentHour = hour();

                                      C:\Users\marco\Documents\Arduino\Nova pasta\RainGauge\RainGauge.ino: In function 'void loop()':

                                      RainGauge:299: error: 'hour' was not declared in this scope

                                      currentHour = hour();

                                      C:\Users\marco\Documents\Arduino\Nova pasta\RainGauge\RainGauge.ino: In function 'void receiveTime(long unsigned int)':

                                      RainGauge:597: error: 'setTime' was not declared in this scope

                                      setTime(time);

                                      RainGauge:599: error: 'hour' was not declared in this scope

                                      sprintf(theTime, "%d:%2d", hour(), minute());

                                      RainGauge:599: error: 'minute' was not declared in this scope

                                      sprintf(theTime, "%d:%2d", hour(), minute());

                                      Using library MySensors at version 2.0.0 in folder: C:\Users\marco\Documents\Arduino\libraries\MySensors
                                      Using library SPI at version 1.0 in folder: C:\Users\marco\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.13\libraries\SPI
                                      Using library Wire at version 1.0 in folder: C:\Users\marco\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.13\libraries\Wire
                                      exit status 1
                                      'timeStatus' was not declared in this scope

                                      Does anyone now how to fix this ??

                                      N 1 Reply Last reply
                                      0
                                      • mrc-coreM mrc-core

                                        Using the new code for version 2.0.0 i'm getting this errors:

                                        C:\Users\marco\Documents\Arduino\Nova pasta\RainGauge\RainGauge.ino: In function 'void setup()':

                                        RainGauge:145: error: 'timeStatus' was not declared in this scope

                                        while (timeStatus() == timeNotSet && millis() - functionTimeout < 30000UL)

                                        RainGauge:145: error: 'timeNotSet' was not declared in this scope

                                        while (timeStatus() == timeNotSet && millis() - functionTimeout < 30000UL)

                                        RainGauge:152: error: 'hour' was not declared in this scope

                                        currentHour = hour();

                                        C:\Users\marco\Documents\Arduino\Nova pasta\RainGauge\RainGauge.ino: In function 'void loop()':

                                        RainGauge:299: error: 'hour' was not declared in this scope

                                        currentHour = hour();

                                        C:\Users\marco\Documents\Arduino\Nova pasta\RainGauge\RainGauge.ino: In function 'void receiveTime(long unsigned int)':

                                        RainGauge:597: error: 'setTime' was not declared in this scope

                                        setTime(time);

                                        RainGauge:599: error: 'hour' was not declared in this scope

                                        sprintf(theTime, "%d:%2d", hour(), minute());

                                        RainGauge:599: error: 'minute' was not declared in this scope

                                        sprintf(theTime, "%d:%2d", hour(), minute());

                                        Using library MySensors at version 2.0.0 in folder: C:\Users\marco\Documents\Arduino\libraries\MySensors
                                        Using library SPI at version 1.0 in folder: C:\Users\marco\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.13\libraries\SPI
                                        Using library Wire at version 1.0 in folder: C:\Users\marco\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.13\libraries\Wire
                                        exit status 1
                                        'timeStatus' was not declared in this scope

                                        Does anyone now how to fix this ??

                                        N Offline
                                        N Offline
                                        niccodemi
                                        wrote on last edited by
                                        #119

                                        @mrc-core is this the sketch you are using?

                                        /*
                                         Arduino Tipping Bucket Rain Gauge
                                        
                                         April 26, 2015
                                        
                                         Version 1.4.1 alpha
                                        
                                         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 5 days.  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.
                                        
                                         There is a build overview video here: https://youtu.be/1eMfKQaLROo
                                        
                                         This sketch features the following:
                                        
                                         * Allows you to set the rain threshold in mm
                                         * Allows you to determine the tripped indicator window up to 120 hours.
                                         * Displays the last 5 days of rain in Variable1 through Variable5
                                           of the Rain Sensor device
                                         * Configuration changes to Sensor device updated every hour
                                         * Should run on any Arduino
                                         * Will retain Tripped/Not Tripped status and data in a power interruption, saving small amount
                                           of data to EEPROM (Circular Buffer to maximize life of EEPROM)
                                         * LED status indicator
                                         * Optional Temp/Humidity (DHT-22 or DHT-11) and Light LUX (BH1750) sensors. To use, uncomment
                                           #define DHT_ON  and/or #define LUX_ON
                                         * Optionally send total accumulation of each day's rainfall or send only individual days rainfall totals.
                                           Comment out #define USE_DAILY to display individual daily rainfall.
                                        
                                         by @BulldogLowell and @PeteWill for free public use
                                        
                                         */
                                        
                                        // Enable debug prints to serial monitor
                                        #define MY_DEBUG 
                                        
                                        // Enable and select radio type attached
                                        #define MY_RADIO_NRF24
                                        //#define MY_RADIO_RFM69
                                        
                                        //#define MY_NODE_ID 7
                                        
                                        #include <SPI.h>
                                        #include <MySensors.h>
                                        #include <math.h>
                                        #include <Time.h>
                                        
                                        #define SKETCH_NAME "Rain Gauge"
                                        #define SKETCH_VERSION "1.4.1a"
                                        
                                        #define DWELL_TIME 125  // this allows for radio to come back to power after a transmission, ideally 0 
                                        
                                        //#define DEBUG_ON  // comment out this line to disable serial debug
                                        #define DHT_ON // uncomment out this line to enable DHT sensor
                                        #define LUX_ON // uncomment out this line to enable BH1750 sensor
                                        //#define USE_DAILY // displays each time segment as an accumulation of prior periods inclusive.  Comment out to display individual daily rainfall totals in the variables sent to your controller.
                                        
                                        #define TIP_SENSOR_PIN 3
                                        
                                        #define CALIBRATE_FACTOR 60 // amount of rain per rain bucket tip e.g. 5 is .05mm
                                        #define DHT_LUX_DELAY 300000  //Delay in milliseconds that the DHT and LUX sensors will wait before sending data
                                        
                                        #define CHILD_ID_RAIN_LOG 3  // Keeps track of accumulated rainfall
                                        #define CHILD_ID_TRIPPED_INDICATOR 4  // Indicates Tripped when rain detected
                                        #define EEPROM_BUFFER_LOCATION 0  // location of the EEPROM circular buffer
                                        #define E_BUFFER_LENGTH 240
                                        #define RAIN_BUCKET_SIZE 120
                                        
                                        #ifdef  DEBUG_ON
                                          #define DEBUG_PRINT(x)   Serial.print(x)
                                          #define DEBUG_PRINTLN(x) Serial.println(x)
                                          #define SERIAL_START(x)  Serial.begin(x)
                                          #else
                                          #define DEBUG_PRINT(x)
                                          #define DEBUG_PRINTLN(x)
                                          #define SERIAL_START(x)
                                        #endif
                                        //
                                        MyMessage msgRainRate(CHILD_ID_RAIN_LOG, V_RAINRATE);
                                        MyMessage msgRain(CHILD_ID_RAIN_LOG, V_RAIN);
                                        //
                                        MyMessage msgRainVAR1(CHILD_ID_RAIN_LOG, V_VAR1);
                                        MyMessage msgRainVAR2(CHILD_ID_RAIN_LOG, V_VAR2);
                                        MyMessage msgRainVAR3(CHILD_ID_RAIN_LOG, V_VAR3);
                                        MyMessage msgRainVAR4(CHILD_ID_RAIN_LOG, V_VAR4);
                                        MyMessage msgRainVAR5(CHILD_ID_RAIN_LOG, V_VAR5);
                                        //
                                        MyMessage msgTripped(CHILD_ID_TRIPPED_INDICATOR, V_TRIPPED);
                                        MyMessage msgTrippedVar1(CHILD_ID_TRIPPED_INDICATOR, V_VAR1);
                                        MyMessage msgTrippedVar2(CHILD_ID_TRIPPED_INDICATOR, V_VAR2);
                                        //
                                        #ifdef DHT_ON
                                          #include <DHT.h>
                                          #define CHILD_ID_HUM 0
                                          #define CHILD_ID_TEMP 1
                                          #define HUMIDITY_SENSOR_DIGITAL_PIN 8
                                          DHT dht;
                                          float lastTemp;
                                          float lastHum;
                                          boolean metric = true;
                                          MyMessage msgHum(CHILD_ID_HUM, V_HUM);
                                          MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
                                        #endif
                                        //
                                        #ifdef LUX_ON
                                          //BH1750 is connected to SCL (analog input A5) and SDA (analog input A4)
                                          #include <BH1750.h>
                                          #include <Wire.h>
                                          #define CHILD_ID_LIGHT 2
                                          BH1750 lightSensor;
                                          MyMessage msg(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
                                          unsigned int lastlux;
                                          byte heartbeat = 10; //Used to send the light lux to gateway as soon as the device is restarted and after the DHT_LUX_DELAY has happened 10 times
                                        #endif
                                        unsigned long sensorPreviousMillis;
                                        int eepromIndex;
                                        int ledPin = 5; // Pin the LED is connected to.  PWM capable pin required
                                        unsigned long dataMillis;
                                        unsigned long serialInterval = 600000UL;
                                        const unsigned long oneHour = 3600000UL;
                                        unsigned long lastTipTime;
                                        unsigned long lastRainTime; //Used for rainRate calculation
                                        unsigned int rainBucket [RAIN_BUCKET_SIZE] ; /* 24 hours x 5 Days = 120 hours */
                                        unsigned int rainRate = 0;
                                        byte rainWindow = 72;         //default rain window in hours.  Will be overwritten with msgTrippedVar1.
                                        volatile int wasTippedBuffer = 0;
                                        int rainSensorThreshold = 50; //default rain sensor sensitivity in hundredths.  Will be overwritten with msgTrippedVar2.
                                        byte state = 0;
                                        byte oldState = -1;
                                        unsigned int lastRainRate = 0;
                                        int lastMeasure = 0;
                                        boolean gotTime = false;
                                        byte lastHour;
                                        byte currentHour;
                                        //
                                        void setup()
                                        {
                                          SERIAL_START(115200);
                                          //
                                          // Set up the IO
                                          pinMode(TIP_SENSOR_PIN, INPUT_PULLUP);
                                          attachInterrupt (digitalPinToInterrupt(TIP_SENSOR_PIN), sensorTipped, FALLING);  // depending on location of the hall effect sensor may need CHANGE
                                          pinMode(ledPin, OUTPUT);
                                          digitalWrite(ledPin, HIGH);
                                          //
                                          //Sync time with the server, this will be called hourly in order to keep time from creeping with the crystal
                                          //
                                          unsigned long functionTimeout = millis();
                                          while (timeStatus() == timeNotSet && millis() - functionTimeout < 30000UL)
                                          {
                                            requestTime();
                                            DEBUG_PRINTLN(F("Getting Time"));
                                            wait(1000); // call once per second
                                            DEBUG_PRINTLN(F("."));
                                          }
                                          currentHour = hour();
                                          lastHour = hour();
                                          //
                                          //retrieve from EEPROM stored values on a power cycle.
                                          //
                                          boolean isDataOnEeprom = false;
                                          for (int i = 0; i < E_BUFFER_LENGTH; i++)
                                          {
                                            byte locator = loadState(EEPROM_BUFFER_LOCATION + i);
                                            if (locator == 0xFE)  // found the EEPROM circular buffer index
                                            {
                                              eepromIndex = EEPROM_BUFFER_LOCATION + i;
                                              DEBUG_PRINT(F("EEPROM Index "));
                                              DEBUG_PRINTLN(eepromIndex);
                                              //Now that we have the buffer index let's populate the rainBucket[] with data from eeprom
                                              loadRainArray(eepromIndex);
                                              isDataOnEeprom = true;
                                              break;
                                            }
                                          }
                                          //
                                          if (!isDataOnEeprom) // Added for the first time it is run on a new Arduino
                                          {
                                            DEBUG_PRINTLN(F("I didn't find valid EEPROM Index, so I'm writing one to location 0"));
                                            eepromIndex = EEPROM_BUFFER_LOCATION;
                                            saveState(eepromIndex, 0xFE);
                                            saveState(eepromIndex + 1, 0xFE);
                                            //then I will clear out any bad data
                                            for (int i = 2; i <= E_BUFFER_LENGTH; i++)
                                            {
                                              saveState(i, 0x00);
                                            }
                                          }
                                          dataMillis = millis();
                                          lastTipTime = millis() - oneHour; //why is this -oneHour?? Doesn't millis() start at 0 when first powered on?
                                          //
                                          request(CHILD_ID_TRIPPED_INDICATOR, V_VAR1);
                                          wait(DWELL_TIME);
                                          request(CHILD_ID_TRIPPED_INDICATOR, V_VAR2);
                                          wait(DWELL_TIME);
                                          //
                                        #ifdef DHT_ON
                                          dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN);
                                          wait(DWELL_TIME);
                                          metric = getConfig().isMetric;
                                        #endif
                                          //
                                        #ifdef LUX_ON
                                          wait(DWELL_TIME);
                                          lightSensor.begin();
                                        #endif
                                          //
                                          DEBUG_PRINTLN(F("Radio Setup Complete!"));
                                          transmitRainData();
                                        }
                                        
                                        
                                        void presentation()  {
                                          // Register all sensors to gw (they will be created as child devices)
                                          sendSketchInfo(SKETCH_NAME, SKETCH_VERSION);
                                          wait(DWELL_TIME);
                                          present(CHILD_ID_RAIN_LOG, S_RAIN);
                                          wait(DWELL_TIME);
                                          present(CHILD_ID_TRIPPED_INDICATOR, S_MOTION);
                                          wait(DWELL_TIME);
                                        
                                        #ifdef DHT_ON
                                          present(CHILD_ID_HUM, S_HUM);
                                          wait(DWELL_TIME);
                                          present(CHILD_ID_TEMP, S_TEMP);
                                          wait(DWELL_TIME);
                                        #endif
                                        
                                        
                                        #ifdef LUX_ON
                                          present(CHILD_ID_LIGHT, S_LIGHT_LEVEL);
                                        #endif
                                        
                                          DEBUG_PRINTLN(F("Sensor Presentation Complete"));
                                        }
                                        
                                        void loop()
                                        {
                                          if (state)
                                          {
                                            prettyFade();  // breathe if tripped
                                          }
                                          else
                                          {
                                            slowFlash();   // blink if not tripped
                                          }
                                        #ifdef DEBUG_ON  // Serial Debug Block
                                          if ( (millis() - dataMillis) >= serialInterval)
                                          {
                                            for (int i = 24; i <= 120; i = i + 24)
                                            {
                                              updateSerialData(i);
                                            }
                                            dataMillis = millis();
                                          }
                                        #endif
                                          //
                                          // 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];
                                            if (measure != lastMeasure)
                                            {
                                              //      DEBUG_PRINT(F("measure value (total rainBucket within rainWindow): "));
                                              //      DEBUG_PRINTLN(measure);
                                              lastMeasure = measure;
                                            }
                                          }
                                          //
                                          state = (measure >= (rainSensorThreshold * 100));
                                          if (state != oldState)
                                          {
                                            send(msgTripped.set(state));
                                            wait(DWELL_TIME);
                                            DEBUG_PRINT(F("New Sensor State... Sensor: "));
                                            DEBUG_PRINTLN(state ? "Tripped" : "Not Tripped");
                                            oldState = state;
                                          }
                                          //
                                          unsigned long tipDelay = millis() - lastRainTime;
                                          if (wasTippedBuffer) // if was tipped, then update the 24hour total and transmit to Vera
                                          {
                                            DEBUG_PRINTLN(F("Sensor Tipped"));
                                            DEBUG_PRINT(F("rainBucket [0] value: "));
                                            DEBUG_PRINTLN(rainBucket [0]);
                                            send(msgRain.set((float)rainTotal(currentHour) / 100, 1)); //Calculate the total rain for the day
                                            wait(DWELL_TIME);
                                            wasTippedBuffer--;
                                            rainRate = ((oneHour) / tipDelay);
                                            if (rainRate != lastRainRate)
                                            {
                                              send(msgRainRate.set(rainRate, 1));
                                              wait(DWELL_TIME);
                                              DEBUG_PRINT(F("RainRate= "));
                                              DEBUG_PRINTLN(rainRate);
                                              lastRainRate = rainRate;
                                            }
                                            lastRainTime = lastTipTime;
                                          }
                                          //
                                          currentHour = hour();
                                          if (currentHour != lastHour)
                                          {
                                            DEBUG_PRINTLN(F("One hour elapsed."));
                                            send(msgRain.set((float)rainTotal(currentHour) / 100, 1)); // send today's rainfall
                                            wait(DWELL_TIME);
                                            saveState(eepromIndex, highByte(rainBucket[0]));
                                            saveState(eepromIndex + 1, lowByte(rainBucket[0]));
                                            DEBUG_PRINT(F("Saving rainBucket[0] to eeprom. rainBucket[0] = "));
                                            DEBUG_PRINTLN(rainBucket[0]);
                                            for (int i = RAIN_BUCKET_SIZE - 1; i >= 0; i--)//cascade an hour of values back into the array
                                            {
                                              rainBucket [i + 1] = rainBucket [i];
                                            }
                                            request(CHILD_ID_TRIPPED_INDICATOR, V_VAR1);
                                            wait(DWELL_TIME);
                                            request(CHILD_ID_TRIPPED_INDICATOR, V_VAR2);
                                            wait(DWELL_TIME);
                                            rainBucket[0] = 0;
                                            eepromIndex = eepromIndex + 2;
                                            if (eepromIndex > EEPROM_BUFFER_LOCATION + E_BUFFER_LENGTH)
                                            {
                                              eepromIndex = EEPROM_BUFFER_LOCATION;
                                            }
                                            DEBUG_PRINT(F("Writing to EEPROM.  Index: "));
                                            DEBUG_PRINTLN(eepromIndex);
                                            saveState(eepromIndex, 0xFE);
                                            saveState(eepromIndex + 1, 0xFE);
                                            requestTime(); // sync the time every hour
                                            wait(DWELL_TIME);
                                            transmitRainData();
                                            rainRate = 0;
                                            send(msgRainRate.set(rainRate, 1));
                                            wait(DWELL_TIME);
                                            DEBUG_PRINTLN(F("Sending rainRate is 0 to controller"));
                                            lastHour = currentHour;
                                          }
                                          if (millis() - sensorPreviousMillis > DHT_LUX_DELAY)
                                          {
                                            #ifdef DHT_ON  //DHT Code
                                              doDHT();
                                            #endif
                                            #ifdef LUX_ON
                                              doLUX();
                                            #endif
                                            sensorPreviousMillis = millis();
                                          }
                                        }
                                        //
                                        #ifdef DHT_ON
                                        void doDHT(void)
                                        {
                                          float temperature = dht.getTemperature();
                                            if (isnan(temperature)) 
                                            {
                                              DEBUG_PRINTLN(F("Failed reading temperature from DHT"));
                                            } else if (temperature != lastTemp) 
                                            {
                                              lastTemp = temperature;
                                              if (!metric) 
                                              {
                                                temperature = dht.toFahrenheit(temperature);
                                              }
                                              send(msgTemp.set(temperature, 1));
                                              wait(DWELL_TIME);
                                              DEBUG_PRINT(F("Temperature is: "));
                                              DEBUG_PRINTLN(temperature);
                                            }
                                            float humidity = dht.getHumidity();;
                                            if (isnan(humidity)) 
                                            {
                                              DEBUG_PRINTLN(F("Failed reading humidity from DHT"));
                                            } else if (humidity != lastHum) 
                                            {
                                              lastHum = humidity;
                                              send(msgHum.set(humidity, 1));
                                              wait(DWELL_TIME);
                                              DEBUG_PRINT(F("Humidity is: "));
                                              DEBUG_PRINTLN(humidity);
                                            }
                                        }
                                        #endif
                                        //
                                        #ifdef LUX_ON
                                        void doLUX(void)
                                        {
                                          unsigned int lux = lightSensor.readLightLevel();// Get Lux value
                                          DEBUG_PRINT(F("Current LUX Level: "));
                                          DEBUG_PRINTLN(lux);
                                          heartbeat++;
                                          if (lux != lastlux || heartbeat > 10) 
                                          {
                                            send(msg.set(lux));
                                            lastlux = lux;
                                          }
                                          if (heartbeat > 10) 
                                          {
                                            heartbeat = 0;
                                          }
                                        }
                                        #endif
                                        //
                                        void sensorTipped()
                                        {
                                          unsigned long thisTipTime = millis();
                                          if (thisTipTime - lastTipTime > 100UL)
                                          {
                                            rainBucket[0] += CALIBRATE_FACTOR; // adds CALIBRATE_FACTOR hundredths of unit each tip
                                            wasTippedBuffer++;
                                          }
                                          lastTipTime = thisTipTime;
                                        }
                                        //
                                        int rainTotal(int hours)
                                        {
                                          int total = 0;
                                          for ( int i = 0; i <= hours; i++)
                                          {
                                            total += rainBucket [i];
                                          }
                                          return total;
                                        }
                                        
                                        void updateSerialData(int x)
                                        {
                                          DEBUG_PRINT(F("Rain last "));
                                          DEBUG_PRINT(x);
                                          DEBUG_PRINTLN(F(" hours: "));
                                          float tipCount = 0;
                                          for (int i = 0; i < x; i++)
                                          {
                                            tipCount = tipCount + rainBucket [i];
                                          }
                                          tipCount = tipCount / 100;
                                          DEBUG_PRINTLN(tipCount);
                                        }
                                        
                                        void loadRainArray(int value) // retrieve stored rain array from EEPROM on powerup
                                        {
                                          for (int i = 0; i < RAIN_BUCKET_SIZE; i++)
                                          {
                                            value = value - 2;
                                            if (value < EEPROM_BUFFER_LOCATION)
                                            {
                                              value = EEPROM_BUFFER_LOCATION + E_BUFFER_LENGTH;
                                            }
                                            DEBUG_PRINT(F("EEPROM location: "));
                                            DEBUG_PRINTLN(value);
                                            byte rainValueHigh = loadState(value);
                                            byte rainValueLow = loadState(value + 1);
                                            unsigned int rainValue = rainValueHigh << 8;
                                            rainValue |= rainValueLow;
                                            rainBucket[i + 1] = rainValue;
                                            //
                                            DEBUG_PRINT(F("rainBucket[ value: "));
                                            DEBUG_PRINT(i + 1);
                                            DEBUG_PRINT(F("] value: "));
                                            DEBUG_PRINTLN(rainBucket[i + 1]);
                                          }
                                        }
                                        
                                        void transmitRainData(void)
                                        {
                                          DEBUG_PRINT(F("In transmitRainData. currentHour = "));
                                          DEBUG_PRINTLN(currentHour);
                                          int rainUpdateTotal = 0;
                                          for (int i = currentHour; i >= 0; i--)
                                          {
                                            rainUpdateTotal += rainBucket[i];
                                            DEBUG_PRINT(F("Adding rainBucket["));
                                            DEBUG_PRINT(i);
                                            DEBUG_PRINTLN(F("] to rainUpdateTotal."));
                                          }
                                          DEBUG_PRINT(F("TX Day 1: rainUpdateTotal = "));
                                          DEBUG_PRINTLN((float)rainUpdateTotal / 100.0);
                                          send(msgRainVAR1.set((float)rainUpdateTotal / 100.0, 1)); //Send current day rain totals (resets at midnight)
                                          wait(DWELL_TIME);
                                        #ifdef USE_DAILY
                                          rainUpdateTotal = 0;
                                        #endif
                                          for (int i = currentHour + 24; i > currentHour; i--)
                                          {
                                            rainUpdateTotal += rainBucket[i];
                                            DEBUG_PRINT(F("Adding rainBucket["));
                                            DEBUG_PRINT(i);
                                            DEBUG_PRINTLN(F("] to rainUpdateTotal."));
                                          }
                                          DEBUG_PRINT(F("TX Day 2: rainUpdateTotal = "));
                                          DEBUG_PRINTLN((float)rainUpdateTotal / 100.0);
                                          send(msgRainVAR2.set((float)rainUpdateTotal / 100.0, 1));
                                          wait(DWELL_TIME);
                                        #ifdef USE_DAILY
                                          rainUpdateTotal = 0;
                                        #endif
                                          for (int i = currentHour + 48; i > currentHour + 24; i--)
                                          {
                                            rainUpdateTotal += rainBucket[i];
                                            DEBUG_PRINT(F("Adding rainBucket["));
                                            DEBUG_PRINT(i);
                                            DEBUG_PRINTLN(F("] to rainUpdateTotal."));
                                          }
                                          DEBUG_PRINT(F("TX Day 3: rainUpdateTotal = "));
                                          DEBUG_PRINTLN((float)rainUpdateTotal / 100.0);
                                          send(msgRainVAR3.set((float)rainUpdateTotal / 100.0, 1));
                                          wait(DWELL_TIME);
                                        #ifdef USE_DAILY
                                          rainUpdateTotal = 0;
                                        #endif
                                          for (int i = currentHour + 72; i > currentHour + 48; i--)
                                          {
                                            rainUpdateTotal += rainBucket[i];
                                            DEBUG_PRINT(F("Adding rainBucket["));
                                            DEBUG_PRINT(i);
                                            DEBUG_PRINTLN(F("] to rainUpdateTotal."));
                                          }
                                          DEBUG_PRINT(F("TX Day 4: rainUpdateTotal = "));
                                          DEBUG_PRINTLN((float)rainUpdateTotal / 100.0);
                                          send(msgRainVAR4.set((float)rainUpdateTotal / 100.0, 1));
                                          wait(DWELL_TIME);
                                        #ifdef USE_DAILY
                                          rainUpdateTotal = 0;
                                        #endif
                                          for (int i = currentHour + 96; i > currentHour + 72; i--)
                                          {
                                            rainUpdateTotal += rainBucket[i];
                                            DEBUG_PRINT(F("Adding rainBucket["));
                                            DEBUG_PRINT(i);
                                            DEBUG_PRINTLN(F("] to rainUpdateTotal."));
                                          }
                                          DEBUG_PRINT(F("TX Day 5: rainUpdateTotal = "));
                                          DEBUG_PRINTLN((float)rainUpdateTotal / 100.0);
                                          send(msgRainVAR5.set((float)rainUpdateTotal / 100.0, 1));
                                          wait(DWELL_TIME);
                                        }
                                        
                                        void receive(const MyMessage &message)
                                        {
                                          if (message.sensor == CHILD_ID_RAIN_LOG)
                                          {
                                            // nothing to do here
                                          }
                                          else if (message.sensor == CHILD_ID_TRIPPED_INDICATOR)
                                          {
                                            if (message.type == V_VAR1)
                                            {
                                              rainWindow = atoi(message.data);
                                              if (rainWindow > 120)
                                              {
                                                rainWindow = 120;
                                              }
                                              else if (rainWindow < 1)
                                              {
                                                rainWindow = 1;
                                              }
                                              if (rainWindow != atoi(message.data))   // if I changed the value back inside the boundries, push that number back to Vera
                                              {
                                                send(msgTrippedVar1.set(rainWindow));
                                              }
                                            }
                                            else if (message.type == V_VAR2)
                                            {
                                              rainSensorThreshold = atoi(message.data);
                                              if (rainSensorThreshold > 10000)
                                              {
                                                rainSensorThreshold = 10000;
                                              }
                                              else if (rainSensorThreshold < 1)
                                              {
                                                rainSensorThreshold = 1;
                                              }
                                              if (rainSensorThreshold != atoi(message.data))  // if I changed the value back inside the boundries, push that number back to Vera
                                              {
                                                send(msgTrippedVar2.set(rainSensorThreshold));
                                              }
                                            }
                                          }
                                        }
                                        
                                        void prettyFade(void)
                                        {
                                          float val = (exp(sin(millis() / 2000.0 * PI)) - 0.36787944) * 108.0;
                                          analogWrite(ledPin, val);
                                        }
                                        
                                        void slowFlash(void)
                                        {
                                          static boolean ledState = true;
                                          static unsigned long pulseStart = millis();
                                          if (millis() - pulseStart < 100UL)
                                          {
                                            digitalWrite(ledPin, !ledState);
                                            pulseStart = millis();
                                          }
                                        }
                                        
                                        void receiveTime(unsigned long time)
                                        {
                                          DEBUG_PRINTLN(F("Time received..."));
                                          setTime(time);
                                          char theTime[6];
                                          sprintf(theTime, "%d:%2d", hour(), minute());
                                          DEBUG_PRINTLN(theTime);
                                        }
                                        

                                        It compiles fine with Arduino IDE 1.6.5, Mysensors 2.0.0 and libraries (Time, BH1750).

                                        1 Reply Last reply
                                        0
                                        • mrc-coreM Offline
                                          mrc-coreM Offline
                                          mrc-core
                                          wrote on last edited by
                                          #120

                                          Yes that's the one.

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


                                          9

                                          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