// https://garretlab.web.fc2.com/arduino/esp32/examples/WiFi/SimpleWiFiServer.html
// updated by me
#include <WiFi.h> // wifi lib of arduino1.5ide
#define WIFI_SSID "ESP32-softAP"
#define WIFI_PWD "12345678"
WiFiServer server(80);
IPAddress ip( 192, 168, 0, 111 );
IPAddress subnet( 255, 255, 255, 0 );
void setup() {
pinMode(5, OUTPUT);
digitalWrite(5,HIGH);
delay(1000);
digitalWrite(5,LOW);
WiFi.mode(WIFI_AP);
WiFi.softAP(WIFI_SSID, WIFI_PWD);
delay(100);
WiFi.softAPConfig(ip, ip, subnet);
delay(100);
server.begin();
}
int value = 0;
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 the LED on pin 5 on.<br>");
client.print("Click <a href=\"/L\">here</a> to turn the LED on pin 5 off.<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(5, HIGH); // GET /H turns the LED on
}
if (currentLine.endsWith("GET /L")) {
digitalWrite(5, LOW); // GET /L turns the LED off
}
}
}
// close the connection:
client.stop();
Serial.println("Client Disconnected.");
}
}
--------------------------------------------------------------------------------
// https://cotechworks.ltt.jp/2023/06/05/post-668/ を改変
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#define WIFI_SSID "ESP8266-softAP" /* SSID */
#define WIFI_PWD "12345678" /* パスワード */
ESP8266WebServer server(80);
IPAddress ip( 192, 168, 0, 1 ); /* ESP8266のIPアドレス */
IPAddress subnet( 255, 255, 255, 0 ); /* サブネットマスク */
void setup() {
/* softAPモードに設定 */
pinMode(0,OUTPUT);
WiFi.mode(WIFI_AP);
WiFi.softAPConfig(ip, ip, subnet);
WiFi.softAP(WIFI_SSID, WIFI_PWD);
server.on("/", []() {
server.send(
200,
"text/html",
"<h1>Hello! Web Server!</h1>"
"Click <a href=\"/H\">here</a>");
});
server.on("/H", []() {
digitalWrite(0,HIGH);
delay(500);
digitalWrite(0,LOW);
server.send(
200,
"text/html",
"<h1>Hello! Web Server!</h1>"
"Click <a href=\"/H\">here</a>");
});
// 上記やや冗長だがわかりやすい
server.begin();
}
void loop() {
/* クライアントからアクセスがあった時の処理 */
server.handleClient();
}
-------------------------------------------------------------------------
https://iot.keicode.com/esp8266/esp8266-webserver.php を参考に冗長解決
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#define WIFI_SSID "ESP8266-softAP" /* SSID */
#define WIFI_PWD "12345678" /* パスワード */
ESP8266WebServer server(80);
IPAddress ip( 192, 168, 0, 1 ); /* ESP8266のIPアドレス */
IPAddress subnet( 255, 255, 255, 0 ); /* サブネットマスク */
// HTML
#define HTML_HEADER "<!doctype html>"\
"<html><head><meta charset=\"UTF-8\"/>"\
"<meta name=\"viewport\" content=\"width=device-width\"/>"\
"</head><body>"
#define HTML_FOOTER "</body></html>"
const String html = HTML_HEADER "<h1>Hello! Web Server!</h1>" "Click <a href=\"/H\">here</a>" HTML_FOOTER; // この位置でないとおかしくなる。。。
void setup() {
/* softAPモードに設定 */
pinMode(0,OUTPUT);
WiFi.mode(WIFI_AP);
WiFi.softAPConfig(ip, ip, subnet);
WiFi.softAP(WIFI_SSID, WIFI_PWD);
/* ルートにアクセスされた時の処理の設定 */
server.on("/", []() {
server.send(
200,
"text/html",
html);
});
server.on("/H", []() {
digitalWrite(0,HIGH);
delay(500);
digitalWrite(0,LOW);
server.send(
200,
"text/html",
html);
});
/* Webサーバーの起動 */
server.begin();
}
void loop() {
/* クライアントからアクセスがあった時の処理 */
server.handleClient();
}