//-------------------------------nacteni potrebnych knihoven-------------------------------------------
#include <WiFi.h>
#include <ETH.h>
#include <HTTPClient.h>
#include <Wire.h>     
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <ESPAsyncWebServer.h>
#include <EEPROM.h>
#include <OneButton.h>


//--------------------------------------definice konstnant-----------------------------------------------
#define EEPROM_SIZE 500 //inicializace EEPROM pameti o velikosti 500


//----------------------------------iniciaizace pinu----------------------------------------------------
//*** nepouzivat piny GPIO15, GPIO17 a GPIO34 ***

#define LED_DATA 33 //led signalizujici odeslani dat do databaze
#define LED_ETH 2 //led signalizujici pripojeni ethernetoveho kabelu do site
#define LED_ON 5 //led zapnuti
#define DI1 32 //inicializace pinu GPIO32 (digital)
#define DI2 16 //inicializace pinu GPIO13 (digital)
#define DI3 12 //inicializace pinu GPIO16 (digital)
#define DI4 35 //inicializace pinu GPIO12 (digital)
#define AI1 36 //inicializace pinu GPI36 (analog)
#define AI2 39 //inicializace pinu GPI39 (analog)
#define BUTTON 13 //inicializace reset tlacitka pro konfiguraci
#define SCL 14 //pin pro BME280 na I2C sbernici
#define SDA 4 //pin pro BME280 na I2C sbernici


//----------------------------------inicializace potrebnych trid ke knihovnam----------------------------
TwoWire I2CBME = TwoWire(0); //inicializace pripojeni k I2C sbernici
Adafruit_BME280 bme; //promenna pro BME280 senzor
OneButton reset_button(BUTTON, true); //inicializace tlacitka reset

//---------------------------------------inicializace vlaken---------------------------------------------------------
TaskHandle_t Task1, Task2, Task3; //inicializace vlaken


//---------------------------------inicializace sitovych adres----------------------------------------
IPAddress ip_OLIMEX(192, 168, 1, 1);
IPAddress dns_OLIMEX(192, 168, 1, 1);
IPAddress gateway_OLIMEX(192, 168, 4, 1);
IPAddress subnet_OLIMEX(255, 255, 255, 0);


//-------------------------------nastaveni potrebnych promennych-----------------------------------------
static bool eth_connected = false;

int lastButtonState = 0;
int buttonState = 0;

float vlhkost = 0;
float teplota = 0;
float tlak = 0;
unsigned variable = 0;
unsigned configuration = 0;
unsigned CISLO_STROJE = 0;

int IP1 = 0;
int IP2 = 0;
int IP3 = 0;
int IP4 = 0;

int dns1 = 0;
int dns2 = 0;
int dns3 = 0;
int dns4 = 0;

int subnet1 = 0;
int subnet2 = 0;
int subnet3 = 0;
int subnet4 = 0;

int gateway1 = 0;
int gateway2 = 0;
int gateway3 = 0;
int gateway4 = 0;


//----------------------------------promenne do html kodu--------------------------------------------
const char* PARAM_INPUT_1 = "cislo_stroje"; //cislo stroje

const char* PARAM_INPUT_2 = "IP1"; //IP adresa
const char* PARAM_INPUT_3 = "IP2";
const char* PARAM_INPUT_4 = "IP3";
const char* PARAM_INPUT_5 = "IP4";

const char* PARAM_INPUT_8 = "dns1"; //dns 
const char* PARAM_INPUT_9 = "dns2";
const char* PARAM_INPUT_10 = "dns3";
const char* PARAM_INPUT_11 = "dns4";

const char* PARAM_INPUT_12 = "gateway1"; //gateway
const char* PARAM_INPUT_13 = "gateway2";
const char* PARAM_INPUT_14 = "gateway3";
const char* PARAM_INPUT_15 = "gateway4";

const char* PARAM_INPUT_16 = "subnet1"; //subnet
const char* PARAM_INPUT_17 = "subnet2";
const char* PARAM_INPUT_18 = "subnet3";
const char* PARAM_INPUT_19 = "subnet4";

String ip_1 = "";
String ip_2 = "";
String ip_3 = "";
String ip_4 = "";

String subnet_1 = "";
String subnet_2 = "";
String subnet_3 = "";
String subnet_4 = "";

String dns_1 = "";
String dns_2 = "";
String dns_3 = "";
String dns_4 = "";

String gateway_1 = "";
String gateway_2 = "";
String gateway_3 = "";
String gateway_4 = "";

//-----------------------------------udaje k prihlaseni k ESP32---------------------------------------
char *ssid_ESP32 = "ESP32";
char *password_ESP32 = "123456789";

//-------------------------------nastaveni WIFI ESP32-----------------------------------------------
IPAddress ip_ESP32(192, 168, 1, 1);
IPAddress gateway_ESP32(192, 168, 1, 1);
IPAddress subnet_ESP32(255, 255, 255, 0);

//---------------------------------------nastaveni portu serveru------------------------------------------------------
AsyncWebServer server(80);

//-------------------------------vysledna HTML stranka serveru----------------------------------------------------------
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html><head>
  <title align=center>Konfigurace Olimex ESP32-GATEWAY</title>
  <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1">
  </head><body>
    <div align="center">
  <h1>Konfigurace Olimex ESP32-GATEWAY</h1>
  <h2>Změna čísla zařízení </h2>
  <form action="/get">
    Číslo zařzení: <input type="text" name="cislo_stroje">
    <input type="submit" value="Submit">
  </form><br>
  <h2>Změna IP adresy</h2>
    <form action="/get">
        IP Address: <input type="text" name="IP1">
        . <input type="text" name="IP2">
        . <input type="text" name="IP3">
        . <input type="text" name="IP4">
    <input type="submit" value="Submit">
  </form><br>
    <h2>Změna DNS</h2>
    <form action="/get">
        DNS: <input type="text" name="dns1">
        . <input type="text" name="dns2">
        . <input type="text" name="dns3">
        . <input type="text" name="dns4">
    <input type="submit" value="Submit">
  </form><br>
    <h2>Změna masky sítě</h2>
    <form action="/get">
        Subnet Mask: <input type="text" name="subnet1">
        . <input type="text" name="subnet2">
        . <input type="text" name="subnet3">
        . <input type="text" name="subnet4">
    <input type="submit" value="Submit">
  </form><br>
    <h2>Změna IP adresy brány</h2>
  <form action="/get">
      Gateway: <input type="text" name="gateway1">
      . <input type="text" name="gateway2">
      . <input type="text" name="gateway3">
      . <input type="text" name="gateway4">
   <input type="submit" value="Submit">
  </form><br>
  <h3>------------------------------------------------------------- Poznámky -------------------------------------------------------------</h3><br>
  <h4>Nejprve postupně nastavte jednotlivé síťové adresy (potvrzujte tlačítkem 'Submit').</h4><br>
  <h4>Po potvrzení adresy Gateway tlačítkem 'Submit' a následném stisknutím tlačítka reset (RST1)<br>na modulu Olimex dojde k nastavení zařízení do pracovního režimu.</h4><br>
  <h4> Nyní je Olimex ESP32-GATEWAY připraveno k normálnímu použití.</h4><br>
  </div>
</body></html>)rawliteral";


//-------------------------------------------------funkce pro zobrazeni stranka nenaezena---------------------------------------------
void notFound(AsyncWebServerRequest *request) 
{
  request->send(404, "text/plain", "Not found");
}


//------------------------------------------funkce po dlouhem (cca 3s) stisknuti tlacitka reset_button--------------------------------
void reset_function()
{
  variable = 0;
  EEPROM.put(0, variable);
  EEPROM.commit();
  digitalWrite(LED_ETH, HIGH);
  delay(500);
  digitalWrite(LED_ETH, LOW);
  delay(500);
  digitalWrite(LED_ETH, HIGH);
  delay(500);
  digitalWrite(LED_ETH, LOW);
}


//-----------------------------------------------funkce pro pripojeni k siti pres ethernetovy kabel-----------------------------------------

void WiFiEvent(WiFiEvent_t event) 
{
  switch (event) {
    case SYSTEM_EVENT_ETH_START:
      Serial.println("ETH Started"); //zacatek funkce pripojovani k siti
      //set eth hostname here
      ETH.setHostname("esp32-ethernet");
      break;
    case SYSTEM_EVENT_ETH_CONNECTED:
      Serial.println("ETH Connected"); //ethernet pripojen
      break;
    case SYSTEM_EVENT_ETH_GOT_IP:
      Serial.print("ETH MAC: ");
      Serial.print(ETH.macAddress()); //vypsani MAC adresy zarizeni
      Serial.print(", IPv4: ");
      Serial.print(ETH.localIP());
      if (ETH.fullDuplex()) {
        Serial.print(", FULL_DUPLEX");
      }
      Serial.print(", ");
      Serial.print(ETH.linkSpeed()); //vypsani rychlosti pripojeni
      Serial.println("Mbps");
      eth_connected = true;
      digitalWrite(LED_ETH, HIGH);
      break;
    case SYSTEM_EVENT_ETH_DISCONNECTED:
      Serial.println("ETH Disconnected"); //ethernet odpojen
      eth_connected = false;
      digitalWrite(LED_ETH, LOW);
      break;
    case SYSTEM_EVENT_ETH_STOP:
      Serial.println("ETH Stopped"); //komunikace zastavena
      eth_connected = false;
      digitalWrite(LED_ETH, LOW);
      break;
    default:
      break;
  }
}


//--------------------------------------setup funkce------------------------------------------------------------------
void setup()
{
 Serial.begin(115200); 
 
    
//--------------------------------------inicializace jednotlivych pinu na desce ESP32---------------------------------
 pinMode(LED_DATA, OUTPUT); //inicializace pinu
 pinMode(LED_ETH, OUTPUT);
 pinMode(LED_ON, OUTPUT);
 digitalWrite(LED_ON, HIGH);
 pinMode(BUTTON, INPUT_PULLUP);
 pinMode(DI1, INPUT_PULLUP); 
 pinMode(DI2, INPUT_PULLUP);
 pinMode(DI3, INPUT);
 pinMode(DI4, INPUT);
 pinMode(AI1, INPUT);
 pinMode(AI2, INPUT);
 
//------------------------------------------nastaveni EEPROM pameti-------------------------------------------------------
 EEPROM.begin(EEPROM_SIZE);

//----------------------------------pocatecni nastaveni sitovych adres pro nove WIFI pripojeni-----------------------------
 
 if(EEPROM.read(0) != 1)
  {
  EEPROM.write(5, CISLO_STROJE);
  
  EEPROM.write(10, ip_OLIMEX[0]);
  EEPROM.write(11, ip_OLIMEX[1]);
  EEPROM.write(12, ip_OLIMEX[2]);
  EEPROM.write(13, ip_OLIMEX[3]);
  
  EEPROM.write(20, dns_OLIMEX[0]);
  EEPROM.write(21, dns_OLIMEX[1]);
  EEPROM.write(22, dns_OLIMEX[2]);
  EEPROM.write(23, dns_OLIMEX[3]);
  
  EEPROM.write(30, gateway_OLIMEX[0]);
  EEPROM.write(31, gateway_OLIMEX[1]);
  EEPROM.write(32, gateway_OLIMEX[2]);
  EEPROM.write(33, gateway_OLIMEX[3]);
  
  EEPROM.write(40, subnet_OLIMEX[0]);
  EEPROM.write(41, subnet_OLIMEX[1]);
  EEPROM.write(42, subnet_OLIMEX[2]);
  EEPROM.write(43, subnet_OLIMEX[3]);
  
  EEPROM.write(0, variable);
  EEPROM.commit();
  }
//-------------------------------------------------spusteni a nastaveni ESP32 WIFI--------------------------------
 if(EEPROM.read(0) == 0)
  {
  WiFi.softAP(ssid_ESP32, password_ESP32);
  Serial.println(WiFi.softAPIP());
  }
//--------------------------------------------spusteni a nastaveni noveho WIFI pripojeni---------------------------
  if((EEPROM.read(0)) == 1)
  {
   
   EEPROM.get(10, ip_OLIMEX[0]);
   EEPROM.get(11, ip_OLIMEX[1]);
   EEPROM.get(12, ip_OLIMEX[2]);
   EEPROM.get(13, ip_OLIMEX[3]);
   
   EEPROM.get(20, dns_OLIMEX[0]);
   EEPROM.get(21, dns_OLIMEX[1]);
   EEPROM.get(22, dns_OLIMEX[2]);
   EEPROM.get(23, dns_OLIMEX[3]);
   
   EEPROM.get(30, gateway_OLIMEX[0]);
   EEPROM.get(31, gateway_OLIMEX[1]);
   EEPROM.get(32, gateway_OLIMEX[2]);
   EEPROM.get(33, gateway_OLIMEX[3]);
   
   EEPROM.get(40, subnet_OLIMEX[0]);
   EEPROM.get(41, subnet_OLIMEX[1]);
   EEPROM.get(42, subnet_OLIMEX[2]);
   EEPROM.get(43, subnet_OLIMEX[3]);
   
   WiFi.onEvent(WiFiEvent);
   
   ETH.begin(); 
   delay(2000);
   ETH.config(ip_OLIMEX, gateway_OLIMEX, subnet_OLIMEX, dns_OLIMEX); //konfigurace adres (IP, GATEWAY, SUBNET, DNS1)
  }

  server.begin(); 
  
 //---------------------------------------------------------------- zaslani webove stranky se vstupnimi parametry ----------------------------------------------------------------
  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/html", index_html);
  });

  //----------------------------------------------------------------  zaslani webove stranky s parametry a GET funkci -----------------------------------------
  server.on("/get", HTTP_GET, [] (AsyncWebServerRequest *request) 
  {
    String inputMessage;

    //-----------------------------------------------------parametr CISLO_STROJE---------------------------
    if (request->hasParam(PARAM_INPUT_1)) 
    {
      inputMessage = request->getParam(PARAM_INPUT_1)->value();
      CISLO_STROJE = inputMessage.toInt();
      EEPROM.put(5, CISLO_STROJE);
      EEPROM.commit();
      request->send(200, "text/html", "Uspesna zmena cisla lisu: " + String(CISLO_STROJE) + "<br><a href=\"/\">Return to Home Page</a>");
    }

    //----------------------------------------------------parametr IP adresy------------------------------
    
      else if((request->hasParam(PARAM_INPUT_2))&&(request->hasParam(PARAM_INPUT_3))&&(request->hasParam(PARAM_INPUT_4))&&(request->hasParam(PARAM_INPUT_5)))
      {
      ip_1 = request->getParam(PARAM_INPUT_2)->value();
      IP1 = ip_1.toInt();
      ip_2 = request->getParam(PARAM_INPUT_3)->value();
      IP2 = ip_2.toInt();
      ip_3 = request->getParam(PARAM_INPUT_4)->value();
      IP3 = ip_3.toInt();
      ip_4 = request->getParam(PARAM_INPUT_5)->value();
      IP4 = ip_4.toInt();
      ip_OLIMEX[0] = IP1;
      ip_OLIMEX[1] = IP2;
      ip_OLIMEX[2] = IP3;
      ip_OLIMEX[3] = IP4;
      EEPROM.put(10, ip_OLIMEX[0]);
      EEPROM.commit();
      EEPROM.put(11, ip_OLIMEX[1]);
      EEPROM.commit();
      EEPROM.put(12, ip_OLIMEX[2]);
      EEPROM.commit();
      EEPROM.put(13, ip_OLIMEX[3]);
      EEPROM.commit();
       request->send(200, "text/html", "Uspesna zmena IP adresy: " + String(IP1) + "." + String(IP2) +"." + String(IP3) + "." + String(IP4) + "<br><a href=\"/\">Return to Home Page</a>");
       configuration = 1;
       delay(1000);
       configuration = 0;
      }

      //-------------------------------------------------parametr DNS----------------------------
      
      else if((request->hasParam(PARAM_INPUT_8))&&(request->hasParam(PARAM_INPUT_9))&&(request->hasParam(PARAM_INPUT_10))&&(request->hasParam(PARAM_INPUT_11)))
      {
      dns_1 = request->getParam(PARAM_INPUT_8)->value();
      dns1 = dns_1.toInt();
      dns_2 = request->getParam(PARAM_INPUT_9)->value();
      dns2 = dns_2.toInt();
      dns_3 = request->getParam(PARAM_INPUT_10)->value();
      dns3 = dns_3.toInt();
      dns_4 = request->getParam(PARAM_INPUT_11)->value();
      dns4 = dns_4.toInt();
      dns_OLIMEX[0] = dns1;
      dns_OLIMEX[1] = dns2;
      dns_OLIMEX[2] = dns3;
      dns_OLIMEX[3] = dns4;
      EEPROM.put(20, dns_OLIMEX[0]);
      EEPROM.commit();
      EEPROM.put(21, dns_OLIMEX[1]);
      EEPROM.commit();
      EEPROM.put(22, dns_OLIMEX[2]);
      EEPROM.commit();
      EEPROM.put(23, dns_OLIMEX[3]);
      EEPROM.commit();
       request->send(200, "text/html", "Uspesna zmena DNS adresy: " + String(dns1) + "." + String(dns2) +"." + String(dns3) + "." + String(dns4) + "<br><a href=\"/\">Return to Home Page</a>");
       configuration = 1;
       delay(1000);
       configuration = 0;
      }

      //---------------------------------------------parametr gateway---------------------------
      
      else if((request->hasParam(PARAM_INPUT_12))&&(request->hasParam(PARAM_INPUT_13))&&(request->hasParam(PARAM_INPUT_14))&&(request->hasParam(PARAM_INPUT_15)))
      {
      gateway_1 = request->getParam(PARAM_INPUT_12)->value();
      gateway1 = gateway_1.toInt();
      gateway_2 = request->getParam(PARAM_INPUT_13)->value();
      gateway2 = gateway_2.toInt();
      gateway_3 = request->getParam(PARAM_INPUT_14)->value();
      gateway3 = gateway_3.toInt();
      gateway_4 = request->getParam(PARAM_INPUT_15)->value();
      gateway4 = gateway_4.toInt();
      gateway_OLIMEX[0] = gateway1;
      gateway_OLIMEX[1] = gateway2;
      gateway_OLIMEX[2] = gateway3;
      gateway_OLIMEX[3] = gateway4;
      EEPROM.put(30, gateway_OLIMEX[0]);
      EEPROM.commit();
      EEPROM.put(31, gateway_OLIMEX[1]);
      EEPROM.commit();
      EEPROM.put(32, gateway_OLIMEX[2]);
      EEPROM.commit();
      EEPROM.put(33, gateway_OLIMEX[3]);
      EEPROM.commit();
       request->send(200, "text/html", "Uspesna zmena IP adresy brany: " + String(gateway1) + "." + String(gateway2) +"." + String(gateway3) + "." + String(gateway4) + "<br><a href=\"/\">Return to Home Page</a>");
       configuration = 1;
       delay(1000);
       configuration = 0;
      }

      //----------------------------------------------------parametr subnet mask---------------------------
      
      else if((request->hasParam(PARAM_INPUT_16))&&(request->hasParam(PARAM_INPUT_17))&&(request->hasParam(PARAM_INPUT_18))&&(request->hasParam(PARAM_INPUT_19)))
      {
      subnet_1 = request->getParam(PARAM_INPUT_16)->value();
      subnet1 = subnet_1.toInt();
      subnet_2 = request->getParam(PARAM_INPUT_17)->value();
      subnet2 = subnet_2.toInt();
      subnet_3 = request->getParam(PARAM_INPUT_18)->value();
      subnet3 = subnet_3.toInt();
      subnet_4 = request->getParam(PARAM_INPUT_19)->value();
      subnet4 = subnet_4.toInt();
      subnet_OLIMEX[0] = subnet1;
      subnet_OLIMEX[1] = subnet2;
      subnet_OLIMEX[2] = subnet3;
      subnet_OLIMEX[3] = subnet4;
      EEPROM.put(40, subnet_OLIMEX[0]);
      EEPROM.commit();
      EEPROM.put(41, subnet_OLIMEX[1]);
      EEPROM.commit();
      EEPROM.put(42, subnet_OLIMEX[2]);
      EEPROM.commit();
      EEPROM.put(43, subnet_OLIMEX[3]);
      EEPROM.commit();
       request->send(200, "text/html", "Uspesna zmena IP adresy: " + String(subnet1) + "." + String(subnet2) +"." + String(subnet3) + "." + String(subnet4) + "<br><a href=\"/\">Return to Home Page</a>");
       variable = 1;
       EEPROM.put(0, variable);
       configuration = 1;
       delay(1000);
       configuration = 0;
      }
      
      //-------------------------------------------------------nedoslo k odeslani zadost GET--------------------
      
    else {
      request->send(200, "text/html", "Nedoslo k odeslani zpravy! <br><a href=\"/\">Return to Home Page</a>");
    }  
    //--------------------------------------------------------konec GET funkce-------------------------------------------------------------------------------------------------------------                           
  });  
  //---------------------------------------------------------konec formulare---------------------------------------------------------------------------------------------------------------------------------
   
  server.onNotFound(notFound);
  

//------------------------------------------------inicializace dlouheho stisknuti tlacitka reset_button-------------------------
  reset_button.attachLongPressStop(reset_function);


//----------------------------------------------nastaveni komunikace pro senzor BME280 na sbernici I2C---------------------
 I2CBME.begin(SDA , SCL , 100000); 


//-------------------------------------------------------inicializace a nastaveni vlaken----------------------------
 xTaskCreatePinnedToCore( //vlakno pro snimani stavu STOP
   loop_STOP,
   "LIS_STOP", //string pro jmeno vlakna
   10000, //velikost zasobniku
   NULL, //parametr tasku
   1, //priorita
   &Task1, //task, kde bezi program
   1); //jadro, na kterem bezi program
   
 delay(500);  // start-up 

 xTaskCreatePinnedToCore(
    loop_RUN, //vlakno pro snimani stavu RUN
    "LIS_RUN", //string pro jmeno vlakna
    10000, //velikost zasobniku
    NULL, //parametr tasku 
    1, //priorita
    &Task2, //task, kde bezi program
    0); //jadro, na kterem bezi program 
    
 delay(500);  // start-up 

 xTaskCreatePinnedToCore( //vlakno pro zmenu IP adresy
    IP_config, 
    "IP_config", //string pro jmeno vlakna
    10000, //velikost zasobniku
    NULL, //task handle
    1, //priorita
    &Task3, //task, kde bezi program
    0); //jadro, na kterem bezi program
 //------------------------------------------------------konec inicializace vlaken--------------------------------
    
}


//-----------------------------------------vlakno pro nastavovani novych sitovych adres-----------------
void IP_config( void * parameter)
{
  for(;;)
  {
   delay(1);
   reset_button.tick();
   
   if((EEPROM.read(0) == 1) && (configuration == 1))
   {  
   EEPROM.get(10, ip_OLIMEX[0]);
   EEPROM.get(11, ip_OLIMEX[1]);
   EEPROM.get(12, ip_OLIMEX[2]);
   EEPROM.get(13, ip_OLIMEX[3]);
   
   EEPROM.get(20, dns_OLIMEX[0]);
   EEPROM.get(21, dns_OLIMEX[1]);
   EEPROM.get(22, dns_OLIMEX[2]);
   EEPROM.get(23, dns_OLIMEX[3]);
   
   EEPROM.get(30, gateway_OLIMEX[0]);
   EEPROM.get(31, gateway_OLIMEX[1]);
   EEPROM.get(32, gateway_OLIMEX[2]);
   EEPROM.get(33, gateway_OLIMEX[3]);
   
   EEPROM.get(40, subnet_OLIMEX[0]);
   EEPROM.get(41, subnet_OLIMEX[1]);
   EEPROM.get(42, subnet_OLIMEX[2]);
   EEPROM.get(43, subnet_OLIMEX[3]);

   ETH.config(ip_OLIMEX, gateway_OLIMEX, subnet_OLIMEX, dns_OLIMEX); //konfigurace adres (IP, GATEWAY, SUBNET, DNS1)
   }
  }
}


//-------------------------------------------------vlakno pro odesilani STOP stavu do databaze----------------------------------
void loop_STOP( void * parameter )//vlakno pro cteni stavu STOP
{
 for (;;) {
 delay(55000); //zapisovani probiha kazdych 55 vterin
  
 if (eth_connected) 
 {
  HTTPClient http_STOP; //vytvoreni klienta pro odeslani dat ze sensoru
  
  http_STOP.begin("http://192.168.80.202/lisovna/ESP_savedata.aspx?id_op=1&id_str=" +String(EEPROM.read(5))+ "&takt=0"); //url adresa pro zapsani stavu stop do databaze
                                                                                                                        // id_op - identifikátor operace, id_str - identifikátor stroje, takt (0 nebo 1)
  int httpCode_STOP = http_STOP.GET(); //nacteni odpovedi ze serveru 
 
  if (httpCode_STOP > 0) //kontrola zapsani dat
  { 
   Serial.println("Upload STOP!");
   digitalWrite(LED_DATA, HIGH);
   delay(100);
   digitalWrite(LED_DATA, LOW);
   delay(100);
   digitalWrite(LED_DATA, HIGH);
   delay(100);
   digitalWrite(LED_DATA, LOW);
  }
   else 
   {
    Serial.println("Error on HTTP_STOP request");//nedoslo k zapsani dat
   }
  http_STOP.end(); //uvolneni http klienta
  }  
 }
}


//-------------------------------------------------------------vlakno pro kontrolu a zapsani stavu RUN do databaze-------------------------------
void loop_RUN( void * parameter)//vlakno pro kontrolu taktu lisu
{
  for (;;) 
  {
  if (eth_connected) 
   {
    buttonState = digitalRead(DI2);
    if (buttonState != lastButtonState)
    {
      delay(1);
    if((digitalRead(DI2) == LOW)) //doslo k taktu
    {
   HTTPClient http_RUN; //vytvoreni klienta pro odeslani dat RUN
   Serial.println(EEPROM.read(5));
   http_RUN.begin("http://192.168.80.202/lisovna/ESP_savedata.aspx?id_op=1&id_str=" +String(EEPROM.read(5))+ "&takt=1");//url adresa pro zapsani stavu run do databaze
   
   int httpCode_RUN = http_RUN.GET(); //nacteni odpovedi ze serveru 
 
   if (httpCode_RUN > 0) //kontrola zapsani dat
   {
        Serial.println("Upload RUN!");
        digitalWrite(LED_DATA, HIGH);
        delay(100);
        digitalWrite(LED_DATA, LOW);
        delay(100);
        digitalWrite(LED_DATA, HIGH);
        delay(100);
        digitalWrite(LED_DATA, LOW);           
       }
   else 
   {
   Serial.println("Error on HTTP RUN request"); //nedoslo k zapsani dat
   Serial.println(httpCode_RUN);
   }
   http_RUN.end(); //uvolneni http klienta 
   }
  }
  lastButtonState = buttonState;
 }
 delay(1);
 }
}


//--------------------------------------------loop smycka (vlakno) pro cteni dat ze senzoru BME280 a odeslani dat do databaze-----------------------------------------
void loop()
{
 bool status = bme.begin(0x76, &I2CBME); //pripojeni k I2C sbernici

//-----------------------------nedoslo ke spojeni BME280 ke sbernici I2C--------------------------
 if (!status) 
 {
  Serial.println("Senzor BME280 nenalezen, zkontrolujte připojení!");
  if (eth_connected)
  {
  HTTPClient http_BME280_ERROR; //vytvoreni klienta pro odeslani ERROR stavu do databaze
  
  http_BME280_ERROR.begin("http://192.168.80.202/lisovna/ESP_savedata.aspx?id_op=2&id_str=" +String(EEPROM.read(5))+ "&ip_addr=" +String(ip_OLIMEX[3])+ "&tepl=0&vlh=0&tlak=0");
  //url adresa pro zapsani ERROR stavu do databaze
  // id_op - identifikátor operace, id_str - identifikátor stroje, odesílaná data (teplota, vlhkost, tlak), koncovka IP adresy
   
  int httpCode_BME280_ERROR = http_BME280_ERROR.GET(); //nacteni odpovedi ze serveru
 
  if (httpCode_BME280_ERROR > 0) //kontrola zapsani data 
   {
    Serial.println("Upload BME280 ERROR!");
    digitalWrite(LED_DATA, HIGH);
    delay(100);
    digitalWrite(LED_DATA, LOW);
    delay(100);
    digitalWrite(LED_DATA, HIGH);
    delay(100);
    digitalWrite(LED_DATA, LOW);
   }
    else 
   {
    Serial.println("Error on HTTP BME280 request");//nedoslo k zapsani dat
   }
  http_BME280_ERROR.end(); //uvolneni http klienta
  }
  delay(3600000);
 }

//--------------------------doslo ke spojeni BME280 ke sbernici I2C---------------------------
 else
 {
 vlhkost = bme.readHumidity(); //nacteni vlhkosti
 teplota = bme.readTemperature(); //nacteni teploty
 tlak = (bme.readPressure()/100.0F); //nacteni tlaku 

 if (eth_connected) 
 { 
  HTTPClient http_BME280; //vytvoreni klienta pro odeslani dat ze sensoru
  
  http_BME280.begin("http://192.168.80.202/lisovna/ESP_savedata.aspx?id_op=2&id_str=" +String(EEPROM.read(5))+ "&ip_addr=" +String(ip_OLIMEX[3])+ "&tepl=" +String(teplota)+ "&vlh=" +String(vlhkost)+ "&tlak=" +String(tlak)+ "");
  //url adresa pro zapsani hodnot ze senzoru do databaze 
  // id_op - identifikátor operace, id_str - identifikátor stroje, odesílaná data (teplota, vlhkost, tlak), koncovka IP adresy
   
  int httpCode_BME280 = http_BME280.GET(); //nacteni odpovedi ze serveru

  if (httpCode_BME280 > 0) //kontrola zapsani data 
   {
    Serial.println("Upload BME280!");
    digitalWrite(LED_DATA, HIGH);
    delay(100);
    digitalWrite(LED_DATA, LOW);
    delay(100);
    digitalWrite(LED_DATA, HIGH);
    delay(100);
    digitalWrite(LED_DATA, LOW);
   }
   else 
   {
    Serial.println("Error on HTTP BME280 request");//nedoslo k zapsani dat
   }
  http_BME280.end(); //uvolneni http klienta
  }
  delay(3600000);
 }  
}
