Dear Friends,
I'm trying to receive 5 bytes from a nrf24l01+ and re-transmit 2 bytes received trough canbus.
Using the attached code I'm able to receive every 100 ms all the bytes from the transmitter without lose any packets only if i disconnect the cables CanH and CanL.
The can bus works perfectly, I'm able to receive,filter and answer in all the conditions (max delay 0,2 ms measured).
Connecting CanH and CanL no problem with the CAN , but I start to lose a lot of packets from the nrf24l01.In this case the delay for a new data received can reach 1 second or more (the Transmitter send new data every 120 ms).
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include "driver/twai.h"
// Configurazione Pin nRF24L01
#define CE_PIN 7
#define CSN_PIN 8
// Configurazione Pin CAN
#define CAN_TX_PIN GPIO_NUM_9
#define CAN_RX_PIN GPIO_NUM_10
#define DIP_1 0
#define DIP_2 1
#define DIP_3 2
#define DIP_4 3
#define RF_TIMEOUT_MS 400
unsigned long lastRfTime = 0;
bool rfTimedOut = true;
byte channel = 101;
byte DIP = 0;
byte indirizzo = 0;
byte thisSlaveAddress[5] = {0x00, 0x00, 0x00, 0x00, 0x00};
//RF24 radio(CE_PIN, CSN_PIN, 2000000);
RF24 radio(CE_PIN, CSN_PIN);
uint8_t rfData[5] = {0x00, 0x00, 0x00, 0x00, 0x00};
void setup() {
pinMode(DIP_1, INPUT_PULLUP); // Consigliato INPUT_PULLUP se i DIP chiudono a GND
pinMode(DIP_2, INPUT_PULLUP);
pinMode(DIP_3, INPUT_PULLUP);
pinMode(DIP_4, INPUT_PULLUP);
Serial.begin(115200);
delay(1000);
// --- 2. Inizializzazione CAN Bus (TWAI) ---
twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT(CAN_TX_PIN, CAN_RX_PIN, TWAI_MODE_NORMAL);
twai_timing_config_t t_config = TWAI_TIMING_CONFIG_500KBITS();
twai_filter_config_t f_config = {
.acceptance_code = (0x589UL << 21),
.acceptance_mask = ~(0x7FFUL << 21),
.single_filter = true
};
if (twai_driver_install(&g_config, &t_config, &f_config) == ESP_OK) {
Serial.println("Driver CAN installed filte ID 0x589.");
} else {
Serial.println("Error install driver CAN.");
while (1);
}
if (twai_start() == ESP_OK) {
Serial.println("Driver CAN start.");
} else {
Serial.println("Error start driver CAN.");
while (1);
}
// --- 1. Inizializzazione RF24 ---
DIP = digitalRead(DIP_1) | (digitalRead(DIP_2) << 1) | (digitalRead(DIP_3) << 2) | (digitalRead(DIP_4) << 3);
indirizzo = channel + DIP;
Serial.println("SimpleRx Starting");
if (!radio.begin()) {
Serial.println("nRF24L01 not responding!");
}
for (int i = 0; i < 5; i++) {
thisSlaveAddress[i] = indirizzo;
}
radio.setDataRate(RF24_250KBPS);
radio.setChannel(channel + DIP);
radio.setPALevel(RF24_PA_LOW);
radio.setAutoAck(false);
radio.enableDynamicPayloads();
radio.openReadingPipe(1, thisSlaveAddress);
radio.printPrettyDetails();
radio.startListening();
Serial.println("nRF24L01 listening.");
}
void loop() {
// --- FASE 1: Controllo ricezione dati da nRF24 ---
if (radio.available()) {
uint8_t len = radio.getDynamicPayloadSize();
if (len == 5) {
radio.read(&rfData, len);
lastRfTime = millis();
rfTimedOut = false;
Serial.printf("RF (%d byte): 0x%02X 0x%02X\n", len, rfData[0], rfData[1]);
}
}
//**********************controllo timeout************
if (!rfTimedOut && (millis() - lastRfTime > RF_TIMEOUT_MS)) {
rfData[0] = 0x00;
rfData[1] = 0x00;
rfTimedOut = true;
Serial.println("Timeout RF24 (>400ms)");
}
// --- FASE 2: Controllo ricezione messaggi CAN ---
twai_message_t rx_msg;
// twai_receive(&rx_msg, 0);
if (twai_receive(&rx_msg, 0) == ESP_OK) {
//Serial.print("R ");
inviaRispostaCAN();
}
}
void inviaRispostaCAN() {
twai_message_t tx_msg;
tx_msg.identifier = 0x181;
tx_msg.extd = 0;
tx_msg.rtr = 0;
tx_msg.data_length_code = 8;
tx_msg.data[0] = 0xBA;
tx_msg.data[1] = 0x02;
if (rfTimedOut == false) tx_msg.data[2] = 0x01;
if (rfTimedOut == true) tx_msg.data[2] = 0x00;
tx_msg.data[3] = rfData[0]; // MSB pot
tx_msg.data[4] = rfData[1]; // LSB pot
tx_msg.data[5] = 0x02; // version
tx_msg.data[6] = 0x00;
tx_msg.data[7] = 0x00;
if (twai_transmit(&tx_msg, pdMS_TO_TICKS(1)) == ESP_OK) {
//do nothing
} else {
Serial.println("Error send CAN.");
}
}
Using decoupling capacitor or ferrite bead on supply or spi signal nothing change.
Does somebodys have an idea of what was wrong?

