RTC and Wire.h

I have working RTC on Arduino. It is Chronodot and I found a few simple examples to try. My question is: how to recognize that the RTC is not connected or connected incorrectly (for example, only one data wire) using Wire.h (not RTC lib) library.

My idea is: commonly to get time from the NTP server and if it fails, get time from the RTC chip. And if the NTP responds then to update the RTC chip.

In the example below is not handled the situation why the RTC can not communicate because I do not know how to do it.

Example:

#include <Time.h> 
#include <Wire.h>
//+some other things for ethernet
//////////////////////////////////////////////////////////////////////
int rtc_seconds; //00-59;
int rtc_minutes; //00-59;
int rtc_hours;//1-12 - 00-23;
int rtc_weekday;//1-7
int rtc_day;//01-31
int rtc_month;//01-12
int rtc_year;//0-99 + 2000;
//+some other things
///////////////////////////////////////////////////////////////////////
void setup() {
  Wire.begin();
  Serial.begin(115200);
  //+some other things
  setSyncProvider(getNtpTime);
  //+some other things
  if (now() < 1374708634){
    get_rtc_time();
    if (get_rtc_datum()){
      //set time from RTC
      setTime(rtc_hours,rtc_minutes,rtc_seconds,rtc_day,rtc_month,rtc_year);
    };
  }
  else{
    //timeset from NTP, so update time in RTC
    rtc_seconds = second();
    rtc_minutes = minute();
    rtc_hours   = hour();
    rtc_weekday = weekday();
    rtc_day     = day();
    rtc_month   = month();
    rtc_year    = year();
    set_rtc_time();
    set_rtc_datum();
  };
};

void loop() {
  get_rtc_time();
  get_rtc_datum();
  display_rtc_datum();
  display_rtc_time();
  delay(1000);
};
///////////////////////////////////////////////////////////////////////
void set_rtc_datum()
{
  Wire.beginTransmission(104);
  Wire.write(3);
  Wire.write(decToBcd(rtc_weekday));
  Wire.write(decToBcd(rtc_day));
  Wire.write(decToBcd(rtc_month));
  Wire.write(decToBcd(rtc_year-2000));
  Wire.endTransmission();
};
bool get_rtc_datum()
{
  Wire.beginTransmission(104);
  Wire.write(3);//set register to 3 (day)
  Wire.endTransmission();
  Wire.requestFrom(104, 4); //get 5 bytes(day,date,month,year,control);
  rtc_weekday = bcdToDec(Wire.read());
  rtc_day     = bcdToDec(Wire.read());
  rtc_month   = bcdToDec(Wire.read());
  rtc_year    = bcdToDec(Wire.read())+2000;
  if (rtc_year > 2000){
    return true;
  }
  else{
    return false;
  };
};
void set_rtc_time()
{
  Wire.beginTransmission(104);
  Wire.write(0);
  Wire.write(decToBcd(rtc_seconds));
  Wire.write(decToBcd(rtc_minutes));
  Wire.write(decToBcd(rtc_hours));
  Wire.endTransmission();
};
void get_rtc_time()
{
  Wire.beginTransmission(104);
  Wire.write(0);//set register to 0
  Wire.endTransmission();
  Wire.requestFrom(104, 3);//get 3 bytes (seconds,minutes,hours);
  rtc_seconds = bcdToDec(Wire.read() & 0x7f);
  rtc_minutes = bcdToDec(Wire.read());
  rtc_hours   = bcdToDec(Wire.read() & 0x3f);
};
//////////////////////////////////////////////////////////////////////
void display_rtc_time()
{
  Serial.print(F(" "));
  if(rtc_hours < 10)
  {
    Serial.print(F("0"));
  };
  Serial.print(rtc_hours);
  Serial.print(F(":"));
  if(rtc_minutes < 10)
  {
    Serial.print(F("0"));
  };
  Serial.print(rtc_minutes);
  Serial.print(F(":"));
  if(rtc_seconds < 10){
    Serial.print(F("0"));
  };
  Serial.print(rtc_seconds);
  Serial.print(F(" "));
  switch (rtc_weekday) {
  case 2:    
    Serial.print(F("pondeli"));
    break;
  case 3:    
    Serial.print(F("utery"));
    break;
  case 4:    
    Serial.print(F("streda"));
    break;
  case 5:    
    Serial.print(F("ctvrtek"));
    break;
  case 6:    
    Serial.print(F("patek"));
    break;
  case 7:    
    Serial.print(F("sobota"));
    break;
  case 1:    
    Serial.print(F("nedele"));
    break;
  default:  
    Serial.print(F("den v tydnu neni nastaven"));
  };
  Serial.println();
};
void display_rtc_datum()
{
  if(rtc_day < 10){
    Serial.print(F("0"));
  }; 
  Serial.print(rtc_day);
  Serial.print(F("."));
  if(rtc_month < 10){
    Serial.print(F("0"));
  };
  Serial.print(rtc_month);
  Serial.print(F("."));
  Serial.print(rtc_year);
};
///////////////////////////////////////////////////////////////////////
byte decToBcd(byte val)
{
  return ( (val/10*16) + (val%10) );
};
byte bcdToDec(byte val)
{
  return ( (val/16*10) + (val%16) );
};
//////////////////////////////////////////////////////////////////////
  Wire.beginTransmission(104);
  Wire.write(0);//set register to 0
  Wire.endTransmission();

endTransmission tells you if it worked or not. So:

  Wire.beginTransmission(104);
  Wire.write(0);//set register to 0
  if (Wire.endTransmission() == 0)
    {
    // set register OK
    }
  else
    {
    // RTC not working
    }

Also:

if (Wire.requestFrom(104, 4) == 4)
  {
  // got the 4 bytes we wanted 
  }
else
  {
  // did not get the time
  }

Oh, thanks very much! This is excactly what I want!

bool get_rtc_time()
{
  Wire.beginTransmission(RTC_ADDRESS);
  Wire.write(0);//set register to 0
  Wire.endTransmission();
  if (Wire.requestFrom(RTC_ADDRESS, 3) == 3) { //get 3 bytes (seconds,minutes,hours);
    rtc_second = bcdToDec(Wire.read() & 0x7f);
    rtc_minute = bcdToDec(Wire.read());
    rtc_hour   = bcdToDec(Wire.read() & 0x3f);
    return true;
  }
  else{
    return false;
  };
};
bool get_rtc_time()
{
  Wire.beginTransmission(RTC_ADDRESS);
  Wire.write(0);//set register to 0
  if (Wire.endTransmission() != 0)
    return false;

...