Esp32 webserver

hi everyone, i try to use my esp32 as a web server bur i have a problem. When i do it by creating an Access Point with the esp32, that works really well. But when i do it by connecting my esp32 to the wifi nothing is happening. When i go to the url, it says this : "192.168.1.75 took too long to respond". here is my code :

// Import required libraries
#include <WiFi.h>
#include <ESPAsyncWebServer.h>
#include <AsyncTCP.h>
#include <SPIFFS.h>
#include <LiquidCrystal.h>
#include <HTTPClient.h>

// Replace with your network credentials
const char* ssid = "***********";
const char* password = "***************";

// set the LCD number of columns and rows
int lcdColumns = 16;
int lcdRows = 2;


// set LCD address, number of columns and rows

LiquidCrystal lcd(19, 23, 18, 17, 16, 15);

// Set LED GPIO
const int ledPin = 23;
// Stores LED state
String ledState;

// Create AsyncWebServer object on port 80
AsyncWebServer server(80);

// Replaces placeholder with LED state value
String processor(const String& var){
  Serial.println(var);
  if(var == "STATE"){
    if(digitalRead(ledPin)){
      ledState = "ON";
    }
    else{
      ledState = "OFF";
    }
    Serial.print(ledState);
    return ledState;
  }
  return String();
}
 
void setup(){

 // initialize LCD
  lcd.begin(16, 2);
  // turn on LCD backlight                      

  
  // Serial port for debugging purposes
  Serial.begin(115200);
  pinMode(ledPin, OUTPUT);

  // Initialize SPIFFS
  if(!SPIFFS.begin(true)){
    Serial.println("An Error has occurred while mounting SPIFFS");
    return;
  }
WiFi.mode(WIFI_STA);
  // Connect to Wi-Fi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi..");
    Serial.println((String)"[+] RSSI : " + WiFi.RSSI() + " dB");
        Serial.print("[+] ESP32 IP : ");
        Serial.println(WiFi.localIP());
  } 

  // Print ESP32 Local IP Address
  Serial.println(WiFi.localIP());
  lcd.setCursor(0, 0);
  lcd.print("IP address is ");
  lcd.setCursor(0, 1);
  lcd.print(WiFi.localIP());
  // Route for root / web page
  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send(SPIFFS, "/index.html", String(), false, processor);
  });
  
  // Route to load style.css file
  server.on("/style.css", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send(SPIFFS, "/style.css", "text/css");
  });

  // Route to set GPIO to HIGH
  server.on("/on", HTTP_GET, [](AsyncWebServerRequest *request){
    digitalWrite(ledPin, HIGH);    
    request->send(SPIFFS, "/index.html", String(), false, processor);
  });
  
  // Route to set GPIO to LOW
  server.on("/off", HTTP_GET, [](AsyncWebServerRequest *request){
    digitalWrite(ledPin, LOW);    
    request->send(SPIFFS, "/index.html", String(), false, processor);
  });

  // Start server
  server.begin();
}
 
void loop(){


  }

can you run a basic station mode web server, e.g. create-a-web-server-w-esp32

thank you horace for your quick response. But i tried the code that you advised me and that didn't work. On the serial monitor, everything looks fine :
10:12:52.796 -> Try Connecting to
10:12:52.796 -> **************
10:12:54.017 -> ..
10:12:55.022 -> WiFi connected successfully
10:12:55.022 -> Got IP: 192.168.1.75
10:12:55.022 -> HTTP server started
but when i go to this IP with my computer wich is conected to the same wifi, nothing is happening and i get again this message : "192.168.1.75took too long to respond".
It is possible that my esp32 has a problem?

how far apart are the ESP32 and PC?
works fine on my PC

Try Connecting to 
XXXXX
...........
WiFi connected successfully
Got IP: 192.168.1.164
HTTP server started

image

can you ping the ESP32
image

Thanks again. My pc and my esp32 are really close (less than a meter). I can ping my esp32 with my pc :


I really don't understand why that doesn't work.

Did you try the very simple HelloServer example?

Try

  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send(SPIFFS, "/index.html", "text/html");
  });

Also, add server.serveStatic( "/" SPIFFS "/" ); before server.begin;

really thank you gfvalvo and Merseyman. I did what you told me gfvalvo and it worked very well so I didn't understand where my mistake was and Merseyman the modification you told me to do worked. Thank you very much I've been trying for 1 week. I don't know if it would be abusing your kindness but could you explain to me what I do now if I want to listen to the requests that my site receives to light up a led for example or to send data to the site. Finally, how can I change the web address to which I want to send and listen to requests, for example: www.hellotest.com if the domain belongs to me.
Thank you again for all.

1 Like

Im glad youre sorted.

Try following this ESP32 Async Web Server – Control Outputs | Random Nerd Tutorials

or you could you Webserial

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