Advertisement
Guest User

Untitled

a guest
Sep 10th, 2021
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.47 KB | None | 0 0
  1. /*
  2. Project: nRF905 Radio Library for Arduino (Low power sensor node example)
  3. Author: Zak Kemble, contact@zakkemble.net
  4. Copyright: (C) 2020 by Zak Kemble
  5. License: GNU GPL v3 (see License.txt)
  6. Web: https://blog.zakkemble.net/nrf905-avrarduino-librarydriver/
  7. */
  8.  
  9. // This examples requires the low power library from https://github.com/rocketscream/Low-Power
  10.  
  11. // This examples configures the nRF905 library to only use 5 connections:
  12. // MOSI
  13. // MISO
  14. // SCK
  15. // SS -> 6
  16. // PWR -> 8
  17.  
  18. // The following pins on the nRF905 must be connected to VCC (3.3V) or GND:
  19. // CE (TRX_EN) -> VCC
  20. // TXE (TX_EN) -> VCC
  21.  
  22. #include <nRF905.h>
  23. #include <SPI.h>
  24. #include <LowPower.h>
  25.  
  26. byte mess_Address = {0xB54CAB34};
  27. byte Message[] = {0b00000000, 0b10000001, 0b10000010, 0b10000011, 0b10000100, 0b10000101, 0b10000110, 0b10000111, 0b10001000, 0b10011000, 0b10101000, 0b10111000, 0b11001000, 0b11011000, 0b11101000, 0b11111001, 0b00101001, 0b00111001, 0b01011001, 0b01101001, 0b01111001, 0b10011010, 0b10011011, 0b10011101, 0b10011110, 0b10011111, 0b10101010, 0b11101011, 0b01101011, 0b11101101, 0b11101110, 0b11111111};
  28. const int buttonPin = 2;
  29. int buttonState = 0;
  30.  
  31. nRF905 transceiver = nRF905();
  32.  
  33. static bool txDone; // NOTE: In polling mode this does not need to be volatile
  34.  
  35. // Event function for TX completion
  36. void nRF905_onTxComplete(nRF905* device)
  37. {
  38. txDone = true;
  39. }
  40.  
  41. void setup()
  42. {
  43. Serial.begin(115200);
  44. Serial.println(F("Sensor node starting..."));
  45.  
  46. pinMode(buttonPin, INPUT);
  47. // standby off TODO
  48. //pinMode(7, OUTPUT);
  49. //digitalWrite(7, HIGH);
  50. // pwr
  51. //pinMode(8, OUTPUT);
  52. //digitalWrite(8, HIGH);
  53. // trx
  54. //pinMode(9, OUTPUT);
  55. //digitalWrite(9, HIGH);
  56.  
  57.  
  58. // This must be called first
  59. SPI.begin();
  60. SPI.setClockDivider(SPI_CLOCK_DIV128);
  61. // Minimal wires (polling mode)
  62. // Up to 5 wires can be disconnected, however this will reduce functionalliy and will put the library into polling mode instead of interrupt mode.
  63. // In polling mode the .poll() method must be called as often as possible. If .poll() is not called often enough then events may be missed.
  64. transceiver.begin(
  65. SPI,
  66. 125000,
  67. 6,
  68. NRF905_PIN_UNUSED, // CE (standby) pin must be connected to VCC (3.3V)
  69. NRF905_PIN_UNUSED, // TRX (RX/TX mode) pin must be connected to VCC (3.3V) (force TX mode)
  70. 8, // PWR
  71. NRF905_PIN_UNUSED, // Without the CD pin collision avoidance will be disabled
  72. NRF905_PIN_UNUSED, // Without the DR pin the library will run in polling mode and poll the status register over SPI. This also means the nRF905 can not wake the MCU up from sleep mode
  73. NRF905_PIN_UNUSED, // Without the AM pin the library must poll the status register over SPI.
  74. NULL, // Running in polling mode so no interrupt function
  75. NULL // Running in polling mode so no interrupt function
  76. );
  77.  
  78. // Register event functions
  79. transceiver.events(
  80. NULL,
  81. NULL,
  82. nRF905_onTxComplete,
  83. NULL
  84. );
  85.  
  86. // Low-mid transmit level -2dBm (631uW)
  87. transceiver.setTransmitPower(NRF905_PWR_10);
  88. transceiver.setChannel(106);
  89. transceiver.setBand(NRF905_BAND_433);
  90. transceiver.setPayloadSize(32, 0); // Will transmit 5 byte payloads, receive 32 byte payloads
  91. transceiver.setAddressSize(4, 4);
  92.  
  93. Serial.println(F("Transciever Started"));
  94. Serial.println(F("Waiting for input"));
  95. }
  96.  
  97. void loop()
  98. {
  99. buttonState = digitalRead(buttonPin);
  100. // Write data to radio
  101. transceiver.write(mess_Address, Message, sizeof(Message));
  102.  
  103. txDone = false;
  104.  
  105. if (buttonState == HIGH) {
  106. // This will power-up the radio and send the data
  107. transceiver.TX(NRF905_NEXTMODE_STANDBY, false);
  108.  
  109. // Transmission will take approx 6-7ms to complete (smaller paylaod sizes will be faster)
  110. while (!txDone) {
  111. transceiver.poll();
  112. delay(1);
  113. }
  114. // Transmission done, power-down the radio
  115. //transceiver.powerDown();
  116. Serial.println(F("Transciever complete"));
  117.  
  118. // NOTE:
  119. // After the payload has been sent the radio will continue to transmit an empty carrier wave until .powerDown() is called. Since this is a sensor node that doesn't need to receive data, only transmit and go into low power mode, this example hard wires the radio into TX mode (TXE connected to VCC) to reduce number of connections to the Arduino.
  120. // TODO
  121. // Sleep for 64 seconds
  122. //uint8_t sleepCounter = 8;
  123. //while(sleepCounter--)
  124. // Sleep for 1 second
  125. LowPower.powerDown(SLEEP_1S, ADC_OFF, BOD_OFF);
  126. }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement