SHARE
TWEET

Untitled

JasminB Oct 16th, 2016 184 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <nRF905.h>
  2. #include <SPI.h>
  3.  
  4. #define RXADDR {0x58, 0x6F, 0x2E, 0x10} // Address of this device (4 bytes)
  5. #define TXADDR {0xFE, 0x4C, 0xA6, 0xE5} // Address of device to send to (4 bytes)
  6.  
  7. String ENQ = "0x05";
  8. String ACK = "0x06";
  9. String reply;
  10. String frame;
  11. String okF = "0x120" ;
  12. String SOH = "0x01";
  13. String EOT = "0x04";
  14.  
  15. void setup() {
  16.   nRF905_init();
  17.   byte addr[] = RXADDR;
  18.   nRF905_setRXAddress(addr);
  19.   byte addr1[] = TXADDR;
  20.   nRF905_setTXAddress(addr1);
  21.   nRF905_receive();
  22.   Serial.begin(38400);
  23. }
  24.  
  25. void loop() {
  26.  
  27.  
  28.   /******************WAIT FOR ENQUIRY (RECEIVE)*******************/
  29.   delay(100);
  30.   reply = "\0";
  31.   Serial.println("WAITING FOR ENQ");
  32.   byte buffer[50] = {0};
  33.   while (!nRF905_getData(buffer, sizeof(buffer)));
  34.   nRF905_setData(buffer, sizeof(buffer));
  35.   while (!nRF905_send());
  36.   nRF905_receive();
  37.   Serial.print(F("Data: "));
  38.   Serial.write(buffer, sizeof(buffer));
  39.   Serial.println();
  40.  
  41.   for (int i = 0; i < sizeof(buffer); i++) {
  42.     if ((buffer[i] > 0) && (buffer[i] < 127) && (buffer[i] != 32))
  43.       reply = reply + char(buffer[i]);
  44.   }
  45.  
  46.   Serial.println(reply);
  47.   delay(100);
  48.  
  49.   if (reply == ENQ) {
  50. ACKLoop:
  51.     /******************SEND ACK (TRANSMIT)*******************/
  52.     delay(100);
  53.     byte replyH[50] = {0};
  54.     ACK.getBytes(replyH, 50);
  55.     nRF905_setData(replyH, sizeof(replyH));
  56.     while (!nRF905_send());
  57.     nRF905_receive();
  58.     Serial.println("ACK SENT");
  59.     Serial.write(replyH, sizeof(replyH));
  60.     Serial.println();
  61.    
  62.     /*****************RECEIVE FRAME (RECEIVE)******************/
  63.     frame = "\0";
  64.     Serial.println("WAITING FOR FRAME");
  65.     byte buffer[50] = {0};
  66.     while (!nRF905_getData(buffer, sizeof(buffer)));
  67.     nRF905_setData(buffer, sizeof(buffer));
  68.     while (!nRF905_send());
  69.     nRF905_receive();
  70.     Serial.print(F("Data: "));
  71.     Serial.write(buffer, sizeof(buffer));
  72.     Serial.println();
  73.  
  74.     for (int i = 0; i < sizeof(buffer); i++) {
  75.       if ((buffer[i] > 0) && (buffer[i] < 127) && (buffer[i] != 32))
  76.         frame = frame + char(buffer[i]);
  77.     }
  78.  
  79.     Serial.println(frame);
  80.     Serial.println(frame.substring(0,4));
  81.    
  82.     /*****************SEND RECEIVE FRAME **********************/
  83.     delay(100);
  84.    
  85.     if ((frame.substring(0, 4) == SOH)&&(frame.substring(n-4, n) == EOT)) {
  86.       Serial.println("FRAME RECEIVED");
  87.       delay(100);
  88.       byte replyH[50] = {0};
  89.       okF.getBytes(replyH, 50);
  90.       nRF905_setData(replyH, sizeof(replyH));
  91.       while (!nRF905_send());
  92.       nRF905_receive();
  93.       Serial.println("ACK SENT");
  94.       Serial.write(replyH, sizeof(replyH));
  95.       Serial.println();
  96.     }
  97.     else {
  98.      
  99.     /*****************NOT RECEIVE FRAME **********************/
  100.    
  101.       Serial.println("FRAME NOT RECEIVED");
  102.       goto ACKLoop;
  103.     }
  104.  
  105.   }
  106.   else {
  107.     Serial.println("Not Received");
  108.  
  109.   }
  110. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top