ESP32 Webserver/Access Point problem

What am I doing wrong?

#include <WiFi.h>
#include <WiFiServer.h>


const char* ssid = "Test";
const char* password = "password";


WiFiServer server(80);

String SendHTML() {
 String ptr = "<!DOCTYPE html> <html>\n";
 ptr +="<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n";
 ptr +="<style>html { font-family: Helvetica; display: inline-block; margin: 0xp auto; text-align: center;}\n";
 ptr +="body{margin-top: 50px;} h1 {color: #444444;margin-bottom: 50px auto 30px;}\n";
 ptr +=".button {display: block;width: 40px;background-color: #3498db;border: none;color: white;padding: 10px 20px;text-decoration: none;font-size: 20px;margin: 0px;cursor: pointer;}\n";
 
 ptr +=".button-open {background-color: #fae52b;}\n";
 ptr +=".button-open:active {background-color: #8a855b;}\n";
 ptr +=".button-close {background-color: #e89a2c;}\n";
 ptr +=".button-close:active {background-color: #a18865;}\n";
 ptr +=".button-stop {background-color: #db332a;}\n";
 ptr +=".button-stop:active {background-color: #96716f;}\n";
 ptr +="p {font-size: 14px;color: #888;margin-bottom: 10px;}\n"
 ptr +="<style>\n";
 ptr +="<head>\n";
 ptr +="<body>\n";
 ptr +="<h1>Hello</h1>\n";
 ptr +="</body>\n";
 ptr +="</html>\n";
 return ptr;
}

void setup() {

 Serial.begin(115200);
 Serial.println();
 Serial.println("Configuring access point...");
 
 WiFi.softAP(ssid, password);
 IPAddress myIP = WiFi.softAPIP();
 Serial.println("AP IP address: ");
 Serial.println(myIP);
 

 server.on("/", handle_OnConnect);
 server.on("/open", handle_open);
 server.on("/close", handle_close);
 server.on("/stop", handle_stop);
 server.onNotFound(handle_NotFound);

 server.begin();
 Serial.println("HTTP server started");
 delay(100);
 }
void loop() {
 server.handleClient();
 }

void handle_OnConnect() {
 server.send(200, "text/html", SendHTML);
}

void handle_open() {
 server.send(200, "text/html", SendHTML);
}

void handle_close() {
 server.send(200, "text/html", SendHTML);
}

void handle_stop() {
 server.send(200, "text/html", SendHTML);
}

void handle_NotFound() {
 server.send(404, "text/plain", "Not found");
}

alexa89:
What am I doing wrong?

Hijacking an existing thread with a totally unrelated post.

Also you didn’t post your code in code tags. Please read the forum guidelines.

Split from an unrelated topic

DO NOT HIJACK TOPICS

Please follow the advice on posting a programming question given in Read this before posting a programming question

In particular note the advice to Auto format code in the IDE and to use code tags when posting code here as it prevents some combinations of characters in code being interpreted as HTML commands such as italics, bold or a smiley character, all of which render the code useless

What's wrong?

A) You've not posted well formatted code in code tags.

B) You've not posted which board you are using.

C) You've not stated the programs expectations and what it actually does.

D) You 'told us' that something is wrong and have asked people to guess what it is.

I'm sure I could nit pick on other things but that will do for now.

As a note, when posting code not in code tags, the thread devolves into a how to properly post, after all you followed the sites instructions about the 'read me first' thingies?

test:64:15: error: expected constructor, destructor, or type conversion before ';' token
void "/", handle_open() {
              ^
C:\Users\YYY\Desktop\test\test.ino: In function 'void setup()':
test:46:10: error: 'class WiFiServer' has no member named 'on'
  server.on("/", handle_OnConnect);
         ^
test:47:10: error: 'class WiFiServer' has no member named 'on'
  server.on("/open", handle_open);
         ^
test:47:22: error: 'handle_open' was not declared in this scope
  server.on("/open", handle_open);
                     ^
test:48:10: error: 'class WiFiServer' has no member named 'on'
  server.on("/close", handle_close);
         ^
test:49:10: error: 'class WiFiServer' has no member named 'on'
  server.on("/stop", handle_stop);
         ^
test:50:10: error: 'class WiFiServer' has no member named 'onNotFound'
  server.onNotFound(handle_NotFound);
         ^
C:\Users\YYY\Desktop\test\test.ino: In function 'void loop()':
test:57:10: error: 'class WiFiServer' has no member named 'handleClient'
  server.handleClient();
         ^
C:\Users\YYY\Desktop\test\test.ino: In function 'void handle_OnConnect()':
test:61:10: error: 'class WiFiServer' has no member named 'send'
  server.send(200, "text/html", SendHTML);
         ^
C:\Users\YYY\Desktop\test\test.ino: At global scope:
test:64:6: error: expected unqualified-id before string constant
void "/", handle_open() {
     ^
C:\Users\YYY\Desktop\test\test.ino: In function 'void handle_close()':
test:69:10: error: 'class WiFiServer' has no member named 'send'
  server.send(200, "text/html", SendHTML);
         ^
C:\Users\YYY\Desktop\test\test.ino: In function 'void handle_stop()':
test:73:10: error: 'class WiFiServer' has no member named 'send'
  server.send(200, "text/html", SendHTML);
         ^
C:\Users\YYY\Desktop\test\test.ino: In function 'void handle_NotFound()':
test:77:10: error: 'class WiFiServer' has no member named 'send'
  server.send(404, "text/plain", "Not found");
         ^
Bibliothek WiFi in Version 1.0 im Ordner: C:\Users\YYY\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\WiFi  wird verwendet
exit status 1
expected constructor, destructor, or type conversion before ';' token

I'm curious what do you think this means error: 'class WiFiServer' has no member named 'on'?

I'm curious, what do you think this error: 'class WiFiServer' has no member named 'send'
means?

I'm curious why you've not edited your post, properly formatted the code, and put the code in code tags?

there is a ‘;’ here

 ptr +="p {font-size: 14px;color: #888;margin-bottom: 10px;}\n"

Instead of including #include <WiFiServer.h> you should use

#include <WiFiClient.h>
#include <WebServer.h>

and

WebServer server(80);

and then within the callback functions you refer to

SendHTML

in

server.send(200, "text/html", SendHTML);

but SendHTML is a function and you want to pass it’s result as an argument to .send(), so it should be

[code]server.send(200, "text/html", SendHTML());[/code

With those changes it compiles, and should more or less work if the html code was correct, but it isn’t.
First of all there is no need for all thos ‘\n’ newlines, they don’t actually do anything, but they shouldn’t be there.
but more importantly,

  ptr +="<style>\n";
  ptr +="<head>\n";
  ptr +="<body>\n";

you are supposed to close the and , but instead you open them again. (it should be & ) And of course all of what the other guys said. (you hijacked a thread ? there is a button to start a new topic you know ? )[/code]

Been trying to give the hint for the OP to properly format his code. The “;” thingy might have shown up from a proper formatting.

The ";" thingy might have shown up from a proper formatting.

might have done yeah, but in the IDE the next line lights up when trying to compile, it should have shown up anyway.

Thank you, have already found a solution.

Thank you, have already found a solution.

Now that is really a bit much, who do you think you are Gurdjieff ?

Thanks again for the help! Really appreciate that you are giving your time to help people. Found another code and adjusted it.

you say thank you but there still remains a smell of

you asking "can you please help me?" and then a kick in the ass "oh I found another code that works for me"
not even taking the time to post the now working code to share it with the community.

This shows you are not willing to learn deeper. Making the next step work is enough for you.
This will keep you on a low knowledge-level. I predict that you will come up with a lot of questions again and again.

So I suggest posting google-requests with some useful keywords will fit best to your strategy

I solved it like this and adjusted it for myself.
The HTML code is of course not perfect and has to be adapted. The text probably isn’t perfect either, but it works. A library must be installed.

The link to it:

#include <WiFi.h>
#include <ESPAsyncWebServer.h>

const char* ssid = "Test1";
const char* password = "password";

AsyncWebServer server(80);

AsyncEventSource events("/events");

void initWiFi() {
  Serial.begin(115200);
  
  WiFi.softAP(ssid, password);
  IPAddress myIP = WiFi.softAPIP();
  Serial.println("AP IP address: ");
  Serial.println(myIP);
  server.begin();
  delay(1000);
  }

const char SendHTML[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html>
<head>
  <title>Test</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <style>
    html { font-family: Helvetica; display: inline-block; margin: 0xp auto; text-align: center;}";
  body{margin-top: 50px;} h1 { color: #444444;margin-bottom: 50px auto 30px;}";
  </style>
  </head>
  <body>
  <style>
    button {
      font-size: 50px;
      padding: 10px;
      border-radius: 20px;
      margin: 20px;
    }
    
  </style>
  <h1>Hello</h1>
  <button id="B1">1</button>


  <button id="B2">2</button>


  <button id="B3">3</button>
  <script>

</script>
</body>
</html>)rawliteral";

void setup() {
  Serial.begin(115200);
  initWiFi();

  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/html", SendHTML);
  });

  events.onConnect([](AsyncEventSourceClient *client){
    if(client->lastId()){
    }}); 
  server.addHandler(&events);
  server.begin();
}

void loop() {}

I hope it helps someone. :slight_smile: