Advertisement
Jack-TW

Arduino nRF24L01 RX

Jul 13th, 2015
465
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.54 KB | None | 0 0
  1. //This demo Working with Arduino Uno or nano.
  2. //For nRF24L01, rec string.
  3. //TX:http://pastebin.com/5Ju5XUPr
  4. //這程式碼是給Arduino+nRf24L01用來傳送訊息的
  5. //It working good.
  6. //部分修改自網路
  7. //接收訊息完整 沒有缺漏
  8. ////Library:https://codeload.github.com/maniacbug/RF24/zip/master
  9. #include <nRF24L01.h>
  10. #include <RF24.h>
  11. #include <RF24_config.h>
  12. #include <SPI.h>
  13.  //Rec MSG
  14. /*
  15. This sketch receives strings from sending unit via nrf24
  16. and prints them out via serial.  The sketch waits until
  17. it receives a specific value (2 in this case), then it
  18. prints the complete message and clears the message buffer.
  19. */
  20.  
  21. int msg[1];
  22. RF24 radio(9,10);
  23. const uint64_t pipe = 0xF0F0F0F0E1LL; //選擇管線,接收與傳送需要一致
  24. int lastmsg = 1;
  25. String theMessage = "";
  26. void setup(void){
  27.   Serial.begin(9600);
  28.     Serial.println("I am getter.");
  29.   radio.begin();
  30.    radio.enableDynamicPayloads();
  31.     radio.setAutoAck(1);
  32.     radio.setRetries(15,15);
  33.     radio.setDataRate(RF24_250KBPS);
  34.     radio.setPALevel(RF24_PA_MAX);
  35.     radio.setChannel(76);//選擇76頻道,避開干擾
  36.   //  radio.openWritingPipe(0xF0F0F0F0E1LL);
  37.   radio.openReadingPipe(1,pipe);
  38.   radio.startListening();
  39. }
  40. void loop(void){
  41.   if (radio.available()){
  42.     bool done = false;  
  43.       done = radio.read(msg, 1);
  44.       char theChar = msg[0];
  45.       if (msg[0] != 11){ //接收傳送需一致
  46.         theMessage.concat(theChar);
  47.         }
  48.       else {
  49.        Serial.println(theMessage);
  50.        theMessage= "";
  51.       }
  52.    }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement