//
// RF95 Recieve test
//
#include <SPI.h>
#include <RH_RF95.h>

#define RFM95_CS  10
#define RFM95_RST 2
#define RFM95_INT 3

// Change to 434.0 or other frequency, must match RX's freq!
#define RF95_FREQ 915.0

// Singleton instance of the radio driver
// Ada fruit
RH_RF95 rf95(RFM95_CS, RFM95_INT);
// Motino
//RH_RF95 rf95;

uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
uint8_t len; 
int icnt = 0;
int snr; //signal to noise ratio
int frqerr;  //freq error
float vin;
int rssi;

#define LED 4

void setup() 
{
  pinMode(LED, OUTPUT);    
  pinMode(A5, INPUT); // moteino reads input voltage on A5 
  //pinMode(RFM95_RST, OUTPUT);
  //digitalWrite(RFM95_RST, HIGH);
  //while (!Serial);  //does not start till serial connected
  Serial.begin(9600);
 
  delay(100);

  Serial.println("Arduino LoRa RX Test!");
  
  len = sizeof(buf);  // buffer size
  
  while (!rf95.init()) 
  {
     Serial.println("LoRa radio init failed");
    while (1);
  }
  Serial.println("LoRa radio init OK!");

  // Defaults after init are 434.0MHz, modulation GFSK_Rb250Fd250, +13dbM
  if (!rf95.setFrequency(RF95_FREQ)) 
  {
    Serial.println("setFrequency failed");
    while (1);
  }
  //Serial.print("Set Freq to: "); Serial.println(RF95_FREQ);

  // Defaults after init are 434.0MHz, 13dBm, Bw = 125 kHz, Cr = 4/5, Sf = 128chips/symbol, CRC on

  // The default transmitter power is 13dBm, using PA_BOOST.
  // If you are using RFM95/96/97/98 modules which uses the PA_BOOST transmitter pin, then 
  // you can set transmitter powers from 5 to 23 dBm:
  rf95.setTxPower(23, false);

}

void loop()
{
  float batt, battv;
  char wmn;
  int  wmni;
  if (rf95.available())
  {
    // Should be a message for us now   
    
    if (rf95.recv(buf, &len))
    {
        buf[len] = 0; //null terminate
        RH_RF95::printBuffer("Received: ", buf, len);
        Serial.print("Got: ");
        Serial.println((char*)buf);     // read on COM port by PC , send raw data
        Serial.print("Length="); Serial.println(len);

    }
    else
    {
        Serial.println("Receive failed");
    }
  }
  else
  {
    //Serial.println("No Data");
    //delay(100);
  }
  icnt += 1;
  delay(50);
}
