Adding NRF24 code to existing sensor sketch

  • If this has been asked before I apologise, but I've got a sketch that counts hourly and daily rainfall from a simple tipping bucket raingauge. I'd like to send this info to my Serial Gateway and MyController. The existing setup works as I've tried and tested them on a DHT11 and distance sensor. I am very new to coding however and am not sure how to go about adding the code necessary to work the NRF24 on my raingauge sketch. Any help appreciated

    This is the sketch I'm using....

    #include "RTClib.h"
    #include <Wire.h>
    #define RainPin 2                         // The Rain input is connected to digital pin 2 on the arduino
    bool bucketPositionA = false;             // one of the two positions of tipping-bucket               
    const double bucketAmount = 0.01610595;   // inches equivalent of ml to trip tipping-bucket
    double dailyRain = 0.0;                   // rain accumulated for the day
    double hourlyRain = 0.0;                  // rain accumulated for one hour
    double dailyRain_till_LastHour = 0.0;     // rain accumulated for the day till the last hour          
    bool first;                               // as we want readings of the (MHz) loops only at the 0th moment 
    RTC_Millis rtc;                           // software RTC time
    void setup(void) {
      Serial.begin(9600);                            // start the serial port
      rtc.begin(DateTime(__DATE__, __TIME__));       // start the RTC
      pinMode(RainPin, INPUT);                       // set the Rain Pin as input.
      delay(4000);                                   // i'm slow in starting the seiral monitor (not necessary)
      Serial.println("Ready!!!");                    // not necessary too
    void loop(void){
      DateTime now =;
      // ++++++++++++++++++++++++ Count the bucket tips ++++++++++++++++++++++++++++++++
      if ((bucketPositionA==false)&&(digitalRead(RainPin)==HIGH)){
        dailyRain+=bucketAmount;                               // update the daily rain
      if ((bucketPositionA==true)&&(digitalRead(RainPin)==LOW)){
      // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
      if(now.minute() != 0) first = true;                     // after the first minute is over, be ready for next read
      if(now.minute() == 0 && first == true){
        hourlyRain = dailyRain - dailyRain_till_LastHour;      // calculate the last hour's rain
        dailyRain_till_LastHour = dailyRain;                   // update the rain till last hour for next calculation
        // facny display for humans to comprehend
        Serial.print(":  Total Rain for the day = ");
        Serial.print(dailyRain,8);                            // the '8' ensures the required accuracy
        Serial.println(" inches");
        Serial.print("     :  Rain in last hour = ");
        Serial.println(" inches");
        first = false;                                        // execute calculations only once per hour
      if(now.hour()== 0) {
        dailyRain = 0.0;                                      // clear daily-rain at midnight
        dailyRain_till_LastHour = 0.0;                        // we do not want negative rain at 01:00
    }                                                        // end of loop```

  • Mod

    Welcome to the MySensors community @Huw-Thomas

    Instructions for how to connect the nrf radio are available at

    Code for reporting rain is available at

    Information on how the different parts of MySensors fit together is available at - understanding this will likely save you a lot of time when building MySensors nodes.

  • @mfalkvidd Thanks, but I've gone through that and have managed to get sensors working (using existing sketches that have the NRF24 code built in) but as I'm a complete beginner here in terms of coding, I just don't know what to add to my sketch and where etc.

  • This might not be a perfect answer, but:

    You may first try to analyse the Power Meter Pulse Sensor. It's quite easy to understand, what it does - despite it uses an interrupt service routine you may not need. This may be helpful to understand how communication between node and controller is organized and timing of data sending for non-sleeping modes can be achieved.

    You may then just put your code into this basis - just add your parts wrt. measuring and delete the not needed parts (or make them comment first).

    Some further remarks:
    There is the rather complex Rain Gauge sketch with a lot of options that may be confusing at first sight. This may already be prepared to do most things you may want to do.

    In Most cases, it's easier to do the statistics part on controller side and just let the nodes report small "snapshots". So resetting the counter and use an RTC is not necessary...

    I don't have the link at the moment, but there also exists a small example, how to combine sonsors. I also found this helpful to understand how to convert standard arduino code with MySensors functionality.

Log in to reply

Suggested Topics

  • 4
  • 124
  • 19
  • 1
  • 5
  • 8
  • 22
  • 12