-
-
-/*  
-   CONNECTIONS:
-   - GND
-   - Pin 2 to HC-05 TXD
-   - Pin 3 to HC-05 RXD
-   - Pin 4 to HC-05 K0EY
-   - Pin 5+6 to HC-05 VCC for power control
- */
-
 #include <SoftwareSerial.h>  
 
 #include <SFE_BMP180.h>
 #include <DHT.h>
+#include <OneWire.h>
+#include <DallasTemperature.h>
 
 #define HC_05_TXD_ARDUINO_RXD 2
 #define HC_05_RXD_ARDUINO_TXD 3

#define HC_05_PWR2            6

#define DHT_PIN 7

#define CMD_W                 1100 // msec

#define msg(x) Serial.println(x)

const int board = 0; // 0 -- for outdoor, 1 -- indor module

SoftwareSerial BTSerial(HC_05_TXD_ARDUINO_RXD, HC_05_RXD_ARDUINO_TXD); // RX | TX
DHT dht(DHT_PIN, DHT22);


enum BTDeviceState {
  error = -2,
  off = -1,
  data = 0,
  at = 1,
};

class BTDevice 
{
private:
  BTDeviceState m_state;
  
public:
  BTDevice(): m_state(off) {  };

  void setState(BTDeviceState state) {
    if (m_state == state) {
      msg("try to set BT to the same state " + state);
      
      return;
    }
    switch (state) {
      case off: {
        
        msg("Power off");
        digitalWrite(HC_05_PWR1, LOW);
        digitalWrite(HC_05_PWR2, LOW);  
        delay(CMD_W);
        m_state = off;
        return;        
      }
      case data: {
        if (m_state == off) {
          digitalWrite(HC_05_SETUPKEY, LOW);  // Set command mode when powering up
          delay(CMD_W);       
          digitalWrite(HC_05_PWR1, HIGH);
          digitalWrite(HC_05_PWR2, HIGH);  
          delay(CMD_W);
                    
          m_state = data;
          return;        
        }
      }
      case at: {
        if (m_state == off) {
          digitalWrite(HC_05_SETUPKEY, HIGH);  // Set command mode when powering up
          delay(CMD_W);       
          digitalWrite(HC_05_PWR1, HIGH);
          digitalWrite(HC_05_PWR2, HIGH);  
          delay(CMD_W);        

          m_state = at;
          return;
        }
        
      }
      
    }
  };

  BTDeviceState state() {return m_state;};
  
};

/*
void BTDevice::initPins() {
  pinMode(HC_05_SETUPKEY, OUTPUT);  // this pin will pull the HC-05 pin 34 (key pin) HIGH to switch module to AT mode
  pinMode(HC_05_PWR1, OUTPUT);      // Connect in parallel to HC-05 VCC
  pinMode(HC_05_PWR2, OUTPUT);      // Connect in parallel to HC-05 VCC
};
*/

BTDevice *bt = new BTDevice();

SFE_BMP180 pressure;

void setup()   /****** SETUP: RUNS ONCE ******/
{

  pinMode(HC_05_SETUPKEY, OUTPUT);  

  d10 = digitalRead(10);
  d8 = digitalRead(8);

  float h = dht.readHumidity();
  float t = dht.readTemperature();

  getPT(P, T);
  
  sensors.requestTemperatures();
  float tX = sensors.getTempCByIndex(0);

  unsigned long CurrentTime = millis();
  unsigned long ElapsedTime = CurrentTime - StartTime;
  unsigned long dt = ElapsedTime > LOOP_TIME ? 0 : LOOP_TIME - ElapsedTime;

  bt_echo_send("(")
  fbt_echo_send(cnt); 
  fbt_echo_send(v0); 
  fbt_echo_send(tX);
  fbt_echo_send(t);
  fbt_echo_send(v1); 
  fbt_echo_send(d8);
  fbt_echo_send(d10);
  fbt_echo_send(T);
  fbt_echo_send(P);
  fbt_echo_send(h);
  fbt_echo_send(dt);
  bt_echo_send(")");

  bt_echo_send("\r\n");
  
  delay(dt);

  cnt++;
}

