1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*  
 * ------------------------------------------------------------------------
 * "PH2LB LICENSE" (Revision 1) : (based on "THE BEER-WARE LICENSE" Rev 42) 
 * <lex@ph2lb.nl> wrote this file. As long as you retain this notice
 * you can do modify it as you please. It's Free for non commercial usage 
 * and education and if we meet some day, and you think this stuff is 
 * worth it, you can buy me a beer in return
 * Lex Bolkesteijn 
 * ------------------------------------------------------------------------ 
 * Filename : rf95_rssi_client.ino  
 * Version  : 0.1 (DRAFT)
 * ------------------------------------------------------------------------
 * Description : A Arduino LoRa antenna measurment client 
 * ------------------------------------------------------------------------
 * Revision : 
 *  - 2016-aug-18 0.1 initial version 
 * ------------------------------------------------------------------------
 * Hardware used : 
 *  - Arduino Uno R3 
 *  - RFM95W ( http://webshop.ideetron.nl/RFM95W )
 *  - ADAPT_RFM95W
 *    ( http://webshop.ideetron.nl/ADAPT_RFM95W )
 *  - ARDUINO-SHIELD-HOPERF
 *    http://webshop.ideetron.nl/ARDUINO-SHIELD-HOPERF 
 *      
 * based on rf95_client.pde 
 * source : http://www.airspayce.com/mikem/arduino/RadioHead/rf95_client_8pde-example.html
 * more info at : http://www.airspayce.com/mikem/arduino/RadioHead/
 * Example sketch showing how to create a simple messageing client
 * with the RH_RF95 class. RH_RF95 class does not provide for addressing or
 * reliability, so you should only use RH_RF95  if you do not need the higher
 * level messaging abilities.
 * It is designed to work with the other example rf95_client
 * Tested with Anarduino MiniWirelessLoRa
 */
 

#include <SPI.h>
#include <RH_RF95.h>

/*
Arduino UNO 

#define DATAOUT 11//MOSI
#define DATAIN  12//MISO
#define SPICLOCK  13//sck
#define SLAVESELECT 10//ss
*/

// Singleton instance of the radio driver
RH_RF95 rf95;

void setup() 
{ 
  pinMode(8, OUTPUT);
  digitalWrite(8, HIGH);
  Serial.begin(9600);
  Serial.println("LoRa test application");
  if (!rf95.init())
  {
    Serial.println("RF95 init failed!");
  }
  else
  {
    Serial.println("RF95 init done!");
    
    rf95.setFrequency(868.10);   
    rf95.setModemConfig(RH_RF95::Bw125Cr45Sf128);
     
    rf95.setPreambleLength(8);
    rf95.setTxPower(20);

  }
}

#define nrOfSamples 10
int sampleCounter = 0; // take 10 samples
int sendrssi[nrOfSamples];
int recvrssi[nrOfSamples];
bool showResult = true;

void loop()
{
  if (sampleCounter < nrOfSamples)
  {
    Serial.println("Sending to rf95_server");
    // Send a message to rf95_server
    uint8_t data[1];
    data[0] = (uint8_t)'?';
    
    rf95.send(data, sizeof(data));
    //Serial.println("waitPacketSent");
    rf95.waitPacketSent();
    //Serial.println("send done "); 
    // Now wait for a reply
    uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
    uint8_t len = sizeof(buf);
   
    if (rf95.waitAvailableTimeout(3000))
    { 
      // Should be a reply message for us now   
      if (rf95.recv(buf, &len))
     { 
        if (len == 2)
        {
          Serial.print("got rssi response: "); 
          int rssi = *(int *)buf;  
          Serial.print(rssi, DEC);  
          Serial.print(" > ");
          int lastRssi = rf95.lastRssi();
          Serial.println(lastRssi, DEC);    
          sendrssi[sampleCounter] = rssi;
          recvrssi[sampleCounter] = lastRssi;
          sampleCounter++;
        }
      }
      else
      {
        Serial.println("recv failed");
      }
    }
    else
    {
      Serial.println("No reply, is rf95_rssi_server running?"); 
    }
    delay(1000); 
  }
  else if (showResult)
  {
      Serial.println("All samples been taken.");
      int avgSendRssi = 0;
      int avgRecvRssi = 0;

      Serial.println("SENDRSSI|RECVRSSI");
      for (int i=0; i<nrOfSamples; i++)
      {   
         Serial.print(sendrssi[i], DEC); 
         Serial.print("|"); 
         Serial.println(recvrssi[i], DEC); 
        
         avgSendRssi += sendrssi[i];
         avgRecvRssi += recvrssi[i]; 
      }
      Serial.println("AVGSENDRSSI|AVGRECVRSSI");
      Serial.print(avgSendRssi  / nrOfSamples, DEC); 
      Serial.print("|"); 
      Serial.println(avgRecvRssi / nrOfSamples, DEC); 
      Serial.println(" "); 
      Serial.println("Reset client for new test."); 
      showResult = false;
  }
  
}