Cannot exit function

Hey guys,

For some reason, I cannot seem to exit the following function and return to void loop() or void setup() (both loops can call this function). I’ve played around with it for hours to no avail.

void getCredentials() {
  WiFiClient client = server.available();

  //if device is connected, host credentials page
  if (client) {
    Serial.println("new client");
    String currentLine = "";
    String httpStr = "";
    bool readingJSON = false;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        
        //if JSON string recieved, deserialize and connect to wifi (getWiFi())
        if (c == '{') {
          readingJSON = true;
        }
        if (readingJSON) {
          httpStr += c;
        }
        if (c == '}') {
          readingJSON = false;
          Serial.println("Deserializing JSON");
          const int capacity_in = JSON_OBJECT_SIZE(10);
          StaticJsonDocument<capacity_in> doc;
          deserializeJson(doc, httpStr);
          Serial.println("");
          Serial.println(httpStr);
          String ssid = doc["ssid"].as<String>();
          String pass = doc["pass"].as<String>();
          ssid.toCharArray(wifi.ssid, 256);
          pass.toCharArray(wifi.pass, 256);
          Serial.print("SSID: ");
          Serial.println(wifi.ssid);
          Serial.print("Password: ");
          Serial.println(wifi.pass);
          needCredentials = false;
          client.stop();
          Serial.println("Credentials stored");
          getWiFi();
          Serial.println("Connection achieved");
          return; //exit function after connecting to wifi
        }
        if (c == '\n') {
          if (currentLine.length() == 0) { //if nothing recieved by server (arduino), host html page

            //html
            client.println("<!doctype html>");
            client.println("<html>");
            client.println();
            client.println("<head>");
            client.println("<title>PotBot Setup</title>");
            client.println("</head>");
            client.println("<style type=\"text/css\"> body {margin:50px; padding:20px; line-height: 250% } </style>");
            client.println("<body style=\"background-color:#43642B;font-family:sans-serif;color:Gray\">");
            client.println("<style type=\"text/css\"> form {margin:50px; padding:20px; line-height: 250% } </style>");
            client.println("<form style=\"background-color:#91C16E;color:DarkSlateGray\";>");
            client.println("<h1>Wifi Setup</h1>");
            client.print("<h2>Network Name:</h2>");
            client.print("<input type=\"text\" name=\"network\" id=\"network\">
"); //ssid input
            client.print("<h2>Password:</h2>");
            client.print("<input type=\"password\" name=\"password\" id=\"password\">
"); //password input
            client.print("<button onClick=\"sendText()\" value=\"Submit\">Submit</button>"); //execute sendText() function on sumbit
            client.print("</form>");
            client.println("</body>");
            client.println("<script>");
            client.println("var network = document.querySelector('#network');"); //get ssid from form
            client.println("var password = document.querySelector('#password');"); //get password from form
            client.println("function sendText() {");
            client.println("nocache=\"&nocache=\" + Math.random() * 1000000;");
            client.println("var request =new XMLHttpRequest();"); 
            client.println("var obj = {" //consolidate ssid and password into javascript object
                           "ssid: network.value,"
                           "pass: password.value};"
                          );
            client.println("var str = JSON.stringify(obj);"); //convert object to json string
            client.println("request.open(\"POST\", \"server\");");
            client.println("request.send(str);"); //send json string
            client.println("};");
            client.println("</script>");
            client.println("</html>");
            client.println();
            break;
          }
          else {
            currentLine = "";

          }
        }
        else if (c != '\r') {
          currentLine += c;
        }

      }
    }
  }
}

It should execute a return after the JSON string is deserialized and the getWiFi() function is called. I know it gets past the getWiFi() function because I have it set to serial print “connection achieved” after calling the getWiFi() function, which it does. However, it seems to stop immediately after that.

Any help would be appreciated. I’ve spent an ungodly amount of time trying to troubleshoot this.

Please post your full sketch.

If possible, you should always post code directly in the forum thread as text using code tags:

  • Do an Auto Format (Tools > Auto Format in the Arduino IDE or Ctrl + B in the Arduino Web Editor) on your code. This will make it easier for you to spot bugs and make it easier for us to read.
  • In the Arduino IDE or Arduino Web Editor, click on the window that contains your sketch code.
  • Press “Ctrl + A”. This will select all the text.
  • Press “Ctrl + C”. This will copy the selected text to the clipboard.
  • In a forum reply here, click the “Reply” button.
  • click on the reply field.
  • Click the </> button on the forum toolbar. This will add the forum’s code tags markup to your reply.
  • Press “Ctrl + V”. This will paste the sketch between the code tags.
  • Move the cursor outside of the code tags before you add any additional text to your reply.
  • Repeat the above process if your sketch has multiple tabs.

This will make it easy for anyone to look at it, which will increase the likelihood of you getting help.

If the sketch is longer than the 9000 characters maximum allowed by the forum, then it’s OK to add it as an attachment. After clicking the “Reply” button, you will see an “Attachments and other settings” link.

When your code requires a library that’s not included with the Arduino IDE please post a link (using the chain links icon on the forum toolbar to make it clickable) to where you downloaded that library from or if you installed it using Library Manger (Sketch > Include Library > Manage Libraries in the Arduino IDE or Libraries > Library Manager in the Arduino Web Editor) then say so and state the full name of the library.

Maybe change this

while (client.connected()) {

to this

if (client.connected()) {

...R