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. General Discussion
  3. Adding NRF24 code to existing sensor sketch

Adding NRF24 code to existing sensor sketch

Scheduled Pinned Locked Moved General Discussion
4 Posts 3 Posters 1.1k Views 2 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.
  • Huw ThomasH Offline
    Huw ThomasH Offline
    Huw Thomas
    wrote on last edited by Huw Thomas
    #1

    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 = rtc.now();
        
      // ++++++++++++++++++++++++ Count the bucket tips ++++++++++++++++++++++++++++++++
      if ((bucketPositionA==false)&&(digitalRead(RainPin)==HIGH)){
        bucketPositionA=true;
        dailyRain+=bucketAmount;                               // update the daily rain
      }
      
      if ((bucketPositionA==true)&&(digitalRead(RainPin)==LOW)){
        bucketPositionA=false;  
      } 
      // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
      
      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.println();
        Serial.print(now.hour());
        Serial.print(":");
        Serial.print(now.minute());
        Serial.print(":  Total Rain for the day = ");
        Serial.print(dailyRain,8);                            // the '8' ensures the required accuracy
        Serial.println(" inches");
        Serial.println();
        Serial.print("     :  Rain in last hour = ");
        Serial.print(hourlyRain,8);
        Serial.println(" inches");
        Serial.println();
        
        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```
    mfalkviddM 1 Reply Last reply
    0
    • Huw ThomasH Huw Thomas

      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 = rtc.now();
          
        // ++++++++++++++++++++++++ Count the bucket tips ++++++++++++++++++++++++++++++++
        if ((bucketPositionA==false)&&(digitalRead(RainPin)==HIGH)){
          bucketPositionA=true;
          dailyRain+=bucketAmount;                               // update the daily rain
        }
        
        if ((bucketPositionA==true)&&(digitalRead(RainPin)==LOW)){
          bucketPositionA=false;  
        } 
        // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        
        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.println();
          Serial.print(now.hour());
          Serial.print(":");
          Serial.print(now.minute());
          Serial.print(":  Total Rain for the day = ");
          Serial.print(dailyRain,8);                            // the '8' ensures the required accuracy
          Serial.println(" inches");
          Serial.println();
          Serial.print("     :  Rain in last hour = ");
          Serial.print(hourlyRain,8);
          Serial.println(" inches");
          Serial.println();
          
          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```
      mfalkviddM Offline
      mfalkviddM Offline
      mfalkvidd
      Mod
      wrote on last edited by
      #2

      Welcome to the MySensors community @Huw-Thomas

      Instructions for how to connect the nrf radio are available at https://www.mysensors.org/build/connect_radio

      Code for reporting rain is available at https://www.mysensors.org/build/rain

      Information on how the different parts of MySensors fit together is available at https://www.mysensors.org/about - understanding this will likely save you a lot of time when building MySensors nodes.

      Huw ThomasH 1 Reply Last reply
      0
      • mfalkviddM mfalkvidd

        Welcome to the MySensors community @Huw-Thomas

        Instructions for how to connect the nrf radio are available at https://www.mysensors.org/build/connect_radio

        Code for reporting rain is available at https://www.mysensors.org/build/rain

        Information on how the different parts of MySensors fit together is available at https://www.mysensors.org/about - understanding this will likely save you a lot of time when building MySensors nodes.

        Huw ThomasH Offline
        Huw ThomasH Offline
        Huw Thomas
        wrote on last edited by
        #3

        @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.

        1 Reply Last reply
        0
        • rejoe2R Offline
          rejoe2R Offline
          rejoe2
          wrote on last edited by
          #4

          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.

          Controller: FHEM; MySensors: 2.3.1, RS485,nRF24,RFM69, serial Gateways

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


          12

          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