From 228429489f677437e553ae6f31c9a14f5a1f81b9 Mon Sep 17 00:00:00 2001 From: Sjef Verhoeven PE5PVB Date: Thu, 23 Jan 2025 11:01:16 +0100 Subject: [PATCH] Added experimental UDP log broadcast. Re-added RT to logbook --- TEF6686_ESP32.ino | 21 +++++++----- src/logbook.cpp | 85 +++++++++++++++++++++++++++++++++++++++++++++-- src/logbook.h | 4 ++- 3 files changed, 97 insertions(+), 13 deletions(-) diff --git a/TEF6686_ESP32.ino b/TEF6686_ESP32.ino index 0469b4e..cf03b3c 100644 --- a/TEF6686_ESP32.ino +++ b/TEF6686_ESP32.ino @@ -1013,16 +1013,19 @@ void loop() { DoMemoryPosTune(); ShowMemoryPos(); } else { - if (!autologged && autolog && RDSstatus && radio.rds.correctPI != 0) { - switch (addRowToCSV()) { - case 0: ShowFreq(2); break; - case 1: ShowFreq(3); break; - case 2: ShowFreq(4); break; - } + if (!autologged && RDSstatus && radio.rds.correctPI != 0) { + if (autolog) { + switch (addRowToCSV()) { + case 0: ShowFreq(2); break; + case 1: ShowFreq(3); break; + case 2: ShowFreq(4); break; + } - delay(200); - while (digitalRead(ROTARY_BUTTON) == LOW) delay(50); - ShowFreq(0); + delay(200); + while (digitalRead(ROTARY_BUTTON) == LOW) delay(50); + ShowFreq(0); + } + if (wifi) sendUDPlog(); autologged = true; } TuneUp(); diff --git a/src/logbook.cpp b/src/logbook.cpp index 1383315..ce3577e 100644 --- a/src/logbook.cpp +++ b/src/logbook.cpp @@ -205,7 +205,7 @@ bool handleCreateNewLogbook() { } // Write the header to the new CSV file - String header = "Date,Time,Frequency,PI,Signal,Stereo,TA,TP,PTY,ECC,PS\n"; + String header = "Date,Time,Frequency,PI,Signal,Stereo,TA,TP,PTY,ECC,PS,Radiotext\n"; file.print(header); // Ensure that the header is written properly // Make sure the data is written before closing the file @@ -261,9 +261,17 @@ byte addRowToCSV() { else if (unit == 1) signal += " dBf"; else if (unit == 2) signal += " dBm"; + // Prepare the radio text with station information, including enhanced options if available + String radioText = String(radio.rds.stationText + " " + radio.rds.stationText32); + if (radio.rds.hasEnhancedRT) { + radioText += " eRT: " + String(radio.rds.enhancedRTtext); + } + // Replace commas in the station name and radio text to avoid CSV conflicts String stationName = radio.rds.stationName; + String radioTextModified = radioText; stationName.replace(",", " "); // Replace commas in station name + radioTextModified.replace(",", " "); // Replace commas in radio text // Handle ECC, PTY, TA, TP, and Stereo flag String TA = radio.rds.hasTA ? "•" : " "; @@ -280,14 +288,15 @@ byte addRowToCSV() { // Construct the CSV row data String row = currentDateTime + "," + frequencyFormatted + "," + - String(radio.rds.picode) + "," + + String(radio.rds.picode).substring(0, 4) + "," + signal + "," + Stereo + "," + TA + "," + TP + "," + pty + "," + ECC + "," + - stationName + "\n"; + stationName + "," + + radioTextModified + "\n"; // Write the row to the file and close it if (file.print(row)) { @@ -424,4 +433,74 @@ void printLogbookCSV() { // Print a message indicating the end of the file content Serial.println("===== End of logbook.csv ====="); +} + +void sendUDPlog() { + // Fetch the current date and time as a string + String currentDateTime = getCurrentDateTime(); + + // Use a placeholder ("-,-") if the date and time could not be retrieved + if (currentDateTime == "") { + currentDateTime = "-,-"; + } + + // Prepare the frequency in a formatted string (e.g., "XX.XX MHz") + int freqInt = (band == BAND_OIRT) ? (int)frequency_OIRT : (int)frequency; + int adjustedFreq = freqInt + (band != BAND_OIRT ? ConverterSet * 100 : 0); + String frequencyFormatted = String(adjustedFreq / 100) + "." + + ((adjustedFreq % 100 < 10) ? "0" : "") + + String(adjustedFreq % 100) + " MHz"; + + // Calculate signal strength based on the selected unit + int SStatusPrint = 0; + if (unit == 0) SStatusPrint = SStatus; // dBμV + else if (unit == 1) SStatusPrint = ((SStatus * 100) + 10875) / 100; // dBf + else if (unit == 2) SStatusPrint = round((float(SStatus) / 10.0 - 10.0 * log10(75) - 90.0) * 10.0); // dBm + + // Format the signal strength with appropriate decimal places and unit + String signal = String(SStatusPrint / 10) + "." + String(abs(SStatusPrint % 10)); + if (unit == 0) signal += " dBμV"; + else if (unit == 1) signal += " dBf"; + else if (unit == 2) signal += " dBm"; + + // Prepare the radio text with station information, including enhanced options if available + String radioText = String(radio.rds.stationText + " " + radio.rds.stationText32); + if (radio.rds.hasEnhancedRT) { + radioText += " eRT: " + String(radio.rds.enhancedRTtext); + } + + // Replace commas in the station name and radio text to avoid CSV conflicts + String stationName = radio.rds.stationName; + String radioTextModified = radioText; + stationName.replace(",", " "); // Replace commas in station name + radioTextModified.replace(",", " "); // Replace commas in radio text + + // Handle ECC, PTY, TA, TP, and Stereo flag + String TA = radio.rds.hasTA ? "•" : " "; + String TP = radio.rds.hasTP ? "•" : " "; + String Stereo = radio.getStereoStatus() ? "•" : " "; + String pty = String(radio.rds.stationTypeCode); + String ECC = "--"; + if (radio.rds.hasECC) { + char eccBuffer[3]; // Buffer to hold 2-digit hex value + null terminator + snprintf(eccBuffer, sizeof(eccBuffer), "%02X", radio.rds.ECC); // Format ECC as uppercase 2-digit hex + ECC = String(eccBuffer); + } + + // Construct the CSV row data + String row = currentDateTime + "," + + frequencyFormatted + "," + + String(radio.rds.picode).substring(0, 4) + "," + + signal + "," + + Stereo + "," + + TA + "," + + TP + "," + + pty + "," + + ECC + "," + + stationName + "," + + radioTextModified + "\n"; + + Udp.beginPacket(remoteip, 9100); + Udp.print(row); + Udp.endPacket(); } \ No newline at end of file diff --git a/src/logbook.h b/src/logbook.h index 02ea104..53fe471 100644 --- a/src/logbook.h +++ b/src/logbook.h @@ -21,9 +21,11 @@ extern unsigned int ConverterSet; extern unsigned int frequency; extern unsigned int frequency_OIRT; extern unsigned int logcounter; +extern IPAddress remoteip; extern TEF6686 radio; extern WebServer webserver; +extern WiFiUDP Udp; void handleRoot(); void handleDownloadCSV(); @@ -33,5 +35,5 @@ String getCurrentDateTime(); bool isDST(time_t t); void handleLogo(); void printLogbookCSV(); - +void sendUDPlog(); #endif \ No newline at end of file