Advertisement
stanleyseow

Arduino nRF to Serial rev003

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