ping_server_interupt.pde 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /**
  2. * An Mirf example which copies back the data it recives.
  3. * While wating the arduino goes to sleep and will be woken up
  4. * by the interupt pin of the mirf.
  5. *
  6. * Warning: Due to the sleep mode the Serial output donsn't work.
  7. *
  8. * Pins:
  9. * Hardware SPI:
  10. * MISO -> 12
  11. * MOSI -> 11
  12. * SCK -> 13
  13. *
  14. * Configurable:
  15. * CE -> 8
  16. * CSN -> 7
  17. */
  18. #include <SPI.h>
  19. #include <Mirf.h>
  20. #include <nRF24L01.h>
  21. #include <MirfHardwareSpiDriver.h>
  22. #include <avr/sleep.h>
  23. void wakeupFunction(){
  24. }
  25. void toSleep(){
  26. attachInterrupt(0,wakeupFunction,LOW);
  27. sleep_mode();
  28. detachInterrupt(0);
  29. }
  30. void setup(){
  31. Serial.begin(9600);
  32. /*
  33. * Set the SPI Driver.
  34. */
  35. Mirf.spi = &MirfHardwareSpi;
  36. /*
  37. * Setup pins / SPI.
  38. */
  39. Mirf.init();
  40. /*
  41. * Configure reciving address.
  42. */
  43. Mirf.setRADDR((byte *)"serv1");
  44. /*
  45. * Set the payload length to sizeof(unsigned long) the
  46. * return type of millis().
  47. *
  48. * NB: payload on client and server must be the same.
  49. */
  50. Mirf.payload = sizeof(unsigned long);
  51. /*
  52. * Write channel and payload config then power up reciver.
  53. */
  54. Mirf.config();
  55. /*
  56. * Configure seep mode to save power.
  57. */
  58. set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  59. sleep_enable();
  60. Serial.println("Listening...");
  61. }
  62. void loop(){
  63. /*
  64. * A buffer to store the data.
  65. */
  66. byte data[Mirf.payload];
  67. /*
  68. * If a packet has been recived.
  69. */
  70. if(!Mirf.isSending() && Mirf.dataReady()){
  71. /*
  72. * Get load the packet into the buffer.
  73. */
  74. Mirf.getData(data);
  75. /*
  76. * Set the send address.
  77. */
  78. Mirf.setTADDR((byte *)"clie1");
  79. /*
  80. * Send the data back to the client.
  81. */
  82. Mirf.send(data);
  83. }else{
  84. /* No data - night night. */
  85. toSleep();
  86. }
  87. }