Hi, I'm having some weird problems with my RF communication and could do with some advice. At this point in the project I want to use 1 microcontroller to request data from another and have this second circuit transmit data back that has been read from an SD card. The user runs a Python script which interfaces with the microcontroller that is requesting data. The microcontroller that is requesting data uses the following hardware:
Orangepip Kona328
Crazepony-UK NRF24L01+ RF Modules with 3.3V Adapter
Has the following connections:
CE - 9
CSN - 10
MOSI - 11
MISO - 12
SCK - 13
Vcc - 5V
GND - GND
And uses this code:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(9, 10); // CE, CSN
const uint64_t pipes[2] = {0xF0F0F0F0E1LL,0xF0F0F0F0D2LL};
int instruction = 0;
const char dev1_send_instr[] = "s1";
void setup() {
Serial.begin(250000);
radio.begin();
radio.openWritingPipe(pipes[1]);
radio.openReadingPipe(1,pipes[0]);
radio.setPALevel(RF24_PA_MIN);
do {
if (Serial.available() > 0) {
instruction = Serial.read();
}
delay(2000);
} while (instruction != 49);
radio.stopListening();
int counter = 0;
if (instruction == 49) {
do {
radio.write(&dev1_send_instr, sizeof(dev1_send_instr));
counter = counter + 1;
Serial.println(counter);
delay(1000);
}while (counter < 10);
}
delay(1000);
radio.startListening();
receive_data();
}
void receive_data() {
while(1) {
if (radio.available()) {
char text[16] = "";
radio.read(&text, sizeof(text));
digitalWrite(LED_BUILTIN, HIGH);
Serial.println(text);
delay(1000);
}
else {
Serial.println("0,0,0");
delay(1000);
}
}
}
void loop() {}
The circuit that is sending the data back consists of this hardware:
Orangepip Kona328
LIS3DH Accelerometer
Crazepony-UK NRF24L01+ RF Modules with 3.3V Adapter
kwmobile SD Card Reader Module
This circuit has the following connections:
For the LIS3DH:
SDA - A4
SCL - A5
Vcc - 5V
GND - GND
For the NRF24L01:
CE - 9
CSN - 10
MOSI - 11
MISO - 12
SCK - 13
Vcc - 5V
GND - GND
For the SD Card Module:
+5 - 5V
GND - GND
CS - 4
MOSI - 11
MISO - 12
SCK - 13
And uses this code:
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Adafruit_LIS3DH.h>
#include <Adafruit_Sensor.h>
RF24 radio(9, 10); // CE, CSN
Adafruit_LIS3DH lis = Adafruit_LIS3DH();
#define SD_CS 4
#define TX_CS 10
const uint64_t pipes[2] = {0xF0F0F0F0E1LL,0xF0F0F0F0D2LL};
char instruction[16] = "";
void setup() {
Serial.begin(250000);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(SD_CS, OUTPUT);
pinMode(TX_CS, OUTPUT);
digitalWrite(SD_CS, HIGH);
digitalWrite(TX_CS, LOW);
radio.begin();
radio.openWritingPipe(pipes[0]);
radio.openReadingPipe(1,pipes[1]);
radio.setPALevel(RF24_PA_MIN);
digitalWrite(TX_CS, HIGH);
digitalWrite(SD_CS, LOW);
if (!SD.begin(SD_CS)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
digitalWrite(SD_CS, HIGH);
digitalWrite(TX_CS, LOW);
radio.startListening();
do {
if (radio.available()) {
radio.read(&instruction, sizeof(instruction));
Serial.print("Received: ");
bool result = radio.isChipConnected();
Serial.println(result);
Serial.println(instruction);
}
else {
Serial.println("Not Received");
}
delay(1000);
} while (strcmp("s1", instruction) != 0);
digitalWrite(TX_CS, HIGH);
Serial.println("here");
radio.stopListening();
delay(1000);
transmit_data();
}
void loop() {}
void transmit_data() {
File data_file;
char filename[] = "data.txt";
char char_array[32];
int index = 0;
digitalWrite(SD_CS, LOW);
digitalWrite(TX_CS, HIGH);
data_file = SD.open(filename);
if (data_file) {
while (data_file.available()) {
char data = data_file.read();
if (data == '\n') {
digitalWrite(SD_CS, HIGH);
digitalWrite(TX_CS, LOW);
radio.write(&char_array, sizeof(char_array));
delay(1000);
for (int x = 0; x < sizeof(char_array) / sizeof(char_array[0]);
x++) {
char_array[x] = 0;
}
index = 0;
digitalWrite(SD_CS, LOW);
digitalWrite(TX_CS, HIGH);
} // end inside if
else {
char_array[index] = data;
index++;
}
} // end while
data_file.close();
} // end outside if
else {
Serial.println("There was an error in opening the file.");
} // end else
digitalWrite(SD_CS, HIGH);
} // end data_transmit
The Python script successfully sends an instruction to the serial port and the Orangepip receives it correctly so the problem isn't here.
Basically the second circuit gets stuck printing out "Not Received" because it never receives the instruction "s1" from the other microcontroller.
Things I've tried:
I uploaded code to the circuit sending the data back that just read from the SD card and sent the data from it. This did not work initially, however when I removed the voltage regulator from the NRF24L01 and powered it from the 3.3V Arduino pin it sent the data. The problem with this is that when I re-upload the code above, radio.available() sometimes returns True even when the request instruction is not transmitted, and doesn't receive the instruction when it is sent.
I thought there could be a problem with having too many components using the 5V pin so I tried powering the NRF24L01 separately but this was also unsuccessful.
Any suggestions?
Thanks