Skip to content

Commit 4e4d651

Browse files
author
gemi254
committed
Updated v1.1.2
1 parent 8ee07be commit 4e4d651

File tree

1 file changed

+27
-29
lines changed

1 file changed

+27
-29
lines changed

examples/ControlAssist-ToggleLed/ControlAssist-ToggleLed.ino

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
#include <ControlAssist.h> // Control assist class
33

44
#if defined(ESP32)
5-
WebServer server(80);
6-
#include <ESPmDNS.h>
5+
WebServer server(80);
6+
#include <ESPmDNS.h>
77
#else
88
ESP8266WebServer server(80);
99
#include <ESP8266mDNS.h>
@@ -84,7 +84,7 @@ html, body {
8484
right: 0;
8585
bottom: 0;
8686
background: var(--toggleInactive);
87-
width: calc(var(--elmSize) * 2.3);
87+
width: calc(var(--elmSize) * 2.3);
8888
transition: .4s;
8989
}
9090
.slider:before {
@@ -117,11 +117,11 @@ input:checked + .slider:before {
117117
</style>
118118
<body>
119119
<div class="container">
120-
<h1>Control Assist sample page</h1>
121-
</div>
120+
<h1>ControlAssist example page</h1>
121+
</div>
122122
<div class="container">
123123
<div class="switch-label">Toggle led</div>
124-
</div>
124+
</div>
125125
<div class="container">
126126
<div class="switch">
127127
<input id="toggleLed" name="toggleLed" type="checkbox">
@@ -133,16 +133,14 @@ input:checked + .slider:before {
133133
<div class="bottom">
134134
<div id="conLed" class="center"></div>
135135
<span id="wsStatus" style="display: none;"></span>
136-
</div>
136+
</div>
137137
<script>
138138
const led = document.getElementById("led"),
139-
toggleLed = document.getElementById("toggleLed"),
140-
wStatus = document.getElementById("wsStatus"),
141-
conLed = document.getElementById("conLed")
139+
toggleLed = document.getElementById("toggleLed");
142140
143141
const changeLed = (v) => {
144142
if(!v) led.classList.remove("on");
145-
else led.classList.add("on");
143+
else led.classList.add("on");
146144
}
147145
148146
toggleLed.addEventListener("change", (event) => {
@@ -153,16 +151,16 @@ toggleLed.addEventListener("wsChange", (event) => {
153151
changeLed(event.target.checked);
154152
});
155153
156-
wStatus.addEventListener("change", (event) => {
157-
if(event.target.innerHTML == "Connected"){
154+
document.getElementById("wsStatus").addEventListener("change", (event) => {
155+
if(event.target.innerHTML.startsWith("Connected: ")){
158156
event.target.style.color = "lightgreen"
159157
conLed.style.backgroundColor = "lightgreen"
160158
}else if(event.target.innerHTML.startsWith("Error:")){
161159
event.target.style.color = "lightred"
162-
conLed.style.backgroundColor = "lightred"
160+
conLed.style.backgroundColor = "lightred"
163161
}else{
164162
event.target.style.color = "lightgray"
165-
conLed.style.backgroundColor = "lightgray"
163+
conLed.style.backgroundColor = "lightgray"
166164
}
167165
conLed.title = event.target.innerHTML
168166
});
@@ -190,25 +188,25 @@ void setup() {
190188
Serial.begin(115200);
191189
Serial.print("\n\n\n\n");
192190
Serial.flush();
193-
LOG_I("Starting..\n");
191+
LOG_I("Starting..\n");
194192

195193
// Connect WIFI ?
196194
if(strlen(st_ssid)>0){
197195
LOG_E("Connect Wifi to %s.\n", st_ssid);
198196
WiFi.mode(WIFI_STA);
199197
WiFi.begin(st_ssid, st_pass);
200198
uint32_t startAttemptTime = millis();
201-
while (WiFi.status() != WL_CONNECTED && millis() - startAttemptTime < 15000) {
199+
while (WiFi.status() != WL_CONNECTED && millis() - startAttemptTime < 20000) {
202200
Serial.print(".");
203201
delay(500);
204202
Serial.flush();
205-
}
203+
}
206204
Serial.println();
207-
}
208-
205+
}
206+
209207
// Check connection
210208
if(WiFi.status() == WL_CONNECTED ){
211-
LOG_I("Wifi AP SSID: %s connected, use 'http://%s' to connect\n", st_ssid, WiFi.localIP().toString().c_str());
209+
LOG_I("Wifi AP SSID: %s connected, use 'http://%s' to connect\n", st_ssid, WiFi.localIP().toString().c_str());
212210
}else{
213211
LOG_E("Connect failed.\n");
214212
LOG_I("Starting AP.\n");
@@ -217,11 +215,11 @@ void setup() {
217215
String hostName = "ControlAssist_" + mac.substring(6);
218216
WiFi.mode(WIFI_AP_STA);
219217
WiFi.softAP(hostName.c_str(),"",1);
220-
LOG_I("Wifi AP SSID: %s started, use 'http://%s' to connect\n", WiFi.softAPSSID().c_str(), WiFi.softAPIP().toString().c_str());
221-
if (MDNS.begin(hostName.c_str())) LOG_V("AP MDNS responder Started\n");
218+
LOG_I("Wifi AP SSID: %s started, use 'http://%s' to connect\n", WiFi.softAPSSID().c_str(), WiFi.softAPIP().toString().c_str());
219+
if (MDNS.begin(hostName.c_str())) LOG_V("AP MDNS responder Started\n");
222220
}
223221

224-
222+
225223
// Setup control assist
226224
ctrl.setHtmlBody(HTML_BODY);
227225
ctrl.bind("toggleLed", ledState, ledChangeHandler);
@@ -230,15 +228,15 @@ void setup() {
230228
// Add a web server handler on url "/"
231229
ctrl.setup(server);
232230
ctrl.begin();
233-
ctrl.dump();
231+
ctrl.dump();
234232
LOG_V("ControlAssist started.\n");
235-
233+
236234
server.on("/d", []() { // Dump controls
237235
server.send(200, "text/plain", "Serial dump");
238236
ctrl.dump();
239237
});
240238

241-
// Start webserver
239+
// Start webserver
242240
server.begin();
243241
LOG_V("HTTP server started\n");
244242

@@ -256,13 +254,13 @@ void loop() {
256254
// Handle websockets
257255
ctrl.loop();
258256

259-
if (millis() - pingMillis >= 5000){
257+
if (millis() - pingMillis >= 5000){
260258
ledState = !ledState;
261259
toggleLed(ledState);
262260
// Set the ledState and send a websocket update
263261
ctrl.put("toggleLed", ledState );
264262
pingMillis = millis();
265-
}
263+
}
266264
}
267265

268266

0 commit comments

Comments
 (0)