Guest User

Untitled

a guest
Jul 23rd, 2014
65
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. * Project: nRF905 AVR/Arduino Library/Driver
  3. * Author: Zak Kemble, contact@zakkemble.co.uk
  4. * Copyright: (C) 2013 by Zak Kemble
  5. * License: GNU GPL v3 (see License.txt)
  6. * Web: http://blog.zakkemble.co.uk/nrf905-avrarduino-librarydriver/
  7. */
  8.  
  9. /*
  10. * Turn the nRF905 on to transmit some data, wait for a reply,
  11. * turn off and wait for a second.
  12. * Output power is set to the lowest setting, receive sensitivity is
  13. * lowered and uses the power up/down feature of the nRF905.
  14. *
  15. * 7 -> CE
  16. * 8 -> PWR
  17. * 9 -> TXE
  18. * 2 -> CD
  19. * 3 -> DR
  20. * 10 -> CSN
  21. * 12 -> SO
  22. * 11 -> SI
  23. * 13 -> SCK
  24. */
  25.  
  26. #include <nRF905.h>
  27. #include <SPI.h>
  28.  
  29. #define TIMEOUT 1000 // 1 second ping timeout
  30.  
  31. void setup()
  32. {
  33. // Start up
  34. nRF905_init();
  35.  
  36. // Put into receive mode
  37. nRF905_receive();
  38.  
  39. Serial.begin(9600);
  40.  
  41. Serial.println(F("Client started"));
  42. }
  43.  
  44. void loop()
  45. {
  46. // Make data
  47. char data[NRF905_MAX_PAYLOAD] = "test test";
  48.  
  49. unsigned long startTime = millis();
  50.  
  51. // Set payload data
  52. nRF905_setData(data, sizeof(data));
  53.  
  54. // Send payload (send fails if other transmissions are going on, keep trying until success)
  55. while(!nRF905_send());
  56.  
  57. // Put into receive mode
  58. nRF905_receive();
  59.  
  60. // Make buffer for reply
  61. char buffer[NRF905_MAX_PAYLOAD];
  62. bool success;
  63.  
  64. // Wait for reply with timeout
  65. unsigned long sendStartTime = millis();
  66. while(1)
  67. {
  68. success = nRF905_getData(buffer, sizeof(buffer));
  69. if(success)// Got data
  70. break;
  71.  
  72. // Timeout
  73. if(millis() - sendStartTime > TIMEOUT)
  74. break;
  75. }
  76.  
  77. if(success)
  78. {
  79. unsigned int totalTime = millis() - startTime;
  80. Serial.print(F("Ping time: "));
  81. Serial.print(totalTime);
  82. Serial.println(F("ms"));
  83.  
  84. // Show raw data
  85. Serial.print(F("Raw: "));
  86. Serial.write((byte*)buffer, sizeof(buffer));
  87. Serial.println();
  88.  
  89. // Printout ping contents
  90. Serial.print(F("Data from server: "));
  91. if(strncmp(buffer, "test test", sizeof(buffer)) == 0)
  92. Serial.println("OK");
  93. else
  94. Serial.println("Fail");
  95. }
  96. else
  97. Serial.println(F("Ping timed out"));
  98.  
  99. delay(1000);
  100. }
RAW Paste Data