I have noticed alot of examples for these webservers require you to input your wifi credentials rather than setup the ESP as the web server (i.e connect your phone/tablet directly to the ESP not via your wifi router)?
Can someone explain how i modify this code to use the ESP directly?
I simply want to have the ESP create a wifi AP where i can set the SSID, USER, PASSWORD and have it redirect me to a page e.g 192.168.4.1 where a page loads witha slider value and when i change that value it sets the analogWrite PWM, so i can hook this up to a L298N motor driver board
Being a web server just means offering an HTTP service over some network.
If you enter your existing WiFi credentials (typically your home internet access) then the ESP joins your WiFi network and this means you don’t have to change anything on the devices connected to that network - they will be able to « see » the ESP.
If you set up the ESP as an access point, ie creating its own WiFi network, then only the devices joining this network will see the ESP and they will loose internet access or the other access to the home WiFi network you are usually connected to
This example allowed the ESP8266 to be the wifi access point and i simply connected via a tablet, loaded the page without needing to connect the ESP to my home network (which is wired - wifi off)
How easy is it to modify the motor controller example to act this way instead, or is there a very simple stripped down version of a web server on an ESP8266 or ESP32 where i can build upon without using my router?
i ask as the deauther example is massive and trying to disect and pull that part out seems a little daunting, i just want a simple version where i can set the web page as a get/post in the url if needed and a basic html built into the code, so it's easy to modify
ok, i i have found an example now that switches the built in led on/off via a webpage without router, so i am going to try to merge the code from the initial example (motor controller pwm) into this one:
/*
WiFiAccessPoint.ino creates a WiFi access point and provides a web server on it.
Steps:
1. Connect to the access point "yourAp"
2. Point your web browser to http://192.168.4.1/H to turn the LED on or http://192.168.4.1/L to turn it off
OR
Run raw TCP "GET /H" and "GET /L" on PuTTY terminal with 192.168.4.1 as IP address and 80 as port
Created for arduino-esp32 on 04 July, 2018
by Elochukwu Ifediora (fedy0)
*/
#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiAP.h>
#define LED_BUILTIN 2 // Set the GPIO pin where you connected your test LED or comment this line out if your dev board has a built-in LED
// Set these to your desired credentials.
const char *ssid = "my_esp";
const char *password = "my_pass123";
WiFiServer server(80);
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(115200);
Serial.println();
Serial.println("Configuring access point...");
// You can remove the password parameter if you want the AP to be open.
WiFi.softAP(ssid, password);
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
server.begin();
Serial.println("Server started");
}
void loop() {
WiFiClient client = server.available(); // listen for incoming clients
if (client) { // if you get a client,
Serial.println("New Client."); // print a message out the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
// the content of the HTTP response follows the header:
client.print("Click <a href=\"/H\">here</a> to turn ON the LED.<br>");
client.print("Click <a href=\"/L\">here</a> to turn OFF the LED.<br>");
// The HTTP response ends with another blank line:
client.println();
// break out of the while loop:
break;
} else { // if you got a newline, then clear currentLine:
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
// Check to see if the client request was "GET /H" or "GET /L":
if (currentLine.endsWith("GET /H")) {
digitalWrite(LED_BUILTIN, HIGH); // GET /H turns the LED on
}
if (currentLine.endsWith("GET /L")) {
digitalWrite(LED_BUILTIN, LOW); // GET /L turns the LED off
}
}
}
// close the connection:
client.stop();
Serial.println("Client Disconnected.");
}
}
in the sketch that you linked in the original post replace
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting...");
}
with
WiFi.softAP(ssid, password);
and you will find your AP show up
then you need to navigate to 192.168.4.1
basically it doesn't matter to the ESP how it is connected it will act the same way. You can even set it up to work as both and the IP that is given by the router will also work as for the AP ( as will the original 4.1)