nrf24l01+ Arduino 代码似乎没有发送/接收数据

问题描述 投票:0回答:1

我已经确认两个 nrf24l01 模块都可以像 nano 一样工作。我也用一个简单的代码尝试过并让它工作。我已经尝试了所有我能想到的方法,但现在却不知该尝试什么。

有人可以检查一下这段代码是否适用于他们的 nrf24l01 模块或其他东西吗?

接收码:

#include "SPI.h"
#include "RF24.h"
#include "nRF24L01.h"

#define CE_PIN 9
#define CSN_PIN 10

#define INTERVAL_MS_SIGNAL_LOST 1000
#define INTERVAL_MS_SIGNAL_RETRY 250

RF24 radio(CE_PIN, CSN_PIN);

const byte address[6] = "00001";

//NRF24L01 buffer limit is 32 bytes (max struct size)
struct payload {
  byte data1;
  char data2;

  //byte pot_value;
};

payload payload;

unsigned long lastSignalMillis = 0;

void setup()
{
  Serial.begin(115200);

  radio.begin();

  //Append ACK packet from the receiving radio back to the transmitting radio
  radio.setAutoAck(false); //(true|false)
  //Set the transmission datarate
  radio.setDataRate(RF24_250KBPS); //(RF24_250KBPS|RF24_1MBPS|RF24_2MBPS)

  radio.setPALevel(RF24_PA_HIGH); //(RF24_PA_MIN|RF24_PA_LOW|RF24_PA_HIGH|RF24_PA_MAX)
  //Default value is the maximum 32 bytes
  radio.setPayloadSize(sizeof(payload));

  radio.openReadingPipe(0, address);
  radio.startListening();
}

void loop()
{
  unsigned long currentMillis = millis();

  if (radio.available()) {
    radio.read(&payload, sizeof(payload));

    Serial.println("Received");

    Serial.print("Data1:");
    Serial.println(payload.data1);

    Serial.print("Data2:");
    Serial.println(payload.data2);

    //Serial.print("Data3:");
    //Serial.println(payload.pot_value);

    lastSignalMillis = currentMillis;
  }

  if (currentMillis - lastSignalMillis > INTERVAL_MS_SIGNAL_LOST) {
    lostConnection();
  }
  delay(INTERVAL_MS_SIGNAL_RETRY);
}

void lostConnection()
{
  Serial.println("We have lost connection, preventing unwanted behavior");
}

发送代码:

#include "SPI.h"
#include "RF24.h"
//#include <printf.h>`--

#define CE_PIN 9
#define CSN_PIN 10

//#define POT_PIN

#define INTERVAL_MS_TRANSMISSION 250

RF24 radio(CE_PIN, CSN_PIN);

const byte address[6] = "00001";

//NRF24L01 buffer limit is 32 bytes (max struct size)
struct payload {
  byte data1;
  char data2;
  //byte pot_value;
};

payload payload;

void setup()
{
  Serial.begin(115200);

  if (radio.isChipConnected()) {
    radio.begin();
    Serial.println("nRF24L01 Intialiazed!");
    
    //Append ACK packet from the receiving radio back to the transmitting radio
    radio.setAutoAck(false); //(true|false)

    //Set the transmission datarate
    radio.setDataRate(RF24_250KBPS); //(RF24_250KBPS|RF24_1MBPS|RF24_2MBPS)

    radio.setPALevel(RF24_PA_HIGH); //(RF24_PA_MIN|RF24_PA_LOW|RF24_PA_HIGH|RF24_PA_MAX)

    //Default value is the maximum 32 bytes
    radio.setPayloadSize(sizeof(payload));

    radio.openWritingPipe(address);
    radio.stopListening();
  }else{
    Serial.println("No connection between nRF24L01 and Arduino! ");
  }
}

void loop()
{
  payload.data1 = 123;
  payload.data2 = 'x';

  //payload.pot_value = analogRead(POT_PIN);

  if(radio.write(&payload, sizeof(payload))) {
    Serial.print("Data1:");
    Serial.println(payload.data1);

    Serial.print("Data2:");
    Serial.println(payload.data2);

    //Serial.print("Data3:");
    //Serial.println(payload.pot_value);

    Serial.println("Sent");

  }else {
    Serial.println("Error! Payload not sent");
  }

  delay(INTERVAL_MS_TRANSMISSION);
}
c++ arduino serial-port
1个回答
0
投票

RF24::openReadingPipe()
RF24::openWritingPipe()
期望地址为
uint64_t
。在您的两个程序中,
address
是一个字节数组,当传递给函数时,它会衰减为指针。所以 open*Pipe() 函数得到不同的指针值而不是数字地址
1
。显然,不同程序中的指针值不太可能相同。 如果启用编译器警告,您会看到

从“const byte* {aka const unsigned char*}”到“uint64_t {aka long long unsigned int}”的无效转换

© www.soinside.com 2019 - 2024. All rights reserved.