#include <LiquidCrystal_I2C.h>
#include "LCD_1602_RUS.h"
#include <WiFi.h>
//#include <PubSubClient.h>
#include "GyverButton.h"
#include "GyverNTP.h"
/* 
Вы не можете использовать АЦП ни на одном из каналов 
ADC2 при включенном Wi-Fi: GPIO4, GPIO0, GPIO2, GPIO15, 
GPIO13, GPIO12, GPIO14, GPIO27, GPIO25 и GPIO26.
Но вы можете использовать ADC1, который использует:
GPIO36, GPIO37, GPIO38, GPIO39, GPIO32, GPIO33, GPIO34 и GPIO35.
https://arduino.stackexchange.com/questions/84888/analog-read-not-working-while-using-wifi
*/


#define BUT1 18
#define BUT2 5
#define BUT3 17
#define BUT4 16

#define ON true
#define OFF false

#define POWER 0
#define MQTT 1
#define NTP 2
#define WIFI 3
#define HOT 4
#define COLD 5
#define STOP 6

GButton but1(BUT1, HIGH_PULL);
GButton but2(BUT2, HIGH_PULL);
GButton but3(BUT3, HIGH_PULL);
GButton but4(BUT4, HIGH_PULL);

int START_temperature, END_temperature = 0; 
float CURR_temperature = 0; 
const int NTC_PIN = 35;
const int NTC_REFERENCE_RESISTANCE = 10000;
const int NTC_REFERENCE_TEMPERATURE = 25;
const int NTC_SERIES_RESISTOR = 10000;
const int BETA = 3950; 
bool Relay_status, 
     Power_icon_sw, 
     Proc_type, 
     WiFi_icon_sw;
const char* ssid = "Wokwi-GUEST";
const char* password = "";           

LCD_1602_RUS lcd(0x27, 16, 2);
GyverNTP ntp(3);

// const char* mqtt_server = "public.cloud.shiftr.io";
// const char* mqtt_username = "";
// const char* mqtt_password = "";
// const char* ClientID = "term_00FDAA";
// const int mqtt_port = 1883;

// WiFiClient WiFiClient;
// PubSubClient client(WiFiClient);

byte power_ico[8] = { 
 0B00011,
 0B00110, 
 0B01100, 
 0B11111, 
 0B00010, 
 0B00100, 
 0B01000, 
 0B10000 };
//byte mqtt_ico [8] = { 0b00000, 0b01110, 0b10001, 0b00100, 0b01010, 0b00000, 0b00100, 0b01110 };
byte mqtt_ico [8] = {
	0b00000,
	0b11011,
	0b00101,
	0b11010,
	0b00101,
	0b11010,
	0b11010,
	0b00000
};
byte ntp_ico [8]  = { 0B10001, 0B01110,	0B11011, 0B11011,	0B11001, 0B11111,	0B01110, 0B10001 };
byte wifi_ico [8] = { 0b00001, 0b00001, 0b00001, 0b00001, 0b00101, 0b00101, 0b10101, 0b10101 };
byte hot_ico[8]   = { 0b00000, 0b10010,	0b01001, 0b10010,	0b01001, 0b10010,	0b01001, 0b00000 };
byte cold_ico[8]  = { 0b00100, 0b10101, 0b01010, 0b10101, 0b01010, 0b10101, 0b00100, 0b00000 };
byte stop_ico[8]  = { 0b00000, 0b01110, 0b11111, 0b10001, 0b10001, 0b11111, 0b01110, 0b00000 };

unsigned long C_01_sec, P_01_sec,
              C_03_sec, P_03_sec,
              C_05_sec, P_05_sec, 
              C_1_sec,  P_1_sec, 
              C_60_sec, P_60_sec; // = millis();

void setup() {
  
  Serial.begin(9600);
  lcd.init();
  lcd.backlight();

lcd.createChar(0, power_ico); 
lcd.createChar(1, mqtt_ico);
lcd.createChar(2, ntp_ico);
lcd.createChar(3, wifi_ico);
lcd.createChar(4, hot_ico);
lcd.createChar(5, cold_ico);
lcd.createChar(6, stop_ico);

DrawTemp(0); DrawTemp(1); DrawTemp(2);
pinMode(25, OUTPUT);


  WiFi.mode (WIFI_STA);
  WiFi.begin (ssid, password);
  Serial.print ("connecting");
  ntp.begin();
  //Icon(MQTT,ON);
  
 
}


//============================loop=======================================
void loop() {
  delay(10);

  but1.tick(); but2.tick(); but3.tick(); but4.tick();
  ntp.tick();

  if (but1.isPress()) {START_temperature--; DrawTemp (0); tone(26, 900, 10);}
  if (but2.isPress()) {START_temperature++; DrawTemp (0); tone(26, 900, 10);}
  if (but3.isPress()) {END_temperature--; DrawTemp (1); tone(26, 900, 10);}
  if (but4.isPress()) {END_temperature++; DrawTemp (1); tone(26, 900, 10);}





//--------------------------------------------------------------------




C_01_sec = C_03_sec = C_05_sec = C_1_sec = C_60_sec = millis();

//---------------------- 0.1 секунды --------------------------------
 if (C_01_sec - P_01_sec > 100) { 
    //  замер с термометра  
  int analogValue = analogRead(NTC_PIN);
  float resistance = NTC_SERIES_RESISTOR / (4095.0 / analogValue - 1.0);
  float tempK = 1.0 / (1.0 / (NTC_REFERENCE_TEMPERATURE + 273.15) + log(resistance / NTC_REFERENCE_RESISTANCE) / BETA);
  CURR_temperature = tempK - 273.15;
  DrawTemp (2);


   P_01_sec = C_01_sec;
}


//---------------------- 0.3 секунды --------------------------------
if (C_03_sec - P_03_sec > 300) {

  // иконка реле
  if (Relay_status) {
        Power_icon_sw = !Power_icon_sw;
        if (Power_icon_sw) Icon(POWER, ON); 
        if (!Power_icon_sw) Icon(POWER, OFF);
  } else {Icon(POWER, OFF); Power_icon_sw = OFF;}

  P_03_sec = C_03_sec;
}

//---------------------- 0.5 секунды --------------------------------
if (C_05_sec - P_05_sec > 500) { 

  // подключение WiFi + иконка
  if (WiFi.status() != WL_CONNECTED) {
    WiFi_icon_sw = !WiFi_icon_sw;
    if (WiFi_icon_sw) Icon (WIFI, ON);
    if (!WiFi_icon_sw) Icon (WIFI, OFF);
    } else {Icon (WIFI, ON); WiFi_icon_sw = OFF; if(!ntp.synced()) ntp.updateNow();}


   P_05_sec = C_05_sec;
}


//---------------------- 1 секунда ----------------------------------
if (C_1_sec - P_1_sec > 1000) { 
// ВЫВОД ЧАСОВ
if (ntp.synced()){Icon(NTP, ON);} else {Icon(NTP, OFF);}
lcd.setCursor (1, 0); lcd.print(ntp.timeString());
//if (client.connected()) {Icon(MQTT,ON);} else {Icon(MQTT,OFF);}

//if (!client.connected()) {client.connect(ClientID, mqtt_username, mqtt_password);}


//Serial.println(client.state());



if (START_temperature == END_temperature) {Relay_status = OFF; Icon(STOP, ON); } else {
    if (START_temperature < END_temperature) { Icon(HOT, ON); // греем
      if (CURR_temperature < START_temperature) {Relay_status = ON;} else if (CURR_temperature > END_temperature) {Relay_status = OFF;}
    }

    if (START_temperature > END_temperature) { Icon(COLD, ON); // холодим
      if (CURR_temperature > START_temperature) {Relay_status = ON;} else if (CURR_temperature < END_temperature) {Relay_status = OFF;}
    } 

}
digitalWrite(25, Relay_status);


Serial.print(START_temperature);  
Serial.print(" "); 
Serial.print(CURR_temperature); 
Serial.print(" "); 
Serial.println(END_temperature);

P_1_sec = C_1_sec;
}


//---------------------- 60 секунд ----------------------------------
if (C_60_sec - P_60_sec > 60000) { 
   



   P_60_sec = C_60_sec;
}

}//=====================end loop==============================================




void DrawTemp(int type_temp) {
// 0 - стартовая температура
// 1 - конечная температура
// 2 - текущая температура

if (type_temp == 0) {char temp_buff[5]; lcd.setCursor(0, 1); sprintf(temp_buff,"% 3d°", START_temperature); lcd.print(temp_buff);}
if (type_temp == 1) {char temp_buff[5]; lcd.setCursor(12, 1); sprintf(temp_buff,"% 3d°", END_temperature); lcd.print(temp_buff);}
if (type_temp == 2) {char temp_buff[7]; lcd.setCursor(5, 1); sprintf(temp_buff,"% 5.1f°", CURR_temperature); lcd.print(temp_buff);}
}

void Icon(int num_icon, bool enabled) {
// 0 - POWER
// 1 - MQTT
// 2 - NTP
// 3 - WIFI
// 4 - HOT
// 5 - COLD
// 6 - STOP
if (num_icon == 0) {if (enabled) {lcd.setCursor(12, 0); lcd.write(0);} else {lcd.setCursor(12, 0); lcd.print(" ");}}
if (num_icon == 1) {if (enabled) {lcd.setCursor(13, 0); lcd.write(1);} else {lcd.setCursor(13, 0); lcd.print(" ");}}
if (num_icon == 2) {if (enabled) {lcd.setCursor(14, 0); lcd.write(2);} else {lcd.setCursor(14, 0); lcd.print(" ");}}
if (num_icon == 3) {if (enabled) {lcd.setCursor(15, 0); lcd.write(3);} else {lcd.setCursor(15, 0); lcd.print(" ");}}
if (num_icon == 4) {if (enabled) {lcd.setCursor(11, 0); lcd.write(4);} else {lcd.setCursor(11, 0); lcd.print(" ");}}
if (num_icon == 5) {if (enabled) {lcd.setCursor(11, 0); lcd.write(5);} else {lcd.setCursor(11, 0); lcd.print(" ");}}
if (num_icon == 6) {if (enabled) {lcd.setCursor(11, 0); lcd.write(6);} else {lcd.setCursor(11, 0); lcd.print(" ");}}
}

NOCOMNCVCCGNDINLED1PWRRelay Module