TTGO T-Beam RTC WTH?

this is a stripped down version of a much bigger program. the big program is an intrusion detector system for large properties

note: the code tags are behaving oddly. I am trying to correct that

one of the current aggravations is the RTC. It is detected, it starts, it does not object when I set it with the correct time, and it returns gibberish when I read it back after I set it.

I tried two T Beams and three RTCs and the result is always the same.

the code:

...

#include <WiFi.h>
#include <Wire.h>
#include <LoRa.h> // Sandeepmistry
#include <time.h> // ESP32Time
#include <TinyGPS++.h>
#include <HardwareSerial.h>
#include "RTClib.h"
RTC_DS3231 rtc;
#define I2C_SDA 21 // GRA 21
#define I2C_SCL 22 // VIO 22

HardwareSerial GPSSerial(2);
#define GPS_RX_PIN 34
#define GPS_TX_PIN 12
TinyGPSPlus gps;

String hostname = "T Beam Gateway 09G1";
const char* ssid = "yourNetworkName"; // "yourNetworkName";
const char* password = "yourNetworkPassword"; // "yourNetworkPassword";
const char* ntpServer = "us.pool.ntp.org";
//char date[10] = "hh:mm:ss";
const long gmtOffset_sec = 0;
const int daylightOffset_sec = 3600;
struct tm timeinfo;
byte second;
byte minute;
byte hour;
byte day;
byte month;
byte weekday;
int year;

int counter = 0; // retry connections

void initWiFi()
{
WiFi.mode(WIFI_STA);
WiFi.setHostname(hostname.c_str());// define hostname

Serial.print("WiFi Scan start ");

// WiFi.scanNetworks will return the number of networks found.
int n = WiFi.scanNetworks();
Serial.print("WiFi Scan done ");
if (n == 0)
{
Serial.println("no networks found");
}
else
{
Serial.print(n);
Serial.println(" networks found");
Serial.println("#! | SSID | RSSI | CH | Encryption");
for (int i = 0; i < n; ++i)
{
// Print SSID and RSSI for each network found
Serial.printf("%2d", i + 1);
Serial.print(" | ");
Serial.printf("%-32.32s", WiFi.SSID(i).c_str());
Serial.print(" | ");
Serial.printf("%4d", WiFi.RSSI(i));
Serial.print(" | ");
Serial.printf("%2d", WiFi.channel(i));
Serial.print(" | ");
switch (WiFi.encryptionType(i))
{
case WIFI_AUTH_OPEN:
Serial.print("open");
break;
case WIFI_AUTH_WEP:
Serial.print("WEP");
break;
case WIFI_AUTH_WPA_PSK:
Serial.print("WPA");
break;
case WIFI_AUTH_WPA2_PSK:
Serial.print("WPA2");
break;
case WIFI_AUTH_WPA_WPA2_PSK:
Serial.print("WPA+WPA2");
break;
case WIFI_AUTH_WPA2_ENTERPRISE:
Serial.print("WPA2-EAP");
break;

    default:
      Serial.print("unknown");
  }
  Serial.println();
  delay(10);
}

}
Serial.println("");
WiFi.scanDelete(); // Delete the scan result to free memory for other code.

Serial.print("Connecting to "); Serial.print(ssid); Serial.println(":");
WiFi.begin(ssid, password);

while (!WiFi.begin(ssid, password))
{
counter++;
Serial.print("WiFiv "); Serial.print(counter); Serial.print(" ");
delay(100);
WiFi.reconnect();

if (counter == 10)
{
  counter = 0;
  Serial.println();
  Serial.print("WiFi AP "); Serial.print(ssid); Serial.print(" not available ");
  Serial.println();
  Serial.println();
  return;
}

}
}

void WiFiStationGotIP(WiFiEvent_t event, WiFiEventInfo_t info)
{
Serial.print("WiFi Station Got IP: ");
Serial.println(IPAddress(info.got_ip.ip_info.ip.addr));
Serial.println();
delay(1000);
}

void initTime(String timezone) // stable
{
if (WiFi.begin(ssid, password))
{
Serial.println("Getting NTP time: ");

while (!getLocalTime(&timeinfo) && counter < 10)
{
  configTime(0, 0, ntpServer);      // connect to NTP server, with 0 TZ offset
  counter++;
  Serial.print("NTPv "); Serial.print(counter); Serial.print(" ");
}

Serial.println();
Serial.println();

if (getLocalTime(&timeinfo))
{
  Serial.print("NTP Time: ");
  printLocalTime();
  setTimezone(timezone);
  Serial.print("Local Time: ");
  printLocalTime();
  setRTC();
}

}
else
{
Serial.println("NTPv: readParseGPS"); Serial.println();
readParseGPS();
}
}

void setTimezone(String timezone) // stable
{
Serial.printf("Setting Timezone to %s\n", timezone.c_str());
setenv("TZ", timezone.c_str(), 1); // adjust the TZ. Clock settings are adjusted to show the new local time
tzset();
Serial.println("timezone set. Set RTC to NTP time ");
}

void setRTC()
{
Serial.print("set RTC to: "); printLocalTime(); Serial.println();
rtc.adjust(DateTime(getLocalTime(&timeinfo)));
DateTime now = rtc.now();
Serial.print("RTC time set to: ");
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println(); Serial.println();
}

void printLocalTime() // stable
{
if (!getLocalTime(&timeinfo))
{
Serial.println("Failed to obtain NTP time");
}
Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S zone %Z %z "); // Friday, April 15 2022 19:18:51 zone MDT -0600
Serial.println();
}

void readParseGPS()
{
if (GPSSerial.available())
{
while (GPSSerial.available() > 0)
gps.encode(GPSSerial.read());
}
if (gps.date.isUpdated())
{
Serial.print(F("GPS Year="));
Serial.print(gps.date.year()); Serial.print(" ");
Serial.print(F("GPS Month="));
Serial.print(gps.date.month()); Serial.print(" ");
Serial.print(F("GPS Day="));
Serial.println(gps.date.day());
}
if (gps.time.isUpdated())
{
Serial.print(F("Hour="));
Serial.print(gps.time.hour()); Serial.print(" ");
Serial.print(F("Minute="));
Serial.print(gps.time.minute()); Serial.print(" ");
Serial.print(F("Second="));
Serial.println(gps.time.second());

second  = gps.time.second();
minute  = gps.time.minute();
hour    = gps.time.hour();
day     = gps.date.day();
month   = gps.date.month();
year    = gps.date.year();
Serial.println("Set RTC to GPS");
setRTC();

}
}

void setup()
{
Serial.begin(115200);
Serial.println();
Serial.println ("T Beam Gateway V009G1"); // current status

GPSSerial.begin(9600, SERIAL_8N1, GPS_RX_PIN, GPS_TX_PIN);
pinMode(GPS_TX_PIN, OUTPUT); // TO GPS RX
pinMode(GPS_RX_PIN, INPUT); // FROM GPS TX

Wire.begin(I2C_SCL, I2C_SDA);

Serial.println ("I2C scanner. Scanning ...");
byte count = 0;

for (byte i = 8; i < 120; i++)
{
Wire.beginTransmission (i);
if (Wire.endTransmission () == 0)
{
Serial.print ("Found address: ");
Serial.print (i, DEC);
Serial.print (" (0x");
Serial.print (i, HEX);
Serial.println (")");
count++;
delay (1); // maybe unneeded?
} // end of good response
} // end of for loop
Serial.println ("Done.");
Serial.print ("Found ");
Serial.print (count, DEC);
Serial.println (" device(s).");
Serial.println();

if (!rtc.begin())
{
Serial.println("RTCv");
Serial.println();
}
else if (rtc.begin())
{
Serial.println("rtc.begin");
Serial.println();
rtc.disable32K(); // disable the 32K Pin
rtc.clearAlarm(1); // reset alarm 1, 2 flag on reboot & recompile
rtc.clearAlarm(2);
rtc.disableAlarm(2); // turn off alarm 2 at reboot
rtc.writeSqwPinMode(DS3231_OFF); // stop signals at SQW Pin
rtc.setAlarm1(DateTime(0, 0, 0, 0, 1, 0), DS3231_A1_Hour);
Serial.println("Alarm(1) set to trigger every ten minutes for testing");
Serial.println();
}

initWiFi();
WiFi.onEvent(WiFiStationGotIP, SYSTEM_EVENT_STA_GOT_IP);
initTime("MST7MDT,M3.2.0,M11.1.0"); // MST Timezone - Leo's Notes
setRTC;
}

void loop()
{
}

...

the result:

16:29:14.810 -> T Beam Gateway V009G1
16:29:14.810 -> I2C scanner. Scanning ...
16:29:14.810 -> Found address: 87 (0x57)
16:29:14.810 -> Found address: 104 (0x68)
16:29:14.810 -> Done.
16:29:14.810 -> Found 2 device(s).
16:29:14.810 -> 
16:29:14.810 -> rtc.begin
16:29:14.810 -> 
16:29:14.810 -> Alarm(1) set to trigger every ten minutes for testing
16:29:14.810 -> 
16:29:14.934 -> WiFi Scan start WiFi Scan done 1 networks found
16:29:19.255 -> #! | SSID                             | RSSI | CH | Encryption
16:29:19.255 ->  1 | yourNetworkName   |  -53 |  1 | WPA2
16:29:19.255 -> 
16:29:19.255 -> Connecting to yourNetworkName:
16:29:19.255 -> Getting NTP time: 
16:29:23.701 -> WiFi Station Got IP: 192.168.52.174
16:29:23.701 -> 
16:29:24.264 -> NTPv 1 
16:29:24.895 -> 
16:29:24.895 -> NTP Time: Sunday, May 07 2023 22:29:24 zone UTC +0000 
16:29:24.895 -> 
16:29:24.895 -> Setting Timezone to MST7MDT,M3.2.0,M11.1.0
16:29:24.895 -> timezone set. Set RTC to NTP time 
16:29:24.895 -> Local Time: Sunday, May 07 2023 16:29:24 zone MDT -0600 
16:29:24.895 -> 
16:29:24.895 -> set RTC to: Sunday, May 07 2023 16:29:24 zone MDT -0600 
16:29:24.895 -> 
16:29:24.895 -> 
16:29:24.895 -> RTC time set to: 2106/2/66:28:17
16:29:24.895 -> 

two questions: how does this get in the middle, when the code above is delayed to prevent this?

16:29:19.255 -> Connecting to yourNetworkName:
16:29:19.255 -> Getting NTP time:
16:29:23.701 -> WiFi Station Got IP: 192.168.52.174

and where does this come from?

16:29:24.895 -> set RTC to: Sunday, May 07 2023 16:29:24 zone MDT -0600
16:29:24.895 ->
16:29:24.895 ->
16:29:24.895 -> RTC time set to: 2106/2/66:28:17
16:29:24.895 ->

Edit your post and insert code tags!

It might be caused by several WiFi.begin() calls in your code but with the mess we see that's hard to tell.

From your code. But I guess you mean why does it show a wrong time. But the answer is the same:

getLocalTime() returns a boolean value. It doesn't make much sense to create a DateTime object out of a boolean value.

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