Raspberrypi and Arduino use nrf24l01 + Communication-Arduino is the receiver

Source: Internet
Author: User
Raspberry Pi sending code: http://www.cnblogs.com/hangxin1940/archive/2013/05/01/3053467.html

# Prepare the rf24 Library

Https://github.com/maniacbug/RF24

First download the required code

Here, we need the five files 'rf24. H' 'rf24. cpp ''rf24 _ config. H' 'nrf24l01. H' 'printf. H'.
Create the rf24 folder in the libraries folder of Arduino and put them in. You can view rf24 in the import of Arduino IDE.

Don't forget, put 'printf. H' in

# Include "wprogram. H"

Change

# Include "Arduino. H"

# Connection mode

RF Arduino
3.3 V 3.3 V
Gnd
Ce D9
CSN D10
Sck D13
Mosi D11
Miso D12

# Sample code

/*
In this example, the receiver accepts the unsigned long integer data and returns the last data to the sending segment.

All Pin Connection Methods

Rf24 Arduino
3.3 V 3.3 V
Gnd
Ce D9
CSN D10
Sck D13
Mosi D11
Miso D12

*/

# Include
# Include "nrf24l01. H"
# Include "rf24.h"
# Include "printf. H"

/*

Ping-back Receiver

*/

//
// Hardware configuration
//

// Set the nrf24l01 + Ce and CSN pins
Rf24 Radio (9, 10 );

// Set the data channel address
Const uint64_t pipes [2] = {0xf0f0f0f0e1ll, 0xf0f0f0d2ll };

Void setup (void ){

// Print information
//

Serial. Begin (57600 );
Printf_begin ();
Printf ("\ n \ rrf24/examples/pingpair/\ n \ r ");
Printf ("role: pong back \ n \ r ");

//
// Set the RF module
//

Radio. Begin ();

// Enable the length of dynamic valid information
Radio. enabledynamicpayloads ();

// Set the number of retransmissions and the delay of each retransmission
// Radio. setretries (15, 15 );

// Set the transmission rate
Radio. setdatarate (rf24_1mbps );

// Set the power amplifier level. There are four levels:
// Rf24_pa_min =-18dbm
// Rf24_pa_low =-12dbm
// Rf24_pa_med =-6dbm
// Rf24_pa_high = 0dbm
Radio. setpalevel (rf24_pa_high );

// Set the channel (0-127)
Radio. setchannel (110 );

// Set the CRC verification Length
// Two types of 8-bit rf24_crc_8 and 16-bit rf24_crc_16
Radio. setcrclength (rf24_crc_16 );

// Open two channels for communication between two devices

// Open the local channel to write messages
Radio. openwritingpipe (pipes [1]);

// Open the recipient's channel to read messages
Radio. openreadingpipe (1, pipes [0]);

//
// Start listening
//
Radio. startlistening ();

//
// Print configuration information
//
Radio. printdetails ();
}

Void loop (void ){

// Whether valid data can be read
If (radio. Available ()){

Unsigned long got_time;

// Whether the valid information is received
Bool done = false;
While (! Done ){
// Obtain the final valid information
Done = radio. Read (& got_time, sizeof (unsigned long ));

// Print it out
Printf ("got payload % lu...", got_time );

// A short delay for easy acceptance of the next valid message
Delay (20 );
}

// Stop receiving first to send a response message.
Radio. stoplistening ();

// Here, the acceptable value is subtracted from the value 100. If you receive the received value, you can compare the sent message with the result received by the recipient.
Got_time-= 100;

// Send
Radio. Write (& got_time, sizeof (unsigned long ));
Printf ("sent response. \ n \ r ");

// Return to listening mode
Radio. startlistening ();

}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.