0

Okay so this is the code for my receiver.

This is quite confusing so let's dub Receiver = DEVICE 1 and Transmitter = DEVICE 2

DEVICE 1: NRF24L01+, HC-05 (Bluetooth)

DEVICE 2: NRF24L01+

So DEVICE 1 will receive data from DEVICE 2. Also When it receives data from my phone that is connected to the HC-05 module, it will radio.StopListening and radio.write.

But for some reason, after i do radio.StopListening, radio.write doesnt work.

Currently, DEVICE 2 has a very simple code to receive data being sent from DEVICE 1. I've done this just to check if there's any data being sent when DEVICE 1's HC-05 module receives data.

Code:

#include "Arduino.h"
#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>

RF24 radio(9, 8);

byte addresses[][6] = {"1Node","2Node"};

void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.setPALevel(RF24_PA_MIN);
  radio.setDataRate(RF24_2MBPS);
  radio.setChannel(124);

  // Open a writing and reading pipe on each radio, with opposite addresses
  radio.openWritingPipe(addresses[0]);
  radio.openReadingPipe(1, addresses[1]);
}

void loop() {
  unsigned char rf_data; //Make a variable to store the data
  unsigned char bt_data;
  unsigned char tosend;
  radio.startListening();

  if (radio.available()){
    while (radio.available()){
      radio.read(&rf_data, sizeof(rf_data));
      Serial.println(rf_data);
      }
    }

  if (Serial.available()>0){
    while (Serial.available()>0){
      bt_data = Serial.read();
      radio.stopListening();
      Serial.println(bt_data); //----------- I'M ABLE TO SERIAL PRINTLN ----
      radio.write(&bt_data, sizeof(bt_data)); //-------- BUT I'M UNABLE TO PERFORM THIS CODE HERE FOR SOME REASON
      }
    }

}

I've added comments to show which part works and which part doesn't

I hope this question isn't too hard to understand.

  • naming the Receiver DEVICE 1 is less confusing? – jsotola Jul 25 '18 at 17:02
  • 1
    @jsotola cant u be a bit more helpful – Denise Jul 26 '18 at 10:32
0

There could be few reasons but, firstly I would like to ask you to check if your write command is working at all. Try to disable reading mode and leave just write mode:

#include "Arduino.h"
#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>

RF24 radio(9, 8);

byte addresses[][6] = {"1Node","2Node"};

void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.setPALevel(RF24_PA_MIN);
  radio.setDataRate(RF24_2MBPS);
  radio.setChannel(124);

  // Open a writing and reading pipe on each radio, with opposite addresses
  radio.openWritingPipe(addresses[0]);
  //radio.openReadingPipe(1, addresses[1]);
}

void loop() {
  unsigned char rf_data; //Make a variable to store the data
  unsigned char bt_data;
  unsigned char tosend;
  //radio.startListening();

 // if (radio.available()){
 //   while (radio.available()){
 //     radio.read(&rf_data, sizeof(rf_data));
 //     Serial.println(rf_data);
 //    }
 //   }

  if (Serial.available()>0){
    while (Serial.available()>0){
      bt_data = Serial.read();
      //radio.stopListening();
      Serial.println(bt_data); //----------- I'M ABLE TO SERIAL PRINTLN ----
      radio.write(&bt_data, sizeof(bt_data)); //-------- BUT I'M UNABLE TO PERFORM THIS CODE HERE FOR SOME REASON
      }
    }

}

Try above code and if it will not work change RF24_2MBPS to RF24_1MBPS and RF24_PA_MIN to RF24_PA_LOW on both modules (transmitter and receiver)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.