Guest User

Untitled

a guest
Jul 22nd, 2014
71
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 RXADDR {0xFE, 0x4C, 0xA6, 0xE5} // Address of this device (4 bytes)
  30. #define TXADDR {0x58, 0x6F, 0x2E, 0x10} // Address of device to send to (4 bytes)
  31.  
  32. #define TIMEOUT 1000 // 1 second ping timeout
  33.  
  34. void setup()
  35. {
  36. // Start up
  37. nRF905_init();
  38.  
  39. // Set address of this device
  40. byte addr[] = RXADDR;
  41. nRF905_setRXAddress(addr);
  42.  
  43. // Lowest transmit level -10db
  44. nRF905_setTransmitPower(NRF905_PWR_n10);
  45.  
  46. // Reduce receive sensitivity to save a few mA
  47. nRF905_setLowRxPower(NRF905_LOW_RX_ENABLE);
  48.  
  49. // Put into receive mode
  50. nRF905_receive();
  51.  
  52. Serial.begin(9600);
  53.  
  54. Serial.println(F("Client started"));
  55. }
  56.  
  57. void loop()
  58. {
  59. static byte counter;
  60.  
  61. // Make data
  62. char data[NRF905_MAX_PAYLOAD] = "test test";
  63.  
  64. // Turn on module
  65. nRF905_powerUp();
  66.  
  67. unsigned long startTime = millis();
  68.  
  69. // Set address of device to send to
  70. byte addr[] = TXADDR;
  71. nRF905_setTXAddress(addr);
  72.  
  73. // Set payload data
  74. nRF905_setData(data, sizeof(data));
  75.  
  76. // Send payload (send fails if other transmissions are going on, keep trying until success)
  77. while(!nRF905_send());
  78.  
  79. // Put into receive mode
  80. nRF905_receive();
  81.  
  82. // Make buffer for reply
  83. char buffer[NRF905_MAX_PAYLOAD];
  84. bool success;
  85.  
  86. // Wait for reply with timeout
  87. unsigned long sendStartTime = millis();
  88. while(1)
  89. {
  90. success = nRF905_getData(buffer, sizeof(buffer));
  91. if(success)// Got data
  92. break;
  93.  
  94. // Timeout
  95. if(millis() - sendStartTime > TIMEOUT)
  96. break;
  97. }
  98.  
  99. // Turn off module
  100. nRF905_powerDown();
  101.  
  102. if(success)
  103. {
  104. unsigned int totalTime = millis() - startTime;
  105. Serial.print(F("Ping time: "));
  106. Serial.print(totalTime);
  107. Serial.println(F("ms"));
  108.  
  109. // Show raw data
  110. Serial.print(F("Raw: "));
  111. Serial.write((byte*)buffer, sizeof(buffer));
  112. Serial.println();
  113.  
  114. // Printout ping contents
  115. Serial.print(F("Data from server: "));
  116. if(strncmp(buffer, "test test", sizeof(buffer)) == 0)
  117. Serial.println("OK");
  118. else
  119. Serial.println("Fail");
  120. }
  121. else
  122. Serial.println(F("Ping timed out"));
  123.  
  124. delay(1000);
  125. }
RAW Paste Data