Help modifying sketch for rf-nano



  • I have set all my sensors up using a nano and nrf24l01 wireless modules. Just received some rf-nanos I ordered but they are not working with my current sketches. I could use some advice on what to change in my moisture sensor sketch to get it to send data to my gateway. I verified the new rf-nanos work with the following 2 sketches.

    rf-nano send sketch:

    /*
        
       RF Nano Send Example
       
       Hey guys! This example code is designed to send one random 
       integer from one RF Nano to a second RF Nano, to show just how 
       simple the code can be. This code is also compatible with an RF Nano 
       (Arduino Nano with integrated NRF24L01)
    
       - Matt (ACBR 2020)
       
       Pins for Radio
       1 - GND 
       2 - VCC 5v
       3 - CE - Arduino pin 9
       4 - CSN - Arduino pin 10
       5 - SCK - Arduino pin 13
       6 - MOSI - Arduino pin 11
       7 - MISO - Arduino pin 12
       8 - UNUSED
     */
     
    #include <SPI.h>
    #include <nRF24L01.h>
    #include <RF24.h>
    #define CE_PIN   10
    #define CSN_PIN 9
    
    const uint64_t pipe = 0x1; // This is the transmit pipeline
    int sendData[1];  // One element array holding our random number
    
    RF24 radio(CE_PIN, CSN_PIN); // Activate the Radio
    
    void setup()   
    {
      Serial.begin(9600);
      radio.begin();
      radio.openWritingPipe(pipe);
      randomSeed(analogRead(A0));
    }
    
    
    void loop()   
    {
      radio.write( sendData, sizeof(sendData));
      sendData[0] = random(10);
      Serial.println(sendData[0]);
    }
    

    rf-nano receive sketch:

    /* 
      
       RF Nano Receive Example
     
       Hey guys! This example code is designed to send one random integer 
       from one RF Nano to a second RF Nano, to show just how simple the 
       code can be. This code is also compatible with an RF Nano (Arduino 
       Nano with integrated NRF24L01)
    
       - Matt (ACBR 2020)
       
       1 - GND 
       2 - VCC 3.3V 
       3 - CE - Arduino pin 9
       4 - CSN - Arduino pin 10
       5 - SCK - Arduino pin 13
       6 - MOSI - Arduino pin 11
       7 - MISO - Arduino pin 12
       8 - UNUSED 
    */
    
    #include <SPI.h>
    #include <nRF24L01.h>
    #include <RF24.h>
    #define CE_PIN   10
    #define CSN_PIN 9
    
    const uint64_t pipe = 0x1;  // This is the transmit pipe
    int sendData[1];  // One element array holding our random number
    
    RF24 radio(CE_PIN, CSN_PIN);
    
    void setup()  
    {
      Serial.begin(9600); 
      delay(1000);
      Serial.println("Nrf24L01 Receiver Starting");
      radio.begin();
      radio.openReadingPipe(1,pipe);
      radio.startListening();
    }
    
    
    void loop()   
    {
      if ( radio.available() )
      {
        bool done = false;
        while (!done)
        {
          radio.read(sendData, sizeof(sendData));
          Serial.print("Random Number: ");
          Serial.println(sendData[0]);
          delay(50);
        }
      }
      else
      {    
          Serial.println("Darn, not working yet!");
      }
    }
    

    sensor sketch needing modification:

    /*
      AnalogReadSerial
    
      Reads an analog input on pin 0, prints the result to the Serial Monitor.
      Graphical representation is available using Serial Plotter (Tools > Serial Plotter menu).
      Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.
    
      This example code is in the public domain.
    
      http://www.arduino.cc/en/Tutorial/AnalogReadSerial
    */
    #define MY_NODE_ID 35
    // Enable debug prints
     #define MY_DEBUG
    // Enable and select radio type attached
    #define MY_RADIO_RF24
    //#define MY_RADIO_NRF5_ESB
    //#define MY_RADIO_RFM69
    //#define MY_RADIO_RFM95
    #define CHILD_ID_A0 0
    //#define CHILD_ID_A1 1
    //#define CHILD_ID_A2 2
    #include <MySensors.h>
    #include #include <RF24.h>
    MyMessage msg(CHILD_ID_A0, V_LEVEL);
    //MyMessage msg2(CHILD_ID_A1, V_LEVEL);
    //MyMessage msg3(CHILD_ID_A2, V_LEVEL);
    // the setup routine runs once when you press reset:
    void setup() {
      // initialize serial communication at 9600 bits per second:
      Serial.begin(9600);
    }
    void presentation()
    {
      sendSketchInfo("Analog Soil Moisture Sensorx3", "1c.0");
      present(CHILD_ID_A0, S_MOISTURE);
     // present(CHILD_ID_A1, S_MOISTURE);
     // present(CHILD_ID_A2, S_MOISTURE);
    }
    // the loop routine runs over and over again forever:
    void loop() {
      // read the input on analog pin 0:
     int sensorValue = analogRead(A0);
     //int sensorValueA1 = analogRead(A1);
     //int sensorValueA2 = analogRead(A2);
      // print out the value you read:
      Serial.println(sensorValue);
      // Serial.println(sensorValueA1);
      //Serial.println(sensorValueA2);
      send(msg.set(sensorValue));
      // send(msg2.set(sensorValueA1));
     // send(msg3.set(sensorValueA2));
      delay(10000);        // delay in between reads for stability
    }
    


  • The comments at the top in the first two sketches mention the pin numbers of the CE and CS pins reversed, the code itself is correct.
    The third sketch lacks these definitions, so it won't work.



  • @Michiel-van-der-Wulp

    The sketch looks like this now but does not work with gateway sketch (below). Is that related to code in the mysensors.h library, or do I need to make a change in the serial gateway sketch?

    /*
      AnalogReadSerial
    
      Reads an analog input on pin 0, prints the result to the Serial Monitor.
      Graphical representation is available using Serial Plotter (Tools > Serial Plotter menu).
      Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.
    
      This example code is in the public domain.
    
      http://www.arduino.cc/en/Tutorial/AnalogReadSerial
    */
    #define MY_NODE_ID 35
    // Enable debug prints
     #define MY_DEBUG
    // Enable and select radio type attached
    #define MY_RADIO_RF24
    //#define MY_RADIO_NRF5_ESB
    //#define MY_RADIO_RFM69
    //#define MY_RADIO_RFM95
    #define CE_PIN   10
    #define CSN_PIN 9
    #define CHILD_ID_A0 0
    //#define CHILD_ID_A1 1
    //#define CHILD_ID_A2 2
    #include <MySensors.h>
    MyMessage msg(CHILD_ID_A0, V_LEVEL);
    //MyMessage msg2(CHILD_ID_A1, V_LEVEL);
    //MyMessage msg3(CHILD_ID_A2, V_LEVEL);
    // the setup routine runs once when you press reset:
    void setup() {
      // initialize serial communication at 9600 bits per second:
      Serial.begin(9600);
    }
    void presentation()
    {
      sendSketchInfo("Analog Soil Moisture Sensorx3", "1c.0");
      present(CHILD_ID_A0, S_MOISTURE);
     // present(CHILD_ID_A1, S_MOISTURE);
     // present(CHILD_ID_A2, S_MOISTURE);
    }
    // the loop routine runs over and over again forever:
    void loop() {
      // read the input on analog pin 0:
     int sensorValue = analogRead(A0);
     //int sensorValueA1 = analogRead(A1);
     //int sensorValueA2 = analogRead(A2);
      // print out the value you read:
      Serial.println(sensorValue);
      // Serial.println(sensorValueA1);
      //Serial.println(sensorValueA2);
      send(msg.set(sensorValue));
      // send(msg2.set(sensorValueA1));
     // send(msg3.set(sensorValueA2));
      delay(10000);        // delay in between reads for stability
    }
    

    Gateway Sketch:

    /*
      AnalogReadSerial
    
      Reads an analog input on pin 0, prints the result to the Serial Monitor.
      Graphical representation is available using Serial Plotter (Tools > Serial Plotter menu).
      Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.
    
      This example code is in the public domain.
    
      http://www.arduino.cc/en/Tutorial/AnalogReadSerial
    */
    #define MY_NODE_ID 35
    // Enable debug prints
     #define MY_DEBUG
    // Enable and select radio type attached
    #define MY_RADIO_RF24
    //#define MY_RADIO_NRF5_ESB
    //#define MY_RADIO_RFM69
    //#define MY_RADIO_RFM95
    #define CE_PIN   10
    #define CSN_PIN 9
    #define CHILD_ID_A0 0
    //#define CHILD_ID_A1 1
    //#define CHILD_ID_A2 2
    #include <MySensors.h>
    MyMessage msg(CHILD_ID_A0, V_LEVEL);
    //MyMessage msg2(CHILD_ID_A1, V_LEVEL);
    //MyMessage msg3(CHILD_ID_A2, V_LEVEL);
    // the setup routine runs once when you press reset:
    void setup() {
      // initialize serial communication at 9600 bits per second:
      Serial.begin(9600);
    }
    void presentation()
    {
      sendSketchInfo("Analog Soil Moisture Sensorx3", "1c.0");
      present(CHILD_ID_A0, S_MOISTURE);
     // present(CHILD_ID_A1, S_MOISTURE);
     // present(CHILD_ID_A2, S_MOISTURE);
    }
    // the loop routine runs over and over again forever:
    void loop() {
      // read the input on analog pin 0:
     int sensorValue = analogRead(A0);
     //int sensorValueA1 = analogRead(A1);
     //int sensorValueA2 = analogRead(A2);
      // print out the value you read:
      Serial.println(sensorValue);
      // Serial.println(sensorValueA1);
      //Serial.println(sensorValueA2);
      send(msg.set(sensorValue));
      // send(msg2.set(sensorValueA1));
     // send(msg3.set(sensorValueA2));
      delay(10000);        // delay in between reads for stability
    }
    


  • @mrhutchinsonmn

    In my sketches I had to define the below for the built in radio to even work:
    #define MY_RF24_CE_PIN 10
    #define MY_RF24_CS_PIN 9

    Also, I have rf-nano's from 2 sources, the one I got from the first source (don't recall where it was from) doesn't send anything at all, according to the RasPi gateway*), the other 4 (from the 2nd source) just worked™

    *) the radio initializes just fine, the gateway doesn't see any messages from it at all!...



  • That worked for me. Thank you!


Log in to reply
 

Suggested Topics

  • 4
  • 2
  • 4
  • 2
  • 9
  • 8

0
Online

11.4k
Users

11.1k
Topics

112.7k
Posts