void ntp()
 {
 // while(!timeClient.update()) {
  //  timeClient.forceUpdate();
  //}
  Serial.println("NTP");
ntp_on=0;
if(!timeClient.update()) {
     for(int i=0; i<2 && !timeClient.update(); i++)
        {timeClient.forceUpdate();
        if(i<1)//проверка подкл к NTP
          {ntp_on=1;
          }
        else
          {
           ntp_on=0; 
          }
        }
  } 
  else{
   ntp_on=1;  
  }


//если подкл
if(ntp_on==1)
 { // The formattedDate comes with the following format:
  // 2018-05-28T16:00:13Z
  // We need to extract date and time
 // formattedDate = timeClient.getFormattedDate();
  formattedTime =timeClient.getFormattedTime();
 Serial.println(formattedTime);
  Serial.println("NTP3");
// getDay
//getHours
//getMinutes
//getSeconds
//getFormattedTime 
// formattedDate =timeClient.getFormattedDate();
 // int splitT = formattedDate.indexOf("T");
//  dayStamp = formattedDate.substring(0, splitT);
//  Serial.println(timeClient.getDay());

  // Extract time
 // timeStamp = formattedDate.substring(splitT+1, formattedDate.length()-1);
// Serial.println("ntp");
//  Serial.println(timeClient.getEpochTime());
   stamp.getDateTime(timeClient.getEpochTime());
EpochTime=timeClient.getEpochTime();
  Serial.println(EpochTime);
  // забираем вот так
//  Serial.println(stamp.year);
  Serial.println(stamp.month);
  Serial.println(stamp.day);
 // Serial.println(stamp.hour);
 // Serial.println(stamp.minute);
 // Serial.println(stamp.second);
  Serial.println(stamp.dayOfWeek);
 timeserv=formatTimeDigits(stamp.hour,stamp.minute); 
 Serial.println("time");
  Serial.println(timeserv);
 //dataserv=formatDateDigits(stamp.day, stamp.month, stamp.year);
 //***************************Из ntp в ds 3231************************************
 DateTime now = rtc.now();
if((stamp.day==now.day())&&(stamp.hour==now.hour()))
{ rtc.adjust(DateTime(stamp.year,stamp.month, stamp.day, stamp.hour,stamp.minute,stamp.second));
 Serial.println("NTP time write");
}

hour1=stamp.hour;
min1=stamp.minute;

 dataserv=stamp.day;
 munthserv=strm[stamp.month-1] ;
 weekserv=strweek[stamp.dayOfWeek];
// tweekday=stamp.dayOfWeek;//1-7
//Serial.println(timeserv);
//Serial.println(dataserv);
 }
else
{Serial.println("NTP not c0nnect");//EpochTime=10983 3:03:03
  //EpochTime=EpochTime;//если NTP не ответил читаем EpochTime с СД карты
}

}

char *formatTimeDigits(int hour, int minute)
{
char *time_str = new char[5];
time_str[0] = '0' + (hour / 10);
time_str[1] = '0' + (hour % 10);
time_str[2] = ':';
time_str[3] = '0' + (minute / 10);
time_str[4] = '0' + (minute % 10);
time_str[5] = '\0';

return time_str;
}

char *formatDateDigits(int date, int mounth, int year)
{
char *date_str = new char[9];

date_str[1] = '0' + (date % 10);
date_str[2] = '/';
date_str[0] = '0' + (date / 10);
date_str[3] = '0' + (mounth / 10);
date_str[4] = '0' + (mounth % 10);
date_str[5] = '/';
date_str[6] = '0' + ((year % 100) / 10);
date_str[7] = '0' + (year % 10);
date_str[8] = '\0';

return date_str;
}

void ntphome()
{DateTime now = rtc.now();
  // stamp.getDateTime(timeClient.getEpochTime());
   // timeserv=formatTimeDigits(stamp.hour,stamp.minute); 
    timeserv=formatTimeDigits(now.hour(),now.minute()); 
   // hour1=stamp.hour;
    hour1=now.hour();
//min1=stamp.minute;
min1=now.minute();

// dataserv=formatDateDigits(stamp.day, stamp.month, stamp.year);
 //dataserv=stamp.day;
 dataserv=now.day();
 data1=now.day();
 munthserv=strm[now.month()-1] ;

 weekserv=strweek[now.dayOfTheWeek()];
 
 tweekday=now.dayOfTheWeek();
month1=now.month();
 year1=now.year();
  Serial.println("time");
  Serial.println(timeserv);
}
