Hello, everyone! I'm using the NRF24L01 wireless transceiver to send integers from one Arduino Uno board to another Uno board.
The way my code works is that I enter a number on the serial monitor using Serial.parseInt() and it enter to transmit it to the receiver. The number is received by the receiver but the transmitting end is also sending a zero '0' automatically after the user sends a number.
The following is a snippet of the transmitter Serial monitor:
The following is a snippet of the receiver serial monitor
I only entered the number 45. However, zero was also sent. I am familiar with C++ and I think that the null character '\0' could be causing the issue but again, integers do not end with a null character - only strings do.
Here is the transmitter code:
#include "Arduino.h"
#include <SPI.h>
#include <RF24.h>
// This is just the way the RF24 library works:
// Hardware configuration: Set up nRF24L01 radio on SPI bus (pins 10, 11, 12, 13) plus pins 7 & 8
RF24 radio(7, 8);
//char data; //could be problematic
byte addresses[][6] = {"1Node", "2Node"};
// -----------------------------------------------------------------------------
// SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP
// -----------------------------------------------------------------------------
void setup() {
Serial.begin(9600);
Serial.println("THIS IS THE TRANSMITTER CODE - YOU NEED THE OTHER ARDIUNO TO SEND BACK A RESPONSE");
// Initiate the radio object
radio.begin();
// Set the transmit power to lowest available to prevent power supply related issues
radio.setPALevel(RF24_PA_MIN);
// Set the speed of the transmission to the quickest available
radio.setDataRate(RF24_2MBPS);
// Use a channel unlikely to be used by Wifi, Microwave ovens etc
radio.setChannel(124);
// Open a writing and reading pipe on each radio, with opposite addresses
radio.openWritingPipe(addresses[1]);
radio.openReadingPipe(1, addresses[0]);
// Random number seeding (we're going to be sending a single random number)
// randomSeed(analogRead(A0));
}
// -----------------------------------------------------------------------------
// LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP
// -----------------------------------------------------------------------------
void loop() {
long int data;
// Generate a single random character to transmit
Serial.println("Enter your number");
while (Serial.available() == 0) {
}
data = Serial.parseInt();
// Ensure we have stopped listening (even if we're not) or we won't be able to transmit
radio.stopListening();
// Did we manage to SUCCESSFULLY transmit that (by getting an acknowledgement back from the other Arduino)?
// Even we didn't we'll continue with the sketch, you never know, the radio fairies may help us
if (!radio.write( &data, sizeof(long int) )and data!='\0') {
Serial.println("No acknowledgement of transmission - receiving radio device connected?");
}
delay(50);
// Now listen for a response
radio.startListening();
// But we won't listen for long, 200 milliseconds is enough
unsigned long started_waiting_at = millis();
// Loop here until we get indication that some data is ready for us to read (or we time out)
while ( ! radio.available() ) {
// Oh dear, no response received within our timescale
if (millis() - started_waiting_at > 200 ) {
Serial.println("No response received - timeout!");
return;
}
}
// Now read the data that is waiting for us in the nRF24L01's buffer
long int dataRx;
// delay(200);
radio.read( &dataRx, sizeof(long int) );
// Show user what we sent and what we got back
Serial.print("Sent: ");
Serial.print(data);
Serial.print(", received: ");
Serial.println(dataRx);
// Try again 1s later
delay(200);
}
This is the receiver code:
#include "Arduino.h"
#include <SPI.h>
#include <RF24.h>
// This is just the way the RF24 library works:
// Hardware configuration: Set up nRF24L01 radio on SPI bus (pins 10, 11, 12, 13) plus pins 7 & 8
RF24 radio(7, 8);
byte addresses[][6] = {"1Node","2Node"};
// -----------------------------------------------------------------------------
// SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP
// -----------------------------------------------------------------------------
void setup() {
Serial.begin(9600);
Serial.println("THIS IS THE RECEIVER CODE - YOU NEED THE OTHER ARDUINO TO TRANSMIT");
// Initiate the radio object
radio.begin();
// Set the transmit power to lowest available to prevent power supply related issues
radio.setPALevel(RF24_PA_MIN);
// Set the speed of the transmission to the quickest available
radio.setDataRate(RF24_2MBPS);
// Use a channel unlikely to be used by Wifi, Microwave ovens etc
radio.setChannel(124);
// Open a writing and reading pipe on each radio, with opposite addresses
radio.openWritingPipe(addresses[0]);
radio.openReadingPipe(1, addresses[1]);
// Start the radio listening for data
radio.startListening();
}
// -----------------------------------------------------------------------------
// We are LISTENING on this device only (although we do transmit a response)
// -----------------------------------------------------------------------------
// ...
void loop() {
// This is what we receive from the other device (the transmitter)
long int data;
// Is there any data for us to get?
if (radio.available()) {
// Go and read the data and put it into that variable
radio.read(&data, sizeof(long int));
// No more data to get, so send it back but add 1 first just for kicks
// First, stop listening so we can talk
radio.stopListening();
// data = data;
delay(100);
radio.write(&data, sizeof(long int));
// Now, resume listening so we catch the next packets.
radio.startListening();
// Tell the user what we sent back (the received number + 1)
Serial.print("Sent response: ");
Serial.println(data);
}
}
// ...
What am I doing wrong here? Any help would be greatly appreciated.