Advertisement
stanleyseow

Arduino nRF to Serial rev002

Jun 4th, 2013
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  This program will TX all data received from the serial pins out to the nRF radio
  3.  and sent out all the data received from the nRF to serial monitor/process the data
  4.  
  5.  For this code, I m using a GPS module Skylab SKM53 running at 9600bps
  6.  
  7.  Max nRF payload size = 30
  8.  1st byte header :-
  9.  8     : fragment/no fragment
  10.  5 - 7 : number of fragments
  11.  4     : unused
  12.  1 - 3 : fragment sequence number
  13.  
  14.  Max buffer is 90 bytes
  15.  
  16.  Serial input pins RX 2 ,TX 3
  17.  nRF pins CE 8, CSN, 9 MOSI 11, MIS0 12, SCK 13
  18.  
  19.  Date : 28/05/2013
  20.  
  21.  Written by Stanley Seow
  22. */
  23.  
  24. #include <SoftwareSerial.h>
  25. #include <SPI.h>
  26. #include "nRF24L01.h"
  27. #include "RF24.h"
  28. #include "printf.h"
  29.  
  30.  
  31. #define DATABUFFERSIZE 90
  32. char dataBuffer[DATABUFFERSIZE];
  33.  
  34. byte dataBufferIndex = 0;
  35. char startChar = '$';
  36. char endChar = '\r';
  37.  
  38. SoftwareSerial mySerial(2,3);
  39.  
  40. // Set up nRF24L01 radio on SPI pin for CE, CSN
  41. RF24 radio(8,9);
  42.  
  43. const uint64_t pipes[2] = { 0xDEDEDEDEE7ULL, 0xDEDEDEDEE9ULL };
  44.  
  45. // Defines for radio buffers
  46. char RXPayload[33] = "";
  47. char TXPayload[33] = "";
  48. char STR1[33],STR2[33],STR3[33] = "";
  49.  
  50. int Sender, storeString = 0;
  51.  
  52. void setup(void)
  53. {
  54.   Serial.begin(115200);
  55.  
  56.   mySerial.begin(9600);
  57.   delay(500);
  58.  
  59.   printf_begin();
  60.   //printf("nRF Serial RX/TX\n\r");
  61.   radio.begin();
  62.  
  63.   // Default radio setings
  64.   radio.enableDynamicPayloads();
  65.   radio.setDataRate(RF24_250KBPS);
  66.   radio.setPALevel(RF24_PA_MAX);
  67.   radio.setChannel(85);
  68.   radio.setRetries(15,15);
  69.  
  70.   //check for which side is the sender ( receive data via softwareserial )
  71.   if ( mySerial.available() ) {
  72.          Sender = true;
  73.   }
  74.    
  75.   if ( Sender ) {
  76.     radio.openWritingPipe(pipes[0]);
  77.     radio.openReadingPipe(1,pipes[1]);
  78.   } else {
  79.     // this side is the receiver connected to Serial Monitor  
  80.     radio.openWritingPipe(pipes[1]);
  81.     radio.openReadingPipe(1,pipes[0]);  
  82.     radio.startListening();  
  83.   }
  84.  
  85.   //radio.printDetails();
  86.   delay(500);
  87.  
  88. }
  89.  
  90. void loop(void) {
  91.  
  92.   if ( !Sender ) {  
  93.       nRF_Receive();
  94.   } else {
  95.       nRF_Sender();
  96.   }
  97. } // End of loop()
  98.  
  99.  
  100. void nRF_Sender() {
  101.  
  102.   char dataBuffer2[DATABUFFERSIZE]="";
  103.   int status = 0;
  104.  
  105. if ( mySerial.overflow() ) { Serial.println("Overflow Checkpoint 1"); }
  106.  
  107.   if (getSerialString() ) {
  108.     //Serial.println("123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890");
  109.  
  110.     Serial.println(dataBuffer);
  111.    
  112.     //Serial.print("\t\t\t[");
  113.     //Serial.print(strlen(dataBuffer));
  114.     //Serial.println("]");  
  115.     // Make a copy of dataBuffer
  116.     memcpy(&dataBuffer2, &dataBuffer,strlen(dataBuffer));
  117.  
  118.     fragmentPackets(dataBuffer2,strlen(dataBuffer2) );
  119.  
  120. if ( mySerial.overflow() ) { Serial.println("Overflow Checkpoint 2"); }
  121.  
  122.     sendPackets(STR1,STR2,STR3);
  123.    
  124. if ( mySerial.overflow() ) { Serial.println("Overflow Checkpoint 3"); }  
  125.  
  126.  }  
  127. }
  128.  
  129.  
  130. boolean getSerialString(){
  131.     static byte dataBufferIndex = 0;
  132.     while(mySerial.available()>0){
  133.         char incomingbyte = mySerial.read();
  134.         if(incomingbyte==startChar){
  135.             dataBufferIndex = 0;  //Initialize our dataBufferIndex variable
  136.             storeString = true;
  137.         }
  138.         if(storeString){
  139.             //Let's check our index here, and abort if we're outside our buffer size
  140.             //We use our define here so our buffer size can be easily modified
  141.             if(dataBufferIndex==DATABUFFERSIZE){
  142.                 //Oops, our index is pointing to an array element outside our buffer.
  143.                 dataBufferIndex = 0;
  144.                 break;
  145.             }
  146.             if(incomingbyte==endChar){
  147.                 dataBuffer[dataBufferIndex] = 0; //null terminate the C string
  148.                 //Our data string is complete.  return true
  149.                 return true;
  150.             }
  151.             else{
  152.                 dataBuffer[dataBufferIndex++] = incomingbyte;
  153.                 dataBuffer[dataBufferIndex] = 0; //null terminate the C string
  154.             }
  155.         }
  156.         else{    
  157.         }
  158.     }
  159.     //We've read in all the available Serial data, and don't have a valid string yet, so return false
  160.     return false;
  161. }
  162.  
  163.  
  164. void fragmentPackets(char Buffer2[DATABUFFERSIZE],int len) {
  165. // Two headers with 30 bytes payload
  166. // 1st byte
  167. //      8 bit - fragment, 1 or 0
  168. // 7 to 5 bit - number of fragment, 111, 7 max fragment
  169. //      4 bit - none
  170. // 1 to 3 bit - fragment number, starting with 0 to 7, 000 to 111
  171. //
  172. // 2nd byte, reserved
  173.  
  174. /*
  175. Serial.print("[");
  176. Serial.print(len);
  177. Serial.print("] ");
  178. Serial.print("Buffer2:");
  179. Serial.println(Buffer2);
  180. */
  181.  
  182.    STR1[0], STR2[0], STR3[0] = 0;
  183.    
  184.    if ( len < 31 ) {
  185.      // for ( int i=0;i<len;i++) {
  186.      //   STR1[i+2] = Buffer2[i]; }
  187.        
  188.      STR1[0] = B00010000;    // No fragmentation, 1 segment
  189.      STR1[1] = 0xff;         // Reversed header  
  190.      STR1[2] = 0;            // null terminate string
  191.      strcat(STR1,Buffer2);
  192.        
  193.    } else if ( len < 61 ) {
  194.       //for ( int i=0;i<30;i++) {
  195.       //  STR1[i+2] = Buffer2[i]; }
  196.        
  197.         STR1[0] = 0xA0; // fragmentation, 1010 0000, fragment bit, 2 fragments, fragment no. 0
  198.         STR1[1] = 0xff; // Reverse header          
  199.         STR1[2] = 0;     // null terminate string
  200.         strncat(STR1, Buffer2,30);
  201.        
  202.       //for ( int i=0;i<len-30;i++) {
  203.       //  STR2[i+2] = Buffer2[i+30]; }
  204.        
  205.         STR2[0] = 0xA1; // fragmentation, 1010 0001, fragment bit, 2 fragments, fragment no. 1
  206.         STR2[1] = 0xff; // Reverse header            
  207.         STR2[2] = 0;     // null terminate string, 32 bytes + 2 extra bytes
  208.         strcat(STR2, &Buffer2[30]);    // Copy the balance of pointer address 30 and above
  209.        
  210.    } else if ( len < 91 )  {  
  211.       //for ( int i=0;i<30;i++) {
  212.       //  STR1[i+2] = Buffer2[i]; }
  213.        
  214.         STR1[0] = 0xB0; // fragmentation, 1011 0000, fragment bit, 3 fragments, fragment no. 0
  215.         STR1[1] = 0xff; // Reverse header          
  216.         STR1[2] = 0;     // null terminate string  
  217.         strncat(STR1, Buffer2,30);    // Copy the 1st 30 bytes to STR1
  218.        
  219.       //for ( int i=0;i<30;i++) {
  220.       //  STR2[i+2] = Buffer2[i+30]; }  
  221.        
  222.         STR2[0] = 0xB1; // fragmentation, 1011 0001, fragment bit, 3 fragments, fragment no. 1
  223.         STR2[1] = 0xff; // Reverse header  
  224.         STR2[2] = 0;     // null terminate string
  225.         strncat(STR2, &Buffer2[30],30);  // Copy the next 30 bytes to STR2
  226.        
  227.       //for ( int i=0;i<len-60;i++) {
  228.       //  STR3[i+2] = Buffer2[i+60]; }
  229.        
  230.         STR3[0] = 0xB2; // fragmentation, 1011 0011, fragment bit, 3 fragments, fragment no. 2
  231.         STR3[1] = 0xff; // Reverse header        
  232.         STR3[2] = 0;     // null terminate string      
  233.         strcat(STR3, &Buffer2[60]);        
  234.        
  235.       } else {
  236.       // payload > 90 bytes, discard or add more fragments  
  237.    } // end if
  238.  
  239. /*
  240. Serial.print("\tSTR1:");
  241. Serial.println(STR1);
  242. Serial.print("\tSTR2:");
  243. Serial.println(STR2);
  244. Serial.print("\tSTR3:");
  245. Serial.println(STR3);
  246. */
  247. }  // end fragmentPackets
  248.  
  249.  
  250. void sendPackets(char TMP1[33], char TMP2[33], char TMP3[33]) {
  251.  
  252. //Serial.print("\tTMP1:");
  253. //Serial.println(TMP1);
  254. //Serial.print("\tTMP2:");
  255. //Serial.println(TMP2);
  256. //Serial.print("\tTMP3:");
  257. //Serial.println(TMP3);
  258.  
  259.   int status = (STR1[0] & B00110000)>>4;
  260.      
  261.   radio.stopListening();
  262.  
  263.   // Mask the 1st Byte, bits 5 & 6 and shift 4 places to right
  264.   // Return 0, 2 or 3 fragments
  265.  
  266.   switch (status) {
  267.     case 1:
  268.         radio.startWrite( TMP1,strlen(TMP1) );
  269.         //delay(1);
  270.       break;
  271.     case 2:
  272.         radio.startWrite( TMP1,strlen(TMP1) );
  273.         //delay(1);
  274.         radio.startWrite( TMP2,strlen(TMP2) );    
  275.         //delay(1);    
  276.       break;
  277.     case 3:
  278.        radio.startWrite(TMP1,strlen(TMP1) );
  279.         //delay(1);
  280.         radio.startWrite(TMP2,strlen(TMP2) );
  281.         //delay(1);
  282.         radio.startWrite(TMP3,strlen(TMP3) );
  283.         //delay(1);
  284.        break;
  285.     } // switch/case  end
  286.    
  287.     radio.startListening();
  288.  
  289. } // nrf_Send end
  290.  
  291.  
  292.  
  293. // Receive payload from nRF at remote end
  294.  
  295. void nRF_Receive(){
  296.  
  297.   uint8_t len=0, mergeA=0, mergeB=0, Head1 = 0;
  298.   // Call startlistening everytime here
  299.    
  300.   if ( radio.available() ) {
  301.     len = radio.getDynamicPayloadSize();
  302.     radio.read( &RXPayload, len );
  303.    
  304.     RXPayload[len] = 0;
  305.  
  306. //Serial.print("nRF RXPayload:");
  307. //Serial.print(RXPayload);  
  308. //Serial.print(" [");
  309. //Serial.print(len);
  310. //Serial.print("]");    
  311. Head1 = RXPayload[0] & 0xff;
  312. //Serial.print(" Head1:");
  313. //Serial.println(Head1,HEX);
  314.  
  315.     if ( Head1 == 0x00 ) {
  316.      // No fragment, strip off first 2 header bytes
  317.         STR1[0] = 0;
  318.         memcpy(&STR1,&RXPayload[2],len-2 );  
  319.         STR1[len-2]=0;
  320.        
  321.         sprintf(dataBuffer,"%s",STR1);
  322.     } else if ( Head1 == 0xA0 ) {
  323.      // Fragment packets, with 2 fragments
  324.         STR1[0] = 0;
  325.         memcpy(&STR1, &RXPayload[2],30);
  326.  
  327.         //for ( int i=0;i<30;i++ ) {
  328.         //  STR1[i] = RXPayload[i+2]; }
  329.         STR1[30]=0;
  330.         mergeA=0;
  331. //Serial.print("STR1:");
  332. //Serial.println(STR1);
  333.         sprintf(dataBuffer,"%s",STR1);
  334.    } else if ( Head1 == 0xA1 ) {
  335.      // Fragment packets, with 2 fragments
  336.         STR2[0] = 0;
  337.         memcpy(&STR2, &RXPayload[2],len-2 );
  338.  
  339.         STR2[len-2]=0;
  340. //Serial.print("STR2:");
  341. //Serial.println(STR2);        
  342.         mergeA=1;
  343.         sprintf(dataBuffer,"%s%s",dataBuffer,STR2);
  344.     } else if ( Head1 == 0xB0 ) {
  345.      // Fragment packets, with 3 fragments    
  346.          STR1[0] = 0;
  347.          memcpy(&STR1, &RXPayload[2],30);
  348.        
  349.          STR1[30]=0;
  350.          mergeB=0;  
  351. //Serial.print("STR1:");
  352. //Serial.println(STR1);
  353.  
  354.     } else if ( Head1 == 0xB1 ) {
  355.      // Fragment packets, with 3 fragments    
  356.         STR2[0] = 0;
  357.         memcpy(&STR2, &RXPayload[2],30);  
  358.  
  359.         STR2[30]=0;        
  360.         mergeB=0;
  361. //Serial.print("STR2:");
  362. //Serial.println(STR2);
  363.         sprintf(dataBuffer,"%s%s",STR1,STR2);
  364.  
  365.     } else if ( Head1 == 0xB2 ) {
  366.      // Fragment packets, with 3 fragments    
  367.          STR3[0] = 0;
  368.         memcpy(&STR3, &RXPayload[2],len-2 );  
  369.  
  370.         STR2[len-2]=0;        
  371.         mergeB=1;
  372. //Serial.print("STR3:");
  373. //Serial.println(STR3);
  374.         sprintf(dataBuffer,"%s%s",dataBuffer,STR3);
  375.  
  376.     } else {
  377.       // Invalid payload or error payloads
  378.       //Serial.println("Invalid Packets");
  379.       dataBuffer[0] = 0;
  380.     }  
  381. } // end radio.available if
  382.    
  383.        
  384.     // Print merge payload to Serial Monitor
  385.     if ( (dataBuffer[0] == '$') && ( mergeA || mergeB) ) {
  386. //Serial.print("nRF RECV     :");
  387.       Serial.println(dataBuffer);
  388. //Serial.print(" [");
  389. //Serial.print(strlen(dataBuffer));
  390. //Serial.println("]");
  391.     // Reset the merge
  392.     mergeA, mergeB = 0;
  393.     }
  394.    
  395. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement