2
2
#include < ControlAssist.h> // Control assist class
3
3
4
4
#if defined(ESP32)
5
- WebServer server (80 );
6
- #include < ESPmDNS.h>
5
+ WebServer server (80 );
6
+ #include < ESPmDNS.h>
7
7
#else
8
8
ESP8266WebServer server (80 );
9
9
#include < ESP8266mDNS.h>
@@ -84,7 +84,7 @@ html, body {
84
84
right: 0;
85
85
bottom: 0;
86
86
background: var(--toggleInactive);
87
- width: calc(var(--elmSize) * 2.3);
87
+ width: calc(var(--elmSize) * 2.3);
88
88
transition: .4s;
89
89
}
90
90
.slider:before {
@@ -117,11 +117,11 @@ input:checked + .slider:before {
117
117
</style>
118
118
<body>
119
119
<div class="container">
120
- <h1>Control Assist sample page</h1>
121
- </div>
120
+ <h1>ControlAssist example page</h1>
121
+ </div>
122
122
<div class="container">
123
123
<div class="switch-label">Toggle led</div>
124
- </div>
124
+ </div>
125
125
<div class="container">
126
126
<div class="switch">
127
127
<input id="toggleLed" name="toggleLed" type="checkbox">
@@ -133,16 +133,14 @@ input:checked + .slider:before {
133
133
<div class="bottom">
134
134
<div id="conLed" class="center"></div>
135
135
<span id="wsStatus" style="display: none;"></span>
136
- </div>
136
+ </div>
137
137
<script>
138
138
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");
142
140
143
141
const changeLed = (v) => {
144
142
if(!v) led.classList.remove("on");
145
- else led.classList.add("on");
143
+ else led.classList.add("on");
146
144
}
147
145
148
146
toggleLed.addEventListener("change", (event) => {
@@ -153,16 +151,16 @@ toggleLed.addEventListener("wsChange", (event) => {
153
151
changeLed(event.target.checked);
154
152
});
155
153
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: ") ){
158
156
event.target.style.color = "lightgreen"
159
157
conLed.style.backgroundColor = "lightgreen"
160
158
}else if(event.target.innerHTML.startsWith("Error:")){
161
159
event.target.style.color = "lightred"
162
- conLed.style.backgroundColor = "lightred"
160
+ conLed.style.backgroundColor = "lightred"
163
161
}else{
164
162
event.target.style.color = "lightgray"
165
- conLed.style.backgroundColor = "lightgray"
163
+ conLed.style.backgroundColor = "lightgray"
166
164
}
167
165
conLed.title = event.target.innerHTML
168
166
});
@@ -190,25 +188,25 @@ void setup() {
190
188
Serial.begin (115200 );
191
189
Serial.print (" \n\n\n\n " );
192
190
Serial.flush ();
193
- LOG_I (" Starting..\n " );
191
+ LOG_I (" Starting..\n " );
194
192
195
193
// Connect WIFI ?
196
194
if (strlen (st_ssid)>0 ){
197
195
LOG_E (" Connect Wifi to %s.\n " , st_ssid);
198
196
WiFi.mode (WIFI_STA);
199
197
WiFi.begin (st_ssid, st_pass);
200
198
uint32_t startAttemptTime = millis ();
201
- while (WiFi.status () != WL_CONNECTED && millis () - startAttemptTime < 15000 ) {
199
+ while (WiFi.status () != WL_CONNECTED && millis () - startAttemptTime < 20000 ) {
202
200
Serial.print (" ." );
203
201
delay (500 );
204
202
Serial.flush ();
205
- }
203
+ }
206
204
Serial.println ();
207
- }
208
-
205
+ }
206
+
209
207
// Check connection
210
208
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 ());
212
210
}else {
213
211
LOG_E (" Connect failed.\n " );
214
212
LOG_I (" Starting AP.\n " );
@@ -217,11 +215,11 @@ void setup() {
217
215
String hostName = " ControlAssist_" + mac.substring (6 );
218
216
WiFi.mode (WIFI_AP_STA);
219
217
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 " );
222
220
}
223
221
224
-
222
+
225
223
// Setup control assist
226
224
ctrl.setHtmlBody (HTML_BODY);
227
225
ctrl.bind (" toggleLed" , ledState, ledChangeHandler);
@@ -230,15 +228,15 @@ void setup() {
230
228
// Add a web server handler on url "/"
231
229
ctrl.setup (server);
232
230
ctrl.begin ();
233
- ctrl.dump ();
231
+ ctrl.dump ();
234
232
LOG_V (" ControlAssist started.\n " );
235
-
233
+
236
234
server.on (" /d" , []() { // Dump controls
237
235
server.send (200 , " text/plain" , " Serial dump" );
238
236
ctrl.dump ();
239
237
});
240
238
241
- // Start webserver
239
+ // Start webserver
242
240
server.begin ();
243
241
LOG_V (" HTTP server started\n " );
244
242
@@ -256,13 +254,13 @@ void loop() {
256
254
// Handle websockets
257
255
ctrl.loop ();
258
256
259
- if (millis () - pingMillis >= 5000 ){
257
+ if (millis () - pingMillis >= 5000 ){
260
258
ledState = !ledState;
261
259
toggleLed (ledState);
262
260
// Set the ledState and send a websocket update
263
261
ctrl.put (" toggleLed" , ledState );
264
262
pingMillis = millis ();
265
- }
263
+ }
266
264
}
267
265
268
266
0 commit comments