Guest User

Untitled

a guest
Sep 10th, 2021
19
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. * Project: nRF905 Radio Library for Arduino (Low power node base station 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. // DR -> 3
  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) -> GND
  21. // PWR -> VCC
  22.  
  23. // The nRF905 will always be in low-power receive mode, waiting for packets from sensor nodes.
  24.  
  25. #include <nRF905.h>
  26. #include <SPI.h>
  27. #include <LowPower.h>
  28.  
  29. #define BASE_STATION_ADDR 0xB54CAB34
  30. #define PAYLOAD_SIZE 32
  31. #define LED A5
  32.  
  33. #define PACKET_NONE 0
  34. #define PACKET_OK 1
  35. #define PACKET_INVALID 2
  36.  
  37. nRF905 transceiver = nRF905();
  38.  
  39. static volatile uint8_t packetStatus; // NOTE: In interrupt mode this must be volatile
  40.  
  41. void nRF905_int_dr(){transceiver.interrupt_dr();}
  42.  
  43. void nRF905_onRxComplete(nRF905* device)
  44. {
  45. packetStatus = PACKET_OK;
  46. }
  47.  
  48. void nRF905_onRxInvalid(nRF905* device)
  49. {
  50. packetStatus = PACKET_INVALID;
  51. }
  52.  
  53. void setup()
  54. {
  55. Serial.begin(115200);
  56. Serial.println(F("Base station starting..."));
  57.  
  58. pinMode(LED, OUTPUT);
  59.  
  60.  
  61. // standby off TODO
  62. //pinMode(7, OUTPUT);
  63. //digitalWrite(7, HIGH);
  64. // pwr
  65. //pinMode(8, OUTPUT);
  66. //digitalWrite(8, HIGH);
  67. // trx
  68. //pinMode(9, OUTPUT);
  69. //digitalWrite(9, LOW);
  70.  
  71.  
  72. // This must be called first
  73. SPI.begin();
  74. SPI.setClockDivider(SPI_CLOCK_DIV128);
  75. // Minimal wires (interrupt mode)
  76. transceiver.begin(
  77. SPI,
  78. 125000,
  79. 6,
  80. NRF905_PIN_UNUSED, // CE (standby) pin must be connected to VCC (3.3V)
  81. NRF905_PIN_UNUSED, // TRX (RX/TX mode) pin must be connected to GND (force RX mode)
  82. NRF905_PIN_UNUSED, // PWR (power-down) pin must be connected to VCC (3.3V)
  83. NRF905_PIN_UNUSED, // Without the CD pin collision avoidance will be disabled
  84. 3, // DR
  85. NRF905_PIN_UNUSED, // Without the AM pin the library the library must poll the status register over SPI.
  86. nRF905_int_dr,
  87. NULL // No interrupt function
  88. );
  89.  
  90. // Register event functions
  91. // NOTE: In interrupt mode these events will run from the interrupt, so any global variables accessed inside the event function should be declared 'volatile'.
  92. // Also avoid doing things in the event functions that take a long time to complete or things that rely on interrupts (delay(), millis(), Serial.print())).
  93. transceiver.events(
  94. nRF905_onRxComplete,
  95. nRF905_onRxInvalid,
  96. NULL,
  97. NULL
  98. );
  99.  
  100. transceiver.setLowRxPower(true);
  101. transceiver.setChannel(106);
  102. transceiver.setBand(NRF905_BAND_433);
  103. transceiver.setPayloadSize(0, 32); // Will transmit 5 byte payloads, receive 32 byte payloads
  104. transceiver.setAddressSize(4, 4);
  105. transceiver.setListenAddress(0xB54CAB34);
  106. Serial.println(F("Base station started"));
  107. }
  108.  
  109. void loop()
  110. {
  111. static uint32_t good;
  112. static uint32_t invalids;
  113. static uint32_t badData;
  114.  
  115. digitalWrite(LED, HIGH);
  116.  
  117. // Atomically copy volatile global variable to local variable since the radio is still in RX mode and another packet could come in at any moment
  118. noInterrupts();
  119. uint8_t pktStatus = packetStatus;
  120. packetStatus = PACKET_NONE;
  121. interrupts();
  122.  
  123. if(pktStatus == PACKET_NONE)
  124. {
  125. Serial.println("Woke for no reason?");
  126. }
  127. else if(pktStatus == PACKET_INVALID)
  128. {
  129. Serial.println("Invalid packet");
  130. invalids++;
  131. }
  132. else
  133. {
  134. Serial.println("Got packet");
  135.  
  136. // Make buffer for data
  137. uint8_t buffer[PAYLOAD_SIZE];
  138.  
  139. // Read payload
  140. transceiver.read(buffer, sizeof(buffer));
  141.  
  142. // Show received data
  143. Serial.print(F("Data from client:"));
  144. for(uint8_t i=0;i<PAYLOAD_SIZE;i++)
  145. {
  146. Serial.print(F(" "));
  147. Serial.print(buffer[i], DEC);
  148. }
  149. Serial.println();
  150.  
  151. Serial.print(F("Analog values: "));
  152. Serial.print(buffer[4]<<8 | buffer[5]);
  153. Serial.print(F(" "));
  154. Serial.print(buffer[6]<<8 | buffer[7]);
  155. Serial.print(F(" "));
  156. Serial.println(buffer[8]<<8 | buffer[9]);
  157.  
  158. Serial.print(F("Digital values: "));
  159. Serial.print(buffer[1]);
  160. Serial.print(F(" "));
  161. Serial.print(buffer[2]);
  162. Serial.print(F(" "));
  163. Serial.println(buffer[3]);
  164.  
  165. good++;
  166. }
  167.  
  168. Serial.println(F("Totals:"));
  169. Serial.print(F(" Good "));
  170. Serial.println(good);
  171. Serial.print(F(" Invalid "));
  172. Serial.println(invalids);
  173. Serial.print(F(" Bad "));
  174. Serial.println(badData);
  175. Serial.println(F("------"));
  176.  
  177. Serial.flush();
  178.  
  179. digitalWrite(LED, LOW);
  180.  
  181. // Sleep
  182. if(packetStatus == PACKET_NONE) // Make sure no new packets were received while since the last wake
  183. LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
  184. }
RAW Paste Data