Repository navigation
Expand file tree
/
Copy pathLED_switcher.ino
More file actions
126 lines (110 loc) · 3.5 KB
/
Copy pathLED_switcher.ino
File metadata and controls
126 lines (110 loc) · 3.5 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <WiFi.h>
#include <WebServer.h>
#include <WebSocketsServer.h>
// Wi-Fi設定(APモード)
const char* ap_ssid = "ESP32-LED";
// const char* ap_password = "12345678"; // 8文字以上
// const char* ssid = "ssid";
// const char* password = "pass";
WebServer server(80);
WebSocketsServer webSocket = WebSocketsServer(81);
const int ledPin = 4;
const int touchPin = T4;
bool ledState = LOW;
unsigned long lastTouchTime = 0; // デバウンス用
// --- HTML ---
const char htmlPage[] PROGMEM = R"rawliteral(
<!DOCTYPE html>
<html lang='ja'>
<head>
<meta charset='UTF-8'>
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
<style>
body {
font-family: Arial, sans-serif;
background:#1e1e1e; color:#f0f0f0;
display:flex; flex-direction:column;
align-items:center; justify-content:center;
height:100vh; margin:0; text-align:center;
}
h1 { font-size:2.5em; margin-bottom:40px; color:#00ffff; }
#ledButton {
padding:25px 50px; font-size:1.5em;
border:none; border-radius:12px;
background:linear-gradient(45deg,#00ffff,#0077ff);
color:#fff; cursor:pointer;
box-shadow:0 6px 8px rgba(0,0,0,0.3);
width:80%; max-width:400px;
}
</style>
</head>
<body>
<h1>LED Switcher</h1>
<button id="ledButton" onclick="toggleLED()">LEDを点ける</button>
<script>
const ws = new WebSocket('ws://' + location.hostname + ':81/');
let ledOn = false;
ws.onmessage = e => { ledOn = e.data==="ON"; updateBtn(); };
function toggleLED(){ ws.send('toggle'); }
function updateBtn(){
const b=document.getElementById('ledButton');
if(ledOn){ b.textContent="LEDを消す"; b.style.background="linear-gradient(45deg,#00ff00,#007700)"; }
else { b.textContent="LEDを点ける"; b.style.background="linear-gradient(45deg,#ff4444,#880000)"; }
}
</script>
</body></html>
)rawliteral";
// --- HTTPハンドラ ---
void handleRoot() {
server.send_P(200, "text/html", htmlPage);
}
// --- WebSocketイベント ---
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
if(type == WStype_TEXT) {
if(strcmp((char*)payload,"toggle")==0){
// LEDを即更新
ledState = !ledState;
digitalWrite(ledPin, ledState ? HIGH : LOW);
// 全端末へ送信
webSocket.broadcastTXT(ledState ? "ON" : "OFF");
}
}
}
void setup() {
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
// --- APモード開始 ---
// WiFi.softAP(ap_ssid, ap_password);
// --パスワードなし--
WiFi.softAP(ap_ssid, nullptr);
Serial.println("AP started, IP: " + WiFi.softAPIP().toString());
// wiFiのコード一応残しておく
// WiFi.begin(ssid, password);
// while(WiFi.status()!=WL_CONNECTED){ delay(50); }
// Serial.println("WiFi connected: " + WiFi.localIP().toString());
// --- HTTPサーバー設定 ---
server.on("/", handleRoot);
// 見つからないページはトップにリダイレクト
server.onNotFound([](){
server.sendHeader("Location", "/");
server.send(302, "text/plain", "");
});
server.begin();
webSocket.begin();
webSocket.onEvent(webSocketEvent);
}
void loop() {
server.handleClient();
webSocket.loop();
// タッチセンサー処理(ソフトデバウンス)
if(touchRead(touchPin) < 40){
unsigned long now = millis();
if(now - lastTouchTime > 250){ // 250ms以内は無視
ledState = !ledState;
digitalWrite(ledPin, ledState ? HIGH : LOW);
webSocket.broadcastTXT(ledState ? "ON" : "OFF");
lastTouchTime = now;
}
}
}