Hello so i've been using NRF24L01 for communication between two nanos and few days ago i bought esp32 (wroom32u), and i want to set up one nano as transmitter and the esp as receiver.
my problem is that my esp32 doesnt recieve anything.
im using RF24 library and radio.available() is never true. I tried using it as tx and nano wouldn't get anything.
basic code im using for transmitter:
#include <SPI.h>
#include "printf.h"
#include "RF24.h"
#define CE_PIN 10
#define CSN_PIN 9
int sensorValueX=0;
int sensorValueY=0;
struct msg{
int x;
int y;
} __attribute__((__packed__)) ;
//tried this based on one forum post didnt work
msg payload = {sensorValueX,sensorValueX};
// instantiate an object for the nRF24L01 transceiver
RF24 radio(CE_PIN, CSN_PIN);
// Let these addresses be used for the pair
const uint64_t address= 0xF0F0F0F0F0;
void setup() {
if (!radio.begin()) {
//Serial.println(F("radio hardware is not responding!!"));
while (1) {} // hold in infinite loop
}
//Serial.print("radio starteds");
radio.setDataRate(RF24_1MBPS);
radio.setPALevel(RF24_PA_LOW);
radio.setPayloadSize(sizeof(msg));
radio.setAutoAck(false);
radio.openWritingPipe(address);
radio.stopListening();
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pins
sensorValueX = analogRead(A0);
sensorValueY = analogRead(A1);
//...
payload.x=sensorValueX;
payload.y=sensorValueY;
bool report=radio.write(&payload, sizeof(msg));
//...
delay(10);
}
and for receiver:
#include <SPI.h>
#include <RF24.h>
RF24 radio(4, 5); // the (ce_pin, csn_pin) connected to the radio
int sensorValueX=0;
int sensorValueY=0;
struct msg{
int x;
int y;
} __attribute__((__packed__)) ;
msg payload = {sensorValueX,sensorValueX};
const uint64_t address= 0xF0F0F0F0F0;
void setup() {
Serial.begin(115200);
if (!radio.begin()) {
Serial.println(F("radio hardware not responding!!"));
while (1) {}
}
radio.setDataRate(RF24_1MBPS);
radio.setPALevel(RF24_PA_LOW);
radio.setChannel(5);//
radio.setPayloadSize(sizeof(msg));
radio.setAutoAck(false);
radio.openReadingPipe(1,address);
radio.startListening();
}
void loop() {
// print out the value you read:
if (radio.available()){
radio.read(&payload, sizeof(msg));
Serial.print(" X: ");
Serial.print(payload.x);
Serial.print(" | Y: ");
Serial.println(payload.y);
}
delay(10); // delay in between reads for stability
}
i tried with different delays addresses payloads, structures and cant get it to work.
also both rf24 modules are fine, nothing is fried,
on esp32 i'm using vspi default pins 23,19,18, 5.
board selected is "ESP32 DEV Module" i tried few other but i've seen on some forum this one works (due to certain error again).
I've tried both external power supply and directly over esp32, also I tried with decoupling capacitor, and all combinations (and different capacities).
By the looks of it radio works but buffer is empty, I'm suspecting something with configuration or address but I don't know.
In post #30 of Robin2's simple rf24 tutorial is a diagnostic sketch to test the hardware connection between the rf24 module and its connected processor. Maybe that will help.
I recommend using an external 3.3V power source for the rf24 modules that is capable of supplying 500mA. That assures enough current. I use home made versions of these adapters.
If you are using the high powered modules (with external antenna), move the modules at least a couple of meters apart. They may not communicate if too close together.
So weird thing happened, I borrowed another esp32, and it works when both are esp32.
So the (ignore channel) code and setup works when both are Arduino nanos and both are esp32.
Is it possible adresses get passed differently or something like that due to one being 32 and other 8 bit, I'm not that familiar with their architectures
Some time ago I did a test with communication between an ESP32 with a RF24 module and an Uno with a RF24 module. I am pretty sure that it was successful. I can repeat the test given a little time. I don't see any reason that there would be trouble with the radios talking between 32 and 8 bit processors.
I wired a rf24 to my ESP32 dev kit board using my home made power adapter (5V from ESP32 Vin) to power the rf24 module and the SPI wiring that you used with the exception of CE (I use 22) and CSN (I use 21). The ESP32 is set up as receiver and used an Uno as the sender. The ESP32 receives and acknowledges just fine.
I will post the code if you want. It is based upon the excellent Robin2 simple rf24 tutorial code.
Update I have removed ack and size part of code and now have transmition but something is not right with structure or something.
When I send lets say 1 and 0 as X and Y I read(on esp) 1 and 0
But when I say let's say 0 and 1, I get 65536 and 0
If I send 1 and 1 I get 65537 and 0
If I send 5 and 0 I get 327680 and 0
If I send 5 and 5 I get 327685 and 0.
I think somehow the y value is read as hex y0000 and it's added to X, I don't know if my structure is wrong or if in esp32 I should do it differently?
Edit:
Apparently changing variables in struct to float solved everything, payload size again could be made based on sizeof(structure).
I think the problem was that int in esp32 is 4 bytes but in Arduino nano int is 2bytes
use of fixed width integer types , e.g. int8_t, int16_t, etc helps with the problem of communicating binary data between systems running C++
clearly care has to be taken with floating point types of where systems may use different sizes as pointed out by @Gbot in post 11
even more of a problem is transmitting binary data between systems using different languages, e.g. C++, C#, Java, Python, etc