schreibfaul1 / ESP32-MiniWebRadio

Internetradio with ESP32, I2S DAC and SPI TFT Display with Touchpad
https://www.youtube.com/watch?v=6QbPee2583o
293 stars 76 forks source link

Wifi connection to networks without password #449

Open cschwick opened 1 month ago

cschwick commented 1 month ago

Dear Wolle,

I have spotted a small "feature" when trying to connect the radio to a Wifi network without password (e.g. when security in companies or institutes is provided via registration of devices they usually do not require a password to connect to the Wifi).

In the function (main.cpp) connectToWifi() the parsing of the lines in the file "stations.csv" is done in a loop reading all characters and searching for the TAB separator. However, if a line in "stations.csv" does not contain a password and hence 2 consecutive TABs, the algorithm misses the second TAB since it assumes between 2 TABs there is always something written. (The i++ in the code below makes the character index i increment by 2 characters when a TAB is found):


        for(int32_t i = 0; i < str.length(); i++) {
            if(str[i] == '\t') {
                if(p == 0) s_ssid = str.substring(q, i);
                if(p == 1) s_password = str.substring(q, i);
                if(p == 2) s_info = str.substring(q, i);
                p++;
                i++;
                q = i;
            }
        }
        // log_i("s_ssid=%s  s_password=%s  s_info=%s", s_ssid.c_str(), s_password.c_str(), s_info.c_str());
        if(s_ssid == "") continue;
        if(s_password == "") continue;
        wifiMulti.addAP(s_ssid.c_str(), s_password.c_str());

A very simple modification solves the issues and allows to also connect to networks without a password (Note that also the line checking for an empty password is commented out in order to "allow" for empty passwords.):


        for(int32_t i = 0; i < str.length(); i++) {
            if(str[i] == '\t') {
                if(p == 0) s_ssid = str.substring(q, i);
                if(p == 1) s_password = str.substring(q, i);
                if(p == 2) s_info = str.substring(q, i);
                p++;
                q = i+1;
            }
        }
        // log_i("s_ssid=%s  s_password=%s  s_info=%s", s_ssid.c_str(), s_password.c_str(), s_info.c_str());
        if(s_ssid == "") continue;
        //if(s_password == "") continue;
        wifiMulti.addAP(s_ssid.c_str(), s_password.c_str());

Finally two "cosmetical" hints: In tft.h I think the variables _TFT_SCK, _TFT_MISO and TFT_MOSI are actually never used and hence the declarations could be removed. And finally the constructor of dlnaList (in common.h line 1685) you assign a default value to the parameter m_name in case the user did not supply a name in the constructor. I guess the value comes from some "copy/paste" coding of the AlarmClock class and probably you want to assign a value like "dlnaList" here... (not that it matters much...)). Similarly the default value for m_name in the slider, vuMeter, displayHeader, and displayFooter classes is "textBox" at the moment. (I understand that in the constructors always the correct name is provided).

I hope you find this useful! Thanks again for this cool project,

Chris

schreibfaul1 commented 1 month ago

Hi Chris, thanks for your contribution. The code is still partly from version 1. I have edited the part that reads the networks.csv. I don't have a public network to test, but hope it works. _TFT_SCK, _TFT_MISO and TFT_MOSI in the tft lib are leftovers from days gone by, I've removed them. I'll look at the rest of your suggestions later. If you still find "problematic code", please let me know.

best regards Wolle

cschwick commented 4 weeks ago

Thanks Wolle, I just tested your mod "in the field" and the radio connects flawlessly to my password-free network!

Chris