• Welcome to LusoRobótica - Robótica em Português.
 

Ajuda atualizar hora NTP Verão/Inverno

Started by dennis_boy, 18 de January de 2020, 19:02

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

dennis_boy

Boa noite recente estou com um projecto onde tenho um menu onde actualizo a hora do DS3231 pelo wifi.
Deparo que precisava nesta altura retirar uma hora.
se vou calcular pelo DS pode estar atrasado ou adiantado, minha ideia era pegar no tempo que vem da net
e calcular onde está e retirar a hora ou manter.

Eu tenho este código la:
    wifi.send(mux_id, (const uint8_t*)buf, 48);
   
   uint32_t len = wifi.recv(mux_id, buffer, sizeof(buffer), 2000);
    if (len > 0) {
      unsigned long highWord = word(buffer[40], buffer[41]);
      unsigned long lowWord = word(buffer[42], buffer[43]);
 
      unsigned long secsSince1900 = highWord << 16 | lowWord;
      unsigned long epoch = secsSince1900 - 2208988800UL;

      clock.setDateTime(epoch);


Abraços.


vasco

Como volta não volta vem à baila a noticia que a europa (ou talvez apenas Portugal) vai deixar de mudar a hora, no relógio ntp que fiz com um esp8266 acabei por meter um switch físico. Numa posição é hora de verão, na outra de inverno...
Na verdade isto é ser um bocado preguiçoso, porque se se tem conectividade à Net para usar NTP também se pode fazer batota e abrir uma ligação http para o servidor qq (google.com por exemplo, e extrair o tempo atual dos headers), por exemplo

$ curl -s --head http://google.com
HTTP/1.1 301 Moved Permanently
Location: http://www.google.com/
Content-Type: text/html; charset=UTF-8
Date: Sun, 19 Jan 2020 01:27:39 GMT
Expires: Tue, 18 Feb 2020 01:27:39 GMT
Cache-Control: public, max-age=2592000
Server: gws
Content-Length: 219
X-XSS-Protection: 0
X-Frame-Options: SAMEORIGIN


Como só nos interessa a data,

$ curl -s --head http://google.com | grep "^Date" | cut -d: -f2- | sed -e 's/^ //'
Sun, 19 Jan 2020 01:29:15 GMT

Stupid men are often capable of things the clever wouldn't dare to contemplate.