RF24 icon indicating copy to clipboard operation
RF24 copied to clipboard

Text file transfer using nRF24L01

Open alkuentrus opened this issue 6 years ago • 2 comments

Sorry to bother you again... I read in a post at the arduino forum that there is an example in this repository that allows sending a text file from a sd card using arduino., but I cannot find it. I found one you made for RPi, but didn't manage to find for arduino. Can you point me in the righ direction please?

alkuentrus avatar Jan 18 '20 22:01 alkuentrus

I made a very simple version for arduino due Emitter:

 #include <SPI.h>
#include <SD.h>
#include "RF24.h"
bool radioNumber = 0;
RF24 radio(7, 8);
byte buf[32] = {0};
File myFile;
byte addresses[][6] = {"1Node", "2Node"};
bool role = 0;
void setup() {
  pinMode(53, OUTPUT);
  digitalWrite(53, LOW);
  Serial.begin (115200) ;
  if (!SD.begin(4))
  {
    Serial.println("initialization of SD failed!");
    return;
  }
  Serial.println("initialization done.");
  digitalWrite(53, HIGH);

  Serial.println(F("E M I S S O R"));
  radio.begin();
  radio.setPALevel(RF24_PA_MAX);
  radio.setPayloadSize(32);
  radio.setDataRate(RF24_250KBPS);
  //  radio.setDataRate(RF24_2MBPS);
  radio.setChannel(0x60);
  radio.printDetails();
  radio.setAutoAck(1);                     // Ensure autoACK is enabled
  radio.setRetries(2, 15);
  if (radioNumber) {
    radio.openWritingPipe(addresses[1]);
    radio.openReadingPipe(1, addresses[0]);
  }
  else
  { radio.openWritingPipe(addresses[0]);
    radio.openReadingPipe(1, addresses[1]);
  }
  sendfile();
}
void readfile() {
  int i = 0;
  while (myFile.available())
  {
    buf[i] = myFile.read();

    i++;
    if (i == 32)
    {
      Serial.println(F(buf));
      radio.write(buf, sizeof(buf));
      delay(10);
      i = 0;
    }
  }
  if (i > 0) {
    radio.flush_tx();
    radio.write(buf, i);
    Serial.write(buf, i); //Output results at serial port
    delay(1);
    i = 0;
    // Serial.println(F(buf));
    Serial.println("");
    Serial.println("Last stream of less than 32 bytes");
    digitalWrite(53, LOW);
  }
}
void sendfile() {
  myFile = SD.open("1.txt");
  if (myFile)
  {
    readfile ();// Função que vai fazer o envio
    myFile.close();
    radio.powerDown(); //desliga o rádio
  }
  else
  {
    Serial.println("error opening file"); // se não abre file, erro
  }
}
void loop() {
  // nada a fazer aqui
}

alkuentrus avatar Jan 22 '20 17:01 alkuentrus

Receiver


#include "RF24.h"


#include <SPI.h>
#include <SD.h>

const int chipSelect = 4;
const int fileclose = 6;
File myFile;
unsigned long tempo;
unsigned long tempo2;
int flag = 0;
bool radioNumber = 1;
RF24 radio(7, 8);

byte buf[32] = {0};
byte addresses[][6] = {"1Node", "2Node"};
bool role = 0;



void setup()
{ pinMode(53, OUTPUT);
  digitalWrite(53, LOW);
  pinMode(2, OUTPUT);
  pinMode(fileclose, INPUT_PULLUP);

  Serial.begin (115200) ;

  if (!SD.begin(4))
  {
    Serial.println("initialization of SD failed!");
    return;
  }
  Serial.println("initialization done.");
  // digitalWrite(53, HIGH);

  Serial.println(F("R E C E T O R"));
  radio.begin();
  radio.setPALevel(RF24_PA_MAX);
  radio.setPayloadSize(32);
  //  radio.setDataRate(RF24_2MBPS);
  radio.setChannel(0x60);
  radio.setDataRate(RF24_250KBPS);
  radio.setAutoAck(1);                     // Ensure autoACK is enabled
  radio.setRetries(1, 15);
  digitalWrite(13, LOW);
  radio.printDetails();
  radio.startListening();
  if (radioNumber)
  {
    radio.openWritingPipe(addresses[1]);
    radio.openReadingPipe(1, addresses[0]);
  }
  else
  {
    radio.openWritingPipe(addresses[0]);
    radio.openReadingPipe(1, addresses[1]);
  }
}

void openfile()
{
  myFile = SD.open("11.txt", FILE_WRITE);
  flag = 1;
}

void loop()

{ 
  if (radio.available()) {
  if (flag == 0) {
    openfile();
    }
    
    radio.read(buf, sizeof(buf));
    radio.flush_rx();
    Serial.println(F(buf));
    myFile.write(buf, sizeof(buf));
    tempo = millis();

  }
  tempo2 = millis();
  if(tempo2-tempo > 5000 && flag == 1){
    digitalWrite(2, HIGH);
    Serial.println(F("File closed"));
    myFile.close();
    flag = 0;
  }
}

alkuentrus avatar Jan 22 '20 17:01 alkuentrus