From 3dda494e455b3a053655f8bb9588d065442e2576 Mon Sep 17 00:00:00 2001 From: Michael Clemens // DK1MI Date: Wed, 24 May 2023 09:13:54 +0000 Subject: [PATCH] =?UTF-8?q?=E2=80=9Ewt32pamon.ino=E2=80=9C=20=C3=A4ndern?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- wt32pamon.ino | 311 +++++++++++++++++++++++++------------------------- 1 file changed, 153 insertions(+), 158 deletions(-) diff --git a/wt32pamon.ino b/wt32pamon.ino index a26acf3..bf42f1e 100644 --- a/wt32pamon.ino +++ b/wt32pamon.ino @@ -11,10 +11,10 @@ *****************************************************************************************************************************/ -#define DEBUG_ETHERNET_WEBSERVER_PORT Serial +#define DEBUG_ETHERNET_WEBSERVER_PORT Serial // Debug Level from 0 to 4 -#define _ETHERNET_WEBSERVER_LOGLEVEL_ 3 +#define _ETHERNET_WEBSERVER_LOGLEVEL_ 3 #define FORMAT_SPIFFS_IF_FAILED true @@ -25,17 +25,19 @@ #include "FS.h" #include "SPIFFS.h" +String version = "0.9"; + Preferences config; -String config_items [ ] = {"show_mV", "show_dBm", "show_watt", "vswr_threshold", "vswr_beep", "selected_band", "antenna_name", "max_led_pwr_fwd", "max_led_pwr_ref", "max_led_vswr", "show_led_fwd", "show_led_ref", "show_led_vswr"}; -String config_defaults [ ] = {"true", "true", "true", "2", "true", "70cm", " ", "100", "100", "3", "true", "true", "true"}; -double fwd_array [3300] = {}; -double ref_array [3300] = {}; +String config_items[] = { "show_mV", "show_dBm", "show_watt", "vswr_threshold", "vswr_beep", "selected_band", "antenna_name", "max_led_pwr_fwd", "max_led_pwr_ref", "max_led_vswr", "show_led_fwd", "show_led_ref", "show_led_vswr" }; +String config_defaults[] = { "true", "true", "true", "2", "true", "70cm", " ", "100", "100", "3", "true", "true", "true" }; +double fwd_array[3300] = {}; +double ref_array[3300] = {}; -int voltage_fwd,voltage_ref; -double fwd_dbm=0, ref_dbm=0; -double fwd_watt=0, ref_watt=0; -byte iii=0; +int voltage_fwd, voltage_ref; +double fwd_dbm = 0, ref_dbm = 0; +double fwd_watt = 0, ref_watt = 0; +byte iii = 0; String conf_content; String conf_textareas = ""; @@ -45,7 +47,7 @@ String band = ""; String default_band = "70cm"; String band_fwd = band + "_fwd"; String band_ref = band + "_ref"; -String band_list []= {"1.25cm", "3cm", "6cm", "9cm", "13cm", "23cm", "70cm", "2m", "HF"}; +String band_list[] = { "1.25cm", "3cm", "6cm", "9cm", "13cm", "23cm", "70cm", "2m", "HF" }; int IO2_FWD = 2; int IO4_REF = 4; @@ -59,56 +61,56 @@ IPAddress mySN(255, 255, 255, 0); IPAddress myDNS(192, 168, 1, 1); -// Reads a file from SPIFF and returns its content as a string -String readFile(fs::FS &fs, const char * path){ - Serial.printf("Reading file: %s\r\n", path); +// Reads a file from SPIFF and returns its content as a string +String readFile(fs::FS &fs, const char *path) { + Serial.printf("Reading file: %s\r\n", path); - File file = fs.open(path); - if(!file || file.isDirectory()){ - Serial.println("failed to open file for reading"); - return ""; - } + File file = fs.open(path); + if (!file || file.isDirectory()) { + Serial.println("failed to open file for reading"); + return ""; + } String ret = ""; - Serial.println("read from file:"); - while(file.available()){ - ret+=char(file.read()); - } - file.close(); - return ret; + Serial.println("read from file:"); + while (file.available()) { + ret += char(file.read()); + } + file.close(); + return ret; } // Takes a string and writes it to a file on SPIFF -void writeFile(fs::FS &fs, const char * path, const char * message){ - Serial.printf("Writing file: %s\r\n", path); +void writeFile(fs::FS &fs, const char *path, const char *message) { + Serial.printf("Writing file: %s\r\n", path); - File file = fs.open(path, FILE_WRITE); - if(!file){ - Serial.println("failed to open file for writing"); - return; - } - if(file.print(message)){ - Serial.println("file written"); - }else { - Serial.println("file write failed"); - } - file.close(); + File file = fs.open(path, FILE_WRITE); + if (!file) { + Serial.println("failed to open file for writing"); + return; + } + if (file.print(message)) { + Serial.println("file written"); + } else { + Serial.println("file write failed"); + } + file.close(); } // converts dBm to Watt double dbm_to_watt(double dbm) { - return pow( 10.0, (dbm - 30.0) / 10.0); + return pow(10.0, (dbm - 30.0) / 10.0); } // checks if a given voltage is lower as the smallest value // in the table or higher than the biggest value -bool is_val_out_of_bounds(int mv, bool fwd){ +bool is_val_out_of_bounds(int mv, bool fwd) { double stored_val = 0; int key_a = 0; int key_b = 0; // searches for the first key (voltage) that has a value (dBm) - for (int i=0; i<3300; i++) { + for (int i = 0; i < 3300; i++) { if (fwd) { stored_val = fwd_array[i]; } else { @@ -117,11 +119,11 @@ bool is_val_out_of_bounds(int mv, bool fwd){ if (stored_val != 0) { key_a = i; break; - } + } } // searches for the last key (voltage) that has a value (dBm) - for (int i=3299; i>0; i--) { + for (int i = 3299; i > 0; i--) { if (fwd) { stored_val = fwd_array[i]; } else { @@ -130,11 +132,11 @@ bool is_val_out_of_bounds(int mv, bool fwd){ if (stored_val != 0) { key_b = i; break; - } + } } - int lowerkey = min(key_a, key_b); // takes both values found above and assigns the lower key - int higherkey = max(key_a, key_b); // takes both values found above and assigns the higher key + int lowerkey = min(key_a, key_b); // takes both values found above and assigns the lower key + int higherkey = max(key_a, key_b); // takes both values found above and assigns the higher key // returns false if given voltage is between the lowest and highest configured voltages // returns true if voltage is out of bounds @@ -147,8 +149,7 @@ bool is_val_out_of_bounds(int mv, bool fwd){ // takes a voltage value and translates it // to dBm based on the corresponding lookup table -double millivolt_to_dbm(int mv, bool fwd) -{ +double millivolt_to_dbm(int mv, bool fwd) { double lastval = 0; double nextval = 0; int lastkey = 0; @@ -161,7 +162,7 @@ double millivolt_to_dbm(int mv, bool fwd) // check if table is ascending or descending double asc_tmp_val = 0; - for (int i=0; i<3300; i++) { + for (int i = 0; i < 3300; i++) { if (fwd) { stored_val = fwd_array[i]; } else { @@ -177,23 +178,23 @@ double millivolt_to_dbm(int mv, bool fwd) ascending = false; break; } - } + } } if (ascending) { - for (int i=0; i<3300; i++) { + for (int i = 0; i < 3300; i++) { if (fwd) { stored_val = fwd_array[i]; } else { stored_val = ref_array[i]; } if (stored_val != 0) { - if (lowest_key_in_table == 0){ - lowest_key_in_table = i; //finds the lowest voltage value stored in the table + if (lowest_key_in_table == 0) { + lowest_key_in_table = i; //finds the lowest voltage value stored in the table } - highest_key_in_table = i; // we will have the highest voltage value in the table at the end of the loop - if (i < mv) { + highest_key_in_table = i; // we will have the highest voltage value in the table at the end of the loop + if (i < mv) { lastval = stored_val; lastkey = i; } else { @@ -204,18 +205,18 @@ double millivolt_to_dbm(int mv, bool fwd) } } } else { - for (int i=3300; i>0; i--) { + for (int i = 3300; i > 0; i--) { if (fwd) { stored_val = fwd_array[i]; } else { stored_val = ref_array[i]; } if (stored_val != 0) { - if (lowest_key_in_table == 0){ - lowest_key_in_table = i; //finds the lowest voltage value stored in the table + if (lowest_key_in_table == 0) { + lowest_key_in_table = i; //finds the lowest voltage value stored in the table } - highest_key_in_table = i; // we will have the highest voltage value in the table at the end of the loop - if (i > mv) { + highest_key_in_table = i; // we will have the highest voltage value in the table at the end of the loop + if (i > mv) { lastval = stored_val; lastkey = i; } else { @@ -226,7 +227,7 @@ double millivolt_to_dbm(int mv, bool fwd) } } } - + double lowerkey = min(lastkey, nextkey); double higherkey = max(lastkey, nextkey); @@ -243,27 +244,26 @@ double millivolt_to_dbm(int mv, bool fwd) } else { result = higherval - ((diffval / diffkey) * (mv - lowerkey)); } - + return result; } // read voltages from both input pins // calculates avaerage value of 50 measurements -void read_directional_couplers() -{ +void read_directional_couplers() { int voltage_sum_fwd = 0; int voltage_sum_ref = 0; // Takes 50 samples and sums them up - for(iii=0; iii<50; iii++) - { voltage_sum_fwd += analogReadMilliVolts(IO2_FWD); + for (iii = 0; iii < 50; iii++) { + voltage_sum_fwd += analogReadMilliVolts(IO2_FWD); voltage_sum_ref += analogReadMilliVolts(IO4_REF); } // calculate the average value by deviding the above sum by 50 - voltage_fwd = voltage_sum_fwd/50; - voltage_ref = voltage_sum_ref/50; + voltage_fwd = voltage_sum_fwd / 50; + voltage_ref = voltage_sum_ref / 50; // calculate the dBm value from the voltage based on the calibration table fwd_dbm = millivolt_to_dbm(voltage_fwd, true); @@ -272,20 +272,17 @@ void read_directional_couplers() // calculate watt from dBm fwd_watt = dbm_to_watt(fwd_dbm); ref_watt = dbm_to_watt(ref_dbm); - } // delivers the dashboard page in "index.h" -void handleRoot() -{ +void handleRoot() { String s = MAIN_page; server.send(200, "text/html", s); } // delivers a 404 page if a non-existant resouurce is requested -void handleNotFound() -{ +void handleNotFound() { String message = F("File Not Found\n\n"); message += F("URI: "); @@ -296,8 +293,7 @@ void handleNotFound() message += server.args(); message += F("\n"); - for (uint8_t i = 0; i < server.args(); i++) - { + for (uint8_t i = 0; i < server.args(); i++) { message += " " + server.argName(i) + ": " + server.arg(i) + "\n"; } @@ -313,10 +309,10 @@ void handleDATA() { double vswr = 0; - if (fwd_watt>ref_watt){ - vswr = (1 + sqrt(ref_watt/fwd_watt)) / (1 - sqrt(ref_watt/fwd_watt)); + if (fwd_watt > ref_watt) { + vswr = (1 + sqrt(ref_watt / fwd_watt)) / (1 - sqrt(ref_watt / fwd_watt)); } - + String vswr_str = "-1"; String fwd_watt_str = ""; String ref_watt_str = ""; @@ -340,16 +336,16 @@ void handleDATA() { String fwd_dbm_str = ""; String ref_dbm_str = ""; if (config.getString(String("show_dBm").c_str()) != "false") { - fwd_dbm_str = String(fwd_dbm,2); - ref_dbm_str = String(ref_dbm,2); + fwd_dbm_str = String(fwd_dbm, 2); + ref_dbm_str = String(ref_dbm, 2); } if (config.getString(String("show_watt").c_str()) != "false") { - fwd_watt_str = String(fwd_watt,10); + fwd_watt_str = String(fwd_watt, 10); } if (config.getString(String("show_watt").c_str()) != "false") { - ref_watt_str = String(ref_watt,10); + ref_watt_str = String(ref_watt, 10); } String rl_str = "-- "; @@ -365,26 +361,26 @@ void handleDATA() { bool ref_oob = is_val_out_of_bounds(voltage_ref, false); // Generate a semicolon seperated string that will be sent to the frontend - String output = fwd_watt_str + ";"; // data[0]: FWD power in Watt - output += fwd_dbm_str + ";"; // data[1]: FWD dBm value - output += voltage_fwd_str + ";"; // data[2]: FWD voltage - output += ref_watt_str + ";"; // data[3]: REF power in Watt - output += ref_dbm_str + ";"; // data[4]: REF dBm value - output += voltage_ref_str + ";"; // data[5]: REF voltage - output += vswr_str + ";"; // data[6]: VSWR value - output += rl_str + ";"; // data[7]: RL value - output += band + ";"; // data[8]: band (e.g. "70cm") - output += String(vswr_threshold) + ";"; // data[9]: VSWR threshold (e.g. "3") - output += antenna_name + ";"; // data[10]: // Name of antenna (e.g. "X200") - output += vswr_beep + ";"; // data[11]: should it beep if VSWR is too high? (true/false) - output += config.getString(String("max_led_pwr_fwd").c_str()) + ";"; // data[12]: highest value in Watt for the FWD LED graph (e.g. "100") - output += config.getString(String("max_led_pwr_ref").c_str()) + ";"; // data[13]: highest value in Watt for the REF LED graph (e.g. "1") - output += config.getString(String("max_led_vswr").c_str()) + ";"; // data[14]: highest value in Watt for the VSWR LED graph (e.g. "3") - output += String(fwd_oob) + ";"; // data[15]: Is the FWD voltage out of bounds? (true/false) - output += String(ref_oob) + ";"; // data[16]: Is the REF voltage out of bounds? (true/false) - output += config.getString(String("show_led_fwd").c_str()) + ";"; // data[17]: Show the FWD LED bar graph? (true/false) - output += config.getString(String("show_led_ref").c_str()) + ";"; // data[18]: Show the REF LED bar graph? (true/false) - output += config.getString(String("show_led_vswr").c_str()); // data[19]: Show the VSWR LED bar graph? (true/false) + String output = fwd_watt_str + ";"; // data[0]: FWD power in Watt + output += fwd_dbm_str + ";"; // data[1]: FWD dBm value + output += voltage_fwd_str + ";"; // data[2]: FWD voltage + output += ref_watt_str + ";"; // data[3]: REF power in Watt + output += ref_dbm_str + ";"; // data[4]: REF dBm value + output += voltage_ref_str + ";"; // data[5]: REF voltage + output += vswr_str + ";"; // data[6]: VSWR value + output += rl_str + ";"; // data[7]: RL value + output += band + ";"; // data[8]: band (e.g. "70cm") + output += String(vswr_threshold) + ";"; // data[9]: VSWR threshold (e.g. "3") + output += antenna_name + ";"; // data[10]: // Name of antenna (e.g. "X200") + output += vswr_beep + ";"; // data[11]: should it beep if VSWR is too high? (true/false) + output += config.getString(String("max_led_pwr_fwd").c_str()) + ";"; // data[12]: highest value in Watt for the FWD LED graph (e.g. "100") + output += config.getString(String("max_led_pwr_ref").c_str()) + ";"; // data[13]: highest value in Watt for the REF LED graph (e.g. "1") + output += config.getString(String("max_led_vswr").c_str()) + ";"; // data[14]: highest value in Watt for the VSWR LED graph (e.g. "3") + output += String(fwd_oob) + ";"; // data[15]: Is the FWD voltage out of bounds? (true/false) + output += String(ref_oob) + ";"; // data[16]: Is the REF voltage out of bounds? (true/false) + output += config.getString(String("show_led_fwd").c_str()) + ";"; // data[17]: Show the FWD LED bar graph? (true/false) + output += config.getString(String("show_led_ref").c_str()) + ";"; // data[18]: Show the REF LED bar graph? (true/false) + output += config.getString(String("show_led_vswr").c_str()); // data[19]: Show the VSWR LED bar graph? (true/false) server.send(200, "text/plane", output); } @@ -394,11 +390,11 @@ void handleCONFIG() { if (conf_textareas == "") { build_textareas(); } - + if (conf_config_table == "") { build_config_table(); } - + conf_content = "\r\n"; conf_content += "