I improved my code. Concerning this question, I am satisfied with the way things function now. Thanks for setting me on the right track!
Solution:
/*
*** Repeating WiFi Web Client ***
This sketch connects to a a web server and makes a request
using a WiFi equipped Arduino board.
[by NN, Tom Igoe, Federico Vanzati 23-04-12 - 13-01-14 ]
Find the full UNO R4 WiFi Network documentation here:
https://docs.arduino.cc/tutorials/uno-r4-wifi/wifi-examples#wi-fi-web-client-repeating
*/
bool verbose = false;
bool debugzz = false;
// for getting the date and time by WiFi communication:
#include "WiFiS3.h"
#include "secrets.h"
#include "RTC.h"
int keyIndex = 0;
int status = WL_IDLE_STATUS;
// for depicting the time
#include "Base4_4Col.h"
#define DGH_line 9
#define DGM_line 10
#define DG_01 0
#define DG_02 1
#define DG_03 2
#define DG_04 3
#define DG_08 4
#define DG_12 5
#define DG_16 6
#define DG_32 7
#define DG_48 8
#include "Arduino_LED_Matrix.h"
/* ============================================== */
WiFiClient client; // Initialize the WiFi client library
char server[] = "hygrometer.wissisch.nl"; // server address
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
const unsigned long inquireInterval = 10L * 1000L; // delay between updates, in milliseconds
String findTimeLine = "Init GO...";
bool fillTimeLine = true;
RTCTime currentTime;
/* ============================================== */
ArduinoLEDMatrix matrix; // Initialize the LED Matrix
unsigned long lastShownHours = 0;
unsigned long lastShownMinutes = 0;
const unsigned long showTimeInterval = 10L * 1000L;
int secNow;
uint32_t showNow[3];
uint8_t frame[8][12];
/* ===================================================================== */
void setup() {
/* ===================================================================== */
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) ;
delay(1000);
// if (debugzz) Serial.println("_______A setup");
RTC.begin();
matrix.begin();
showMatrixHours(16);
while (!ConnectToWifi()) ;
while (!GetServerTime()) ;
SetRealerTime(findTimeLine);
// if (debugzz) Serial.println("_______b setup");
checkDateTime();
findTimeLine = "First GO...";
fillTimeLine = true;
uint8_t frame[8][12] = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};
// if (debugzz) Serial.println("_______V setup : " + findTimeLine + "^^ooooooooo");
// if (debugzz) Serial.println("");
// if (debugzz) Serial.println("");
}
/* ===================================================================== */
void loop() {
/* ===================================================================== */
// if there's incoming data from the net connection.
// send it out the serial port. This is for debugging
// purposes only:
// if the number of 'inquireInterval' seconds have passed since your last connection,
// then connect again and send data:
if (millis() - lastConnectionTime > inquireInterval) {
// if (debugzz) Serial.println("_______b loop");
while (!GetServerTime()) ;
SetRealerTime(findTimeLine);
// checkDateTime();
findTimeLine = "next GO...";
fillTimeLine = true;
// if (debugzz) Serial.println("_______c loop");
}
delay(200);
/*
if (millis() - lastShownHours > showTimeInterval) { */
showMatrixHours(currentTime.getHour());
// Serial.print("Hours: " + (String)currentTime.getHour() + " ");
delay(4000); /*
}
if (millis() - lastShownHours > showTimeInterval + 4000) { */
showMatrixMinutes(currentTime.getMinutes());
// Serial.println("Minutes: " + (String)currentTime.getMinutes());
delay(6000); /*
}
*/
// if (debugzz) Serial.println("_______V loop");
}
/* ===================================================================== */
/* --------------------------------------------------------------------- */
bool ConnectToWifi(){
/* --------------------------------------------------------------------- */
// if (debugzz) Serial.println("_______A ConnectToWifi");
// check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
// don't continue
while (true);
}
// check firmware version (just warn):
String fv = WiFi.firmwareVersion();
if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
Serial.println("Please upgrade the firmware");
}
// attempt to connect to WiFi network:
while (status != WL_CONNECTED) {
if (verbose) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
}
status = WiFi.begin(ssid, pass);
//wait 10 seconds for connection:
delay(10000);
}
// you're connected now
printWifiStatus();
// if (debugzz) Serial.println("_______V ConnectToWifi");
return true;
}
/* --------------------------------------------------------------------- */
bool GetServerTime(){
/* --------------------------------------------------------------------- */
// if (debugzz) Serial.println("_______A GetServerTime");
if (findTimeLine.substring(0,4) == "Init" || findTimeLine.substring(0,5) == "First") delay(2000);
do {
httpRequest();
delay(1200);
} while (!TalkToServer()) ;
if (findTimeLine.length() == 31) {
return true;
} else {
return false;
}
// if (debugzz) Serial.println("_______V GetServerTime");
}
/* --------------------------------------------------------------------- */
void httpRequest() {
/* --------------------------------------------------------------------- */
// this method makes a HTTP connection to the server:
//if (debugzz) Serial.println("_______A httpRequest");
// close any connection before send a new request.
// This will free the socket on the NINA module
client.stop();
// if there's a successful connection:
if (client.connect(server, 80)) {
//if (verbose) Serial.println("connecting...");
// send the HTTP GET request:
client.println("GET /timeline.php HTTP/1.1");
client.println("Host: hygrometer.wissisch.nl");
client.println("User-Agent: ArduinoWiFi/1.1");
client.println("Connection: close");
client.println();
// note the time that the connection was made:
lastConnectionTime = millis();
//if (debugzz) Serial.println("_______b httpRequest");
} else {
// if you couldn't make a connection:
Serial.println("connection failed");
//if (debugzz) Serial.println("_______c httpRequest");
}
if (debugzz) Serial.println("-");
}
/* --------------------------------------------------------------------- */
bool TalkToServer(){
/* --------------------------------------------------------------------- */
// if (debugzz) Serial.println("_______A TalkToServer");
readRequest();
//if (verbose) Serial.println("INFO: " + findTimeLine); // date and time info
if (findTimeLine.length() != 31) return false;
// if (debugzz) Serial.println("_______V TalkToServer");
return true;
}
/* --------------------------------------------------------------------- */
void readRequest() {
/* --------------------------------------------------------------------- */
// if (debugzz) Serial.println("_______A readRequest");
uint32_t received_data_num = 0;
while (client.available()) {
/* actual data reception */
char c = client.read();
buildTimeLine(c); // distills the Date and Time for correcting the R4's RealTimeClock
/* print data to serial port */
if (verbose) Serial.print(c);
/* wrap data to 80 columns*/
received_data_num++;
if(received_data_num % 80 == 0) {
}
}
// if (debugzz) Serial.println("_______V readRequest");
}
/* --------------------------------------------------------------------- */
void buildTimeLine(char nu){
/* --------------------------------------------------------------------- */
if (fillTimeLine) findTimeLine += (String)nu;
if(findTimeLine.substring(findTimeLine.length() - 7) == "<body>\n") findTimeLine = ""; // strip info before
if(findTimeLine.substring(findTimeLine.length() - 5) == "[END]") {
fillTimeLine = false; // ignore info after
// // if (debugzz) Serial.println("_______b buildTimeLine");
}
}
/* --------------------------------------------------------------------- */
void SetRealerTime(String fTL){ // fTL is like "2027-11-26 19:42:37, 5, 1 [END]"
/* --------------------------------------------------------------------- */
// if (debugzz) Serial.println("_______A SetRealerTime : " + fTL);
int jaar, maand, dag, uur, minuut, seconde, w_dag, licht_b;
jaar = (int)fTL.substring(0,4).toInt();
// // if (debugzz) Serial.println("_______b SetRealerTime : " + (String)jaar + "<");
maand = (int)fTL.substring(5,7).toInt();
dag = (int)fTL.substring(8,10).toInt();
uur = (int)fTL.substring(11,13).toInt();
minuut = (int)fTL.substring(14,16).toInt();
seconde = (int)fTL.substring(17,19).toInt();
w_dag = (int)fTL.substring(21,22).toInt();
licht_b = (int)fTL.substring(24,25).toInt();
RTCTime startTime(dag, deliverMonth(maand), jaar, uur, minuut, seconde, deliverDoW(w_dag), deliverLightS(licht_b));
RTC.setTime(startTime);
// if (debugzz) Serial.println("_______V SetRealerTime");
}
/* --------------------------------------------------------------------- */
Month deliverMonth(int mnd){
/* --------------------------------------------------------------------- */
// if (debugzz) Serial.println("_______A deliverMonth");
switch (mnd) {
case 1: return Month::JANUARY; break;
case 2: return Month::FEBRUARY; break;
case 3: return Month::MARCH; break;
case 4: return Month::APRIL; break;
case 5: return Month::MAY; break;
case 6: return Month::JUNE; break;
case 7: return Month::JULY; break;
case 8: return Month::AUGUST; break;
case 9: return Month::SEPTEMBER; break;
case 10: return Month::OCTOBER; break;
case 11: return Month::NOVEMBER; break;
case 12: return Month::DECEMBER; break;
default: Serial.println("Wrong month code (" + (String)mnd + ")"); return Month::JANUARY; break;
}
// if (debugzz) Serial.println("_______V !!! deliverMonth");
}
/* --------------------------------------------------------------------- */
DayOfWeek deliverDoW(int dow){
/* --------------------------------------------------------------------- */
// if (debugzz) Serial.println("_______A deliverDoW");
switch (dow) {
case 0: return DayOfWeek::SUNDAY; break;
case 1: return DayOfWeek::MONDAY; break;
case 2: return DayOfWeek::TUESDAY; break;
case 3: return DayOfWeek::WEDNESDAY; break;
case 4: return DayOfWeek::THURSDAY; break;
case 5: return DayOfWeek::FRIDAY; break;
case 6: return DayOfWeek::SATURDAY; break;
default: Serial.println("Wrong day of week code (" + (String)dow + ")"); return DayOfWeek::SUNDAY; break;
}
// if (debugzz) Serial.println("_______V !!! deliverDoW");
}
/* --------------------------------------------------------------------- */
SaveLight deliverLightS(int sli){
/* --------------------------------------------------------------------- */
// if (debugzz) Serial.println("_______A deliverLightS");
switch (sli) {
case 0: return SaveLight::SAVING_TIME_INACTIVE; break;
case 1: return SaveLight::SAVING_TIME_ACTIVE; break;
default: Serial.println("Wrong save light code (" + (String)sli + ")"); return SaveLight::SAVING_TIME_INACTIVE; break;
}
// if (debugzz) Serial.println("_______V !!! deliverLightS");
}
/* --------------------------------------------------------------------- */
void printWifiStatus() {
/* --------------------------------------------------------------------- */
// if (debugzz) Serial.println("_______A printWifiStatus");
if (verbose){
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your board's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
// if (debugzz) Serial.println("_______V printWifiStatus");
}
/* --------------------------------------------------------------------- */
void clearMatrix(){
/* --------------------------------------------------------------------- */
// if (debugzz) Serial.println("_______A clearMatrix");
uint32_t showNow[3] = {
0x0,
0x0,
0x0
};
matrix.loadFrame(showNow);
// if (debugzz) Serial.println("_______V clearMatrix");
}
/* --------------------------------------------------------------------- */
void showMatrixHours(int numb){
/* --------------------------------------------------------------------- */
// if (debugzz) Serial.println("_______A showMatrixHours");
// Empty matrix with Hours-marker:
showNow[0] = Base4_4Col[DGH_line][0];
showNow[1] = Base4_4Col[DGH_line][1];
showNow[2] = Base4_4Col[DGH_line][2];
while (numb > 0){
if (numb >= 16){
showNow[0] |= Base4_4Col[DG_16][0];
showNow[1] |= Base4_4Col[DG_16][1];
showNow[2] |= Base4_4Col[DG_16][2];
numb -= 16;
}
if (numb >= 12){
showNow[0] |= Base4_4Col[DG_12][0];
showNow[1] |= Base4_4Col[DG_12][1];
showNow[2] |= Base4_4Col[DG_12][2];
numb -= 12;
}
if (numb >= 8){
showNow[0] |= Base4_4Col[DG_08][0];
showNow[1] |= Base4_4Col[DG_08][1];
showNow[2] |= Base4_4Col[DG_08][2];
numb -= 8;
}
if (numb >= 4){
showNow[0] |= Base4_4Col[DG_04][0];
showNow[1] |= Base4_4Col[DG_04][1];
showNow[2] |= Base4_4Col[DG_04][2];
numb -= 4;
}
if (numb >= 3){
showNow[0] |= Base4_4Col[DG_03][0];
showNow[1] |= Base4_4Col[DG_03][1];
showNow[2] |= Base4_4Col[DG_03][2];
numb -= 3;
}
if (numb >= 2){
showNow[0] |= Base4_4Col[DG_02][0];
showNow[1] |= Base4_4Col[DG_02][1];
showNow[2] |= Base4_4Col[DG_02][2];
numb -= 2;
}
if (numb >= 1){
showNow[0] |= Base4_4Col[DG_01][0];
showNow[1] |= Base4_4Col[DG_01][1];
showNow[2] |= Base4_4Col[DG_01][2];
numb -= 1;
}
}
matrix.loadFrame(showNow);
// if (debugzz) Serial.println("_______V showMatrixHours");
}
/* --------------------------------------------------------------------- */
void showMatrixMinutes(int numb){
/* --------------------------------------------------------------------- */
// if (debugzz) Serial.println("_______A showMatrixMinutes");
// Empty matrix with Minutes-marker:
showNow[0] = Base4_4Col[DGM_line][0];
showNow[1] = Base4_4Col[DGM_line][1];
showNow[2] = Base4_4Col[DGM_line][2];
while (numb > 0){
if (numb >= 48){
showNow[0] |= Base4_4Col[DG_48][0];
showNow[1] |= Base4_4Col[DG_48][1];
showNow[2] |= Base4_4Col[DG_48][2];
numb -= 48;
}
if (numb >= 32){
showNow[0] |= Base4_4Col[DG_32][0];
showNow[1] |= Base4_4Col[DG_32][1];
showNow[2] |= Base4_4Col[DG_32][2];
numb -= 32;
}
if (numb >= 16){
showNow[0] |= Base4_4Col[DG_16][0];
showNow[1] |= Base4_4Col[DG_16][1];
showNow[2] |= Base4_4Col[DG_16][2];
numb -= 16;
}
if (numb >= 12){
showNow[0] |= Base4_4Col[DG_12][0];
showNow[1] |= Base4_4Col[DG_12][1];
showNow[2] |= Base4_4Col[DG_12][2];
numb -= 12;
}
if (numb >= 8){
showNow[0] |= Base4_4Col[DG_08][0];
showNow[1] |= Base4_4Col[DG_08][1];
showNow[2] |= Base4_4Col[DG_08][2];
numb -= 8;
}
if (numb >= 4){
showNow[0] |= Base4_4Col[DG_04][0];
showNow[1] |= Base4_4Col[DG_04][1];
showNow[2] |= Base4_4Col[DG_04][2];
numb -= 4;
}
if (numb >= 3){
showNow[0] |= Base4_4Col[DG_03][0];
showNow[1] |= Base4_4Col[DG_03][1];
showNow[2] |= Base4_4Col[DG_03][2];
numb -= 3;
}
if (numb >= 2){
showNow[0] |= Base4_4Col[DG_02][0];
showNow[1] |= Base4_4Col[DG_02][1];
showNow[2] |= Base4_4Col[DG_02][2];
numb -= 2;
}
if (numb >= 1){
showNow[0] |= Base4_4Col[DG_01][0];
showNow[1] |= Base4_4Col[DG_01][1];
showNow[2] |= Base4_4Col[DG_01][2];
numb -= 1;
}
}
matrix.loadFrame(showNow);
// note the time that the connection was made:
// lastConnectionTime = millis();
// if (debugzz) Serial.println("_______V showMatrixMinutes");
}
/* --------------------------------------------------------------------- */
void checkDateTime(){
/* --------------------------------------------------------------------- */
// // if (debugzz) Serial.println("_______A checkDateTime");
// Serial.println("_______A checkDateTime");
RTC.getTime(currentTime); // Get current time from RTC
Serial.print("date time = ");
Serial.print(currentTime.getDayOfMonth());
Serial.print(".");
int m = (int)currentTime.getMonth() + 1;
Serial.print(m);
Serial.print(".");
Serial.print((String)currentTime.getYear() + " ");
Serial.print(currentTime.getHour());
Serial.print(":");
Serial.print(currentTime.getMinutes());
Serial.print(":");
Serial.println(currentTime.getSeconds());
// if (debugzz) Serial.println("_______V checkDateTime");
// Serial.println("_______A checkDateTime");
}
/*
Sketch uses 68332 bytes (26%) of program storage space. Maximum is 262144 bytes.
Global variables use 7468 bytes (22%) of dynamic memory, leaving 25300 bytes for local variables. Maximum is 32768 bytes.
*/
The correct date and time is delivered by my own webpage:
http://hygrometer.wissisch.nl/timeline.php?zone=Australia/Sydney
(Omitting the zone delivers the time for Amsterdam)
Use this 'service' freely (but keep an interval of at least 2 minutes, svp.)