I am trying to read a times string received from a server to use to adjust the time on the Arduino WiFi Rev2.
This is the code:
include <Wire.h>
#include <RTClib.h>
#include <SoftwareSerial.h>
#include <WiFiNINA.h>
// Declare global variables
// - Real-time clock object
RTC_DS1307 RT_Clock;
DateTime NowDtTm;
char TimeLine[25];
char SyncDate[12];
char SyncTime[9];
void setup ()
{
// Launch send diagnostic messages
Serial.begin(9600);
while (!Serial);
Serial.println( "" );
Serial.println( F( "Arduino ***** DIAGS RUNNING *****" ));
// RTC launch
Wire.begin();
RT_Clock.begin();
Serial.println( F( "RT_Clock running" ) );
// Send time adjust trigger. and wait for response
// Send2Srvr( "AdjT", "Initial time adjust request", true );
AdjRTC();
}
void loop()
{
}
void AdjRTC()
{
int NrChar = myTcpClient.readBytesUntil( '\n', TimeLine, 100);
Serial.println( TimeLine );
sscanf(TimeLine, "%s,%s", SyncDate, SyncTime );
Serial.println( SyncDate );
Serial.println( SyncTime );
// Note current date and time
NowDtTm = RT_Clock.now();
sprintf(ChgMsgFr, "%04d-%02d-%02d %02d:%02d:%02d", NowDtTm.year(), NowDtTm.month(), NowDtTm.day(), NowDtTm.hour(), NowDtTm.minute(), NowDtTm.second());
// Adjust
RT_Clock.adjust(DateTime(SyncDate, SyncTime));
// Note New date and time
NowDtTm = RT_Clock.now();
sprintf(ChgMsgTo, "%04d-%02d-%02d %02d:%02d:%02d", NowDtTm.year(), NowDtTm.month(), NowDtTm.day(), NowDtTm.hour(), NowDtTm.minute(), NowDtTm.second());
// Send2Srvr( "Ctrl", ChgMsgFr, false );
delay(250);
// Send2Srvr( "Ctrl", ChgMsgTo, false );
}
I’ve left out the stuff about the TCP connection and other things but these are all working fine. I just wanted to try getting an adjustment string from the server and the sscanf doesn’t seem to work.
These are the three diagnostic lines when the message is received:
14:44:55.576 → Sep 24 2021,14:44:54
14:44:55.622 → Sep
14:44:55.622 →
The first is perfectly correct. I then want to break it into Date and Time based on the comma separator but it gate “Sep” as the date and nothing for the time. Like it is seeings the space after Sep as a field divider rather than the comma.
In a separate run, I printed NrChar and it was 20 - seems correct.
Also printing the TimeLine string between a leading and trailing pipe “|” also shows no extra spaces.
Any suggestions?