ping_client.pde 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * A Mirf example to test the latency between two Ardunio.
  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. * Note: To see best case latency comment out all Serial.println
  15. * statements not displaying the result and load
  16. * 'ping_server_interupt' on the server.
  17. */
  18. #include <SPI.h>
  19. #include <Mirf.h>
  20. #include <nRF24L01.h>
  21. #include <MirfHardwareSpiDriver.h>
  22. void setup(){
  23. Serial.begin(9600);
  24. /*
  25. * Setup pins / SPI.
  26. */
  27. /* To change CE / CSN Pins:
  28. *
  29. * Mirf.csnPin = 9;
  30. * Mirf.cePin = 7;
  31. */
  32. /*
  33. Mirf.cePin = 7;
  34. Mirf.csnPin = 8;
  35. */
  36. Mirf.spi = &MirfHardwareSpi;
  37. Mirf.init();
  38. /*
  39. * Configure reciving address.
  40. */
  41. Mirf.setRADDR((byte *)"clie1");
  42. /*
  43. * Set the payload length to sizeof(unsigned long) the
  44. * return type of millis().
  45. *
  46. * NB: payload on client and server must be the same.
  47. */
  48. Mirf.payload = sizeof(unsigned long);
  49. /*
  50. * Write channel and payload config then power up reciver.
  51. */
  52. /*
  53. * To change channel:
  54. *
  55. * Mirf.channel = 10;
  56. *
  57. * NB: Make sure channel is legal in your area.
  58. */
  59. Mirf.config();
  60. Serial.println("Beginning ... ");
  61. }
  62. void loop(){
  63. unsigned long time = millis();
  64. Mirf.setTADDR((byte *)"serv1");
  65. Mirf.send((byte *)&time);
  66. while(Mirf.isSending()){
  67. }
  68. Serial.println("Finished sending");
  69. delay(10);
  70. while(!Mirf.dataReady()){
  71. //Serial.println("Waiting");
  72. if ( ( millis() - time ) > 1000 ) {
  73. Serial.println("Timeout on response from server!");
  74. return;
  75. }
  76. }
  77. Mirf.getData((byte *) &time);
  78. Serial.print("Ping: ");
  79. Serial.println((millis() - time));
  80. delay(1000);
  81. }