esp32与esp8266通过nrf24l01通信

10 篇文章 0 订阅

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);
    }
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值