我正在做这个项目。在此,我首先将nodemcu连接到了我的移动热点。然后,我在ip上创建了一个网页。该网页如下所示: 192.168.43.10 ip
我需要编码部分的帮助。我想要的是,当我将nodemcu连接到我的热点时,nodemcu充当服务器并提供了热点。当我在网页中输入新的ssid和密码时,我希望我的nodemcu连接到新输入的wifi凭据。我的代码是:
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WiFiClient.h>
#include <ESP8266mDNS.h>
//Put your SSID & Password*/
const char* ssid = "Shabnam"; // Enter SSID here
const char* password = "nishu1234"; //Enter Password here
//WiFiClient client;
ESP8266WebServer server(80);
//char serverName[] = "web.comporium.net";
void setup(void) {
Serial.begin(9600);
delay(100);
Serial.println("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected..!");
Serial.print("Got IP: "); Serial.println(WiFi.localIP());
Serial.println("HTTP server started");
// server.flush();
server.on("/", handle_OnConnect);
server.onNotFound(handle_NotFound);
server.begin();
// delay(200);
}
void loop(void) {
/* WiFiClient client = server.available();
if (!client) {
return;
}
Serial.println("");
Serial.println("New client");
// Wait for data from client to become available
while(client.connected() && !client.available()){
delay(1);
}
// Read the first line of HTTP request
String req = client.readStringUntil('\r');
// First line of HTTP request looks like "GET /path HTTP/1.1"
// Retrieve the "/path" part by finding the spaces
int addr_start = req.indexOf(' ');
int addr_end = req.indexOf(' ', addr_start + 1);
if (addr_start == -1 || addr_end == -1) {
Serial.print("Invalid request: ");
Serial.println(req);
return;
}
req = req.substring(addr_start + 1, addr_end);
Serial.print("Request: ");
Serial.println(req);
client.flush();
//client.on("/", handle_OnConnect);
//client.onNotFound(handle_NotFound);
*/
server.handleClient();
//sendGET();
}
void handle_OnConnect() {
server.send(200, "text/html", SendHTML(ssid,password));
}
char viewport, container;
void handle_NotFound(){
server.send(404, "text/plain", "Not found");
}
String SendHTML(String ssid,String password){
String ptr ="<!DOCTYPE html> <html> <head> <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"> <style> body {font-family: Arial, Helvetica, sans-serif;} form {border: 3px solid #f1f1f1;} input[type=text], input[type=password] { width: 100%; padding: 12px 20px; margin: 8px 0; display: inline-block; border: 1px solid #ccc; box-sizing: border-box; } button { background-color: #4CAF50; color: white; padding: 14px 20px; margin: 8px 0; border: none; cursor: pointer; width: 100%; } button:hover { opacity: 0.8; } .cancelbtn { width: auto; padding: 10px 18px; background-color: #f44336; } .container { padding: 16px; } span.psw { float: right; padding-top: 16px; } } </style> </head> <body> <h2>WiFi Login Page</h2> <form action=\"welcome.php\" method=\"post\"><div class=\"container\"> <label for=\"ssid\"><b>SSID</b></label> <input type=\"text\" placeholder=\"Enter SSID\" name=\"ssid\" required> <label for=\"psw\"><b>Password</b></label> <input type=\"password\" placeholder=\"Enter Password\" name=\"psw\" required> <button type=\"submit\">Login</button> <label> <input type=\"checkbox\" checked=\"checked\" name=\"remember\"> Remember me </label> </div></form> </body> </html>";
return ptr;
/*Serial.println("Connecting to ");
Serial.println(ssid);
<div class=\"container\" style=\"background-color:#f1f1f1\"> <button type=\"button\" class=\"cancelbtn\">Cancel</button></div> //connect to your local wi-fi network
WiFi.begin(ssid, password);
//check wi-fi is connected to wi-fi network
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected..!");
Serial.print("Got IP: "); Serial.println(WiFi.localIP());
Serial.println("HTTP server started");
*/
}
/*void sendGET() //client function to send/receive GET request data.
{
if (client.connect(serverName, 80)) { //starts client connection, checks for connection
Serial.println("connected");
client.println("GET /~shb/arduino.txt HTTP/1.1"); //download text
client.println("Host: web.comporium.net");
client.println("Connection: close"); //close 1.1 persistent connection
client.println(); //end of get request
}
else {
Serial.println("connection failed"); //error message if no client connect
Serial.println();
}
while(client.connected() && !client.available()) delay(1); //waits for data
while (client.connected() || client.available()) { //connected or data available
char c = client.read(); //gets byte from ethernet buffer
Serial.print(c); //prints byte to serial monitor
}
Serial.println();
Serial.println("disconnecting.");
Serial.println("==================");
Serial.println();
client.stop(); //stop client
}*/
请忽略代码中的注释。请帮助我进行此服务器客户端编程。