
void checkMillis()
{
  if ((millis() - lastRun) >= 100)
  {
    lastRun = millis();
    every100ms();
  }
}

void every100ms()
{
  if (count100ms < 10)
    count100ms++;
  else
  {
    count100ms = 0;
    everySecond();
  }
  //checkSaveConfigTriggered();
}

void everySecond()
{
  if (countSeconds < 60)
    countSeconds++;
  else
  {
    countSeconds = 0;
    everyMinute();
  }

  conf_confChangedLogNote(false); // only outputs once if false and only if config is not saved

  //webSocket.sendTXT(num, "Test\n");
  // if(confWeb.enableConsole && webSocket.connectedClients() > 0) {
  //   char buf[40];
  //   sprintf(buf, "%u\n", millis());
  //   webSocket.broadcastTXT(buf);
  // }

  #ifdef ENABLE_LCD_I2C
    display_handleTimeout();
  #endif

  #ifdef FIRMWARE_VARIANT_THERMOSTAT
    thermostat_checkValuesChanged();
  #endif 

  if (sysInfoEverySecond) log_sysdata();

  #ifdef ENABLE_FEATURE_WSCONSOLE
    clearValidSessionId();
  #endif

  if ( countMeasureInterval < confSens.measureInterval || (confSens.measureInterval == 0 && countMeasureInterval < 15) ) {
    // default of 15s if confSens.measureInterval was set to 0
    countMeasureInterval++;
  }
  else
  {
    countMeasureInterval = 0;

    #ifdef ENABLE_SENSORS_ONEWIRE
      oneWireSensors_getData();
    #endif
    
    #ifdef ENABLE_SENSOR_DHT22
      DHT_measureTempHum();
    #endif
    
    #ifdef FIRMWARE_VARIANT_THERMOSTAT
      thermostat_mainFunction();
    #endif
  }

  #ifdef FIRMWARE_VARIANT_THERMOSTAT
    if(thermostat_heatingPause > 0) {
      thermostat_heatingPause--;
      thermostat_publish_heatingPauseTime();
    }

    if(thermostat_heatingLockTime > 0) {
      thermostat_heatingLockTime--;
      thermostat_publish_heatingLockTime();
    }
  #endif // FIRMWARE_VARIANT_THERMOSTAT


  #ifdef ENABLE_LCD_I2C
    if (display_overrideInterval > 0) {
      if (display_countInterval < display_overrideInterval)
        display_countInterval++;
      else
      {
        display_countInterval = 0;
        display_overrideInterval = 0;
        display_update();
      }  
    }
    else {
      if (display_countInterval < confDisplay.displayInterval)
        display_countInterval++;
      else
      {
        display_countInterval = 0;
        display_update();
      }
    }
  #endif // ENABLE_LCD_I2C


  if (system_doRestart) {
    restart();
  }

  if(system_restartAtMillis > 0 && millis() > system_restartAtMillis) {
    restart();
  }

  if (configChangedMqttConnResetRequired && !confCheckUnsaved()) {
    configChangedMqttConnResetRequired = false;
    mqttResetConnection();
  }

  if ((millis() - system_pendingRestart_lastMillis) > 3000) {
    system_pendingRestart = false;
  }

  //  if ( preset != pendingPreset) {
  //    if ( (millis() - pendingPreset_millis) > pendingPreset_timeout ) {
  //      preset = pendingPreset;
  //    }
  //  }

  #ifdef ENABLE_I2C_PORTEXPANDER
    portexpander_readInputs();
    //portexpander_toggleOutputs();
  #endif

  #ifdef FIRMWARE_VARIANT_HEATCONTROL
    heatcontrol_mainFunction();
  #endif

} // everySecond()




void everyMinute()
{
  #ifdef ENABLE_FEATURE_NTP_TIME
    if (confTime.ntpEnable) {
      syncClock();
    }
  #endif

  log_sysdata();

  conf_confChangedLogNote(true);
  

  if (confMqtt.mqtt_enable) {

    if (!confMqtt.mqtt_willRetain) {
      mqtt_publishConnectMsg();
    }

    mqtt_publishStatus();

    if (confMqtt.mqtt_enable_heartbeat) {
      mqttPublishHeartbeat();
    }

    if (confMqtt.mqtt_outPubInterval > 0 && outPubInterval_states_count >= confMqtt.mqtt_outPubInterval) {
      outPubInterval_states_count = 0;
      #ifdef FIRMWARE_VARIANT_THERMOSTAT
        thermostat_publishCurrentValues(true); // true forces publishing
      #endif

      #ifdef FIRMWARE_VARIANT_HEATCONTROL
        heatcontrol_publish_sw_disableControl_heating();
        heatcontrol_publish_sw_disableControl_pump();
        heatcontrol_publish_in_heat_active();
        heatcontrol_publish_in_heat_request();
        heatcontrol_publish_out_heat();
        heatcontrol_publish_out_pump();
        heatcontrol_publish_lockTime();
        heatcontrol_publish_pumpBacklash();
        heatcontrol_publish_testMode();
        heatcontrol_publish_heatCurve_current();
        heatcontrol_publish_lockActive();
        
        heatcontrol_pump_forceRunAfterTimeout();
      #endif
    }
    else {
      outPubInterval_states_count++;
      #ifdef FIRMWARE_VARIANT_THERMOSTAT
      thermostat_publishCurrentValues(false); // false publishes only changed values
      #endif
    }

    if (confMqtt.mqtt_outPubInterval_sensors > 0 && outPubInterval_sensors_count >= confMqtt.mqtt_outPubInterval_sensors) {
      outPubInterval_sensors_count = 0;
      publishCurrentSensorValues(true); // true forces publishing
    }
    else {
      outPubInterval_sensors_count++;
      publishCurrentSensorValues(false); // false publishes only changed values
    }

    DHT_logCurrentData();
  }
} // everyMinute()
