コンテンツにスキップ

LEDの点灯/消灯(URLで処理)

LEDの点灯/消灯

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
void setup() {
  pinMode(4, OUTPUT);
}

void loop() {
  digitalWrite(4, HIGH);
  delay(1000);
  digitalWrite(4, LOW);
  delay(1000);
}

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
62
63
64
65
66
#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="/on">ON</a><br>
<a href="/off">OFF</a><br>
</center>

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

void handleRoot() {
  server.send(200, "text/html", page);
}

void handleOn(){
  digitalWrite(4, HIGH);
  Serial.println("LED ON");
  server.send(200, "text/html", page);
}

void handleOff(){
  digitalWrite(4, LOW);
  Serial.println("LED OFF");
  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.on("/on", handleOn);
  server.on("/off", handleOff);
  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/on LED on
http://192.168.0.1/off LED off