ping_server.pde 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * An Mirf example which copies back the data it recives.
  3. *
  4. * Pins:
  5. * Hardware SPI:
  6. * MISO -> 12
  7. * MOSI -> 11
  8. * SCK -> 13
  9. *
  10. * Configurable:
  11. * CE -> 8
  12. * CSN -> 7
  13. *
  14. */
  15. #include <SPI.h>
  16. #include <Mirf.h>
  17. #include <nRF24L01.h>
  18. #include <MirfHardwareSpiDriver.h>
  19. void setup(){
  20. Serial.begin(9600);
  21. /*
  22. * Set the SPI Driver.
  23. */
  24. Mirf.spi = &MirfHardwareSpi;
  25. /*
  26. * Setup pins / SPI.
  27. */
  28. Mirf.init();
  29. /*
  30. * Configure reciving address.
  31. */
  32. Mirf.setRADDR((byte *)"serv1");
  33. /*
  34. * Set the payload length to sizeof(unsigned long) the
  35. * return type of millis().
  36. *
  37. * NB: payload on client and server must be the same.
  38. */
  39. Mirf.payload = sizeof(unsigned long);
  40. /*
  41. * Write channel and payload config then power up reciver.
  42. */
  43. Mirf.config();
  44. Serial.println("Listening...");
  45. }
  46. void loop(){
  47. /*
  48. * A buffer to store the data.
  49. */
  50. byte data[Mirf.payload];
  51. /*
  52. * If a packet has been recived.
  53. *
  54. * isSending also restores listening mode when it
  55. * transitions from true to false.
  56. */
  57. if(!Mirf.isSending() && Mirf.dataReady()){
  58. Serial.println("Got packet");
  59. /*
  60. * Get load the packet into the buffer.
  61. */
  62. Mirf.getData(data);
  63. /*
  64. * Set the send address.
  65. */
  66. Mirf.setTADDR((byte *)"clie1");
  67. /*
  68. * Send the data back to the client.
  69. */
  70. Mirf.send(data);
  71. /*
  72. * Wait untill sending has finished
  73. *
  74. * NB: isSending returns the chip to receving after returning true.
  75. */
  76. Serial.println("Reply sent.");
  77. }
  78. }