Czas, zegar, clock w SuplaDevice

Awatar użytkownika
pzygmunt
Posty: 18282
Rejestracja: wt sty 19, 2016 9:26 am
Lokalizacja: Paczków
Kontakt:

Sprawdziłem i serwer poprawnie zwraca czas, również dla Twojego użytkownika.
Awatar użytkownika
klew
Posty: 8184
Rejestracja: czw cze 27, 2019 12:16 pm
Lokalizacja: Wrocław

pmar75 pisze: wt gru 28, 2021 10:45 pm Korzystam z publicznego serwera. Na moim koncie strefa czasowa jest ok , ale na esp8266 pobiera o godzinę wcześniej
Serwer svr1.supla.org. :?:
Pokaż proszę co jest wyświetlane na serialu chwilę po zarejestrowaniu urządzenia (albo całe logi od startu do ~20s po rejestracji na serwerze)
Widzimy się na Supla Offline Party vol. 2 :!:
pmar75
Posty: 10
Rejestracja: wt mar 01, 2016 11:58 pm

15:34:11.913 -> **** Digital write[0], pin: 16; value: 0
15:34:11.913 -> **** Digital write[1], pin: 14; value: 1
15:34:11.913 -> Channel(1) value changed to 1
15:34:11.913 -> Supla - starting initialization
15:34:11.913 -> Storage initialization
15:34:11.913 -> readStorage: 8; Read: [53 55 50 4C 41 1 0 1 ]
15:34:11.947 -> Storage: Number of sections 1
15:34:11.947 -> Reading section: 0
15:34:11.947 -> readStorage: 7; Read: [3 5E 0 0 0 0 0 ]
15:34:11.947 -> Section type: 3; size: 94
15:34:11.947 -> Validating storage state section with current device configuration
15:34:11.947 -> Storage state section validation completed. Loading elements state...
15:34:11.947 -> readStorage: 4; Read: [0 0 0 0 ]
15:34:11.947 -> readStorage: 1; Read: [0 ]
15:34:11.947 -> readStorage: 4; Read: [0 0 0 0 ]
15:34:11.947 -> readStorage: 1; Read: [0 ]
15:34:11.981 -> readStorage: 4; Read: [0 0 0 0 ]
15:34:11.981 -> readStorage: 1; Read: [0 ]
15:34:11.981 -> readStorage: 8; Read: [70 8 0 0 0 0 0 0 ]
15:34:11.981 -> Channel(6) value changed to 2160
15:34:11.981 -> ImpulseCounter[6] - set counter to 2160
15:34:11.981 -> readStorage: 8; Read: [90 2 0 0 0 0 0 0 ]
15:34:11.981 -> Channel(7) value changed to 656
15:34:11.981 -> ImpulseCounter[7] - set counter to 656
15:34:11.981 -> readStorage: 8; Read: [0 B 0 0 0 0 0 0 ]
15:34:11.981 -> Channel(8) value changed to 2816
15:34:12.015 -> ImpulseCounter[8] - set counter to 2816
15:34:12.015 -> readStorage: 4; Read: [0 0 0 0 ]
15:34:12.015 -> readStorage: 1; Read: [0 ]
15:34:12.015 -> readStorage: 4; Read: [0 0 0 0 ]
15:34:12.015 -> readStorage: 1; Read: [0 ]
15:34:12.015 -> readStorage: 4; Read: [0 0 0 0 ]
15:34:12.015 -> readStorage: 1; Read: [0 ]
15:34:12.015 -> readStorage: 4; Read: [0 0 0 0 ]
15:34:12.015 -> readStorage: 1; Read: [0 ]
15:34:12.015 -> readStorage: 4; Read: [0 0 0 0 ]
15:34:12.015 -> readStorage: 1; Read: [0 ]
15:34:12.015 -> readStorage: 4; Read: [0 0 0 0 ]
15:34:12.015 -> readStorage: 1; Read: [0 ]
15:34:12.049 -> readStorage: 4; Read: [0 0 0 0 ]
15:34:12.049 -> readStorage: 1; Read: [0 ]
15:34:12.049 -> readStorage: 4; Read: [0 0 0 0 ]
15:34:12.049 -> readStorage: 1; Read: [0 ]
15:34:12.049 -> readStorage: 4; Read: [0 0 0 0 ]
15:34:12.049 -> readStorage: 1; Read: [0 ]
15:34:12.049 -> readStorage: 4; Read: [0 0 0 0 ]
15:34:12.049 -> readStorage: 1; Read: [0 ]
15:34:12.049 -> readStorage: 4; Read: [0 0 0 0 ]
15:34:12.049 -> readStorage: 1; Read: [0 ]
15:34:12.049 -> **** Digital write[0], pin: 16; value: 0
15:34:12.049 -> **** Digital write[1], pin: 14; value: 0
15:34:12.083 -> Channel(1) value changed to 0
15:34:12.083 -> Channel(4) value changed to 1
15:34:12.083 -> Channel(19) value changed to 1
15:34:12.083 -> Initializing network layer
15:34:12.083 -> WiFi: establishing connection with SSID: "m2m"
15:34:12.083 -> Using Supla protocol version 12
15:34:12.083 -> Current status: [5] SuplaDevice initialized
15:34:12.151 -> **** Digital write[0], pin: 16; value: 0
15:34:12.151 -> **** Digital write[1], pin: 14; value: 1
15:34:12.151 -> Channel(1) value changed to 1
15:34:12.695 -> HTTP/1.1 200 OK
15:34:12.695 -> Date: Wed, 29 Dec 2021 14:34:14 GMT
15:34:12.695 -> Content-Type: application/json; charset=utf-8
15:34:12.729 -> Transfer-Encoding: chunked
15:34:12.729 -> Connection: close
15:34:12.729 -> X-Frame-Options: SAMEORIGIN
15:34:12.729 -> X-XSS-Protection: 1; mode=block
15:34:12.729 -> X-Content-Type-Options: nosniff
15:34:12.729 -> X-Download-Options: noopen
15:34:12.729 -> X-Permitted-Cross-Domain-Policies: none
15:34:12.729 -> Referrer-Policy: strict-origin-when-cross-origin
15:34:12.729 -> Access-Control-Allow-Origin: *
15:34:12.729 -> Access-Control-Allow-Methods: POST, OPTIONS
15:34:12.729 -> Access-Control-Allow-Headers: X-Requested-With, X-Prototype-Version, Origin, Accept, Content-TypeEstablishing Secured connection without certificate matching with: svr1.supla.org (port: 2016)
15:34:14.735 -> Connected to Supla Server
15:34:14.735 -> Current status: [10] Register in progress
15:34:14.735 -> Sending: [53 55 50 4C 41 C 1 0 0 0 45 0 0 0 55 4 0 0 6E 69 63 7A 79 6B 65 72 40 67 6D 61 69 6C 2E 63 6F 6D 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 51 7A F6 72 2B 64 15 34 F2 8B 95 3 65 D4 59 C5 8F 13 86 43 A7 5F C6 D5 15 23 EB 79 FE 0 F7 DF 45 53 50 38 32 36 36 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 55 73 65 72 20 53 57 2C 20 6C 69 62 20 32 2E 33 2E 35 0 0 0 73 76 72 31 2E 73 75 70 6C 61 2E 6F 72 67 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 15 0 54 B 0 0 EF 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 54 B 0 0 EF 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 2 54 B 0 0 EF 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 E8 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 E8 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 5 E8 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6 92 13 0 0 0 0 0 0 0 0 0 0 0 0 0 0 70 8 0 0 0 0 0 0 7 92 13 0 0 0 0 0 0 0 0 0 0 0 0 0 0 90 2 0 0 0 0 0 0 8 92 13 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 B 0 0 0 0 0 0 9 54 B 0 0 EF 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 A 54 B 0 0 EF 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 B 54 B 0 0 EF 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 C 54 B 0 0 EF 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 D 54 B 0 0 EF 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 E 54 B 0 0 EF 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 F 54 B 0 0 EF 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 10 54 B 0 0 EF 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 11 54 B 0 0 EF 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 12 54 B 0 0 EF 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 13 E8 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 14 54 B 0 0 EF 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ]
15:34:15.075 -> Sending: [53 55 50 4C 41 ]
15:34:15.177 -> Received: [53 55 50 4C 41 C 1 0 0 0 46 0 0 0 7 0 0 0 3 0 0 0 78 10 1 53 55 50 4C 41 ]
15:34:15.177 -> Device registered (activity timeout 120 s, server version: 16, server min version: 1)
15:34:15.211 -> Current status: [17] Registered and ready
15:34:15.211 -> Changing activity timeout to 30
15:34:15.211 -> Sending: [53 55 50 4C 41 C 2 0 0 0 D2 0 0 0 1 0 0 0 1E ]
15:34:15.245 -> Sending: [53 55 50 4C 41 ]
15:34:15.279 -> Sending: [53 55 50 4C 41 C 3 0 0 0 E0 1 0 0 0 0 0 0 ]
15:34:15.415 -> Sending: [53 55 50 4C 41 ]
15:34:15.449 -> Received: [53 55 50 4C 41 C 2 0 0 0 DC 0 0 0 3 0 0 0 1E A F0 53 55 50 4C 41 ]
15:34:15.449 -> Activity timeout set to 30 s
15:34:15.449 -> Sending: [53 55 50 4C 41 C 4 0 0 0 67 0 0 0 E 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 ]
15:34:15.551 -> Sending: [53 55 50 4C 41 ]
15:34:15.585 -> Received: [53 55 50 4C 41 C 3 0 0 0 EA 1 0 0 1A 0 0 0 E5 7 C 1D 4 F 22 10 E 0 0 0 45 75 72 6F 70 65 2F 57 61 72 73 61 77 0 53 55 50 4C 41 ]
15:34:15.585 -> Sending: [53 55 50 4C 41 C 5 0 0 0 67 0 0 0 E 0 0 0 4 0 0 0 0 0 1 0 0 0 0 0 0 0 ]
15:34:15.619 -> Sending: [53 55 50 4C 41 ]
15:34:15.687 -> Sending: [53 55 50 4C 41 C 6 0 0 0 67 0 0 0 E 0 0 0 6 0 0 0 0 0 70 8 0 0 0 0 0 0 ]
15:34:15.721 -> Sending: [53 55 50 4C 41 ]
15:34:15.789 -> Sending: [53 55 50 4C 41 C 7 0 0 0 67 0 0 0 E 0 0 0 7 0 0 0 0 0 90 2 0 0 0 0 0 0 ]
15:34:15.823 -> Sending: [53 55 50 4C 41 ]
15:34:15.891 -> Sending: [53 55 50 4C 41 C 8 0 0 0 67 0 0 0 E 0 0 0 8 0 0 0 0 0 0 B 0 0 0 0 0 0 ]
15:34:15.925 -> Sending: [53 55 50 4C 41 ]
15:34:15.993 -> Sending: [53 55 50 4C 41 C 9 0 0 0 67 0 0 0 E 0 0 0 13 0 0 0 0 0 1 0 0 0 0 0 0 0 ]
15:34:16.027 -> Sending: [53 55 50 4C 41 ]
15:34:40.577 -> Sending: [53 55 50 4C 41 C A 0 0 0 28 0 0 0 10 0 0 0 1F 0 0 0 0 0 0 0 16 1D 2 0 0 0 0 0 ]
15:34:40.611 -> Sending: [53 55 50 4C 41 ]
15:34:40.645 -> Received: [53 55 50 4C 41 C 4 0 0 0 32 0 0 0 10 0 0 0 2 72 CC 61 0 0 0 0 A0 2A 1 0 0 0 0 0 53 55 50 4C 41 ]
15:35:05.635 -> Sending: [53 55 50 4C 41 C B 0 0 0 28 0 0 0 10 0 0 0 38 0 0 0 0 0 0 0 A 33 3 0 0 0 0 0 ]
15:35:05.737 -> Sending: [53 55 50 4C 41 ]
15:35:05.771 -> Received: [53 55 50 4C 41 C 5 0 0 0 32 0 0 0 10 0 0 0 1B 72 CC 61 0 0 0 0 DE 39 3 0 0 0 0 0 53 55 50 4C 41 ]
15:35:30.801 -> Sending: [53 55 50 4C 41 C C 0 0 0 28 0 0 0 10 0 0 0 51 0 0 0 0 0 0 0 AD 42 5 0 0 0 0 0 ]
15:35:30.863 -> Sending: [53 55 50 4C 41 ]
15:35:30.897 -> Received: [53 55 50 4C 41 C 6 0 0 0 32 0 0 0 10 0 0 0 34 72 CC 61 0 0 0 0 76 F2 4 0 0 0 0 0 53 55 50 4C 41 ]
15:35:55.887 -> Sending: [53 55 50 4C 41 C D 0 0 0 28 0 0 0 10 0 0 0 6A 0 0 0 0 0 0 0 85 FA 6 0 0 0 0 0 ]
Awatar użytkownika
klew
Posty: 8184
Rejestracja: czw cze 27, 2019 12:16 pm
Lokalizacja: Wrocław

Nie widzę tu żadnych logów z klasy Clock. Na pewno ją dodałeś?
Widzimy się na Supla Offline Party vol. 2 :!:
pmar75
Posty: 10
Rejestracja: wt mar 01, 2016 11:58 pm

W programie mam tylko na początku Supla::Clock suplaClock; A potem wyciągam godzinę za pomocą suplaClock.getHour(); do zmiennej i chciałem użyć jej w swoim kodzie .
Awatar użytkownika
klew
Posty: 8184
Rejestracja: czw cze 27, 2019 12:16 pm
Lokalizacja: Wrocław

pmar75 pisze: śr gru 29, 2021 3:13 pm W programie mam tylko na początku Supla::Clock suplaClock; A potem wyciągam godzinę za pomocą suplaClock.getHour(); do zmiennej i chciałem użyć jej w swoim kodzie .
W pierwszych postach tego wątku było opisane jak ustawić Clock. Przyjrzyj je, bo z tego co pamiętam tam coś jeszcze trzeba było wywołać.

W logach widać też jakieś inne ustawienie czasu z sieci - wyrzuć ten fragment kodu, bo to nie będzie razem działać.
Jak ustawisz poprawnie klasę Clock to co jakiś czas pojawia się logi z informacją o pobranym czasie.
Widzimy się na Supla Offline Party vol. 2 :!:
pmar75
Posty: 10
Rejestracja: wt mar 01, 2016 11:58 pm

Dzięki za wskazówki działa :D

17:25:43.011 -> Received local time from server: 2021-12-29 17:25:44
Awatar użytkownika
lukfud
Posty: 2091
Rejestracja: czw lis 23, 2017 11:33 pm
Lokalizacja: Warszawa

klew pisze: pn paź 12, 2020 12:19 pm W planach jest dołożenie możliwości obsługi zewnętrznego zegara z podtrzymaniem na baterii (RTC).
@klew, jak już widziałeś wrzuciłem issue https://github.com/SUPLA/supla-device/issues/20.

Zmodyfikowałem trochę klasę i nawet mi coś z tego wyszło. Model DS3231
Dodałem synchronizację czasu systemowego z RTC na starcie i synchronizację RTC z serwerem po wykryciu zaniku zasilania na module zegarka.

Kod: Zaznacz cały

23:47:43.091 -> Current local time: 1970-1-1 0:0:1
23:47:43.091 -> Received local time from RTC: 2022-9-11 23:47:43
clock.cpp

Kod: Zaznacz cały

/*
 Copyright (C) AC SOFTWARE SP. Z O.O.

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#include <supla-common/srpc.h>
#include <supla/log_wrapper.h>

#include "../time.h"
#include "clock.h"
#include "RTClib.h"

namespace Supla {

Clock::Clock()
    : localtime(0), lastServerUpdate(0), lastMillis(0), isClockReady(false) {
}

void Clock::onInit() {
  if (!rtc.begin()) {
    SUPLA_LOG_DEBUG("# couldn't find RTC");
  } else {
    struct tm timeinfo {};
    isRTCReady = true;
    DateTime now = rtc.now();

    SUPLA_LOG_DEBUG(
            "Current local time: %d-%d-%d %d:%d:%d",
            getYear(),
            getMonth(),
            getDay(),
            getHour(),
            getMin(),
            getSec());

    timeinfo.tm_year = now.year() - 1900;
    timeinfo.tm_mon = now.month() - 1;
    timeinfo.tm_mday = now.day();
    timeinfo.tm_hour = now.hour();
    timeinfo.tm_min = now.minute();
    timeinfo.tm_sec = now.second();

    localtime = mktime(&timeinfo);

#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
    timeval tv = {localtime, 0};
    settimeofday(&tv, nullptr);
#elif defined(ARDUINO_ARCH_AVR)
    set_system_time(mktime(&timeinfo));
#endif
    SUPLA_LOG_DEBUG(
            "Received local time from RTC: %d-%d-%d %d:%d:%d",
            getYear(),
            getMonth(),
            getDay(),
            getHour(),
            getMin(),
            getSec());
  }
}

bool Clock::isReady() {
  return isClockReady;
}

int Clock::getYear() {
  struct tm timeinfo;
  time_t currentTime = time(0);
  gmtime_r(&currentTime, &timeinfo);
  return timeinfo.tm_year + 1900;
}

int Clock::getMonth() {
  struct tm timeinfo;
  //  timeinfo = gmtime(time(0));
  time_t currentTime = time(0);
  gmtime_r(&currentTime, &timeinfo);
  return timeinfo.tm_mon + 1;
}

int Clock::getDay() {
  struct tm timeinfo;
  //  timeinfo = gmtime(time(0));
  time_t currentTime = time(0);
  gmtime_r(&currentTime, &timeinfo);
  return timeinfo.tm_mday;
}

int Clock::getDayOfWeek() {
  struct tm timeinfo;
  //  timeinfo = gmtime(time(0));
  time_t currentTime = time(0);
  gmtime_r(&currentTime, &timeinfo);
  return timeinfo.tm_wday + 1;  // 1 - Sunday, 2 - Monday ...
}

int Clock::getHour() {
  struct tm timeinfo;
  // timeinfo = gmtime(time(0));
  time_t currentTime = time(0);
  gmtime_r(&currentTime, &timeinfo);
  return timeinfo.tm_hour;
}

int Clock::getMin() {
  struct tm timeinfo;
  // timeinfo = gmtime(time(0));
  time_t currentTime = time(0);
  gmtime_r(&currentTime, &timeinfo);
  return timeinfo.tm_min;
}

int Clock::getSec() {
  struct tm timeinfo;
  time_t currentTime = time(0);
  gmtime_r(&currentTime, &timeinfo);
  return timeinfo.tm_sec;
}

void Clock::parseLocaltimeFromServer(TSDC_UserLocalTimeResult *result) {
  struct tm timeinfo {};

  isClockReady = true;

  SUPLA_LOG_DEBUG(
            "Current local time: %d-%d-%d %d:%d:%d",
            getYear(),
            getMonth(),
            getDay(),
            getHour(),
            getMin(),
            getSec());

  timeinfo.tm_year = result->year - 1900;
  timeinfo.tm_mon = result->month - 1;
  timeinfo.tm_mday = result->day;
  timeinfo.tm_hour = result->hour;
  timeinfo.tm_min = result->min;
  timeinfo.tm_sec = result->sec;

  localtime = mktime(&timeinfo);

#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
  timeval tv = {localtime, 0};
  settimeofday(&tv, nullptr);
#elif defined(ARDUINO_ARCH_AVR)
  set_system_time(mktime(&timeinfo));
#endif
  SUPLA_LOG_DEBUG(
            "Received local time from server: %d-%d-%d %d:%d:%d",
            getYear(),
            getMonth(),
            getDay(),
            getHour(),
            getMin(),
            getSec());

  if (isRTCReady && rtc.lostPower()) {
    rtc.adjust(DateTime(getYear(), getMonth(), getDay(), getHour(),
                        getMin(), getSec()));
    SUPLA_LOG_DEBUG("# RTC lost power, let's set the time");
  }
}

void Clock::onTimer() {
  if (isClockReady) {
    uint64_t curMillis = millis();
    int seconds = (curMillis - lastMillis) / 1000;
    if (seconds > 0) {
      lastMillis = curMillis - ((curMillis - lastMillis) % 1000);
      for (int i = 0; i < seconds; i++) {
#if defined(ARDUINO_ARCH_AVR)
        system_tick();
#endif
        localtime++;
      }
    }
  }
}

bool Clock::iterateConnected(void *srpc) {
  if (lastServerUpdate == 0 ||
      millis() - lastServerUpdate > 5 * 60000) {  // update every 5 min
    srpc_dcs_async_get_user_localtime(srpc);
    lastServerUpdate = millis();
    return false;
  }
  return true;
}

};  // namespace Supla
clock.h

Kod: Zaznacz cały

/*
 Copyright (C) AC SOFTWARE SP. Z O.O.

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#ifndef SRC_SUPLA_CLOCK_CLOCK_H_
#define SRC_SUPLA_CLOCK_CLOCK_H_

#include <supla-common/proto.h>
#include <supla/element.h>
#include <time.h>

#include "RTClib.h"

namespace Supla {

class Clock : public Element {
 public:
  Clock();
  void onInit();
  virtual bool isReady();
  virtual int getYear();
  virtual int getMonth();
  virtual int getDay();
  virtual int getDayOfWeek();  // 1 - Sunday, 2 - Monday
  virtual int getHour();
  virtual int getMin();
  virtual int getSec();

  void onTimer();
  bool iterateConnected(void *srpc);

  virtual void parseLocaltimeFromServer(TSDC_UserLocalTimeResult *result);

 protected:
  RTC_DS3231 rtc;
  time_t localtime;
  uint64_t lastServerUpdate;
  uint64_t lastMillis;
  bool isClockReady;
  bool isRTCReady = false;
};

};  // namespace Supla

#endif  // SRC_SUPLA_CLOCK_CLOCK_H_
Co o tym sądzisz? :)
https://www.facebook.com/groups/supladiy/
Awatar użytkownika
klew
Posty: 8184
Rejestracja: czw cze 27, 2019 12:16 pm
Lokalizacja: Wrocław

lukfud pisze: ndz wrz 11, 2022 10:33 pm Co o tym sądzisz? :)
Zastanawiam się, który czas jest dokładniejszy. Bo ogólnie ten systemowy czas u mnie prawie zawsze miał już lekki rozjazd względem tego co dostawałem z serwera co 5 min. Ten zewnętrzny zegar pewnie jest dokładniejszy, więc może lepiej opierać się tutaj na zegarku RTC niż na systemowym?
Jak w w praktyce wygląda? Sprawdzałeś czasy z RTC vs czas systemowe vs czas z serwerów?
Widzimy się na Supla Offline Party vol. 2 :!:
Awatar użytkownika
lukfud
Posty: 2091
Rejestracja: czw lis 23, 2017 11:33 pm
Lokalizacja: Warszawa

klew pisze: pn wrz 12, 2022 7:03 am
lukfud pisze: ndz wrz 11, 2022 10:33 pm Co o tym sądzisz? :)
Zastanawiam się, który czas jest dokładniejszy. Bo ogólnie ten systemowy czas u mnie prawie zawsze miał już lekki rozjazd względem tego co dostawałem z serwera co 5 min. Ten zewnętrzny zegar pewnie jest dokładniejszy, więc może lepiej opierać się tutaj na zegarku RTC niż na systemowym?
Jak w w praktyce wygląda? Sprawdzałeś czasy z RTC vs czas systemowe vs czas z serwerów?
Nie porównywałem ze sobą wszystkich czasów, ale zrobię to i dam znać.
Mam log z nocy i czas systemowy rozjeżdża się z serwerem o 1 sekundę po wcześniejszym łączeniu się z zewnętrznym serwisem.
Przy tej modyfikacji RTC potraktowałem jako backup, kiedy nie ma połączenia z serwerem a nastąpił restart ESP.
https://www.facebook.com/groups/supladiy/
ODPOWIEDZ

Wróć do „Arduino IDE”