Skip to content

LEDの点灯/消灯(引数で処理)

LEDをWebから操作

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>

const char ssid[] = "ESP32AP-AKIRA";
const char pass[] = "11111111";
const IPAddress ip(192,168,0,1);
const IPAddress subnet(255,255,255,0);
WebServer server(80);
const char page[] PROGMEM = R"=====(
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width,initial-scale=1">
</head>
<body>
<center>
LED<br>
<a href="/?led=on">ON</a><br>
<a href="/?led=off">OFF</a><br>
</center>

</body>
</html>
)=====";

void handleRoot() {
  if (server.hasArg("led")) {
      String led = server.arg("led");
      Serial.println(led);
      if(led == "on") {
        digitalWrite(4, HIGH);
      } else if(led == "off") {
        digitalWrite(4, LOW);
      }
  }
  server.send(200, "text/html", page);
}

void setup()
{
  Serial.begin(115200);
  WiFi.softAP(ssid,pass);
  delay(100);
  WiFi.softAPConfig(ip,ip,subnet);
  IPAddress serverIP = WiFi.softAPIP();
  server.on("/", handleRoot);
  server.begin();

  pinMode(4, OUTPUT);

  Serial.println();
  Serial.print("AccessPoint:");
  Serial.println(ssid);
  Serial.print("IP:");
  Serial.println(serverIP);
}

void loop() {
  server.handleClient();
}

項目
http://192.168.0.1/?led=on LED on
http://192.168.0.1/?led=off LED off