//  #include <LowPower.h>
#include <BME280I2C.h>
#include <Wire.h>
#include <ModbusRtu.h>
int8_t state = 0;
#define SERIAL_BAUD 9600

/**
     Modbus object declaration
     u8id : node id = 0 for master, = 1..247 for slave
     u8serno : serial port (use 0 for Serial)
     u8txenpin : 0 for RS-232 and USB-FTDI
                  or any pin number > 1 for RS-485
*/
Modbus slave(1, 0, 8); // this is slave @1 and RS-232 or USB-FTDI

BME280I2C bme;    // Default : forced mode, standby time = 1000 ms
// Oversampling = pressure Ã—1, temperature Ã—1, humidity Ã—1, filter off,

//////////////////////////////////////////////////////////////////
void setup()
{
  Serial.begin(SERIAL_BAUD);
  slave.begin( 19200, SERIAL_8E1 ); // 19200 baud, 8-bits, even, 1-bit stop

  while (!Serial) {} // Wait

  Wire.begin();

  while (!bme.begin())
  {
    Serial.println("Snímaè BME280 nalezen");
    delay(500);
  }

  switch (bme.chipModel())
  {
    case BME280::ChipModel_BME280:
      Serial.println("Snímaè BME280 nalezen");
      break;
    case BME280::ChipModel_BMP280:
      Serial.println("Snímaè BMP280 nalezen");
      break;
    default:
      Serial.println("Nalezen neznámý snímaè");
  }
}


//////////////////////////////////////////////////////////////////
void loop()
{

  printBME280Data(&Serial);
  delay(30);
  //  LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);

}


//////////////////////////////////////////////////////////////////
void printBME280Data
(
  Stream * client
)
{

  float temp(NAN), hum(NAN), pres(NAN);
  uint16_t au16data[16] = {temp * 100, hum * 100, pres, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

  BME280::TempUnit tempUnit(BME280::TempUnit_Celsius);
  BME280::PresUnit presUnit(BME280::PresUnit_Pa);

  bme.read(pres, temp, hum, tempUnit, presUnit);

  state = slave.poll(au16data, 3);
  delay(50);

  //  client->print("Teplota: ");
  //  client->print(temp);
  //  client->print("°" + String(tempUnit == BME280::TempUnit_Celsius ? 'C' : 'F'));
  //  client->print("\t\tRelativní Vlhkost: ");
  //  client->print(hum);
  //  client->print("%");
  //  client->print("\t\tTlak: ");
  //  client->print(pres);
  //  client->println("Pa");
  //  client->print(state);
  //*** delay(50);
}

