What is life... why does this code behave like this?

Dear Blynkers,

It has been a while since I have used blynk for the last time (also since the last time i codes) and I am having this weird problem. It is not really blynk related, but it is for a blynk project I am creating.

I’m trying to let my ESP host a webpage on which you can choose a WiFi network and enter a password when i cannot connect to a network itself.

For the ESP to receive the data i constructed this:

(‘req’ is the response from the device (my phone in my case)

    Serial.println("______________SSID_____________");
    int first = req.indexOf("network=") + 8;
    int last = req.indexOf("&");
    Serial.println(req);
    Serial.println(first);
    Serial.println(last);
    Serial.println(req.substring(first, last));
    req.substring(first, last).toCharArray(ssid, req.substring(first, last).length() + 1);
    Serial.println("______________PASSWORD_____________");
    first = req.indexOf("pass=") + 5;
    last = req.indexOf(" HTTP");
    Serial.println(req);
    Serial.println(first);
    Serial.println(last);
    Serial.println(req.substring(first, last));
    req.substring(first, last).toCharArray(password, req.substring(first, last).length() + 1);
    Serial.println("______________WiFi_____________");
    Serial.println(ssid);
    Serial.println(password);
    WiFi.mode(WIFI_STA);
    mytimeout = millis() / 1000;
    WiFi.begin(ssid, password);

It reads a string like this:

GET /wifi_data.asp?network=networkname&pass=password HTTP/1.1

and used the .indexof method to get the ssid and the password out of the string and convert it to a char.

but for some reason it does not work.
the console prints this: (i entered “Überhaupt” as my ssid and “networkpassword” as the password.)

_SSID
GET /wifi_data.asp?network=Uberhaupt&pass=networkpassword HTTP/1.1
27
36
Uberhaupt
_PASSWORD
GET /wifi_data.asp?network=Uberhaupt&pass=networkpassword HTTP/1.1
42
57
networkpassword
_WiFi
assword
networkpassword

the ssid does not correctly save into the char ssid with the line:

req.substring(first, last).toCharArray(ssid, req.substring(first, last).length() + 1);

or it changes… I have absolutely no idea…

I hope someone can help me.
Many thanks!

Bas Peter

What about using a library like WifiManager for ESP? It also has minor Blynk intergration.

Its easy to setup and includes the functions you desire.