src/cWiFi.cpp
// get time from Wi-Fi module. return false if failed
get time from Wi-Fi module. return false if failed
get time from Wi - Fi module . return false if failed
bool cWiFi::get_datetime_from_wifi(struct tm *p_timeinfo){
  if( !_b_enable || !_b_init )
    return false;
  struct tm timeinfo;
  const char WRONG_FORMAT_MSG[] = "E:Wrong time format from Wi-Fi module : ";
  update();
  send_command("\r\nTIME\r\n");
  const uint32_t t = millis();
  _t_last_timeinfo = t - INTERVAL_UPDATE_TIME + 60000;
  do{
    if( _fn_update )
      _fn_update();
    if( Serial2.available() ){
      char c = Serial2.read();
      if( c=='\r' || c=='\n' ){
        if( _n_serial_buf>0 ){
          _serial_buf[_n_serial_buf] = 0;
          int len = _n_serial_buf;
          _n_serial_buf = 0;
          Log.info("WiFi:");
          Log.infoln(_serial_buf);
          if( strncmp( _serial_buf, "OK:TIME:", 8 )==0 ){
            int val, i1 = 8, cnt = 0;
            _serial_buf[len++] = ':';
            _serial_buf[len] = 0;
            for(int i=i1;i<len;i++){
              if( _serial_buf[i]==':' ){
                _serial_buf[i] = 0;
                if( string_to_int( _serial_buf + i1, &val ) ){
                  switch(cnt++){
                    case 0:
                      timeinfo.tm_year = val - 1900;
                      break;
                    case 1:
                      timeinfo.tm_mon = val - 1;
                      break;
                    case 2:
                      timeinfo.tm_mday = val;
                      break;
                    case 3:
                      timeinfo.tm_hour = val;
                      break;
                    case 4:
                      timeinfo.tm_min = val;
                      break;
                    case 5:
                      timeinfo.tm_sec = val;
                      break;
                  }
                }
                else{
                  Log.error(WRONG_FORMAT_MSG);
                  Log.errorln(_serial_buf);
                  return false;
                }
                i1 = i+1;
              }
            }
            if( cnt==6 ){
              *p_timeinfo = timeinfo;
              _timeinfo = timeinfo;
              _t_timeinfo = _t_last_timeinfo = millis();
              return true;
            }
            else{
              Log.error(WRONG_FORMAT_MSG);
              Log.errorln(_serial_buf);
              return false;
            }
          }
        }
      }
      else{
        if( _n_serial_buf>=WIFI_BUFFER_SIZE-2 ){
          _serial_buf[WIFI_BUFFER_SIZE-2] = 0;
          Log.warn("W:Too long serial msg:");
          Log.warnln(_serial_buf);
          _n_serial_buf = 0;
        }
        else{
          _serial_buf[_n_serial_buf++] = c;
        }
      }
    }
  }
  while( uint32_t(millis()-t) < 5000 );
  Log.infoln("I:get_datetime() timeout");
  return false;
}
bool cWiFi :: get_datetime_from_wifi ( struct tm * p_timeinfo ) { if ( ! _b_enable || ! _b_init ) return false ; struct tm timeinfo ; const char WRONG_FORMAT_MSG [ ] = " " ; update ( ) ; send_command ( " \r \n \r \n " ) ; const uint32_t t = millis ( ) ; _t_last_timeinfo = t - INTERVAL_UPDATE_TIME + 60000 ; do { if ( _fn_update ) _fn_update ( ) ; if ( Serial2 . available ( ) ) { char c = Serial2 . read ( ) ; if ( c == ' \r ' || c == ' \n ' ) { if ( _n_serial_buf > 0 ) { _serial_buf [ _n_serial_buf ] = 0 ; int len = _n_serial_buf ; _n_serial_buf = 0 ; Log . info ( " " ) ; Log . infoln ( _serial_buf ) ; if ( strncmp ( _serial_buf , " " , 8 ) == 0 ) { int val , i1 = 8 , cnt = 0 ; _serial_buf [ len ++ ] = ' ' ; _serial_buf [ len ] = 0 ; for ( int i = i1 ; i < len ; i ++ ) { if ( _serial_buf [ i ] == ' ' ) { _serial_buf [ i ] = 0 ; if ( string_to_int ( _serial_buf + i1 , & val ) ) { switch ( cnt ++ ) { case 0 : timeinfo . tm_year = val - 1900 ; break ; case 1 : timeinfo . tm_mon = val - 1 ; break ; case 2 : timeinfo . tm_mday = val ; break ; case 3 : timeinfo . tm_hour = val ; break ; case 4 : timeinfo . tm_min = val ; break ; case 5 : timeinfo . tm_sec = val ; break ; } } else { Log . error ( WRONG_FORMAT_MSG ) ; Log . errorln ( _serial_buf ) ; return false ; } i1 = i + 1 ; } } if ( cnt == 6 ) { * p_timeinfo = timeinfo ; _timeinfo = timeinfo ; _t_timeinfo = _t_last_timeinfo = millis ( ) ; return true ; } else { Log . error ( WRONG_FORMAT_MSG ) ; Log . errorln ( _serial_buf ) ; return false ; } } } } else { if ( _n_serial_buf >= WIFI_BUFFER_SIZE - 2 ) { _serial_buf [ WIFI_BUFFER_SIZE - 2 ] = 0 ; Log . warn ( " " ) ; Log . warnln ( _serial_buf ) ; _n_serial_buf = 0 ; } else { _serial_buf [ _n_serial_buf ++ ] = c ; } } } } while ( uint32_t ( millis ( ) - t ) < 5000 ) ; Log . infoln ( " " ) ; return false ; }
get time from Wi-Fi module.
get time from Wi - Fi module .
// if failed wait for 1 min