Advertisement
Guest User

Untitled

a guest
Jan 10th, 2016
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. #include <SPI.h>
  2. #include "RF24.h"
  3.  
  4. byte addresses[][6] = {"1Node", "2Node"};
  5.  
  6. char character;
  7. int incomingByte = 0;
  8. /****************** User Config ***************************/
  9. /*** Set this radio as radio number 0 or 1 ***/
  10. bool radioNumber = 0;
  11.  
  12. /* Hardware configuration: Set up nRF24L01 radio on SPI bus plus pins 7 & 8 */
  13. RF24 radio(7, 8);
  14. /**********************************************************/
  15.  
  16.  
  17. // Used to control whether this node is sending or receiving
  18. bool role = 0;
  19.  
  20.  
  21. String message = ""; //message variable, empty until a message come
  22. char messagechar[200];
  23.  
  24. void setup() {
  25.  
  26. Serial.begin(115200);
  27. radio.begin();
  28. radio.setPALevel(RF24_PA_HIGH);
  29. if (radioNumber) {
  30. radio.openWritingPipe(addresses[1]);
  31. radio.openReadingPipe(1, addresses[0]);
  32. } else {
  33. radio.openWritingPipe(addresses[0]);
  34. radio.openReadingPipe(1, addresses[1]);
  35. }
  36. Serial.println(F("welcome in messenger"));
  37. Serial.println(F("enter a message"));
  38. }
  39.  
  40. void loop() {
  41.  
  42. while (Serial.available()) { //getting message from the serial
  43.  
  44. character = Serial.read();
  45. message.concat(character);
  46. delay (10);
  47. }
  48.  
  49. if (message != "") { //if there is a message
  50. radio.stopListening();
  51.  
  52. if (message.length() > 32) {
  53. Serial.println(" warning you message got cut");
  54. Serial.print(" Amount of letter (maximum is 32): ");
  55. Serial.println(message.length());
  56. }
  57.  
  58.  
  59. message.toCharArray(messagechar, 33); // convert String into char for transmisssion (maxium 32 characters)
  60. Serial.print(" you sent : ");
  61. Serial.println(messagechar);
  62. message = ""; // reset the message variable
  63.  
  64.  
  65. if (!radio.write( &messagechar, sizeof(messagechar) )) {
  66. Serial.println(F("failed"));
  67. messagechar[0] = (char)0; // reset the message variable
  68. } else {
  69. messagechar[0] = (char)0; // reset the message variable
  70. }
  71. }
  72.  
  73.  
  74. radio.startListening();
  75. while (radio.available()) { // While there is data ready
  76. radio.read( &messagechar, sizeof(messagechar) ); // Get the payload
  77.  
  78.  
  79.  
  80. if ( strlen(messagechar) != 0) { //if we received a message, display it
  81. Serial.print("friend sent : ");
  82. Serial.println(messagechar);
  83. messagechar[0] = (char)0;; //reset the char message
  84.  
  85.  
  86. }
  87. delay (250);
  88. messagechar[0] = (char)0;; //reset the char message
  89.  
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement