i am trying to send and receive data using arduino uno and nrf24l01 with an antenna attached to it so i tried out 4 to 5 online codes for it but none of worked then i downloaded rf24_g library and used its sample code for transmitter and receiver so i was transmitting it but at the receiving end i could not get the data which transmitted both codes are given below please can you help me figure out how move forward with the idea
this is the transmitting code
/* This sketch sends a packet with random data to another radio and waits for
- the packet to be sent back. It prints out the random data and the received data, which should be the same.
*/
#include <rf24g.h>
// We must instantiate the RF24_G object outside of the setup function so it is available in the loop function
RF24_G test;
void setup() {
Serial.begin(9600);
// create the RF24G object with an address of 4, using pins 7 and 8
test = RF24_G(1, 7, 8);
}
void loop() {
// create a random number
uint8_t randNumber = 100;
// create a variable to store the received number
int actual;
// declare the sender packet variable
packet sender;
// declare the receiver packet variable
packet receiver;
// set the destination of the packet to address 1
sender.setAddress(1);
// write the payload to the packet
sender.addPayload(&randNumber, sizeof(int));
// print out the original payload
Serial.print("original number:");
Serial.println(randNumber);
// send the packet, if it is successful try to read back the packet
if (test.write(&sender) == true) {
// wait until a packet is received
while (test.available() != true);
// copy the packet into the receiver object
test.read(&receiver);
// copy the payload into the actual value
receiver.readPayload(&actual, sizeof(int));
// print out the actual value received
Serial.print("received number:");
Serial.println(actual);
}
}
this is the receiving code
/* This is a small sketch that listens
- for packets and forwards them back to the sender.
*/
#include <rf24g.h>
// we must instantiate the RF24_G object outside of the setup function so it is available in the loop function
RF24_G test;
int i = 0;
void setup() {
Serial.begin(9600);
// create the RF24G object with an address of 1, using pins 7 and 8
test = RF24_G(11, 7, 8);
// print out the details of the radio's configuration (useful for debug)
}
void loop() {
// declare packet variable
packet receiver;
// declare string to place the packet payload in
char payload[30];
// check if the radio has any packets in the receive queue
if (test.available() == true) {
Serial.println("packet received!");
// read the data into the packet
test.read(&receiver);
// print the packet number of the received packet
// if these are not consecutive packets are being lost due to timeouts.
Serial.print("count: ");
Serial.println(receiver.getCnt());
// print the source address of the received packet
Serial.print("address: ");
Serial.println(receiver.getAddress());
// load the payload into the payload string
receiver.readPayload(payload, 30);
// print the payload
Serial.print("payload: ");
Serial.println(payload);
// since the address in the packet object is already
// set to the address of the receiver, it doesn't need to be changed
// hence, we can write the packet back to the receiver
// we may check to see if the transmission failed, if so we just drop the packet
if (test.write(&receiver) == false) {
Serial.println("transmit back failed!");
Serial.println("dropping packet...");
}
}
}
