Skip to content

Instantly share code, notes, and snippets.

@Levi-Lesches
Last active February 20, 2020 05:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Levi-Lesches/28f0670f0fd140e5c584f2c614e077f8 to your computer and use it in GitHub Desktop.
Save Levi-Lesches/28f0670f0fd140e5c584f2c614e077f8 to your computer and use it in GitHub Desktop.
Arduino NRF radio controls
/*
This script is for one NRF to send data to the other. The NRF sending the data
called the server, and the device receiving the data is called the client.
The setup for both devices is the same, the looping is what's different.
Here is a pinout for both devices:
* CE --> 9
* CSN --> 10
* MOSI --> 11
* MISO --> 12
* SCK --> 13
*/
// Here is the code for the client:
#include <SPI.h>
#include <RH_NRF24.h>
RH_NRF24 nrf24(9, 10); // CE, CSN
void setup() {
// same as the server setup.
Serial.begin(115200);
// These functions all return either true or false depending on whether
// they worked or not. We can use those values in an if statement.
// Initialize the device
if (nrf24.init()) {
Serial.println("init succesfull");
} else {
Serial.println("init failed");
}
// Set the radio channel.
//
// Only devices on the same channel can communicate. Defaults to channel 2.
if (nrf24.setChannel(1)) {
Serial.println("channel set succesfully");
} else {
Serial.println("setChannel failed");
}
// Set the frequency.
//
// Defaults to 2.402 GHz (channel 2), 2Mbps, 0dBm.
if (nrf24.setRF(RH_NRF24::DataRate2Mbps, RH_NRF24::TransmitPower0dBm)) {
Serial.println("setRF succesfull");
} else {
Serial.println("setRF failed");
}
}
void loop() {
/*
This is a three step process:
1. Allot two variables:
a. A list of bytes to hold the message. This list is 28 spaces long
since that is the longest message we can hold. Keep in mind this
list will always be 28 spaces long, just might not have all those
spaces filled.
b. A length variable to keep track of how long the message actually
is. In other words, how many spaces in `data` are filled.
2. Check if there is data available. Instead of returning its results,
the nrf24.recv function takes two arguments:
a. A list of bytes. This is the same list as 1a, and will be filled
with the message received.
b. A *pointer* to a variable representing the length of the message.
This is the same as 1b, but instead of just the *value* of the
variable (since there is none), we let the function change it.
This is like "returning" a value.
3. Print the message -- convert data (1a) into letters and print.
*/
byte data [28] ; // 28 is max number of characters
byte length;
if (nrf24.available()) { // returns true of there is any more data to receive
if (nrf24.recv(data, &length)) { // true if there is a message
Serial.println ( (char*) data) ; // cast it to an array of char
Serial.print ("Length of the message: ") ;
Serial.println (length) ;
}
}
}
// --------------END OF CLIENT CODE-------------
// Here is the code for the server:
#include <SPI.h>
#include <RH_NRF24.h>
RH_NRF24 nrf24(9, 10); // CE, CSN
String readLine() {
/*
This function waits until the user types something and presses "enter", and
returns whatever was typed.
Note that no code can run while this function is waiting.
TODO: replace with Serial.readline()
*/
String result;
while (true) {
if (Serial.available() > 0) { // if there is a letter to collect
char letter = Serial.read(); // read that letter (just one)
if (letter == '\n') {
return result + '\0'; // end with a "null terminator"
} else if (letter != '\r') { // we can ignore that ("carriage return")
result += letter;
}
}
}
}
void setup() {
// same as the client setup.
Serial.begin(115200);
// These functions all return either true or false depending on whether
// they worked or not. We can use those values in an if statement.
// Initialize the device
if (nrf24.init()) {
Serial.println("init succesfull");
} else {
Serial.println("init failed");
}
// Set the radio channel.
//
// Only devices on the same channel can communicate. Defaults to channel 2.
if (nrf24.setChannel(1)) {
Serial.println("channel set succesfully");
} else {
Serial.println("setChannel failed");
}
// Set the frequency.
//
// Defaults to 2.402 GHz (channel 2), 2Mbps, 0dBm.
if (nrf24.setRF(RH_NRF24::DataRate2Mbps, RH_NRF24::TransmitPower0dBm)) {
Serial.println("setRF succesfull");
} else {
Serial.println("setRF failed");
}
}
void loop() {
/*
1. Reads user input from the Serial Monitor.
2. Fits the message into a list of bytes.
3. Sends the byte array over the radio.
*/
Serial.println("Type in a message and press Enter");
// Step 1
String message = readLine(); // Read the user input
int length = message.length() + 1; // Get its length (+ null terminator)
// Step 2
char data [length]; // Create the byte array
message.toCharArray(data, length); // Copies message into the array
// Step 3
nrf24.send(data, sizeof(data));
nrf24.waitPacketSent(); // wait for the message to actually send
// Let the user know the message sent.
Serial.print("Sent message: ");
Serial.println(message);
Serial.println("");
delay (2000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment