Irfandy

Server

Jul 20th, 2014
484
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. * Wait for data and reply.
  11. * Output power is set to the lowest setting, receive sensitivity is lowered.
  12. *
  13. * 7 -> CE
  14. * 8 -> PWR
  15. * 9 -> TXE
  16. * 2 -> CD
  17. * 3 -> DR
  18. * 10 -> CSN
  19. * 12 -> SO
  20. * 11 -> SI
  21. * 13 -> SCK
  22. */
  23.  
  24. #include <nRF905.h>
  25. #include <SPI.h>
  26.  
  27. #define RXADDR {0x58, 0x6F, 0x2E, 0x10} // Address of this device (4 bytes)
  28. #define TXADDR {0xFE, 0x4C, 0xA6, 0xE5} // Address of device to send to (4 bytes)
  29.  
  30. void setup()
  31. {
  32. // Start up
  33. nRF905_init();
  34.  
  35. // Set address of this device
  36. byte addr[] = RXADDR;
  37. nRF905_setRXAddress(addr);
  38.  
  39. // Lowest transmit level -10db
  40. nRF905_setTransmitPower(NRF905_PWR_n10);
  41.  
  42. // Reduce receive sensitivity to save a few mA
  43. nRF905_setLowRxPower(NRF905_LOW_RX_ENABLE);
  44.  
  45. // Put into receive mode
  46. nRF905_receive();
  47.  
  48. Serial.begin(9600);
  49.  
  50. Serial.println(F("Server started"));
  51. }
  52.  
  53. void loop()
  54. {
  55. Serial.println(F("Waiting for ping..."));
  56.  
  57. // Make buffer for data
  58. char buffer[NRF905_MAX_PAYLOAD];
  59.  
  60. // Wait for data
  61. while(!nRF905_getData(buffer, sizeof(buffer)));
  62.  
  63. Serial.println(F("Got ping"));
  64.  
  65. // Set address of device to send to
  66. byte addr[] = TXADDR;
  67. nRF905_setTXAddress(addr);
  68.  
  69. // Set payload data (reply with data received)
  70. nRF905_setData(buffer, sizeof(buffer));
  71.  
  72. Serial.println(F("Sending reply..."));
  73.  
  74. // Send payload (send fails if other transmissions are going on, keep trying until success)
  75. while(!nRF905_send());
  76.  
  77. // Put back into receive mode
  78. nRF905_receive();
  79.  
  80. Serial.println(F("Reply sent"));
  81.  
  82. // Printout ping contents
  83. Serial.print(F("Data: "));
  84. nRF905_getData(buffer, sizeof(buffer));
  85.  
  86.  
  87. if(strcmp((char*)buffer, "test test") == 0)
  88.  
  89. Serial.println("OK");
  90.  
  91. else
  92.  
  93. Serial.println("Fail");
  94. }
RAW Paste Data