|
@@ -16,13 +16,17 @@
|
16
|
16
|
|
17
|
17
|
#endif
|
18
|
18
|
|
|
19
|
+#include <WebSocketsServer.h>
|
|
20
|
+
|
19
|
21
|
#include "wifi.h"
|
20
|
22
|
#include "config.h"
|
21
|
23
|
#include "config_pins.h"
|
|
24
|
+#include "Functionality.h"
|
22
|
25
|
#include "SimpleUpdater.h"
|
23
|
26
|
#include "WifiStuff.h"
|
24
|
27
|
|
25
|
28
|
UPDATE_WEB_SERVER server(80);
|
|
29
|
+WebSocketsServer socket = WebSocketsServer(81);
|
26
|
30
|
SimpleUpdater updater;
|
27
|
31
|
unsigned long last_server_handle_time = 0;
|
28
|
32
|
|
|
@@ -36,54 +40,116 @@ void wifi_set_message_buffer(String a, String b, String c, String d) {
|
36
|
40
|
message_buffer_b = b;
|
37
|
41
|
message_buffer_c = c;
|
38
|
42
|
message_buffer_d = d;
|
|
43
|
+
|
|
44
|
+ a.replace("\"", "'");
|
|
45
|
+ b.replace("\"", "'");
|
|
46
|
+ c.replace("\"", "'");
|
|
47
|
+ d.replace("\"", "'");
|
|
48
|
+
|
|
49
|
+ String ws = "{\n";
|
|
50
|
+ ws += "\"a\": \"" + a + "\",\n";
|
|
51
|
+ ws += "\"b\": \"" + b + "\",\n";
|
|
52
|
+ ws += "\"c\": \"" + c + "\",\n";
|
|
53
|
+ ws += "\"d\": \"" + d + "\",\n";
|
|
54
|
+ ws += "\"state\": \"" + String(control_state_name()) + "\"\n";
|
|
55
|
+ ws += "}";
|
|
56
|
+ socket.broadcastTXT(ws);
|
39
|
57
|
}
|
40
|
58
|
|
41
|
59
|
void handleRoot() {
|
42
|
|
- String message = F("<html><head>\n");
|
43
|
|
- message += F("<title>Giess-o-mat</title>\n");
|
44
|
|
- message += F("</head><body>\n");
|
45
|
|
- message += F("<h1>Giess-o-mat</h1>\n");
|
|
60
|
+ String message = F("<!DOCTYPE html>\n");
|
|
61
|
+ message += F("<html><head>\n");
|
|
62
|
+ message += F("<meta charset='utf-8'/>\n");
|
|
63
|
+ message += F("<meta name='viewport' content='width=device-width, initial-scale=1'/>\n");
|
|
64
|
+ message += F("<title>Gieß-o-mat</title>\n");
|
46
|
65
|
|
47
|
|
- message += F("\n<pre>\n");
|
48
|
|
- message += F(" ----------------------\n");
|
|
66
|
+ message += F("<style type='text/css'>\n");
|
|
67
|
+ message += F(".container {\n");
|
|
68
|
+ message += F("display: flex;\n");
|
|
69
|
+ message += F("}\n");
|
49
|
70
|
|
50
|
|
- message += F("| ");
|
51
|
|
- message += message_buffer_a;
|
52
|
|
- for (int i = 0; i < (20 - message_buffer_a.length()); i++) {
|
53
|
|
- message += ' ';
|
54
|
|
- }
|
55
|
|
- message += F(" |\n");
|
|
71
|
+ message += F(".ui {\n");
|
|
72
|
+ message += F("width: max-content;\n");
|
|
73
|
+ message += F("margin-right: 1em;\n");
|
|
74
|
+ message += F("padding: 0 1.0em;\n");
|
|
75
|
+ message += F("border: 1px dashed black;\n");
|
|
76
|
+ message += F("}\n");
|
56
|
77
|
|
57
|
|
- message += F("| ");
|
58
|
|
- message += message_buffer_b;
|
59
|
|
- for (int i = 0; i < (20 - message_buffer_b.length()); i++) {
|
60
|
|
- message += ' ';
|
61
|
|
- }
|
62
|
|
- message += F(" |\n");
|
|
78
|
+ message += F(".info {\n");
|
|
79
|
+ message += F("flex-grow: 1;\n");
|
|
80
|
+ message += F("}\n");
|
63
|
81
|
|
64
|
|
- message += F("| ");
|
65
|
|
- message += message_buffer_c;
|
66
|
|
- for (int i = 0; i < (20 - message_buffer_c.length()); i++) {
|
67
|
|
- message += ' ';
|
68
|
|
- }
|
69
|
|
- message += F(" |\n");
|
|
82
|
+ message += F(".pad {\n");
|
|
83
|
+ message += F("background: black;\n");
|
|
84
|
+ message += F("border: 3px solid black;\n");
|
|
85
|
+ message += F("width: max-content;\n");
|
|
86
|
+ message += F("padding: 1.5em;\n");
|
|
87
|
+ message += F("margin-left: auto;\n");
|
|
88
|
+ message += F("margin-right: auto;\n");
|
|
89
|
+ message += F("}\n");
|
70
|
90
|
|
71
|
|
- message += F("| ");
|
72
|
|
- message += message_buffer_d;
|
73
|
|
- for (int i = 0; i < (20 - message_buffer_d.length()); i++) {
|
74
|
|
- message += ' ';
|
75
|
|
- }
|
76
|
|
- message += F(" |\n");
|
|
91
|
+ message += F(".pad input {\n");
|
|
92
|
+ message += F("background: #fff0cf;\n");
|
|
93
|
+ message += F("font-weight: bold;\n");
|
|
94
|
+ message += F("font-family: monospace;\n");
|
|
95
|
+ message += F("padding: 0.5em 1em;\n");
|
|
96
|
+ message += F("margin: 0.5em;\n");
|
|
97
|
+ message += F("}\n");
|
|
98
|
+
|
|
99
|
+ // https://codepen.io/hawkz/pres/RpPaGK
|
|
100
|
+ message += F(".lcd {\n");
|
|
101
|
+ //message += F("background: #9ea18c;\n");
|
|
102
|
+ message += F("background: #9ed18c;\n");
|
|
103
|
+ message += F("border: 3px solid black;\n");
|
|
104
|
+ message += F("width: max-content;\n");
|
|
105
|
+ message += F("padding: 0.65em 1em;\n");
|
|
106
|
+ message += F("box-shadow: inset 0 0 5px 5px rgba(0,0,0,.1);\n");
|
|
107
|
+ message += F("font-weight: bold;\n");
|
|
108
|
+ message += F("font-family: monospace;\n");
|
|
109
|
+ message += F("letter-spacing: 0.1em;\n");
|
|
110
|
+ message += F("font-size: 1.2em;\n");
|
|
111
|
+ message += F("line-height: 160%;\n");
|
|
112
|
+ message += F("color: #21230e;\n");
|
|
113
|
+ message += F("text-shadow: -1px 2px 1px rgba(0,0,0,.1);\n");
|
|
114
|
+ message += F("}\n");
|
|
115
|
+ message += F("</style>\n");
|
77
|
116
|
|
78
|
|
- message += F(" ----------------------\n");
|
|
117
|
+ message += F("</head><body>\n");
|
|
118
|
+ message += F("<h1>Gieß-o-mat</h1>\n");
|
|
119
|
+
|
|
120
|
+ message += F("<div class='container'>\n");
|
|
121
|
+ message += F("<div class='ui'>\n");
|
|
122
|
+ message += F("<pre class='lcd'>\n");
|
|
123
|
+ message += message_buffer_a + '\n';
|
|
124
|
+ message += message_buffer_b + '\n';
|
|
125
|
+ message += message_buffer_c + '\n';
|
|
126
|
+ message += message_buffer_d + '\n';
|
79
|
127
|
message += F("</pre>\n");
|
80
|
128
|
|
81
|
|
- message += F("\n<p>\n");
|
|
129
|
+ message += F("<form class='pad'>\n");
|
|
130
|
+ message += F("<input type='button' value='1'>");
|
|
131
|
+ message += F("<input type='button' value='2'>");
|
|
132
|
+ message += F("<input type='button' value='3'>");
|
|
133
|
+ message += F("<br>\n");
|
|
134
|
+ message += F("<input type='button' value='4'>");
|
|
135
|
+ message += F("<input type='button' value='5'>");
|
|
136
|
+ message += F("<input type='button' value='6'>");
|
|
137
|
+ message += F("<br>\n");
|
|
138
|
+ message += F("<input type='button' value='7'>");
|
|
139
|
+ message += F("<input type='button' value='8'>");
|
|
140
|
+ message += F("<input type='button' value='9'>");
|
|
141
|
+ message += F("<br>\n");
|
|
142
|
+ message += F("<input type='button' value='*'>");
|
|
143
|
+ message += F("<input type='button' value='0'>");
|
|
144
|
+ message += F("<input type='button' value='#'>");
|
|
145
|
+ message += F("</form>\n");
|
|
146
|
+
|
|
147
|
+ message += F("<p id='state'>\n");
|
82
|
148
|
message += F("State: ");
|
83
|
|
- // TODO
|
84
|
|
- message += F("\n</p>\n");
|
|
149
|
+ message += control_state_name();
|
|
150
|
+ message += F("</p></div>\n");
|
85
|
151
|
|
86
|
|
- message += F("\n<p>\n");
|
|
152
|
+ message += F("<div class='info'><p>\n");
|
87
|
153
|
message += F("Version: ");
|
88
|
154
|
message += FIRMWARE_VERSION;
|
89
|
155
|
message += F("\n<br>\n");
|
|
@@ -134,13 +200,50 @@ void handleRoot() {
|
134
|
200
|
#endif
|
135
|
201
|
|
136
|
202
|
message += F("<p>\n");
|
137
|
|
- message += F("Try <a href=\"/update\">/update</a> for OTA firmware updates!\n");
|
138
|
|
- message += F("</p>\n");
|
139
|
|
- message += F("</body></html>\n");
|
|
203
|
+ message += F("Try <a href='/update'>/update</a> for OTA firmware updates!\n");
|
|
204
|
+ message += F("</p></div>\n");
|
|
205
|
+ message += F("</div></body>\n");
|
|
206
|
+
|
|
207
|
+ message += F("<script type='text/javascript'>\n");
|
|
208
|
+ message += F("var socket = new WebSocket('ws://' + window.location.hostname + ':81');\n");
|
|
209
|
+ message += F("socket.onmessage = function(e) {\n");
|
|
210
|
+ message += F( "var msg = JSON.parse(e.data);\n");
|
|
211
|
+ message += F( "var str = msg.a + '\\n' + msg.b + '\\n' + msg.c + '\\n' + msg.d;\n");
|
|
212
|
+ message += F( "console.log(str);\n");
|
|
213
|
+ message += F( "var lcd = document.getElementsByClassName('lcd');\n");
|
|
214
|
+ message += F( "lcd[0].innerHTML = str;\n");
|
|
215
|
+ message += F( "var state = document.getElementById('state');\n");
|
|
216
|
+ message += F( "state.innerHTML = \"State: \" + msg.state;\n");
|
|
217
|
+ message += F("};\n");
|
|
218
|
+
|
|
219
|
+ message += F("var buttons = document.getElementsByTagName('input');\n");
|
|
220
|
+ message += F("for (let i = 0; i < buttons.length; i++) {\n");
|
|
221
|
+ message += F( "buttons[i].addEventListener('click', updateButton);\n");
|
|
222
|
+ message += F("}\n");
|
|
223
|
+ message += F("function updateButton() {\n");
|
|
224
|
+ message += F( "socket.send(this.value);\n");
|
|
225
|
+ message += F("}\n");
|
|
226
|
+ message += F("</script>\n");
|
|
227
|
+ message += F("</html>\n");
|
140
|
228
|
|
141
|
229
|
server.send(200, "text/html", message);
|
142
|
230
|
}
|
143
|
231
|
|
|
232
|
+void webSocketEvent(uint8_t num, WStype_t type, uint8_t *payload, size_t length) {
|
|
233
|
+ if ((type != WStype_TEXT) || (length != 1)) {
|
|
234
|
+ return;
|
|
235
|
+ }
|
|
236
|
+
|
|
237
|
+ char c = payload[0];
|
|
238
|
+ if ((c >= '0') && (c <= '9')) {
|
|
239
|
+ control_act_input(c - '0');
|
|
240
|
+ } else if (c == '*') {
|
|
241
|
+ control_act_input(-1);
|
|
242
|
+ } else if (c == '#') {
|
|
243
|
+ control_act_input(-2);
|
|
244
|
+ }
|
|
245
|
+}
|
|
246
|
+
|
144
|
247
|
void wifi_setup() {
|
145
|
248
|
// Build hostname string
|
146
|
249
|
String hostname = "giess-o-mat";
|
|
@@ -205,6 +308,9 @@ void wifi_setup() {
|
205
|
308
|
server.begin();
|
206
|
309
|
MDNS.addService("http", "tcp", 80);
|
207
|
310
|
|
|
311
|
+ socket.begin();
|
|
312
|
+ socket.onEvent(webSocketEvent);
|
|
313
|
+
|
208
|
314
|
Serial.println("WiFi: setup done");
|
209
|
315
|
}
|
210
|
316
|
|
|
@@ -212,6 +318,7 @@ void wifi_run() {
|
212
|
318
|
if ((millis() - last_server_handle_time) >= SERVER_HANDLE_INTERVAL) {
|
213
|
319
|
last_server_handle_time = millis();
|
214
|
320
|
server.handleClient();
|
|
321
|
+ socket.loop();
|
215
|
322
|
|
216
|
323
|
#ifdef ARDUINO_ARCH_ESP8266
|
217
|
324
|
MDNS.update();
|