esp32 发送端
/*
接线
NRF24L01 ESP32
1 GND GND
2 VCC 3.3V
3 (CE) D22
4 (CSN) D21
5 (SCK) D18
6 (MOSI) D23
7 (MISO) D19
8 IRQ * D4*
∗: 中断线是可选的
*/
// SimpleTx - the master or the transmitter
#include <Arduino.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 22
#define CSN_PIN 21
const byte slaveAddress[5] = {'R','x','A','A','A'};
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
char dataToSend[12] = "I am Sender";
void setup() {
Serial.begin(115200);
Serial.println("SimpleTx Starting");
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.setRetries(3,5); // delay, count
radio.openWritingPipe(slaveAddress);
}
void loop() {
bool rslt;
rslt = radio.write( &dataToSend, sizeof(dataToSend) );
// Always use sizeof() as it gives the size as the number of bytes.
// For example if dataToSend was an int sizeof() would correctly return 2
Serial.print("Data Sent:");
Serial.print(dataToSend);
if (rslt) {
Serial.println(" Acknowledge received");
}
else {
Serial.println(" Tx failed");
}
delay(1000);
}
esp8266接收端
/*
ESP8266 nRF24L01 电源 3.3V 1GND- 2VCC引脚 8脚可不接
D4 CE 3
D2 CSN 4
D5 SCK 5
D7 MOSI 6
D6 MISO 7
*/
//网上的ESP8266 + nRF24l01与ESP32 + nRF24l01 通信的例子 ESP8266作为接收端
//接收端代码
// SimpleRx - the slave or the receiver
#include <Arduino.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
const byte thisSlaveAddress[5] = {'R','x','A','A','A'};
RF24 radio(D4, D2);
char dataReceived[12]; // this must match dataToSend in the TX
void setup() {
Serial.begin(115200);
Serial.println("SimpleRx Starting");
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.openReadingPipe(1, thisSlaveAddress);
radio.startListening();
}
void loop() {
if (radio.available()) {
radio.read( &dataReceived, sizeof(dataReceived) );
Serial.print("Data received: ");
Serial.println(dataReceived);
}
}