Update experimental UDP protocol

This commit is contained in:
Sjef Verhoeven PE5PVB
2025-01-28 15:25:17 +01:00
parent b6ac075875
commit d1c2b59751
3 changed files with 51 additions and 25 deletions

View File

@@ -944,6 +944,7 @@ void setup() {
void loop() { void loop() {
if (wifi) webserver.handleClient(); if (wifi) webserver.handleClient();
if (wifi && radio.rds.correctPI != 0) sendUDPlog();
if (wifi && millis() >= NTPtimer + 1800000) { if (wifi && millis() >= NTPtimer + 1800000) {
NTPupdate(); NTPupdate();
@@ -1025,7 +1026,6 @@ void loop() {
while (digitalRead(ROTARY_BUTTON) == LOW) delay(50); while (digitalRead(ROTARY_BUTTON) == LOW) delay(50);
ShowFreq(0); ShowFreq(0);
} }
if (wifi) sendUDPlog(true);
autologged = true; autologged = true;
} }
TuneUp(); TuneUp();
@@ -1078,8 +1078,6 @@ void loop() {
break; break;
} }
} }
} else {
if (wifi) sendUDPlog(false);
} }
if (millis() >= tuningtimer + 200) rdsflagreset = false; if (millis() >= tuningtimer + 200) rdsflagreset = false;

View File

@@ -237,7 +237,7 @@ byte addRowToCSV() {
} }
// Fetch the current date and time as a string // Fetch the current date and time as a string
String currentDateTime = getCurrentDateTime(); String currentDateTime = getCurrentDateTime(false);
// Use a placeholder ("-,-") if the date and time could not be retrieved // Use a placeholder ("-,-") if the date and time could not be retrieved
if (currentDateTime == "") { if (currentDateTime == "") {
@@ -313,7 +313,7 @@ byte addRowToCSV() {
} }
} }
String getCurrentDateTime() { String getCurrentDateTime(bool inUTC) {
// Check if the RTC has been set // Check if the RTC has been set
if (!rtcset) { if (!rtcset) {
return "-,-"; // Return placeholder when the RTC is not set return "-,-"; // Return placeholder when the RTC is not set
@@ -325,22 +325,30 @@ String getCurrentDateTime() {
return "-,-"; // Return placeholder if local time is unavailable return "-,-"; // Return placeholder if local time is unavailable
} }
// Adjust timeInfo using the GMT offset // Convert struct tm to time_t format
time_t currentEpoch = mktime(&timeInfo); // Convert struct tm to time_t format time_t currentEpoch = mktime(&timeInfo);
// Calculate GMT offset // Determine UTC offset as a number
currentEpoch += (NTPupdated ? NTPoffset * 3600 : radio.rds.offset); // Apply GMT offset if NTPupdated, else RDS offset int utcOffsetHours = 0;
// Apply DST adjustment if NTPupdated and autoDST are true if (!inUTC) {
if (NTPupdated && autoDST) { // Adjust for time zone offset and DST if not in UTC
struct tm tempTimeInfo; currentEpoch += (NTPupdated ? NTPoffset * 3600 : radio.rds.offset); // Apply GMT offset if NTPupdated, else RDS offset
localtime_r(&currentEpoch, &tempTimeInfo); // Convert to struct tm for DST calculation
if (isDST(mktime(&tempTimeInfo))) { // Check if DST is in effect // Apply DST adjustment if NTPupdated and autoDST are true
currentEpoch += 3600; // Add 1-hour DST offset if (NTPupdated && autoDST) {
struct tm tempTimeInfo;
localtime_r(&currentEpoch, &tempTimeInfo); // Convert to struct tm for DST calculation
if (isDST(mktime(&tempTimeInfo))) { // Check if DST is in effect
currentEpoch += 3600; // Add 1-hour DST offset
}
} }
} else {
// Calculate UTC offset in hours when inUTC is true
utcOffsetHours = (NTPupdated ? NTPoffset : radio.rds.offset / 3600);
} }
// Convert adjusted time back to struct tm format // Convert adjusted (or original) time back to struct tm format
localtime_r(&currentEpoch, &timeInfo); localtime_r(&currentEpoch, &timeInfo);
// Buffer for formatted date-time string // Buffer for formatted date-time string
@@ -358,12 +366,25 @@ String getCurrentDateTime() {
String timeWithAMPM = String(hour) + ":" + (timeInfo.tm_min < 10 ? "0" : "") + String(timeInfo.tm_min) + ":" + (timeInfo.tm_sec < 10 ? "0" : "") + String(timeInfo.tm_sec) + " " + ampm; String timeWithAMPM = String(hour) + ":" + (timeInfo.tm_min < 10 ? "0" : "") + String(timeInfo.tm_min) + ":" + (timeInfo.tm_sec < 10 ? "0" : "") + String(timeInfo.tm_sec) + " " + ampm;
// Append UTC offset if inUTC
if (inUTC) {
String utcOffsetStr = ", " + String(utcOffsetHours) + ",";
return String(buf) + "," + timeWithAMPM + utcOffsetStr;
}
// Return the final formatted date and time for the USA region // Return the final formatted date and time for the USA region
return String(buf) + "," + timeWithAMPM; return String(buf) + "," + timeWithAMPM;
} else { } else {
// European format: DD-MM-YYYY, HH:MM:SS // European format: DD-MM-YYYY, HH:MM:SS
strftime(buf, sizeof(buf), "%d-%m-%Y", &timeInfo); // Format as DD-MM-YYYY strftime(buf, sizeof(buf), "%d-%m-%Y", &timeInfo); // Format as DD-MM-YYYY
String timeEuropean = String(timeInfo.tm_hour) + ":" + (timeInfo.tm_min < 10 ? "0" : "") + String(timeInfo.tm_min) + ":" + (timeInfo.tm_sec < 10 ? "0" : "") + String(timeInfo.tm_sec); // Format time with leading zero if needed String timeEuropean = String(timeInfo.tm_hour) + ":" + (timeInfo.tm_min < 10 ? "0" : "") + String(timeInfo.tm_min) + ":" + (timeInfo.tm_sec < 10 ? "0" : "") + String(timeInfo.tm_sec); // Format time with leading zero if needed
// Append UTC offset if inUTC
if (inUTC) {
String utcOffsetStr = ", " + String(utcOffsetHours) + ",";
return String(buf) + "," + timeEuropean + utcOffsetStr;
}
return String(buf) + "," + timeEuropean; return String(buf) + "," + timeEuropean;
} }
} }
@@ -437,9 +458,9 @@ void printLogbookCSV() {
Serial.println("===== End of logbook.csv ====="); Serial.println("===== End of logbook.csv =====");
} }
void sendUDPlog(bool scanner_active) { void sendUDPlog() {
// Get the current date and time as a string // Get the current date and time as a string
String currentDateTime = getCurrentDateTime(); String currentDateTime = getCurrentDateTime(true);
// Use a placeholder if the date and time could not be retrieved // Use a placeholder if the date and time could not be retrieved
if (currentDateTime == "") { if (currentDateTime == "") {
@@ -496,7 +517,7 @@ void sendUDPlog(bool scanner_active) {
String AF = ""; String AF = "";
if (radio.rds.hasAF && radio.af_counter > 0) { if (radio.rds.hasAF && radio.af_counter > 0) {
for (byte i = 0; i < radio.af_counter; i++) { for (byte i = 0; i < radio.af_counter; i++) {
AF += String(radio.af[i].frequency / 100) + "." + String((radio.af[i].frequency % 100) / 10) + AF += String(radio.af[i].frequency / 100) + "." + String((radio.af[i].frequency % 100) / 10) +
(i == radio.af_counter - 1 ? "" : ";"); (i == radio.af_counter - 1 ? "" : ";");
} }
} }
@@ -505,7 +526,7 @@ void sendUDPlog(bool scanner_active) {
String EON = ""; String EON = "";
if (radio.eon_counter > 0) { if (radio.eon_counter > 0) {
for (byte i = 0; i < radio.eon_counter; i++) { for (byte i = 0; i < radio.eon_counter; i++) {
EON += String(radio.eon[i].picode) + EON += String(radio.eon[i].picode) +
(radio.eon[i].ps.length() > 0 ? String(";" + radio.eon[i].ps) : ";") + (radio.eon[i].ps.length() > 0 ? String(";" + radio.eon[i].ps) : ";") +
(radio.eon[i].mappedfreq > 0 ? String(";" + String(radio.eon[i].mappedfreq / 100) + "." + String((radio.eon[i].mappedfreq % 100) / 10)) : ";") + (radio.eon[i].mappedfreq > 0 ? String(";" + String(radio.eon[i].mappedfreq / 100) + "." + String((radio.eon[i].mappedfreq % 100) / 10)) : ";") +
(radio.eon[i].mappedfreq2 > 0 ? String(";" + String(radio.eon[i].mappedfreq2 / 100) + "." + String((radio.eon[i].mappedfreq2 % 100) / 10)) : ";") + (radio.eon[i].mappedfreq2 > 0 ? String(";" + String(radio.eon[i].mappedfreq2 / 100) + "." + String((radio.eon[i].mappedfreq2 % 100) / 10)) : ";") +
@@ -538,11 +559,17 @@ void sendUDPlog(bool scanner_active) {
EON + "," + EON + "," +
RTPLUS + "\n"; RTPLUS + "\n";
// Send the data via UDP if it's new or the scanner is active // Send the data via UDP if it's new
if (UDPlogold != row || scanner_active == true) { if (UDPlogold != row) {
Udp.beginPacket(remoteip, 9100); IPAddress broadcastIP = makeBroadcastAddress(remoteip);
Udp.beginPacket(broadcastIP, 9100);
Udp.print(row); Udp.print(row);
Udp.endPacket(); Udp.endPacket();
UDPlogold = row; UDPlogold = row;
} }
}
IPAddress makeBroadcastAddress(IPAddress ip) {
// Assuming a typical subnet mask of 255.255.255.0
return IPAddress(ip[0], ip[1], ip[2], 255);
} }

View File

@@ -31,9 +31,10 @@ void handleRoot();
void handleDownloadCSV(); void handleDownloadCSV();
bool handleCreateNewLogbook(); bool handleCreateNewLogbook();
byte addRowToCSV(); byte addRowToCSV();
String getCurrentDateTime(); String getCurrentDateTime(bool inUTC);
bool isDST(time_t t); bool isDST(time_t t);
void handleLogo(); void handleLogo();
void printLogbookCSV(); void printLogbookCSV();
void sendUDPlog(bool scanner_active); void sendUDPlog();
IPAddress makeBroadcastAddress(IPAddress ip);
#endif #endif