#include <RFM69.h>
#include <SPI.h>

#define NODEID      15
#define NETWORKID   222
#define GATEWAYID   1
#define FREQUENCY   RF69_868MHZ         //Match this with the version of your Moteino! (others: RF69_433MHZ, RF69_868MHZ)
#define KEY         "1234567890123456"  //has to be same 16 characters/bytes on all nodes, not more not less!

#define SERIAL_BAUD 115200

boolean requestACK = true;              
#define ACK_TIME    15                  // Numbers of ms to wait for an ack

RFM69 radio;

// ########################################################################################################################
// Data Structure to be sent
// ########################################################################################################################
typedef struct {
  int Vcc;        // Supply voltage
  int Value1;
  int Value2;
} 
Payload;
Payload theData;

// ########################################################################################################################
// SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP
// ########################################################################################################################
void setup(){
  Serial.begin(SERIAL_BAUD);  
  Serial.println("*** RF-Sender (868 MHz) ***");
  Serial.print("*** NodeID: "); 
  Serial.print(NODEID);
  Serial.print(", Group: "); 
  Serial.println(NETWORKID);
  Serial.println("***************************");

  radio.initialize(FREQUENCY,NODEID,NETWORKID);
  radio.encrypt(KEY);
  radio.sleep();
}

// ########################################################################################################################
// LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP
// ########################################################################################################################
void loop() {

  theData.Value1 = 1000;
  theData.Value2 = 2000;

  theData.Vcc = readVcc();       // Get supply voltage

  //Serielle Ausgabe
  Serial.print("Value1: ");
  Serial.println(theData.Value1);
  Serial.print("Value2: ");
  Serial.println(theData.Value2);
  Serial.print("Vcc: ");
  Serial.println(theData.Vcc);

  radio.send(GATEWAYID, (const void*)(&theData), sizeof(theData), requestACK); 
  if (requestACK)
  {
    if (waitForAck(GATEWAYID))
      Serial.println("> ACK ok");
    else 
      Serial.println("> no ACK!");
  }
  Serial.println("*****************");
  radio.sleep(); 

  delay(5000);
}


// ########################################################################################################################
// waitForAck
// ########################################################################################################################
//wait a few milliseconds for proper ACK to me, return true if indeed received
static bool waitForAck(byte theNodeID)
{
  long now = millis();
  while (millis() - now <= ACK_TIME) {
    if (radio.ACKReceived(theNodeID))
      return true;
  }
  return false;
}

// ########################################################################################################################
// Read current supply voltage
// ########################################################################################################################
long readVcc() {
  bitClear(PRR, PRADC); 
  ADCSRA |= bit(ADEN);             // Enable the ADC
  long result;
  // Read 1.1V reference against Vcc
#if defined(__AVR_ATtiny84__)
  ADMUX = _BV(MUX5) | _BV(MUX0);   // For ATtiny84
#else
  ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);   // For ATmega328
#endif
  delay(2);                        // Wait for Vref to settle
  ADCSRA |= _BV(ADSC);             // Convert
  while (bit_is_set(ADCSRA,ADSC));
  result = ADCL;
  result |= ADCH<<8;
  result = 1126400L / result;      // Back-calculate Vcc in mV
  ADCSRA &= ~ bit(ADEN); 
  bitSet(PRR, PRADC);              // Disable the ADC to save power   
  return result;
}











