You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用RF24库时树莓派4无法接收ESP8266的C结构体(ESP8266转ESP32正常)

树莓派4通过RF24库接收ESP8266结构体数据失败问题

这是我的首个树莓派项目,我尝试使用RF24库接收ESP8266发送的C类型结构体。ESP8266与ESP32通信时一切正常,对应的ESP32代码如下:

#include <Arduino.h>
#include <SPI.h>
#include <RF24.h>

//PMX Data Structure
struct nrfDataStruct{
  uint8_t change;
  float pm10;
  float sumBins;
  float temp;
  float altitude;
  float hum;
  float xtra;
}payload;

//RF24
RF24 radio(3, 10); //CE, CSN

void setup() {
  Serial.begin(115200);
  while(!radio.begin()) {
    noRFscreen();
  }
  radio.openReadingPipe(1, 1); //发送端默认使用管道0,所以接收端用管道1(范围0-5)
  radio.setChannel(108); //PMX专用信道
  radio.setDataRate(RF24_250KBPS); //PMX使用250kbps,适合远距离传输
  radio.setAutoAck(false);  //数据包大小固定,无需确认机制
  radio.setPALevel(RF24_PA_MAX);
  radio.startListening();
}

void loop() {
  if (radio.available()) {
    radio.read(&payload, sizeof(payload));
  }
  delay(10);
}

现在我想在64位系统的树莓派4上实现相同功能,已编译安装了同款RF24库,但无论使用C++库还是Python的pyRF24库,都无法正确接收数据,希望有人能提供帮助。


树莓派4的C++代码及输出

#include <iostream>
#include <string>
#include <time.h>
#include <RF24/RF24.h>

#define CSN_PIN 0
#ifdef MRAA
    #define CE_PIN 15
#else
    #define CE_PIN 22
#endif

RF24 radio(CE_PIN, CSN_PIN);

//PMX Data Structure
struct nrfDataStruct{
  uint8_t change;
  float pm10;
  float sumBins;
  float temp;
  float altitude;
  float hum;
  float xtra;
}payload;

struct timespec startTimer, endTimer;
uint32_t getMicros();
uint32_t delayTime = 10000;
uint32_t timerElapsed = 0;

int main(){
    // 硬件检测
    if (!radio.begin()){
        std::cout << "radio hardware is not responding!!!\n";
        return 1;
    }
    clock_gettime(CLOCK_MONOTONIC_RAW, &startTimer);
    radio.openReadingPipe(1, 1);
    radio.setChannel(108);
    radio.setDataRate(RF24_250KBPS);
    radio.setAutoAck(false);
    radio.setPALevel(RF24_PA_MAX);
    radio.startListening();

    while(true) {
        if ((timerElapsed = getMicros()) >= delayTime) {
            if (radio.available()) {
                radio.read(&payload, sizeof(payload));
                std::cout << "PMX Data: " << payload.change << " " << payload.pm10 << " " << payload.sumBins << " " << payload.temp << " " << payload.altitude << " " << payload.hum << " " << payload.xtra << "\n";
            }
            clock_gettime(CLOCK_MONOTONIC_RAW, &startTimer);
        }
    }
    
    return EXIT_SUCCESS;
}

uint32_t getMicros(){
    clock_gettime(CLOCK_MONOTONIC_RAW, &endTimer);
    uint32_t seconds = endTimer.tv_sec - startTimer.tv_sec;
    uint32_t useconds = endTimer.tv_nsec - startTimer.tv_nsec / 1000;
    return ((seconds)*1000 + useconds) + 0.5;
}

输出:

...
PMX Data: 0 0 0 0 0 0 0
PMX Data: 0 0 0 0 0 0 0
PMX Data: 0 0 0 0 0 0 0
...

pyRF24库代码及输出

import struct
from pyrf24 import RF24, RF24_CRC_DISABLED, RF24_PA_MAX, RF24_250KBPS

radio = RF24(22, 0)

if not radio.begin():
    raise OSError("nRF24L01 hardware isn't responding")

radio.set_pa_level(RF24_PA_MAX)

radio.open_rx_pipe(1, 1)
radio.setChannel(108)
radio.setDataRate(RF24_250KBPS)
radio.setAutoAck(False)

# 计算负载大小
radio.payload_size = struct.calcsize("<Bffffff")
radio.print_details()

while True:
    radio.listen = True
    has_payload, pipe_number = radio.available_pipe()
    if has_payload:
        length = radio.payload_size
        received = radio.read(length)
        nrfDataStruct = struct.unpack("<Bffffff", received)
        print(nrfDataStruct)

输出:

================ SPI Configuration ================
CSN Pin         = /dev/spidev0.0
CE Pin          = Custom GPIO22
SPI Speedz      = 10 Mhz
================ NRF Configuration ================
STATUS          = 0x0e RX_DR=0 TX_DS=0 MAX_RT=0 RX_P_NO=7 TX_FULL=0
RX_ADDR_P0-1    = 0xe7e7e7e7e7 0x0000000001
RX_ADDR_P2-5    = 0xc3 0xc4 0xc5 0xc6
TX_ADDR         = 0xe7e7e7e7e7
RX_PW_P0-6      = 0x19 0x19 0x19 0x19 0x19 0x19
EN_AA           = 0x00
EN_RXADDR       = 0x03
RF_CH           = 0x6c
RF_SETUP        = 0x27
CONFIG          = 0x0e
DYNPD/FEATURE   = 0x00 0x00
Data Rate       = 250 KBPS
Model           = nRF24L01+
CRC Length      = 16 bits
PA Power        = PA_MAX
ARC             = 0

无其他输出。


树莓派4引脚连接

  • CE接引脚15(对应GPIO22)
  • CS接引脚24(对应GPIO8,通常标记为CE0)

内容的提问来源于stack exchange,提问作者shyney

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 12:09:54