#include <RFM69.h>    //get it here: https://github.com/LowPowerLab/RFM69
#include <SPI.h>      //get it here: https://github.com/LowPowerLab/SPIFlash
#include <LowPower.h> //get library from: https://github.com/LowPowerLab/LowPower
                      //writeup here: http://www.rocketscream.com/blog/2011/07/04/lightweight-low-power-arduino-library/

//*********************************************************************************************
#define NODEID        12    //unique for each node on same network
#define NETWORKID     909  //the same on all nodes that talk to each other
#define GATEWAYID     1
//Match frequency to the hardware version of the radio on your Moteino (uncomment one):
//#define FREQUENCY     RF69_433MHZ
#define FREQUENCY     RF69_868MHZ
//#define FREQUENCY     RF69_915MHZ
#define IS_RFM69HW    //uncomment only for RFM69HW! Leave out if you have RFM69W!
#define ENCRYPTKEY    "farfallafarfalla" //exactly the same 16 characters/bytes on all nodes!
#define SENDEVERYXLOOPS   8 //each loop sleeps 8 seconds, so send status message every this many loops (default "4" = 32 seconds)
//*********************************************************************************************

#define LED           9  // Moteinos have LEDs on D9
//#define BLINK_EN         //uncomment to make LED flash when messages are sent, leave out if you want low power
int PIN_FINA= 4;
int PIN_FINB= 5;
int PIN_V_DHT=7;
int finA=0;
int finB=0;
int finAB=3;    
int last_finAB=0;   
//temperature / humidity  =====================================
#include "DHT.h"
#define DHTPIN 6     			// digital pin we're connected to
#define DHTTYPE DHT11     // DHT 22  (AM2302) blue one
//#define DHTTYPE DHT21   // DHT 21 (AM2301) white one
// Initialize DHT sensor for normal 16mhz Arduino
DHT dht(DHTPIN, DHTTYPE);
float t=19.99;
float h=79.99;
#define SERIAL_BAUD   115200
//#define SERIAL_EN      //uncomment this line to enable serial IO debug messages, leave out if you want low power
#ifdef SERIAL_EN
  #define DEBUG(input)   {Serial.print(input); delay(1);}
  #define DEBUGln(input) {Serial.println(input); delay(1);}
#else
  #define DEBUG(input);
  #define DEBUGln(input);
#endif

RFM69 radio;
volatile boolean motionDetected=false;
//struct for wireless data transmission
typedef struct {		
  int       nodeID; 		
  int       deviceID;		
  unsigned long   var1_usl; 	
  float     var2_float;   	
  float     var3_float;		
} Payload;
Payload theData_1;
Payload theData_2;


void setup() {
#ifdef SERIAL_EN
  Serial.begin(SERIAL_BAUD);
#endif  
  radio.initialize(FREQUENCY,NODEID,NETWORKID);
#ifdef IS_RFM69HW
  radio.setHighPower(); //uncomment only for RFM69HW!
#endif
  radio.encrypt(ENCRYPTKEY);
    radio.setPowerLevel(16);
pinMode(PIN_FINA,INPUT_PULLUP); 
pinMode(PIN_FINB,INPUT_PULLUP); 
pinMode(PIN_V_DHT,OUTPUT); 

  char buff[50];
  sprintf(buff, "\nTransmitting at %d Mhz...", FREQUENCY==RF69_433MHZ ? 433 : FREQUENCY==RF69_868MHZ ? 868 : 915);
  DEBUGln(buff);
    dht.begin();
  
}

char sendBuf[32];
byte sendLen;
byte sendLoops=0;


void loop() {
    int finA=digitalRead(PIN_FINA);
    int finB=digitalRead(PIN_FINB);
    finAB=finA+2*finB;    
  if (finAB!= last_finAB)
        {
          theData_1.nodeID= NODEID;		
          theData_1.deviceID=21;		
          theData_1.var1_usl=finAB;
          theData_1.var2_float=0 ; 	
          theData_1.var3_float	=0;	
          radio.sendWithRetry(GATEWAYID, (const void*)(&theData_1), sizeof(theData_1));
        delay(10);
        last_finAB=finAB;
         radio.sleep();
        }
  
   if (sendLoops>=SENDEVERYXLOOPS)
  { sendLoops=0;
    digitalWrite(PIN_V_DHT, HIGH);
    sendLen = strlen(sendBuf);
    float h = dht.readHumidity();
    float t = dht.readTemperature();
    digitalWrite(PIN_V_DHT, LOW);    
    theData_2.nodeID= NODEID;		
    theData_2.deviceID=23;		
    theData_2.var1_usl=0;
    theData_2.var2_float=t; 	
    theData_2.var3_float=h;	
    radio.sendWithRetry(GATEWAYID, (const void*)(&theData_2), sizeof(theData_2));
    delay(10); 
   radio.sleep();
  }

  sendLoops++;
  LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
}