0% found this document useful (0 votes)
133 views

Chat Code NRF

This document describes an nRF serial chat program that allows simple wireless communication between two devices. It reads serial input, chops it into payloads of up to 32 bytes, and sends it over nRF24L01 radios. Received payloads are displayed on the serial monitor and an LCD. The program handles buffer overflow cases when input is longer than the payload size.

Uploaded by

Hung Pham
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
133 views

Chat Code NRF

This document describes an nRF serial chat program that allows simple wireless communication between two devices. It reads serial input, chops it into payloads of up to 32 bytes, and sends it over nRF24L01 radios. Received payloads are displayed on the serial monitor and an LCD. The program handles buffer overflow cases when input is longer than the payload size.

Uploaded by

Hung Pham
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

/*

nRF Serial Chat


Date : 22 Aug 2013
Author : Stanley Seow
e-mail : [email protected]
Version : 0.90
Desc :
I worte this simple interactive serial chat over nRF that can be used for both s
ender
and receiver as I swapped the TX & RX addr during read/write operation.
It read input from Serial Monitor and display the output to the other side
Serial Monitor or 16x2 LCD (if available)... like a simple chat program.
Max payload is 32 bytes for radio but the serialEvent will chopped the entire bu
ffer
for next payload to be sent out sequentially.
*/
#include
#include
#include
#include
#include

<LiquidCrystal.h>
<SPI.h>
"nRF24L01.h"
"RF24.h"
"printf.h"

LiquidCrystal lcd(10, 7, 3, 4, 5, 6);


RF24 radio(8,9);
const uint64_t pipes[2] = { 0xDEDEDEDEE7LL, 0xDEDEDEDEE9LL };
boolean stringComplete = false; // whether the string is complete
static int dataBufferIndex = 0;
boolean stringOverflow = false;
char charOverflow = 0;
char SendPayload[31] = "";
char RecvPayload[31] = "";
char serialBuffer[31] = "";
void setup(void) {
Serial.begin(57600);
lcd.begin(16,2);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("RF Chat V0.90");
printf_begin();
radio.begin();
radio.setDataRate(RF24_250KBPS);
radio.setPALevel(RF24_PA_MAX);
radio.setChannel(70);
radio.enableDynamicPayloads();
radio.setRetries(15,15);
radio.setCRCLength(RF24_CRC_16);

radio.openWritingPipe(pipes[0]);
radio.openReadingPipe(1,pipes[1]);
radio.startListening();
radio.printDetails();
Serial.println();
Serial.println("RF Chat V0.90");
delay(500);
lcd.clear();
}
void loop(void) {
nRF_receive();
serial_receive();
} // end loop()
void serialEvent() {
while (Serial.available() > 0 ) {
char incomingByte = Serial.read();
if (stringOverflow) {
serialBuffer[dataBufferIndex++] = charOverflow; // Place saved overflo
w byte into buffer
serialBuffer[dataBufferIndex++] = incomingByte; // saved next byte int
o next buffer
stringOverflow = false;
// turn overflow flag
off
} else if (dataBufferIndex > 31) {
stringComplete = true;
// Send this buffer out to radio
stringOverflow = true;
// trigger the overflow flag
charOverflow = incomingByte; // Saved the overflow byte for next loop
dataBufferIndex = 0;
// reset the bufferindex
break;
}
else if(incomingByte=='\n'){
serialBuffer[dataBufferIndex] = 0;
stringComplete = true;
} else {
serialBuffer[dataBufferIndex++] = incomingByte;
serialBuffer[dataBufferIndex] = 0;
}
} // end while()
} // end serialEvent()
void nRF_receive(void) {
int len = 0;
if ( radio.available() ) {
bool done = false;
while ( !done ) {
len = radio.getDynamicPayloadSize();
done = radio.read(&RecvPayload,len);
delay(5);
}
RecvPayload[len] = 0; // null terminate string

lcd.setCursor(0,0);
lcd.print("R:");
Serial.print("R:");
lcd.setCursor(2,0);
lcd.print(RecvPayload);
Serial.print(RecvPayload);
Serial.println();
RecvPayload[0] = 0; // Clear the buffers
}
} // end nRF_receive()
void serial_receive(void){
if (stringComplete) {
strcat(SendPayload,serialBuffer);
// swap TX & Rx addr for writing
radio.openWritingPipe(pipes[1]);
radio.openReadingPipe(0,pipes[0]);
radio.stopListening();
bool ok = radio.write(&SendPayload,strlen(SendPayload));
lcd.setCursor(0,1);
lcd.print("S:");
Serial.print("S:");
lcd.setCursor(2,1);
lcd.print(SendPayload);
Serial.print(SendPayload);
Serial.println();
stringComplete = false;
// restore TX & Rx addr for reading
radio.openWritingPipe(pipes[0]);
radio.openReadingPipe(1,pipes[1]);
radio.startListening();
SendPayload[0] = 0;
dataBufferIndex = 0;
} // endif
} // end serial_receive()

You might also like