arduinoesp32eeprom

ESP32 writeStringEEPROM adds unwanted nembers when I use the letter '&'


I am trying to use this code: https://github.com/jatocode/WifiConnect/blob/master/WifiConnect/WifiConnect.ino with my ESP32. The problem is that my wifi network name is "y&t" and when I am trying to use it the progrem saves to the EEPROM "y&26t" insted, with other networks names in my area it dosn't have a problem.

enter image description here enter image description here

I will be greatful if somebady knows how to solve this problem.


Solution

  • Thanks to @Juraj I solved the problem.

    I used in this code a decoder from here: https://circuits4you.com/2019/03/21/esp8266-url-encode-decode-example/

    /*
      Configuration AP example
      The example shows a very simple Configuration Access Point.
      created in August 2019
      by Juraj Andrassy https://github.com/jandrassy
    */
    #include <WiFi.h>
    #include <WiFiClient.h>
    #include <WiFiAP.h>
    #include <Arduino.h>
    // #include <String.h>
    
    String urldecode(String str);
    unsigned char h2int(char c);
    
    void setup() {
    
      Serial.begin(115200);
      delay(500);
    
      // WiFi.disconnect(); // forget the persistent connection to test the Configuration AP
      
      WiFi.persistent(true);
    
      // waiting for connection to remembered  Wifi network
      Serial.println("Waiting for connection to WiFi");
      WiFi.begin(); // use SSID and password stored by SDK
      WiFi.waitForConnectResult();
    
      if (WiFi.status() != WL_CONNECTED) {
        Serial.println();
        Serial.println("Could not connect to WiFi. Starting configuration AP...");
        configAP();
      } else {
        Serial.println("WiFi connected");
      }
    }
    
    void loop() {
    }
    
    void configAP() {
    
      WiFiServer configWebServer(80);
    
      WiFi.mode(WIFI_AP_STA); // starts the default AP (factory default or setup as persistent)
    
      Serial.print("Connect your computer to the WiFi network ");
      // Serial.print(WiFi.softAP());
      Serial.println();
      IPAddress ip = WiFi.softAPIP();
      Serial.print("and enter http://");
      Serial.print(ip);
      Serial.println(" in a Web browser");
    
      configWebServer.begin();
    
      while (true) {
    
        WiFiClient client = configWebServer.available();
        if (client) {
          char line[64];
          int l = client.readBytesUntil('\n', line, sizeof(line));
          line[l] = 0;
          client.find((char*) "\r\n\r\n");
          if (strncmp_P(line, PSTR("POST"), strlen("POST")) == 0) {
            l = client.readBytes(line, sizeof(line));
            line[l] = 0;
    
            // parse the parameters sent by the html form
            const char* delims = "=&";
            strtok(line, delims);
            const char* ssid = strtok(NULL, delims);
            strtok(NULL, delims);
            const char* pass = strtok(NULL, delims);
            
            // decoding the ssid and the password for ASCII characters
            String ssidS = String(ssid);
            String ssidSdecode= urldecode(ssidS);
            ssid=const_cast<char*>(ssidSdecode.c_str());
            String passS = String(pass);
            String passSdecode= urldecode(passS);
            pass=const_cast<char*>(passSdecode.c_str());
            
            // send a response before attemting to connect to the WiFi network
            // because it will reset the SoftAP and disconnect the client station
            client.println(F("HTTP/1.1 200 OK"));
            client.println(F("Connection: close"));
            client.println(F("Refresh: 10")); // send a request after 10 seconds
            client.println();
            client.println(F("<html><body><h3>Configuration AP</h3><br>connecting...</body></html>"));
            client.stop();
    
            Serial.println();
            Serial.print("Attempting to connect to WPA SSID: ");
            Serial.println(ssid);
            WiFi.begin(ssid, pass);
            WiFi.waitForConnectResult();
    
            // configuration continues with the next request
    
          } else {
    
            client.println(F("HTTP/1.1 200 OK"));
            client.println(F("Connection: close"));
            client.println();
            client.println(F("<html><body><h3>Configuration AP</h3><br>"));
    
            int status = WiFi.status();
            if (status == WL_CONNECTED) {
              client.println(F("Connection successful. Ending AP."));
            } else {
              client.println(F("<form action='/' method='POST'>WiFi connection failed. Enter valid parameters, please.<br><br>"));
              client.println(F("SSID:<br><input type='text' name='i'><br>"));
              client.println(F("Password:<br><input type='password' name='p'><br><br>"));
              client.println(F("<input type='submit' value='Submit'></form>"));
            }
            client.println(F("</body></html>"));
            client.stop();
    
            if (status == WL_CONNECTED) {
              delay(1000); // to let the SDK finish the communication
              Serial.println("Connection successful. Ending AP.");
              configWebServer.stop();
              WiFi.mode(WIFI_STA);
            }
          }
        }
      }
    }
    
    String urldecode(String str) {
        String encodedString="";
        char c;
        char code0;
        char code1;
        for (int i =0; i < str.length(); i++) {
            c=str.charAt(i);
          if (c == '+') {
            encodedString+=' ';  
          } else if (c == '%') {
            i++;
            code0=str.charAt(i);
            i++;
            code1=str.charAt(i);
            c = (h2int(code0) << 4) | h2int(code1);
            encodedString+=c;
          } else {
            encodedString+=c;  
          }
          
          yield();
        }
      return encodedString;
    }
     
    unsigned char h2int(char c) {
        if (c >= '0' && c <='9') {
            return((unsigned char)c - '0');
        }
        
        if (c >= 'a' && c <='f') {
            return((unsigned char)c - 'a' + 10);
        }
        
        if (c >= 'A' && c <='F') {
            return((unsigned char)c - 'A' + 10);
        }
        return(0);
    }