nopnop2002 / esp-idf-mirf

nRF24L01 Driver for esp-idf
MIT License
54 stars 10 forks source link

ESP32 (using esp-idf-mirf) can not communicate with Arduino (using RF24) #13

Open ntdkhoa0410 opened 1 day ago

ntdkhoa0410 commented 1 day ago

Hello, as the title said, ESP32 as a transmitter using library nopnop2002/esp-idf-mirf can not communicate with Arduino Mega using nRF24/RF24 I have tried to print all the details to the monitor and everything seems to match, but on the receiver side, no message is receive. Can you guys have a look at my code, suppose the hardware set up is correct as if both use RF24 libs, they work fine.

Arduino receiver code (RF24 lib)

// Include neccessary library
#include <string.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

//Define pins
#define CE_PIN   31
#define CSN_PIN 53
#define LED_PIN 13

// Variable for delay functions
unsigned long previousMillis = 0; // Stores the last time the LED was updated
const long interval = 1000; // Interval for the delay (in milliseconds)

// Radio variables
char text[10] = "s";
char text_old[10] ;
const byte address[6] = "00002";
uint8_t payLoad = 16;

// State to be changed when receive messaged
bool led_state = 0;

// Create a Radio
RF24 radio(CE_PIN, CSN_PIN);

// Prototype
void radio_ini();
void print_debug();
void radio_handling();

void setup() {
  Serial.begin(115200);
  strcpy(text_old,text);
  pinMode(LED_PIN, OUTPUT);
  radio_ini();
}
void loop() {
  if (radio.available()) {
    radio_handling();
  }
  print_debug();
}

void radio_ini(){
  radio.begin();
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MAX);
  radio.setPayloadSize(payLoad);
  radio.setChannel(120);
  radio.startListening();
}

void radio_handling(){
  radio.read(&text, sizeof(text)); // Read incoming data
  //Serial.println(text);
  // Compare original with temp, if message received -> LED changes states
  if (strcmp(text, text_old) != 0) {
    led_state = !led_state;
    digitalWrite(LED_PIN,led_state);
    strcpy(text_old, text);
  }
}

void print_debug(){
  unsigned long currentMillis = millis(); // Get the current time
  if (currentMillis - previousMillis >= interval) {
    uint8_t chn = radio.getChannel();
    previousMillis = currentMillis;

    Serial.print("Current channel: ");
    Serial.print(chn);
    Serial.print("\n");
    bool connected = radio.isChipConnected();
    if (connected) {
    Serial.println("Connected");
    }
    else {
    Serial.println("Not connected");
    }
    char buffer[870] = {'\0'};
    uint16_t used_chars = radio.sprintfPrettyDetails(buffer);
    Serial.println(buffer);
  }
}

ESP32 transmitter code (ESP-IDF-MIRF lib)

main.c

#include <stdio.h>
#include "defines.h"
#include "components_list.h"
#include "esp_timer.h"

// Variable for delay functions
static unsigned long previousMillis = 0; // Stores the last time the LED was updated
static const long interval = 1000;       // Interval for the delay (in milliseconds)

void ini(void)
{
    led_init();

    // Init for mirf
    uint8_t payload = 16;
    uint8_t channel = 120;
    char *address = "20000";
    nrf24l01_init(payload, channel, address);
}

void app_main(void)
{
    ini();
    while (1)
    {
        unsigned long currentMillis = esp_timer_get_time() / 1000;
        if (currentMillis - previousMillis >= interval) // Update and send message every 1 second
        {
            previousMillis = currentMillis;
            sender();
        }
    }
}

nrf24l01_init function:

void nrf24l01_init(uint8_t payload, uint8_t channel, char *address)
{
    ESP_LOGI(pcTaskGetName(NULL), "Start");
    // NRF24_t dev;
    Nrf24_init(&dev);

    Nrf24_config(&dev, channel, payload);

    // Set destination address using 5 characters
    esp_err_t ret = Nrf24_setTADDR(&dev, (uint8_t *)address);
    if (ret != ESP_OK)
    {
        ESP_LOGE(pcTaskGetName(NULL), "nrf24l01 not installed");
        while (1)
        {
            vTaskDelay(1);
        }
    }

    AdvancedSettings(&dev);
    // Print settings
    Nrf24_printDetails(&dev);
}

AdvancedSettings function:

void AdvancedSettings(NRF24_t *dev)
{

    ESP_LOGW(pcTaskGetName(NULL), "Set RF Data Ratio to 1MBps");
    Nrf24_SetSpeedDataRates(dev, 0);

    ESP_LOGW(pcTaskGetName(NULL), "CONFIG_RETRANSMIT_DELAY=%d", CONFIG_RETRANSMIT_DELAY);
    Nrf24_setRetransmitDelay(dev, CONFIG_RETRANSMIT_DELAY);
}

Sender function:

void sender(void)
{
    uint8_t buf[16];
    uint8_t var_Num = updateMessage();
    sprintf((char *)buf, "Message %" PRIu8, var_Num);
    Nrf24_send(&dev, buf);
    // vTaskDelay(1);
    ESP_LOGI(pcTaskGetName(NULL), "The data to send: %s", buf);
    ESP_LOGI(pcTaskGetName(NULL), "Wait for sending.....");
    if (Nrf24_isSend(&dev, 1000))
    {
        ESP_LOGI(pcTaskGetName(NULL), "Send success:%s", buf);
    }
    else
    {
        ESP_LOGW(pcTaskGetName(NULL), "Send fail:");
    }
    blink_led();
}

The monitor of Arduino:

image

The monitor of ESP32:

image

nopnop2002 commented 1 day ago

The ESP32's RX address and the Arduino's TX address do not match.

The Arduino's TX address is 0xE7E7E7E7E7, but the ESP32's RX address does not have this address.

Therefore, ESP32 cannot receive Ack packets from Arduino.

ESP32 --> Arduino Send data packet to 0x3230303030. Arduino can receive data packet. Wait ack packet from 0x3230303030.

Ardino --> ESP32 Receive data packet from 0x3230303030. Send back ack packet to 0xE7E7E7E7E7 with Enhanced ShockBurst features. ESP32 can't receive ack packet.

ntdkhoa0410 commented 1 day ago

I see, so I either set

  1. Arduino Tx address to 0x3230303030 or
  2. ESP32 Rx address to 0xE7E7E7E7E7 Am i correct? Is there anyway to set ESP32 Rx address to 0xE7E7E7E7E7 using your library, the mirf.h?
nopnop2002 commented 1 day ago

Am i correct?

Yes. You are correct. However, communication is not possible unless not only the channel and payload size but also all registers have the same value. It is not known whether all registers on your arduino library and my library have the same value. Only a few register values ​​are displayed as NRF Configuration. You need to check the values ​​of all registers in your Arduino library and my library. Unfortunately, there are no standard register values ​​for nRF24L01. Register values ​​are library implementation dependent.

This library ensures intercommunication. https://github.com/nopnop2002/Arduino-STM32-nRF24L01

Is there anyway to set ESP32 Rx address to 0xE7E7E7E7E7 using your library, the mirf.h?

esp_err_t ret = Nrf24_setRADDR(&dev, rx_addr);

================ SPI Configuration ================
CSN Pin          = GPIO17
CE Pin           = GPIO16
Clock Speed      = 4000000
================ NRF Configuration ================
STATUS           = 0x0e RX_DR=0 TX_DS=0 MAX_RT=0 RX_P_NO=7 TX_FULL=0
RX_ADDR_P0-1     = 0x3230303030 0xe7e7e7e7e7
RX_ADDR_P2-5     = 0xc3 0xc4 0xc5 0xc6
TX_ADDR          = 0x3230303030
RX_PW_P0-6       = 0x20 0x20 0x00 0x00 0x00 0x00
EN_AA            = 0x3f
EN_RXADDR        = 0x03
RF_CH            = 0x5a
RF_SETUP         = 0x0f
CONFIG           = 0x2b
DYNPD/FEATURE    = 0x00 0x00
Data Rate        = 2Mbps
CRC Length       = 8 bits
PA Power         = PA_MAX
Retransmit       = 250 us
ntdkhoa0410 commented 19 hours ago

@nopnop2002 i see, let me try to change the address first and play up a little bit. I maybe need to use Arduino-STM32-nRF24L01 . I can only work at nights as i get back home from work, so i will update you in a few days, thank you

ntdkhoa0410 commented 6 hours ago

I tried to match them, but still no results: ESP32 Tx (using esp-idf-mirf.h) image Arduino Rx (using rf24.h) image