ESP32 RTC code not connecting to WiFi

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:

  1. checked some 10 times that my SSID and password are typed correctly into the code (between " " quotes)
  2. moved the board almost attached to the router
  3. made sure my WiFi frequency is 2.4 GHz
  4. reset multiple times the board, the pc and the modem
  5. 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

You should never publish your wifi credentials on the internet. But, you need to verify that they are correct, by using them with a different device.

@aarg Oops! I forgot to hide those. Thanks for pointing it out. I guess I’ll finally find the time to change my password to one I can actually remember!

I have several other devices connected with those same credentials, so I guess they are verified…

Sounds like your not connecting to wifi. Comment out a bunch of code that does the other stuff and just try connecting to your wifi first. Also you should try using a timeout for wifi with an error msg
just so you know whats happening.

1 Like

Or just use an example sketch from the ESP core library that is already installed on your system.

1 Like

Or that.

@anon57585045 @noweare Thanks! I’ll try both tips, hopefully tonight. Will let you know

@anon57585045 @noweare Guess what... Turns out that I had mistyped one characther of the password (the number "1" instead of lowercase "l", which look almost identical in whathever font Arduino uses... I found out after seeing that even with your tips it wasn't working and ended up changing my router credentials :smiley: Now it works! Thanks for your help.

Yeah, some fonts make that difference nearly impossible to read.

1 Like

Glad you got it working. You won't forget that one. Get it "that one" ha ha

1 Like

Yeah, that was funny! i won't forget :smiley:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.