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();
}