Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Project: nRF905 Radio Library for Arduino (Low power sensor node example)
- Author: Zak Kemble, contact@zakkemble.net
- Copyright: (C) 2020 by Zak Kemble
- License: GNU GPL v3 (see License.txt)
- Web: https://blog.zakkemble.net/nrf905-avrarduino-librarydriver/
- */
- // This examples requires the low power library from https://github.com/rocketscream/Low-Power
- // This examples configures the nRF905 library to only use 5 connections:
- // MOSI
- // MISO
- // SCK
- // SS -> 6
- // PWR -> 8
- // The following pins on the nRF905 must be connected to VCC (3.3V) or GND:
- // CE (TRX_EN) -> VCC
- // TXE (TX_EN) -> VCC
- #include <nRF905.h>
- #include <SPI.h>
- #include <LowPower.h>
- byte mess_Address = {0xB54CAB34};
- 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};
- const int buttonPin = 2;
- int buttonState = 0;
- nRF905 transceiver = nRF905();
- static bool txDone; // NOTE: In polling mode this does not need to be volatile
- // Event function for TX completion
- void nRF905_onTxComplete(nRF905* device)
- {
- txDone = true;
- }
- void setup()
- {
- Serial.begin(115200);
- Serial.println(F("Sensor node starting..."));
- pinMode(buttonPin, INPUT);
- // standby off TODO
- //pinMode(7, OUTPUT);
- //digitalWrite(7, HIGH);
- // pwr
- //pinMode(8, OUTPUT);
- //digitalWrite(8, HIGH);
- // trx
- //pinMode(9, OUTPUT);
- //digitalWrite(9, HIGH);
- // This must be called first
- SPI.begin();
- SPI.setClockDivider(SPI_CLOCK_DIV128);
- // Minimal wires (polling mode)
- // Up to 5 wires can be disconnected, however this will reduce functionalliy and will put the library into polling mode instead of interrupt mode.
- // 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.
- transceiver.begin(
- SPI,
- 125000,
- 6,
- NRF905_PIN_UNUSED, // CE (standby) pin must be connected to VCC (3.3V)
- NRF905_PIN_UNUSED, // TRX (RX/TX mode) pin must be connected to VCC (3.3V) (force TX mode)
- 8, // PWR
- NRF905_PIN_UNUSED, // Without the CD pin collision avoidance will be disabled
- 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
- NRF905_PIN_UNUSED, // Without the AM pin the library must poll the status register over SPI.
- NULL, // Running in polling mode so no interrupt function
- NULL // Running in polling mode so no interrupt function
- );
- // Register event functions
- transceiver.events(
- NULL,
- NULL,
- nRF905_onTxComplete,
- NULL
- );
- // Low-mid transmit level -2dBm (631uW)
- transceiver.setTransmitPower(NRF905_PWR_10);
- transceiver.setChannel(106);
- transceiver.setBand(NRF905_BAND_433);
- transceiver.setPayloadSize(32, 0); // Will transmit 5 byte payloads, receive 32 byte payloads
- transceiver.setAddressSize(4, 4);
- Serial.println(F("Transciever Started"));
- Serial.println(F("Waiting for input"));
- }
- void loop()
- {
- buttonState = digitalRead(buttonPin);
- // Write data to radio
- transceiver.write(mess_Address, Message, sizeof(Message));
- txDone = false;
- if (buttonState == HIGH) {
- // This will power-up the radio and send the data
- transceiver.TX(NRF905_NEXTMODE_STANDBY, false);
- // Transmission will take approx 6-7ms to complete (smaller paylaod sizes will be faster)
- while (!txDone) {
- transceiver.poll();
- delay(1);
- }
- // Transmission done, power-down the radio
- //transceiver.powerDown();
- Serial.println(F("Transciever complete"));
- // NOTE:
- // 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.
- // TODO
- // Sleep for 64 seconds
- //uint8_t sleepCounter = 8;
- //while(sleepCounter--)
- // Sleep for 1 second
- LowPower.powerDown(SLEEP_1S, ADC_OFF, BOD_OFF);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement