Arduino mega stops sending via UART

Hello, guys.. my I have tested some sensors all together just to make sure everithing working than I would like to start put everithing to objects etc.. but now as far I got sometimes temperature sensors part stop working to be exact no values on the Nextion display. Sometimes it start with no rtc time change on display. But the LED 13 stil blinking so looks like arduino it self is running.
Looking to it second day and no luck find out.

#include <RTClib.h>
#include <Wire.h>

#include <Nextion.h>
#include <DHT.h>

#include <OneWire.h>
#include <DallasTemperature.h>
#include <SoftwareSerial.h>

int systemFlowCheckInt = 1;
int systemFlowCheckLastInt = 0;

int actualTimeInterval = 0;
int time_will_read_at = 0;
int time_read_interval = 1;

RTC_DS3231 rtc;

DateTime now;
int actual_day = 1;
int actual_month = 1;
int actual_year = 2022;
int actual_hour = 0;
int actual_minute = 0;
int actual_second = 0;

#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

float hum = 0;
float temp = 0;

#define ONE_WIRE_BUS 4
OneWire oneWire(ONE_WIRE_BUS);



//Electric heater
int el_heat_dayli=0;//0 night 1 day 2 away
int el_heat_on_level_day = 20; //degree celsius
int el_heat_on_level_night = 16; //degree celsius

int el_heater_check_interval = 30; //minutes
int el_heater_temperature_will_check_at = 0; //minutes
#define el_heater 25

//Temperature sensors
String stringOne = "";

int temperature_will_check_at = 4;
int temperature_check_interval = 3;
DallasTemperature sensors(&oneWire);
int numberOfDevices = 0;
DeviceAddress tempDeviceAddress;
float actual_inside_temp = 0;
float actual_outside_temp = 0;
float actual_temp_switch_board = 0;
bool wasTempInside = false;
bool wasTempOutside = false;
bool wasTempSwitchBoard = false;
/* TEMPERATURE END*/



#define ledPin 13
#define mainLIghtPin 23
#define boilerPin 25
#define D_MAX_PROGRESS_VALUE 100

NexDSButton btnMainLIght = NexDSButton(4, 1, "bt0");
NexDSButton btnBoiler = NexDSButton(3, 5, "bt6");
NexProgressBar boilerTimerProgressBar1 = NexProgressBar(3, 15, "j0");
NexProgressBar boilerTimerProgressBar2 = NexProgressBar(2, 18, "j0");
NexDSButton btnNight = NexDSButton(2, 8, "bt8");
NexDSButton btnDay = NexDSButton(2, 58, "bt5");

NexNumber boilerTimerValue1 = NexNumber(3, 20, "n0");
NexNumber boilerTimerValue2 = NexNumber(2, 34, "n0");


int prescaler_timeconstantvalue = 49900; //ONE SECOND 49900





NexTouch *nex_listen_list[] = {
  &btnMainLIght,
  &btnBoiler,
  &btnNight,
  &btnDay,
  NULL
};


void btnMainLIghtPushCallback(void *ptr)
{
  uint32_t dual_state = 0;
  btnMainLIght.getValue(&dual_state);

  if (dual_state == 1)
  {
    //Serial2.print("Světla zapnuta");
    digitalWrite(mainLIghtPin, LOW);


  }
  else if (dual_state == 0)
  {
    //Serial2.print("Světla vypnuta");
    digitalWrite(mainLIghtPin, HIGH);
  }

}
void btnNightPushCallback(void *ptr)
{

  Serial.print("Night");
  
  el_heat_dayli = 0;


}
void btnDayPushCallback(void *ptr)
{
  Serial.print("Day");

  el_heat_dayli=1;




}
/*
  void btnMainLIghtPopCallback(void *ptr)
  {
  //digitalWrite(LED_BUILTIN, LOW);
  }
*/
void btnBoilerPushCallback(void *ptr)
{
  /*
  uint32_t dual_state = 0;
  btnBoiler.getValue(&dual_state);

  if (dual_state == 1)
  {
    
    WATER_HEATER_ON = true;
    Serial2.print("Water heater manual on");
    digitalWrite(boilerPin, LOW);

    WATER_HEATER_BTN_CHANGED = true;

    //read timer on value and save it to
    //WATER_HEATER_TIMER_ON
    WATER_HEATER_TIMER_ON = true;


    if (WATER_HEATER_TIMER_ON) {
      WATER_HEATER_TIMER_VALUE = WATER_HEATER_INTERVAL;
      WATER_BOILER_PROGRESS_VALUE = D_MAX_PROGRESS_VALUE;
      boilerTimerProgressBar1.setValue(100);
      // boilerTimerProgressBar2.setValue(100);


    }

  }

  if (dual_state == 0)
  {
    WATER_HEATER_ON = false;
    WATER_HEATER_TIMER_ON = false;
    WATER_HEATER_BTN_CHANGED = false;
    Serial2.print("Water heater manual off");
    digitalWrite(boilerPin, HIGH);

    boilerTimerValue1.setValue(0);
    //boilerTimerValue2.setValue(0);
    boilerTimerProgressBar1.setValue(0);
    //boilerTimerProgressBar2.setValue(0);
  }



*/

}
/*
  void btnBoilerPopCallback(void *ptr)
  {
  //digitalWrite(LED_BUILTIN, LOW);
  }
*/

void setup(void) {

  noInterrupts();
  TCCR1A = 0;
  TCCR1B = 0;

  TCNT1 = prescaler_timeconstantvalue;            // preload timer
  TCCR1B |= (1 << CS10) | (1 << CS12);  // 1024 prescaler
  TIMSK1 |= (1 << TOIE1);               // enable timer overflow interrupt ISR
  interrupts();                         // enable all interrupts

  Serial2.begin(115200);
  delay(500);
  Serial2.print("Baud=9600");
  Serial2.write(0xff);
  Serial2.write(0xff);
  Serial2.write(0xff);
  Serial2.end();

  Serial2.begin(9600);

  //Serial2.begin(115200);
  //Serial.begin(9600);
  //Serial.print("Baud=115200");

  nexInit();

  dht.begin();

  Wire.begin();
  rtc.begin();
  rtc.adjust(DateTime(2022, 9, 22, 23, 29, 0));



  delay(1000);





  btnMainLIght.attachPush(btnMainLIghtPushCallback);
  //btnMainLIght.attachPop(btnMainLIghtPopCallback);
  btnBoiler.attachPush(btnBoilerPushCallback);
  //btnBoiler.attachPop(btnBoilerPopCallback);

  btnNight.attachPush(btnNightPushCallback);
  btnDay.attachPush(btnDayPushCallback);



  

  pinMode(ledPin, OUTPUT);
  pinMode(mainLIghtPin, OUTPUT);
  digitalWrite(mainLIghtPin, HIGH);
  //pinMode(mainLIght, INPUT_PULLUP);

  pinMode(el_heater, OUTPUT);
  digitalWrite(el_heater, HIGH);

  pinMode(boilerPin, OUTPUT);
  digitalWrite(boilerPin, HIGH);

  

  /* TEMP */
  // Start up the library
  sensors.begin();

  // Grab a count of devices on the wire
  numberOfDevices = sensors.getDeviceCount();
  /*
    Serial2x.print("Locating temperature sensors...");
    Serial2x.print("Found ");
    Serial2x.print(numberOfDevices, DEC);
    Serial2x.println(" sensors.");
  */

  for (int i = 0; i < numberOfDevices; i++) {

    if (sensors.getAddress(tempDeviceAddress, i)) {
      Serial.print("Nalezen teplotní sensor ");
      Serial.print(1, DEC);
      Serial.println();
    } else {
      Serial.print("Nalezen teplotní sensor bez adresy");
      Serial.print(i, DEC);
      Serial.println();
    }
  }
  /* TEMP END*/

}
//void (*resetFunc)(void)=0;

ISR(TIMER1_OVF_vect)                    // interrupt service routine for overflow
{


  TCNT1 = prescaler_timeconstantvalue;                                // preload timer
  digitalWrite(ledPin, digitalRead(ledPin) ^ 1);  //Turns LED ON and OFF

/*
  actualTimeInterval++;
  if (actualTimeInterval > 59) {
    actualTimeInterval = 0;
    
  }
*/



/*
  if (WATER_HEATER_TIMER_ON) {
    WATER_HEATER_TIMER_PREVIOUS_VALUE = WATER_HEATER_TIMER_VALUE;
    WATER_HEATER_TIMER_VALUE = WATER_HEATER_TIMER_VALUE - 1;
    WATER_BOILER_PROGRESS_VALUE = WATER_BOILER_PROGRESS_VALUE - WATER_BOILER_PROGRESS_STEP;
    WATER_HEATER_TIMER_ONE_CYCLE_SEMAPHOR = true;

    if (WATER_HEATER_TIMER_VALUE <= 0) {
      WATER_HEATER_ON = false;
      WATER_HEATER_TIMER_VALUE = 0;
      WATER_HEATER_TIMER_ON = false;
      digitalWrite(boilerPin, HIGH);//HIGH
      Serial2.println("Water heater end on timer off");

    }
  }

  */
  /*
    Serial.print("time_last_readed> ");
          Serial.print(time_last_readed);
          Serial.print("interval> ");
          Serial.println(actualTimeInterval);
  */
}

void getSensors(void) {

  




  //--------------------- TEMPERATURE
  /*
  if ((temperature_will_check_at < actual_second)) {
    temperature_will_check_at = actual_second + temperature_check_interval;
    if (temperature_will_check_at >= 60)temperature_will_check_at = temperature_will_check_at - 60;
*/
    Serial.println("Načítání teplot:");

    numberOfDevices = sensors.getDeviceCount();
    sensors.requestTemperatures();

    for (int i = 0; i <= numberOfDevices; i++) {
      // Search the wire for address
      if (sensors.getAddress(tempDeviceAddress, i)) {


        if (i == 0) {

          actual_inside_temp = sensors.getTempC(tempDeviceAddress);
          //Serial.print("Teplota uvnitř: ");Serial.println(actual_inside_temp);
          //Serial2x.print(tempC);
          stringOne = "t3.txt=\"";
          stringOne.concat(String(actual_inside_temp, 1));
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.print(stringOne);
          Serial2.print(F("\""));
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.write(0xff);

          wasTempInside = true;
          //Serial2.println("...");
        }
        if (i == 1) {

          actual_outside_temp = sensors.getTempC(tempDeviceAddress);
          //Serial.print("Teplota rozvaděč: ");Serial.println(actual_temp_switch_board);
          //Serial2x.print(tempC);
          stringOne = "t0.txt=\"";
          stringOne.concat(String(actual_outside_temp, 1));
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.print(stringOne);
          Serial2.print(F("\""));
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.write(0xff);

          wasTempOutside = true;
          //Serial2.println("...");
        }
        if (i == 2) {

          actual_temp_switch_board = sensors.getTempC(tempDeviceAddress);
          //Serial.print("Teplota venku: ");Serial.println(actual_outside_temp);
          //Serial2x.print(tempC);
          stringOne = "t8.txt=\"";
          stringOne.concat(String(actual_temp_switch_board, 1));
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.print(stringOne);
          Serial2.print(F("\""));
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.write(0xff);

          wasTempSwitchBoard = true;
          //Serial2.println("...");
        }


        if (!wasTempInside) {
          //Serial2.print("Teplota uvnitř neodpovídá... ");

          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.print(F("t3.txt=\"ERR"));
          Serial2.print(F("\""));
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.write(0xff);


        }
        
      }
    }//For end
    
    if (!wasTempOutside) {
      //Serial2.print("Teplota venku neodpovídá... ");

      Serial2.write(0xff);
      Serial2.write(0xff);
      Serial2.write(0xff);
      Serial2.print(F("t0.txt=\"ERR"));
      Serial2.print(F("\""));
      Serial2.write(0xff);
      Serial2.write(0xff);
      Serial2.write(0xff);


    }
    if (!wasTempSwitchBoard) {
      //Serial2.print("Teplota v rozvaděči neodpovídá... ");

      Serial2.write(0xff);
      Serial2.write(0xff);
      Serial2.write(0xff);
      Serial2.print(F("t8.txt=\"ERR"));
      Serial2.print(F("\""));
      Serial2.write(0xff);
      Serial2.write(0xff);
      Serial2.write(0xff);


    }



    wasTempInside = false;
    wasTempOutside = false;
    wasTempSwitchBoard = false;



    /* HUMIDITY */
    hum = dht.readHumidity();

    //temp = dht.readTemperature();


    //Serial.print("Vlhkost: ");
    //Serial.print(hum);
    //Serial.print(" %, Teplota: ");
    //Serial.print(temp);
    //Serial.println(" Celsius");
    /* HUMIDITY */

    stringOne = "t17.txt=\"";
    stringOne.concat(String(hum, 1));
    Serial2.write(0xff);
    Serial2.write(0xff);
    Serial2.write(0xff);
    Serial2.print(stringOne);
    Serial2.print(F("\""));
    Serial2.write(0xff);
    Serial2.write(0xff);
    Serial2.write(0xff);



  //}//interval check






  //Serial.print(stringOne);

}

void loop() {
  nexLoop(nex_listen_list);
  

  
  //Get time

/*
  if ((time_will_read_at <= actualTimeInterval)) {
    time_will_read_at = actualTimeInterval + time_read_interval;
    if (time_will_read_at >= 60)time_will_read_at = time_will_read_at - 60;
    */
    Serial.println("Time read");
    now = rtc.now();

    actual_day = now.day();
    actual_month = now.month();
    actual_year = now.year();
    actual_hour = now.hour();
    actual_minute = now.minute();
    actual_second = now.second();


    //sprintf(t, "%02d:%02d:%02d %02d/%02d/%02d", now.hour(), now.minute(), now.second(), now.day(), now.month(), now.year());
    String stringOne = "t2.txt=\"22";
    stringOne.concat(String(actual_hour, 1));
    stringOne.concat(":");
    stringOne.concat(actual_minute);
    stringOne.concat(":");
    stringOne.concat(actual_second);


    Serial2.write(0xff);
    Serial2.write(0xff);
    Serial2.write(0xff);
    Serial2.print(stringOne);
    Serial2.print(F("\""));
    Serial2.write(0xff);
    Serial2.write(0xff);
    Serial2.write(0xff);

    //Serial.print(stringOne);

    stringOne = "t1.txt=\"22";
    stringOne.concat(String(actual_day, 1));
    stringOne.concat(".");
    stringOne.concat(actual_month);
    stringOne.concat(".");
    stringOne.concat(actual_year);


    Serial2.write(0xff);
    Serial2.write(0xff);
    Serial2.write(0xff);
    Serial2.print(stringOne);
    Serial2.print(F("\""));
    Serial2.write(0xff);
    Serial2.write(0xff);
    Serial2.write(0xff);
  //}


  getSensors();

  
    if((el_heater_temperature_will_check_at<=actual_second)){
            el_heater_temperature_will_check_at = actual_second+el_heater_check_interval;
             if (el_heater_temperature_will_check_at>=60)el_heater_temperature_will_check_at=el_heater_temperature_will_check_at-60;

      Serial.println("Heater check");
      if (el_heat_dayli==0){
    
        if(actual_inside_temp<el_heat_on_level_night){
  
          digitalWrite(el_heater, LOW);
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.print(F("t24.txt=\"N-Topí"));
          Serial2.print(F("\""));
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.write(0xff);
        }else{
          digitalWrite(el_heater, HIGH);
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.print(F("t24.txt=\"N-Netopí"));
          Serial2.print(F("\""));
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.write(0xff);
        }
      }
      if (el_heat_dayli==1){
    
        if(actual_inside_temp<el_heat_on_level_day){
  
          digitalWrite(el_heater, LOW);
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.print(F("t24.txt=\"D-Topí"));
          Serial2.print(F("\""));
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.write(0xff);
        }else{
          digitalWrite(el_heater, HIGH);
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.print(F("t24.txt=\"D-Netopí"));
          Serial2.print(F("\""));
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.write(0xff);
        }
      }
    }
  



}

Test everything separately. If everything works then put it together, piece by piece, starting with the display.

@zdenekrejci:

:warning: In the "Italian" section of the forum you can only write in Italian ... so please, next time pay more attention in which section you put your posts.

Your thread has been moved to the English language section. :slight_smile:

Guglielmo

appologize.. thanks

my attempt to compiled the posted code found nexLoop() nexInit() missing.

are you testing the posted code or a previously loaded version

An int on a Mega is 16 bits which can only go up to 32767. It should probably be const unsigned long

im not sure how you meant your post - add nexInit before nexLoop? BTW is it not enaught to have nexInit in setup()?

BTW I just saw that on nextion there is after start wrong time i have set rtc.adjust(DateTime(2022, 9, 22, 23, 29, 0)); but what i see on screen is not 2022,9,22,23,29,0 but 2022,9,22,22,29,0... not sure it it has something to do with the problem that its stopning after time?

I changed this one and testing it now...

So I removed part with RTC and tested over night and works so look like continuing reading RTC is causing the stoping.
Do you have idea what is wrong with RTC?

If there is noise no the I2C bus, it can hang and will wait forever. Newer versions of the Wire library have a timeout feature so you are able to regain control and reset the bus if it hangs.

I have now edited few things as you guys sugested works ok with no hangs but with one exception. The time is still not valid if I adjust hours its still 22 on output.

#include <Nextion.h>

#include <RTClib.h>



#include <DHT.h>

#include <OneWire.h>
#include <DallasTemperature.h>
#include <SoftwareSerial.h>
#include <Wire.h>

//int systemFlowCheckInt = 1;
//int systemFlowCheckLastInt = 0;

int actualTimeInterval = 0;
int time_will_read_at = 0;
int time_read_interval = 10;

RTC_DS1307 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};


int actual_day = 0;
int actual_month = 0;
int actual_year = 0;
int actual_hour = 0;
int actual_minute = 0;
int actual_second = 0;

#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

float hum = 0;
float temp = 0;

#define ONE_WIRE_BUS 4
OneWire oneWire(ONE_WIRE_BUS);

#define NIGHT 0
#define DAY 1
#define HOME 2
#define AWAY 3

//Electric heater
int el_heat_dayli=NIGHT;//0 night 1 day 2 away
int el_heat_on_level_day = 18; //degree celsius
int el_heat_on_level_night = 15; //degree celsius

int el_heater_check_interval = 1; //minutes
int el_heater_temperature_will_check_at = 0; //minutes
#define el_heater 25

//Temperature sensors
String stringOne = "";

int temperature_will_check_at = 4;
int temperature_check_interval = 3;
DallasTemperature sensors(&oneWire);
int numberOfDevices = 0;
DeviceAddress tempDeviceAddress;
float actual_inside_temp = 0;
float actual_outside_temp = 0;
float actual_temp_switch_board = 0;
bool wasTempInside = false;
bool wasTempOutside = false;
bool wasTempSwitchBoard = false;
/* TEMPERATURE END*/



#define ledPin 13
#define mainLIghtPin 23
#define boilerPin 25
#define D_MAX_PROGRESS_VALUE 100

NexDSButton btnMainLIght = NexDSButton(4, 1, "bt0");
NexDSButton btnBoiler = NexDSButton(3, 5, "bt6");
NexProgressBar boilerTimerProgressBar1 = NexProgressBar(3, 15, "j0");
NexProgressBar boilerTimerProgressBar2 = NexProgressBar(2, 18, "j0");
NexDSButton btnNight = NexDSButton(2, 8, "bt8");
NexDSButton btnDay = NexDSButton(2, 58, "bt5");

NexNumber boilerTimerValue1 = NexNumber(3, 20, "n0");
NexNumber boilerTimerValue2 = NexNumber(2, 34, "n0");


const unsigned long prescaler_timeconstantvalue = 49900; //ONE SECOND 49900 OR 49900???





NexTouch *nex_listen_list[] = {
  &btnMainLIght,
  &btnBoiler,
  &btnNight,
  &btnDay,
  NULL
};


void btnMainLIghtPushCallback(void *ptr)
{
  uint32_t dual_state = 0;
  btnMainLIght.getValue(&dual_state);

  if (dual_state == 1)
  {
    //Serial2.print("Světla zapnuta");
    digitalWrite(mainLIghtPin, LOW);


  }
  else if (dual_state == 0)
  {
    //Serial2.print("Světla vypnuta");
    digitalWrite(mainLIghtPin, HIGH);
  }

}
void btnNightPushCallback(void *ptr)
{

  Serial.print("Night");
  
  el_heat_dayli = NIGHT;


}
void btnDayPushCallback(void *ptr)
{
  Serial.print("Day");

  el_heat_dayli=DAY;




}
/*
  void btnMainLIghtPopCallback(void *ptr)
  {
  //digitalWrite(LED_BUILTIN, LOW);
  }
*/
void btnBoilerPushCallback(void *ptr)
{
  /*
  uint32_t dual_state = 0;
  btnBoiler.getValue(&dual_state);

  if (dual_state == 1)
  {
    
    WATER_HEATER_ON = true;
    Serial2.print("Water heater manual on");
    digitalWrite(boilerPin, LOW);

    WATER_HEATER_BTN_CHANGED = true;

    //read timer on value and save it to
    //WATER_HEATER_TIMER_ON
    WATER_HEATER_TIMER_ON = true;


    if (WATER_HEATER_TIMER_ON) {
      WATER_HEATER_TIMER_VALUE = WATER_HEATER_INTERVAL;
      WATER_BOILER_PROGRESS_VALUE = D_MAX_PROGRESS_VALUE;
      boilerTimerProgressBar1.setValue(100);
      // boilerTimerProgressBar2.setValue(100);


    }

  }

  if (dual_state == 0)
  {
    WATER_HEATER_ON = false;
    WATER_HEATER_TIMER_ON = false;
    WATER_HEATER_BTN_CHANGED = false;
    Serial2.print("Water heater manual off");
    digitalWrite(boilerPin, HIGH);

    boilerTimerValue1.setValue(0);
    //boilerTimerValue2.setValue(0);
    boilerTimerProgressBar1.setValue(0);
    //boilerTimerProgressBar2.setValue(0);
  }



*/

}
/*
  void btnBoilerPopCallback(void *ptr)
  {
  //digitalWrite(LED_BUILTIN, LOW);
  }
*/

void setup(void) {

  noInterrupts();
  TCCR1A = 0;
  TCCR1B = 0;

  TCNT1 = prescaler_timeconstantvalue;            // preload timer
  TCCR1B |= (1 << CS10) | (1 << CS12);  // 1024 prescaler
  TIMSK1 |= (1 << TOIE1);               // enable timer overflow interrupt ISR
  interrupts();                         // enable all interrupts

  Serial2.begin(115200);
  delay(500);
  Serial2.print("Baud=9600");
  Serial2.write(0xff);
  Serial2.write(0xff);
  Serial2.write(0xff);
  Serial2.end();

  Serial2.begin(9600);

  Serial.begin(9600);

  nexInit();

  dht.begin();

  Wire.begin();
  Wire.setWireTimeout( 1000);
  
  #ifndef ESP8266
  while (!Serial); // wait for serial port to connect. Needed for native USB
  #endif

  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    Serial.flush();
    while (1) delay(10);
  }


   rtc.adjust(DateTime(2022, 9, 24, 23, 15, 0));



  delay(1000);





  btnMainLIght.attachPush(btnMainLIghtPushCallback);
  //btnMainLIght.attachPop(btnMainLIghtPopCallback);
  btnBoiler.attachPush(btnBoilerPushCallback);
  //btnBoiler.attachPop(btnBoilerPopCallback);

  btnNight.attachPush(btnNightPushCallback);
  btnDay.attachPush(btnDayPushCallback);



  

  pinMode(ledPin, OUTPUT);
  pinMode(mainLIghtPin, OUTPUT);
  digitalWrite(mainLIghtPin, HIGH);
  //pinMode(mainLIght, INPUT_PULLUP);

  pinMode(el_heater, OUTPUT);
  digitalWrite(el_heater, HIGH);

  pinMode(boilerPin, OUTPUT);
  digitalWrite(boilerPin, HIGH);

  

  /* TEMP */
  // Start up the library
  sensors.begin();

  // Grab a count of devices on the wire
  numberOfDevices = sensors.getDeviceCount();
  /*
    Serial2x.print("Locating temperature sensors...");
    Serial2x.print("Found ");
    Serial2x.print(numberOfDevices, DEC);
    Serial2x.println(" sensors.");
  */

  for (int i = 0; i < numberOfDevices; i++) {

    if (sensors.getAddress(tempDeviceAddress, i)) {
      Serial.print("Nalezen teplotní sensor ");
      Serial.print(1, DEC);
      Serial.println();
    } else {
      Serial.print("Nalezen teplotní sensor bez adresy");
      Serial.print(i, DEC);
      Serial.println();
    }
  }
  /* TEMP END*/

}
//void (*resetFunc)(void)=0;

ISR(TIMER1_OVF_vect)                    // interrupt service routine for overflow
{


  TCNT1 = prescaler_timeconstantvalue;                                // preload timer
  digitalWrite(ledPin, digitalRead(ledPin) ^ 1);  //Turns LED ON and OFF


  actualTimeInterval++;
  if (actualTimeInterval > 59) {
    actualTimeInterval = 0;
    
  }




/*
  if (WATER_HEATER_TIMER_ON) {
    WATER_HEATER_TIMER_PREVIOUS_VALUE = WATER_HEATER_TIMER_VALUE;
    WATER_HEATER_TIMER_VALUE = WATER_HEATER_TIMER_VALUE - 1;
    WATER_BOILER_PROGRESS_VALUE = WATER_BOILER_PROGRESS_VALUE - WATER_BOILER_PROGRESS_STEP;
    WATER_HEATER_TIMER_ONE_CYCLE_SEMAPHOR = true;

    if (WATER_HEATER_TIMER_VALUE <= 0) {
      WATER_HEATER_ON = false;
      WATER_HEATER_TIMER_VALUE = 0;
      WATER_HEATER_TIMER_ON = false;
      digitalWrite(boilerPin, HIGH);//HIGH
      Serial2.println("Water heater end on timer off");

    }
  }

  */
  /*
    Serial.print("time_last_readed> ");
          Serial.print(time_last_readed);
          Serial.print("interval> ");
          Serial.println(actualTimeInterval);
  */
}

void getSensors(void) {

  




  //--------------------- TEMPERATURE
  /*
  if ((temperature_will_check_at < actual_second)) {
    temperature_will_check_at = actual_second + temperature_check_interval;
    if (temperature_will_check_at >= 60)temperature_will_check_at = temperature_will_check_at - 60;
*/
    //Serial.println("Načítání teplot:");

    numberOfDevices = sensors.getDeviceCount();
    sensors.requestTemperatures();

    for (int i = 0; i <= numberOfDevices; i++) {
      // Search the wire for address
      if (sensors.getAddress(tempDeviceAddress, i)) {


        if (i == 0) {

          actual_inside_temp = sensors.getTempC(tempDeviceAddress);
          //Serial.print("Teplota uvnitř: ");Serial.println(actual_inside_temp);
          //Serial2x.print(tempC);
          stringOne = "t3.txt=\"";
          stringOne.concat(String(actual_inside_temp, 1));
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.print(stringOne);
          Serial2.print(F("\""));
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.flush();
          wasTempInside = true;
          //Serial2.println("...");
        }
        if (i == 1) {

          actual_outside_temp = sensors.getTempC(tempDeviceAddress);
          //Serial.print("Teplota rozvaděč: ");Serial.println(actual_temp_switch_board);
          //Serial2x.print(tempC);
          stringOne = "t0.txt=\"";
          stringOne.concat(String(actual_outside_temp, 1));
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.print(stringOne);
          Serial2.print(F("\""));
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.flush();
          wasTempOutside = true;
          //Serial2.println("...");
        }
        if (i == 2) {

          actual_temp_switch_board = sensors.getTempC(tempDeviceAddress);
          //Serial.print("Teplota venku: ");Serial.println(actual_outside_temp);
          //Serial2x.print(tempC);
          stringOne = "t8.txt=\"";
          stringOne.concat(String(actual_temp_switch_board, 1));
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.print(stringOne);
          Serial2.print(F("\""));
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.flush();
          wasTempSwitchBoard = true;
          //Serial2.println("...");
        }


        if (!wasTempInside) {
          //Serial2.print("Teplota uvnitř neodpovídá... ");

          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.print(F("t3.txt=\"ERR"));
          Serial2.print(F("\""));
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.flush();

        }
        
      }
    }//For end
    
    if (!wasTempOutside) {
      //Serial2.print("Teplota venku neodpovídá... ");

      Serial2.write(0xff);
      Serial2.write(0xff);
      Serial2.write(0xff);
      Serial2.print(F("t0.txt=\"ERR"));
      Serial2.print(F("\""));
      Serial2.write(0xff);
      Serial2.write(0xff);
      Serial2.write(0xff);
      Serial2.flush();

    }
    if (!wasTempSwitchBoard) {
      //Serial2.print("Teplota v rozvaděči neodpovídá... ");

      Serial2.write(0xff);
      Serial2.write(0xff);
      Serial2.write(0xff);
      Serial2.print(F("t8.txt=\"ERR"));
      Serial2.print(F("\""));
      Serial2.write(0xff);
      Serial2.write(0xff);
      Serial2.write(0xff);
      Serial2.flush();

    }



    wasTempInside = false;
    wasTempOutside = false;
    wasTempSwitchBoard = false;



    /* HUMIDITY */
    hum = dht.readHumidity();

    //temp = dht.readTemperature();


    //Serial.print("Vlhkost: ");
    //Serial.print(hum);
    //Serial.print(" %, Teplota: ");
    //Serial.print(temp);
    //Serial.println(" Celsius");
    /* HUMIDITY */

    stringOne = "t17.txt=\"";
    stringOne.concat(String(hum, 1));
    Serial2.write(0xff);
    Serial2.write(0xff);
    Serial2.write(0xff);
    Serial2.print(stringOne);
    Serial2.print(F("\""));
    Serial2.write(0xff);
    Serial2.write(0xff);
    Serial2.write(0xff);
    Serial2.flush();


  //}//interval check






  //Serial.print(stringOne);

}/*
#ifdef __arm__
// should use uinstd.h to define sbrk but Due causes a conflict
extern "C" char* sbrk(int incr);
#else  // __ARM__
extern char *__brkval;
#endif  // __arm__

int freeMemory() {
  char top;
#ifdef __arm__
  return &top - reinterpret_cast<char*>(sbrk(0));
#elif defined(CORE_TEENSY) || (ARDUINO > 103 && ARDUINO != 151)
  return &top - __brkval;
#else  // __arm__
  return __brkval ? &top - __brkval : &top - __malloc_heap_start;
#endif  // __arm__
}
*/

void loop() {
  nexLoop(nex_listen_list);
  
  //Serial.print(" FreeMemory:"); Serial.println(freeMemory());
  
  //Get time


  if ((time_will_read_at <= actualTimeInterval)) {
    time_will_read_at = actualTimeInterval + time_read_interval;
    if (time_will_read_at >= 60)time_will_read_at = time_will_read_at - 60;
    
    //Serial.println("Time read");
    //now = rtc.now().unixtime();
    //Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
    
    DateTime now = rtc.now();

    actual_day = now.day();
    actual_month = now.month();
    actual_year = now.year();
    actual_hour = now.hour();
    actual_minute = now.minute();
    actual_second = now.second();
    
    String stringOne = "t2.txt=\"22";
    stringOne.concat(String(actual_hour, 1));
    stringOne.concat(":");
    stringOne.concat(actual_minute);
    //stringOne.concat(":");
    //stringOne.concat(actual_second);


    Serial2.write(0xff);
    Serial2.write(0xff);
    Serial2.write(0xff);
    Serial2.print(stringOne);
    Serial2.print(F("\""));
    Serial2.write(0xff);
    Serial2.write(0xff);
    Serial2.write(0xff);
    Serial2.flush();
    //Serial.print(stringOne);

    stringOne = "t1.txt=\"22";
    stringOne.concat(String(actual_day, 1));
    stringOne.concat(".");
    stringOne.concat(actual_month);
    stringOne.concat(".");
    stringOne.concat(actual_year);


    Serial2.write(0xff);
    Serial2.write(0xff);
    Serial2.write(0xff);
    Serial2.print(stringOne);
    Serial2.print(F("\""));
    Serial2.write(0xff);
    Serial2.write(0xff);
    Serial2.write(0xff);
    Serial2.flush();
    
  }


  getSensors();

  
    if((el_heater_temperature_will_check_at<=actual_minute)){
            el_heater_temperature_will_check_at = actual_minute+el_heater_check_interval;
             if (el_heater_temperature_will_check_at>=60)el_heater_temperature_will_check_at=el_heater_temperature_will_check_at-60;

      //Serial.println("Heater check");
      //DateTime future (now + TimeSpan(7, 12, 30, 6));
      
      if (el_heat_dayli==NIGHT){
    
        if(actual_inside_temp<el_heat_on_level_night){
  
          digitalWrite(el_heater, LOW);
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.print(F("t24.txt=\"N-Topí"));
          Serial2.print(F("\""));
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.flush();
        }else{
          digitalWrite(el_heater, HIGH);
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.print(F("t24.txt=\"N-Netopí"));
          Serial2.print(F("\""));
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.flush();
        }
      }
      if (el_heat_dayli==DAY){
    
        if(actual_inside_temp<el_heat_on_level_day){
  
          digitalWrite(el_heater, LOW);
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.print(F("t24.txt=\"D-Topí"));
          Serial2.print(F("\""));
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.flush();
        }else{
          digitalWrite(el_heater, HIGH);
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.print(F("t24.txt=\"D-Netopí"));
          Serial2.print(F("\""));
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.flush();
        }
      }
    }
  



}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.