Esp8266使用nRF24L01教程





  1.  
    /*
     * See documentation at https://nRF24.github.io/RF24
     * See License information at root directory of this library
     * Author: Brendan Doherty (2bndy5)
     */
    
    /**
     * A simple example of sending data from 1 nRF24L01 transceiver to another.
     *
     * This example was written to be used on 2 devices acting as "nodes".
     * Use the Serial Monitor to change each node's behavior.
     */
     /*
     使用RF24库
     ESP8266 与 nRF24L01接线
     
    ESP8266      nRF24L01  电源 3.3V 1GND-  2VCC引脚  8脚可不接
    D4           CE  3
    D2           CSN 4
    D5           SCK 5
    D7           MOSI 6
    D6           MISO 7
     */
    //官方的案例
    #include <SPI.h>
    #include "printf.h"
    #include "RF24.h"
    
    // instantiate an object for the nRF24L01 transceiver
    RF24 radio(D4, D2);  // using pin 7 for the CE pin, and pin 8 for the CSN pin
    
    // Let these addresses be used for the pair
    uint8_t address[][6] = { "1Node", "2Node" };
    // It is very helpful to think of an address as a path instead of as
    // an identifying device destination
    
    // to use different addresses on a pair of radios, we need a variable to
    // uniquely identify which address this radio will use to transmit
    bool radioNumber = 1;  // 0 uses address[0] to transmit, 1 uses address[1] to transmit
    
    // Used to control whether this node is sending or receiving
    bool role = false;  // true = TX role, false = RX role
    
    // For this example, we'll be using a payload containing
    // a single float number that will be incremented
    // on every successful transmission
    float payload = 0.0;
    
    void setup() {
    
      Serial.begin(115200);
      while (!Serial) {
        // some boards need to wait to ensure access to serial over USB
      }
    
      // initialize the transceiver on the SPI bus
      if (!radio.begin()) {
        Serial.println(F("radio hardware is not responding!!"));
        while (1) {}  // hold in infinite loop
      }
    
      // print example's introductory prompt
      Serial.println(F("RF24/examples/GettingStarted"));
    
      // To set the radioNumber via the Serial monitor on startup
      Serial.println(F("Which radio is this? Enter '0' or '1'. Defaults to '0'"));
      while (!Serial.available()) {
        // wait for user input
      }
      char input = Serial.parseInt();
      radioNumber = input == 1;
      Serial.print(F("radioNumber = "));
      Serial.println((int)radioNumber);
    
      // role variable is hardcoded to RX behavior, inform the user of this
      Serial.println(F("*** PRESS 'T' to begin transmitting to the other node"));
    
      // Set the PA Level low to try preventing power supply related problems
      // because these examples are likely run with nodes in close proximity to
      // each other.
      radio.setPALevel(RF24_PA_LOW);  // RF24_PA_MAX is default.
    
      // save on transmission time by setting the radio to only transmit the
      // number of bytes we need to transmit a float
      radio.setPayloadSize(sizeof(payload));  // float datatype occupies 4 bytes
    
      // set the TX address of the RX node into the TX pipe
      radio.openWritingPipe(address[radioNumber]);  // always uses pipe 0
    
      // set the RX address of the TX node into a RX pipe
      radio.openReadingPipe(1, address[!radioNumber]);  // using pipe 1
    
      // additional setup specific to the node's role
      if (role) {
        radio.stopListening();  // put radio in TX mode
      } else {
        radio.startListening();  // put radio in RX mode
      }
    
      // For debugging info
      // printf_begin();             // needed only once for printing details
      // radio.printDetails();       // (smaller) function that prints raw register values
      // radio.printPrettyDetails(); // (larger) function that prints human readable data
    
    }  // setup
    
    void loop() {
    
      if (role) {
        // This device is a TX node
    
        unsigned long start_timer = micros();                // start the timer
        bool report = radio.write(&payload, sizeof(float));  // transmit & save the report
        unsigned long end_timer = micros();                  // end the timer
    
        if (report) {
          Serial.print(F("Transmission successful! "));  // payload was delivered
          Serial.print(F("Time to transmit = "));
          Serial.print(end_timer - start_timer);  // print the timer result
          Serial.print(F(" us. Sent: "));
          Serial.println(payload);  // print payload sent
          payload += 0.01;          // increment float payload
        } else {
          Serial.println(F("Transmission failed or timed out"));  // payload was not delivered
        }
    
        // to make this example readable in the serial monitor
        delay(1000);  // slow transmissions down by 1 second
    
      } else {
        // This device is a RX node
    
        uint8_t pipe;
        if (radio.available(&pipe)) {              // is there a payload? get the pipe number that recieved it
          uint8_t bytes = radio.getPayloadSize();  // get the size of the payload
          radio.read(&payload, bytes);             // fetch payload from FIFO
          Serial.print(F("Received "));
          Serial.print(bytes);  // print the size of the payload
          Serial.print(F(" bytes on pipe "));
          Serial.print(pipe);  // print the pipe number
          Serial.print(F(": "));
          Serial.println(payload);  // print the payload's value
        }
      }  // role
    
      if (Serial.available()) {
        // change the role via the serial monitor
    
        char c = toupper(Serial.read());
        if (c == 'T' && !role) {
          // Become the TX node
    
          role = true;
          Serial.println(F("*** CHANGING TO TRANSMIT ROLE -- PRESS 'R' TO SWITCH BACK"));
          radio.stopListening();
    
        } else if (c == 'R' && role) {
          // Become the RX node
    
          role = false;
          Serial.println(F("*** CHANGING TO RECEIVE ROLE -- PRESS 'T' TO SWITCH BACK"));
          radio.startListening();
        }
      }
    
    }  // loop
    
    
    
    
    
    
    //网上的ESP8266 + nRF24l01与ESP32 + nRF24l01 通信的例子 ESP8266作为接收端
    //接收端代码
    // SimpleRx - the slave or the receiver
    #include <Arduino.h>
    #include <SPI.h>
    #include <nRF24L01.h>
    #include <RF24.h>
    
    void getData(void);
    void showData(void);
    
    const byte thisSlaveAddress[5] = {'R','x','A','A','A'};
    
    RF24 radio(D4, D2);
    
    char dataReceived[10]; // this must match dataToSend in the TX
    bool newData = false;
    
    //===========
    
    void setup() {
    
        Serial.begin(115200);
    
        Serial.println("SimpleRx Starting");
        radio.begin();
        radio.setDataRate( RF24_250KBPS );
        radio.openReadingPipe(1, thisSlaveAddress);
        radio.startListening();
    }
    
    //=============
    
    void loop() {
        getData();
        showData();
    }
    
    //==============
    
    void getData() {
        if ( radio.available() ) {
            radio.read( &dataReceived, sizeof(dataReceived) );
            newData = true;
        }
    }
    
    void showData() {
        if (newData == true) {
            Serial.print("Data received ");
            Serial.println(dataReceived);
            newData = false;
        }
    }
    
    

nRF24L01 介绍: nRF24L01是一款工作在 2.4~2.5GHz 世界通用ISM频段的单片无线收发器芯片,输出功率、频道选择和协议的设置可以通过SPI接口进行设置。有极低的电流消耗,当工作在发射模式下发射功率为-6dBm时电流消耗为9.0mA,接收模式时为12.3mA。掉电模式和待机模式下电流消耗更低。 nRF24L01参考数据: 供电电压:1.9 V~3.6V; 最大发射功率:0 dBm; 最大数据传输率:2000 kbps; 发射模式下电流消耗(0dBm时):11.3 mA; 接收模式下电流消耗(2000kbps):12.3 mA; 接收模式数据传输率为1000kbps下的 灵敏度:-85 dBm; 掉电模式下电流消耗:900 nA。 淘宝上面有两种nRF24L01模块,一种是单纯的nRF24L01模块,号称传输距离250m的,几块钱就有交易;另外一种是NRF24L01+PA+LNA模块,包含放大,号称可以传输1000m,价格几十块。实际距离视地形和障碍物而定,是否够远只能通过试用确定。 单纯nRF24L01模块做示例: nRF24L01与Arduino UNO硬件连接 此模块是使用SPI方式连接,在标准SPI口基础增加CE和CSN引脚: nRF24L01 Arduino UNO VCC <-> 3.3V GND <-> GND CE <-> D9 CSN <-> D10 MOSI<-> D11 MISO<-> D12 SCK <-> D13 IRQ <-> 不接 还有就是nRF24L01属于对传模块。每块芯片既是发射器,也是接收器。所以一般来说,要使用两块nRF24L01+两块Arduino才能进行测试。 实验1 将Sender机A0的AD转换值无线发送到Receiver机,Sender机(A0端与电源两端接一个电位器,阻值随意,一般1k~100k均可) 原理图如截图: 代码: 首先需要安装Mirf库,可以在https://playground.arduino.cc/InterfacingWithHardware/Nrf24L01下载或者本文附件下载。 实验2 将上述的数据绘图表。修改一下输出格式,然后用现成的串口图表软件显示出来。详见附件内容操作说明文档。 上位机: 直接用现成软件比如这个串口猎人。配置如图。依照图片顺序配置成截图一样即可。 附件内容截图:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值