SHARE
TWEET

Untitled

JasminB Oct 16th, 2016 256 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 {0xFE, 0x4C, 0xA6, 0xE5} // Address of this device (4 bytes)
  5. #define TXADDR {0x58, 0x6F, 0x2E, 0x10} // Address of device to send to (4 bytes)
  6.  
  7. String ENQ = "0x05";
  8. String ACK = "0x06";
  9. String SOH = "0x01";
  10. String SA = "_EH1";
  11. String DA = "_EC1";
  12. String PW = "_tenAaie";
  13. String data = "PB URGENT!!!";
  14. String CRC = "CRC";
  15. String EOT = "0x04";
  16. String okF = "0x120";
  17. String reply;
  18. String frame;
  19.  
  20. void setup() {
  21.   Serial.begin(38400);
  22.   nRF905_init();
  23.   byte addr[] = RXADDR;
  24.   nRF905_setRXAddress(addr);
  25.   byte addr1[] = TXADDR;
  26.   nRF905_setTXAddress(addr1);
  27.   nRF905_receive();
  28. }
  29.  
  30. void loop() {
  31.  
  32.   //SEND ENQ
  33.   byte replyH[50] = {0};
  34.   ENQ.getBytes(replyH, 50);
  35.   nRF905_setData(replyH, sizeof(replyH));
  36.   while (!nRF905_send());
  37.   nRF905_receive();
  38.   Serial.println("ENQ SENT");
  39.   Serial.write(replyH, sizeof(replyH));
  40.   Serial.println();
  41.  
  42.   ///WAIT FOR reply (receive)
  43.   reply = "\0";
  44.   Serial.println("WAITING FOR REPLY");
  45.   byte buffer[50] = {0};
  46.   while (!nRF905_getData(buffer, sizeof(buffer)));
  47.   nRF905_setData(buffer, sizeof(buffer));
  48.   while (!nRF905_send());
  49.   nRF905_receive();
  50.   Serial.print(F("Data: "));
  51.   Serial.write(buffer, sizeof(buffer));
  52.   Serial.println();
  53.  
  54.   for (int i = 0; i < sizeof(buffer); i++) {
  55.     if ((buffer[i] > 0) && (buffer[i] < 127) && (buffer[i] != 32))
  56.       reply = reply + char(buffer[i]);
  57.   }
  58.  
  59.   Serial.println(reply);
  60.  
  61.   if (reply == ACK) {
  62.     Serial.println("YEY ACKNOWLEDGE!");
  63.     frame = SOH + DA + SA + PW + data  + CRC + EOT;
  64.  
  65. frameLoop:
  66.  
  67.     //SEND ENQ
  68.     delay(1000);
  69.     byte replyH[50] = {0};
  70.     frame.getBytes(replyH, 50);
  71.     nRF905_setData(replyH, sizeof(replyH));
  72.     while (!nRF905_send());
  73.     nRF905_receive();
  74.     Serial.println("FRAME SENT");
  75.     Serial.write(replyH, sizeof(replyH));
  76.     Serial.println();
  77.  
  78.     ///WAIT FOR REPLY (receive)
  79.     reply = "\0";
  80.     Serial.println("WAITING FOR REPLY");
  81.     byte buffer[50] = {0};
  82.     while (!nRF905_getData(buffer, sizeof(buffer)));
  83.     nRF905_setData(buffer, sizeof(buffer));
  84.     while (!nRF905_send());
  85.     nRF905_receive();
  86.     Serial.print(F("Data: "));
  87.     Serial.write(buffer, sizeof(buffer));
  88.     Serial.println();
  89.  
  90.     for (int i = 0; i < sizeof(buffer); i++) {
  91.       if ((buffer[i] > 0) && (buffer[i] < 127) && (buffer[i] != 32))
  92.         reply = reply + char(buffer[i]);
  93.     }
  94.  
  95.     Serial.println(reply);
  96.  
  97.     if (reply == okF) {
  98.       Serial.println("YEY THE FRAME HAS BEEN RECEIVED!");
  99.     }
  100.     else {
  101.       Serial.println("UGHHH NEED TO RESEND FRAME");
  102.       goto frameLoop;
  103.     }
  104.   }
  105.   else {
  106.     Serial.println("ugh");
  107.   }
  108.  
  109.   delay(1000);
  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