Hello, I have a nodeMCU and I am trying to understand how the code for different modes work. I know that station mode is used to connect to another WIFI network and the AP mode (Access Point) mode is used to create a wifi network that other device can connect to.
Below I have two different codes for the nodeMcu,
-The first code is from the examples called WiFiAccessPoint and it acts as an AP called “EspAP” and provides you with the password, then the code creates a Web server with a local IP address that you can to the website and it says you are connected.
-The second code operates in station mode and connects to my provided SSID and P.W.
it creates a led int and assigns it a pin.It then starts a web server that, then it initializes my local Ip ( which i guess when i uploaded the code, my home router assigned it an IP with DHCP?)
when i direct a browser to that page, i get an interactive button tht lets me control an led on my nodeMCU.
My Question is, can i Implement the interactive webpage button into the WiFiAccess point code that normally just provides a text message?
My second question is that both codes seem very similar, What are the main differences between them and if you give an example that shows their differences?
/* Create a WiFi access point and provide a web server on it. */
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#ifndef APSSID
#define APSSID "ESPap"
#define APPSK "thereisnospoon"
#endif
/* Set these to your desired credentials. */
const char *ssid = APSSID;
const char *password = APPSK;
ESP8266WebServer server(80);
/* Just a little test message. Go to http://192.168.4.1 in a web browser
connected to this access point to see it.
*/
void handleRoot() {
server.send(200, "text/html", "<h1>You are connected</h1>");
}
void setup() {
delay(1000);
Serial.begin(115200);
Serial.println();
Serial.print("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.on("/", handleRoot);
server.begin();
Serial.println("HTTP server started");
}
void loop() {
server.handleClient();
}
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.print("Use this URL to connect: ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");
}
void loop() {
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
Serial.println("new client");
while(!client.available()){
delay(1);
}
// Read the first line of the request
String request = client.readStringUntil('\r');
Serial.println(request);
client.flush();
// Match the request
int value = LOW;
if (request.indexOf("/LED=ON") != -1) {
digitalWrite(ledPin, HIGH);
value = HIGH;
}
if (request.indexOf("/LED=OFF") != -1) {
digitalWrite(ledPin, LOW);
value = LOW;
}
// Set ledPin according to the request
//digitalWrite(ledPin, value);
// Return the response
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println(""); // do not forget this one
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.print("Led is now: ");
if(value == HIGH) {
client.print("On");
} else {
client.print("Off");
}
client.println("
");
client.println("<a href=\"/LED=ON\"\"><button>On </button></a>");
client.println("<a href=\"/LED=OFF\"\"><button>Off </button></a>
");
client.println("</html>");
delay(1);
Serial.println("Client disonnected");
Serial.println("");
}