ESP Webserver returns "Wifi failed"

Hello again, my webserver is returning Wifi Failed when looking in the browser. I don't need the page, but I am posting text and that also is not working. This is a snippet of my code: Is there anything I am doing wrong? The ap starts normal according to the serial monitor.

#include <Preferences.h>
#include <WebServer.h>
#include "esp32/rtc.h"
#include <FastLED.h>
#include <HTTPClient.h>
#include <time.h>

String uploadData(String row, float value);
String downloadData(String row, bool value);
String executeQuery(String query);

void setLed(String hex);
void fadeToColor(CRGB fromColor, CRGB toColor, int steps, int delayTime);
int minutesUntil(int targetMinutes);
int now();



int count = 0;
bool Joanne = true, active = true;

WebServer server(80);
Preferences data;

HTTPClient http;

CRGB led[1];

int SlaapTijd = 0, OntwaakTijd = 0;
int offTime = -1;

bool isNear() {if (digitalRead(D6) == LOW){return true;}else{return false;}}
bool isCharging() {if (digitalRead(D0) == LOW){return true;}else{return false;}}
bool afterSleep() {if (data.getInt("SlaapTijd") < now()){return true;}else{return false;}}
bool beforeWake() {if (now() < data.getInt("OntwaakTijd")){return true;}else{return false;}}
bool wasCharging = false;

bool connectWifi() {

  data.begin("Snuffel", false);

  String ssid = data.getString("ssid", "");
  String password = data.getString("pass", "");

  if (ssid.length() == 0 || password.length() == 0) {
      return false;
  }

  Serial.println("Verbinden met netwerk...");
  WiFi.begin(ssid.c_str(), password.c_str());

  int timeout = 10; // 10 seconden timeout om verbinding te maken
  while (WiFi.status() != WL_CONNECTED && timeout > 0) {
      delay(1000);
      timeout--;
  }

  if (WiFi.status() == WL_CONNECTED) {
    Serial.println("Verbonden met wifi");
    setupRTC();

    stopAPMode();
  } else {Serial.println("Kan niet verbinden");}

  data.end();

  return isWiFiActive();
}

// Functie om AP-modus te starten
void startAPMode() {
  WiFi.mode(WIFI_AP);
    WiFi.softAP("Snuffel", "123456789");
    delay(100);
    Serial.println("Access Point Started!");
    Serial.println(WiFi.softAPIP());
    server.on("/", handleRoot);
    server.begin();
}

void stopAPMode() {
    WiFi.softAPdisconnect(true);
    server.stop();
    WiFi.mode(WIFI_STA);
}

bool isWiFiActive() {
    return WiFi.status() == WL_CONNECTED;
}

void handleRoot() {
  data.putString("ssid", server.arg("ssid"));
  data.putString("pass", server.arg("pass"));
  String testValue = server.arg("test");

  if (testValue == "true") {
    server.send(200, "text/plain", "SnuffelWuppieValidated");
  } else {
    if (connectWifi()) {
        server.send(200, "text/plain", "Wifi Connected");
    } else {
        server.send(200, "text/plain", "Wifi Failed");
    }
  }
}

void getData() {
  if (downloadData("NewData", Joanne).toInt() == 1) {
    Serial.println("Downloading data...");
    data.putString("Lkleur", downloadData("Lkleur", Joanne));
    data.putString("Kkleur", downloadData("Kkleur", Joanne));
    data.putInt("SlaapTijd", downloadData("SlaapTijd", Joanne).toInt());
    data.putInt("OntwaakTijd", downloadData("OntwaakTijd", Joanne).toInt());
    data.putInt("SlaapDuratie", downloadData("SlaapDuratie", Joanne).toInt());
    uploadData("NewData", 0);
  }
}

void looped() {
  if (count == 10) {
    if (!isWiFiActive()) {
        connectWifi();
    }
    if (isCharging()) {
        getData();
    }
    if (active && downloadData("Slapen", !Joanne).toInt() == 1) {
        setLed(data.getString("Lkleur"));
    } else {
        setLed(data.getString("Kkleur"));
    }
    count = 0;
  }
  count++;
}

void setup() {
  Serial.begin(9600);

  FastLED.addLeds<WS2812B, 10, GRB>(led, 1);
  Serial.println("What about here");

  delay(500);

  led[0] = CRGB(0, 0, 0);
  FastLED.show();

  Serial.println("What about here6");

  pinMode(D6, INPUT);
  pinMode(D0, INPUT);

  Serial.println("What about here5");

  http.begin("my url");
  http.addHeader("Content-Type", "application/x-www-form-urlencoded");

  Serial.println("What about here2");

  if (!connectWifi()) {
    startAPMode();
  }

  Serial.println("What about here3");

}

void loop() {
  server.handleClient();
  delay(1000);

  data.begin("Snuffel", false);

  looped();

  if (isWiFiActive()) {
    if (isNear() && !afterSleep() && !active) {

      offTime = data.getInt("SlaapDuratie") + now();
      active = true;

      if (offTime > 1440) {
        offTime -= 1440;
      }

    } else if ((minutesUntil(offTime) > data.getInt("SlaapDuratie")) && !afterSleep()) {active = false;}

  }

  data.end();
}

You should not open a new topic when you have an active topic on the same subject: Esp32-C3 Bootlooping
Answer the open questions in your previous thread.

I answered that and sinds this is another topic I thought I would open a new one. This is about the web server not working. Not related to bootlooping.

I had just a look at the code, so I might have missed something, but I suspect it's because on "handleRoot()" you unnecessarily call "connectWiFi()" again. Try changing it into a "isWiFiActive()" call:

void handleRoot() {
  data.putString("ssid", server.arg("ssid"));
  data.putString("pass", server.arg("pass"));
  String testValue = server.arg("test");

  if (testValue == "true") {
    server.send(200, "text/plain", "SnuffelWuppieValidated");
  } else {
    //if (connectWifi()) {
    if (isWiFiActive()) {
        server.send(200, "text/plain", "Wifi Connected");
    } else {
        server.send(200, "text/plain", "Wifi Failed");
    }
  }
}

BTW, why do you need that check? When executing "handleRoot()" you're already connected to WiFi, you won't receive a GET if not connected, don't you think?... :wink:

Thanks for the reaction! I am not checking, the webserver is in AP mode at that point. It then recieves an ssid and pass for a network to connect to. I found the real problem: the data line wasnt open. It still returns Wifi Failed, but works as expected...

Your comment did help tho, so thanks!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.