Exemplo n.º 1
0
void TimeSync::process(bool dataToRead)
{
    Debug::out(LOG_DEBUG, "TimeSync::process(%s) entering with eState=%d nextProcessTime=%u", dataToRead ? "true" : "false", (int)eState, nextProcessTime);
    switch(eState) {
    case INIT:
    case INIT_NTP:
        sendNtpPacket();
        break;
    case NTP_WAIT_PACKET1:
    case NTP_WAIT_PACKET2:
        if(dataToRead) {
            readNtpPacket();
        } else {
            sendNtpPacket();
        }
        break;
    case NTP_WAIT_PACKET3:
        if(dataToRead) {
            readNtpPacket();
        } else {
            eState = NTP_FAILED;
        }
        break;
    case NTP_FAILED:
    case INIT_WEB:
        sendWebRequest();
        break;
    case WEB_WAIT_RESPONSE:
        checkWebResponse();
        break;
    case WEB_FAILED:
    case DATE_FAILED:
        if(s >= 0) {
            close(s);
            s = -1;
        }
        nextProcessTime = Utils::getEndTime(60*1000);// retry in  1 minute
        eState = INIT;
        break;
    case DATE_SET:
        if(s >= 0) {
            close(s);
            s = -1;
        }
        nextProcessTime = Utils::getEndTime(30*60*1000);// do it again in 30 minutes
        eState = INIT;
        break;
    }
    Debug::out(LOG_DEBUG, "TimeSync::process(%s) exiting with eState=%d nextProcessTime=%u", dataToRead ? "true" : "false", (int)eState, nextProcessTime);
}
Exemplo n.º 2
0
bool BlindCloud::loop( void )
{
  if ( connected(false) )
  {
    if ( m_ntp_offset==0 ) {
      uint8_t packetBuffer[ NTP_PACKET_SIZE ];
      
      sendNtpPacket( ); // send an NTP packet to a time server
      delay(150);
      if ( m_udp.parsePacket()>0 )
      {
#ifdef BLIND_VERBOSE
        Serial.println("BlindCloud : NTP Packet received..");
#endif
        m_udp.read( packetBuffer, sizeof(packetBuffer) );

        const unsigned long highWord      = word(packetBuffer[40], packetBuffer[41]);
        const unsigned long lowWord       = word(packetBuffer[42], packetBuffer[43]);
        
        // combine the four bytes (two words) into a long integer (that is the seconds since Jan 1 1900)
        // i.e. 2208988800UL are seconds from Jan 1 1900 to Jan 1 1970 that is the epoch
        const unsigned long secsSince1970 = ( (highWord<<16) | lowWord )-2208988800UL; // i.e. seventyYears
        
        m_ntp_offset  = secsSince1970-(millis()/1000);
        
        // From UTC time to local time zone (Rome is +1h = +3600 sec )
        m_ntp_offset += (m_ntp_time_zone*60*60);
      }
      
      while ( m_udp.parsePacket()>0 )   // clean-up buffer
        m_udp.read( packetBuffer, sizeof(packetBuffer) );
    }
/*
#ifdef BLIND_VERBOSE
    if ( m_ntp_offset>0 ) {

      static uint8_t count = 0;
      count++;
      if ( count==16 ) {
        count = 0;
        printNtpTime( m_ntp_offset+(millis()/1000) );
      }
    }
#endif
*/
    //m_mqtt.loop();
    
#ifdef BLIND_VERBOSE
//    Serial.print("m_state is : "); Serial.println(m_state);
#endif

    return true;
  }

  return false;
}
Exemplo n.º 3
0
void sendTimeRequest() {

	lastNtpRequestTime = now();
	ntpState = WAITING_FOR_RESPONSE;
	Serial.print(currServerIndex);
	sendNtpPacket(servers[currServerIndex]);                       
	currServerIndex = ++currServerIndex % (sizeof servers / sizeof (IPAddress));
	Serial.print("Sent time request");

}