Hi, I am a beginner working on a project with a Lilygo T-Watch 2021 board (ESP32 based). I am trying to use the RTC example code I found on the product GitHub page (I have installed ESP32 support and all the necessary libraries), but even if the code compiles and uploads without errors, it cannot connect to my WiFi and get the time.
This is my code, with some extra comments I put there to help remembering how it works:
#include <Arduino.h>
#include <TWatch_hal.h>
#include <WiFi.h>
//"TWatch_config.h" needs to be configured in the library for the first time
// This routine needs to annotate the LVGL GUI
TWatchClass *twatch = nullptr; // twatch is declared as object of the TWatchClass class. non so cosa cambia l'asterisco
TFT_eSPI *tft = nullptr;
PCF8563_Class *rtc = nullptr;
/*****************Time synchronization parameters********************/
/* A Constant variable in C++ is a variable whose value does not change during the execution of the program - const */
const char *ntpServer1 = "pool.ntp.org"; // const char * makes your string immutable and the pointer location still can flexibly change
const char *ntpServer2 = "ntp1.aliyun.com";
const long gmtOffset_sec = 60 * 60 * 8; // dovvrebbero essere 8 ore in secondi
const int daylightOffset_sec = 0;
const char *ssid = "Telecom-14113461"; // "your 2.4G wifi ssid";
const char *pass = "fzZeVMDCG13wTDewRcr1G3r5"; // "your password";
void SyncTime(void *param) {
struct tm timeinfo; // Structure is a user-defined data type created to store different types of values under a single variable.
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println(""); // AGGIUNTO
Serial.println("WiFi connected."); // AGGIUNTO
// Synchronize RTC time
while (!getLocalTime(&timeinfo)) {
// init and get the time DA TOGLIERE DA QUI SECONDO ME
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer2, ntpServer1);
Serial.println("Failed to obtain time , try again");
}
twatch->rtc_set_time(timeinfo.tm_year + 1900, timeinfo.tm_mon, timeinfo.tm_mday, timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec);
// disconnect WiFi as it's no longer needed
WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);
}
void setup() {
twatch = TWatchClass::getWatch(); /* Scope Resolution Operator written as two colons :: and is used to access the instance variables and instance methods using their class name in different situations. */
tft = twatch->tft_get_instance();
rtc = twatch->rtc_get_instance();
Serial.begin(9600);
Serial.println("RTC Demo");
twatch->hal_auto_update(true, 0);
tft->fillScreen(TFT_BLACK);
twatch->backlight_set_value(255);
tft->setTextFont(2);
tft->setTextColor(TFT_WHITE, TFT_BLACK);
tft->drawString("T-Watch RTC Test", 62, 90);
tft->drawString("Press BTN1 to synchronize time", 20, 108);
twatch->button_bind_event(TWATCH_BTN_1, BUTTON_CLICK, SyncTime); // -> arrow operator pointer name -> veriable name
twatch->rtc_set_time(2022, 2, 19, 10, 56, 30);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println(rtc->formatDateTime(PCF_TIMEFORMAT_YYYY_MM_DD_H_M_S));
tft->drawString(rtc->formatDateTime(PCF_TIMEFORMAT_YYYY_MM_DD), 90, 126); // Instance methods are used to store or process data stored in instance variables and are used only by the object of the class.
tft->drawString(rtc->formatDateTime(PCF_TIMEFORMAT_HMS), 95, 144);
delay(1000);
}
After uploading I get the expected messages both on the serial monitor and on the watch screen, but when I press the board button 1 to initiate the time sync, the serial monitor starts printing the dots "." before the "unsynced time and it just goes on forever. It does not print "WiFi connected", nor "Failed to obtain time".
I have tried the following:
- checked some 10 times that my SSID and password are typed correctly into the code (between " " quotes)
- moved the board almost attached to the router
- made sure my WiFi frequency is 2.4 GHz
- reset multiple times the board, the pc and the modem
- tested signal reception with a WiFi scan example code taken from the same Lilygo TWatch library, and the board correctly sees my network and RSSI.
Can anybody see where the problem might be? Thanks in advance!
Angelo