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
/*  
 * ------------------------------------------------------------------------
 * "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_server.ino  
 * Version  : 0.1 (DRAFT)
 * ------------------------------------------------------------------------
 * Description : A Arduino LoRa antenna measurment server 
 * ------------------------------------------------------------------------
 * 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_server.pde 
 * source : http://www.airspayce.com/mikem/arduino/RadioHead/rf95_server_8pde-example.html
 * more info at : http://www.airspayce.com/mikem/arduino/RadioHead/
 * Example sketch showing how to create a simple messageing server
 * 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>

// Singleton instance of the radio driver
RH_RF95 rf95;
int led = 13;

void setup() 
{
  pinMode(8, OUTPUT);
  digitalWrite(8, HIGH);
  pinMode(led, OUTPUT);     
  Serial.begin(9600);
  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);
  }
 
}

void loop()
{ 
  if (rf95.available())
  {
    // Should be a message for us now   
    uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
    uint8_t len = sizeof(buf);
    if (rf95.recv(buf, &len))
    {
      digitalWrite(led, HIGH); 

      if (len > 0 && (char)buf[0] == '?')
      {
        int rssi = rf95.lastRssi();
      
        Serial.println("got ?"); 
        Serial.print("RSSI: ");
        Serial.println(rssi, DEC);  
        
        // send back the rssi
        rf95.send((uint8_t *)&rssi, 2);
        rf95.waitPacketSent();
        Serial.println("Sent a reply");
      }
      digitalWrite(led, LOW);
    }
    else
    {
      Serial.println("recv failed");
    }
  } 
}