ASensor
 All Classes Functions Pages
NRF24Transceiver.h
1 #pragma once
2 /*
3  ASensor - Sensor library. Can be used standalone or in conjunction with AWind library
4  Copyright (C)2015 Andrei Degtiarev. All right reserved
5 
6  You can always find the latest version of the library at
7  https://github.com/AndreiDegtiarev/ASensor
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the MIT license.
11  Please see the included documents for further information.
12 */
13 #include "Transceiver.h"
14 const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0x7365727631LL };
15 
16 int serial_putc( char c, FILE * )
17 {
18  Serial.write( c );
19  return c;
20 }
23 {
24  RF24 *_radio;
25 
26  char receivePayload[32];
27 public:
28  NRF24Transceiver(int cepin, int cspin)
29  {
30  _radio = new RF24(cepin,cspin);
31  }
32  virtual void setup()
33  {
34  fdevopen( &serial_putc, 0 );
35  printf("Sending nodeID & 1 sensor data\n\r");
36  _radio->begin();
37  // Enable this seems to work better
38  _radio->enableDynamicPayloads();
39  _radio->setDataRate(RF24_1MBPS);
40  _radio->setPALevel(RF24_PA_MAX);
41  _radio->setChannel(76);
42  _radio->setRetries(15,15);
43  _radio->openWritingPipe(pipes[0]);
44  _radio->openReadingPipe(1,pipes[1]);
45  // Dump the configuration of the rf unit for debugging
46  _radio->printDetails();
47  }
48  void send_data(const __FlashStringHelper *nodeID,const __FlashStringHelper *name,float value)
49  {
50  bool timeout=0;
51  unsigned long send_time, rtt = 0;
52  char outBuffer[40]="";
53  char floatBuf[6];
54  dtostrf(value,5,2,floatBuf);
55  sprintf(outBuffer,"%s %s %s",nodeID,name,floatBuf);
56  //Serial.println(outBuffer);
57  send_time = millis();
58  // Stop listening and write to radio
59  _radio->stopListening();
60  // Send to hub
61  for(int i=5;i>=0;i--)
62  {
63  if ( _radio->write( outBuffer, strlen(outBuffer)) )
64  {
65  //printf("Send successful\n\r");
66  break;
67  }
68  else
69  {
70  printf("Send failed. Attempt:%d\n\r",i);
71  delay(200);
72  }
73  }
74  _radio->startListening();
75  delay(20);
76  while ( _radio->available() && !timeout )
77  {
78  uint8_t len = _radio->getDynamicPayloadSize();
79  _radio->read( receivePayload, len);
80  receivePayload[len] = 0;
81  //printf("inBuffer: %s\n\r",receivePayload);
82  // Compare receive payload with outBuffer
83  if ( ! strcmp(outBuffer, receivePayload) )
84  {
85  rtt = millis() - send_time;
86  printf("inBuffer --> rtt: %i \n\r",rtt);
87  }
88  // Check for timeout and exit the while loop
89  if ( millis() - send_time > _radio->getMaxTimeout() )
90  {
91  Serial.println("Timeout!!!");
92  timeout = 1;
93  }
94  delay(10);
95  }
96  }
97 };
Base class for Transceiver objects (like NRF24Transceiver)
Definition: Transceiver.h:15
Warpper about RF24 library.
Definition: NRF24Transceiver.h:22