This repository has been archived by the owner on Jul 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebsocketserver.ino
316 lines (298 loc) · 17.9 KB
/
websocketserver.ino
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#include <WiFiNINA.h>
#include <WebSocketServer.h>
#include <Arduino_LSM6DS3.h>
#include <Base64.h>
#include <RTCZero.h>
#include "wifi_secrets.h"
//#define DEBUG 1
#ifdef DEBUG
#define Sprint(a) (Serial.print(a))
#define Sprintln(a) (Serial.println(a))
#else
#define Sprint(a)
#define Sprintln(a)
#endif
char ssid[] = SECRET_SSID;
char pass[] = SECRET_PASS;
const int IPLastByte = 99;
const int webPort = 80;
const int socketPort = 8080;
WiFiServer webServer(webPort);
WiFiServer socketServer(socketPort);
WebSocketServer webSocketServer;
WiFiClient socketClient;
RTCZero rtc;
const int ledPin = 12;
boolean on = false;
float volume = 0;
int cowbell = 1000;
float zdata[] = {1,1,1,1,1};
int zdatasize = sizeof(zdata) / sizeof(zdata[0]);
float tdata[] = {20,20,20,20,20,20,20,20,20,20};
int tdatasize = sizeof(tdata) / sizeof(tdata[0]);
int ledState = 0;
unsigned long epoch;
unsigned long previousCowbellMillis = 0;
unsigned long previousGravityMillis = 0;
unsigned long previousTemperatureMillis = 0;
unsigned long previousClockMillis = 0;
unsigned long previousAdjustMillis = 0;
char interface_gz_base64[] = "H4sICO6yoF0CAHdlYnNvY2tldHNlcnZlci5odG1sAOxYe2/TMBD/H4nvEEATIMj70WQwJKdvYNDAxmOIP9LUedA8SuI2XRHfnXOcNekKiPGSkPBWx3e+O//ufBdHfnij97x78nbS50KSxI+uX3tIn1zspsHRTZzeBA7HPQyxO6tGMCYRifGjcUpw7rsefigyRj2bYOJyXujmBSZHN5fE582bF3NxlM65HMdHN4swy4m3JFzkZelNLsyxf3Rz5hL3MErcAItrnk48mLoFNrT7CCEboT7qQ0+fY2RndDgIoOvSDjnwA/7FfKvZx0+hP+mW6MrtrBzLduKMjp9aL5Gd2Ohn2ugteemYDT08Rcf5IEW9E/EMoTJwLsuj2LEfgwvF9KREp6MW7pcOOj5LUeqMB+XwdPR6ijJTVMonk4mVoN5EPAvQpqaL7oeNuJ3fo1t4Bqdo1JNPkP1xBP5KQ8VDz16Tlj3Au7JI92QjuedIL15p6Ek3DFC3DKfB/N7gzTh2guNLTthz1EPrM7u/7o8d5U1vltk9Xz5z0LmovEXHohWj3lNxGpT37qnO/EXY//F4DrzuGL0I7GgD+4Pk2h7Di9BaVJ2zF3k0OJa14Y8bhXzavB1TfBaz1/jP6BSdvNDQFVsq30POcD58Jobg74jZa9uH9YLgx+0tkmTcHXvym8KO0EuR2WvF03VOVXExGE5H8x83OjQ9x7Y/9nofNLY/e/73nk0S+3jw9s0V4vkc2aeT1yWKzWGAnvlte0CvrDPUHaEXDh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72yV9vMhrMY/PhYdK9i6a/MuJgUO7tuV5O2Qbq1mGwtSEYfADkOCgLeeazBmKGPcAWvxTBvD6Z6XRfjed7l6eGB0F/sl5j8O99mC9mw58plwiNRs6HiovTq9Sw3ye+dV3/v8k/ALQ/CglHeQAA";
void setup() {
#ifdef DEBUG
Serial.begin(115200);
while (!Serial);
#endif
pinMode(LED_BUILTIN, OUTPUT);
pinMode(ledPin, OUTPUT);
rtc.begin();
IMU.begin();
WiFiConnect();
}
void loop() {
if (WiFi.status() != WL_CONNECTED) {
digitalWrite(LED_BUILTIN, LOW);
Sprintln("\n--Lost WiFi connection");
WiFi.end();
WiFiConnect();
}
WiFiClient webClient = webServer.available();
if (webClient.connected()) {
Sprint("\n--New client: "); Sprint(webClient.remoteIP()); Sprint(":"); Sprintln(webClient.remotePort());
String header = "";
unsigned long requestStartMillis = millis();
while (webClient.available()) {
unsigned long requestCurrentMillis = millis();
char c = webClient.read();
if (c != '\r') {
header += c;
}
if ((requestCurrentMillis - requestStartMillis) > 5000) {
Sprintln("--Request is taking too long");
webClient.println("HTTP/1.1 408 Request Timeout\nConnection: close\nContent-Type: text/plain; charset=utf-8\n\n408 Request Timeout\n");
break;
} else if (header.length() == 3 && header != "GET") {
Sprintln("--Wrong method in header");
webClient.println("HTTP/1.1 405 Method Not Allowed\nAllow: GET\nConnection: close\nContent-Type: text/plain; charset=utf-8\n\n405 Method not allowed; GET only\n");
break;
} else if (header.indexOf("pgrade") > -1 && header.indexOf("ebsocket") > -1) {
Sprintln("--Websocket upgrade requested on webserver port");
webClient.println("HTTP/1.1 406 Not Acceptable\nConnection: close\nContent-Type: text/plain; charset=utf-8\n\n406 Not Acceptable; websocket not available on this port\n");
break;
} else if (header.indexOf("event-stream") > -1) {
Sprintln("--SSE content type request in header");
webClient.println("HTTP/1.1 406 Not Acceptable\nConnection: close\nContent-Type: text/plain; charset=utf-8\n\n406 Not Acceptable; SSE not available\n");
break;
} else if (header.length() > 512) {
Sprintln("--Header too long");
webClient.println("HTTP/1.1 431 Request Header Fields Too Large\nConnection: close\nContent-Type: text/plain; charset=utf-8\n\n431 Request header fields too large\n");
break;
} else if (!webClient.available() && (header.length() <= 10 || (header.length() > 10 && header.substring(header.length() - 2) != "\n\n"))) {
Sprintln("--Incomplete header");
webClient.println("HTTP/1.1 400 Bad Request\nConnection: close\nContent-Type: text/plain; charset=utf-8\n\n400 Bad Request; incomplete header\n");
break;
} else if (header.substring(header.length() - 2) == "\n\n") {
Sprint(header.substring(0, header.length() - 1));
if (header.indexOf("GET / HTTP") > -1) {
sendBase64Page(interface_gz_base64, webClient, 1024);
Sprintln("--Interface webpage sent");
} else if (header.indexOf("GET /author HTTP") > -1) {
webClient.println("HTTP/1.1 200 OK\nConnection: close\nContent-Type: text/plain; charset=utf-8\n\nOscar den Uijl, [email protected]\n");
Sprintln("--Email address sent");
} else if (header.indexOf("GET /coffee HTTP") > -1) {
webClient.println("HTTP/1.1 418 I'm a teapot\nConnection: close\nContent-Type: text/plain; charset=utf-8\n\n418 I'm a teapot\n");
Sprintln("--Coffee request denied");
} else {
webClient.println("HTTP/1.1 404 Not Found\nConnection: close\nContent-Type: text/plain; charset=utf-8\n\n404 Not Found\n");
Sprintln("--Page not found");
}
}
}
webClient.stop();
Sprintln("--Client disconnected");
return;
}
if (!socketClient.connected()) {
socketClient = socketServer.available();
if (socketClient.connected() && webSocketServer.handshake(socketClient)) {
Sprint("\n--Websocket connected to: "); Sprint(socketClient.remoteIP()); Sprint(":"); Sprintln(socketClient.remotePort());
if (on) {
webSocketServer.sendData("sw:true");
} else {
webSocketServer.sendData("sw:false");
}
webSocketServer.sendData("volume:" + (String)round((volume*100)/255));
webSocketServer.sendData("cowbell:" + (String)((1000-cowbell)/10));
Sprintln("--Settings sent");
} else {
socketClient.stop();
}
}
if (socketClient.connected()) {
String data = webSocketServer.getData();
if (data.length() > 0) {
String name = data.substring(0, data.indexOf(":"));
String value = data.substring(data.indexOf(":") + 1);
boolean goodSettings = true;
if (name == "switch") {
on = (value == "true");
} else if (name == "volume") {
volume = (value.toFloat()*255)/100;
if (cowbell == 1000) {
analogWrite(ledPin, (int)round(volume));
}
} else if (name == "cowbell") {
cowbell = 1000 - (value.toInt()*10);
} else {
goodSettings = false;
webSocketServer.sendData("message:Bad data; ignored");
}
if (goodSettings) {
webSocketServer.sendData("message:" + name + " set to " + value);
}
}
}
unsigned long currentCowbellMillis = millis();
if ((currentCowbellMillis - previousCowbellMillis) > cowbell) {
previousCowbellMillis = currentCowbellMillis;
if (on) {
if (cowbell == 1000) {
analogWrite(ledPin, (int) round(volume));
} else {
if (ledState == 0) {
ledState = 1;
analogWrite(ledPin, (int) round(volume));
} else {
ledState = 0;
analogWrite(ledPin, 0);
}
}
} else {
analogWrite(ledPin, 0);
}
}
float t, tsum = 0;
if (IMU.temperatureAvailable()) {
IMU.readTemperature(t);
for (int i = 0; i < tdatasize - 1; i++) {
tdata[i] = tdata[i+1];
}
tdata[tdatasize-1] = t;
for (int i = 0; i < tdatasize; i++) {
tsum += tdata[i];
}
if (socketClient.connected()) {
unsigned long currentTemperatureMillis = millis();
if ((currentTemperatureMillis - previousTemperatureMillis) > 5000) {
previousTemperatureMillis = currentTemperatureMillis;
webSocketServer.sendData("temperature:" + String(tsum/tdatasize, 3));
}
}
}
float x, y, z, zsum = 0;
if (IMU.accelerationAvailable()) {
IMU.readAcceleration(x, y, z);
if (x > 0.2 || x < -0.2 || y > 0.2 || y < -0.2 || z > 1.2 || z < 0.8) {
analogWrite(ledPin, 0);
Sprintln("\n--TILT!!");
if (on) {
on = false;
if (socketClient.connected()) {
webSocketServer.sendData("sw:false");
}
}
}
for (int i = 0; i < zdatasize - 1; i++) {
zdata[i] = zdata[i+1];
}
zdata[zdatasize-1] = z;
for (int i = 0; i < zdatasize; i++) {
zsum += zdata[i];
}
if (socketClient.connected()) {
unsigned long currentGravityMillis = millis();
if ((currentGravityMillis - previousGravityMillis) > 2000) {
previousGravityMillis = currentGravityMillis;
webSocketServer.sendData("gravity:" + String(zsum/zdatasize, 3));
}
}
}
unsigned long currentClockMillis = millis();
if (socketClient.connected()) {
if ((currentClockMillis - previousClockMillis) > 1000) {
previousClockMillis = currentClockMillis;
webSocketServer.sendData("time:" + String(rtc.getEpoch()));
}
}
unsigned long currentAdjustMillis = millis();
if ((currentAdjustMillis - previousAdjustMillis) > 300000) {
previousAdjustMillis = currentAdjustMillis;
epoch = WiFi.getTime();
if (epoch > 0) {
rtc.setEpoch(epoch);
Sprintln("\n--RTC adjusted");
};
}
}
void WiFiConnect() {
WiFi.setHostname("nano-webserver");
while (WiFi.status() != WL_CONNECTED) {
Sprintln("Connecting to " + (String)ssid + " ...");
WiFi.begin(ssid, pass);
delay(5000);
}
IPAddress IP = WiFi.localIP();
IP[3] = IPLastByte;
WiFi.config(IP, WiFi.gatewayIP(), WiFi.gatewayIP(), WiFi.subnetMask());
Sprintln("Connected to " + (String)ssid);
int tries = 5;
do {
Sprintln("Getting NTP time ...");
epoch = WiFi.getTime();
tries--;
delay(500);
} while (epoch == 0 && tries > 0);
if (tries == 0) {
Sprintln("Failed to get NTP time");
} else {
rtc.setEpoch(epoch);
Sprintln("RTC set to NTP time");
}
webServer.begin();
socketServer.begin();
#ifdef DEBUG
printWifiStatus();
#endif
WiFi.lowPowerMode();
digitalWrite(LED_BUILTIN, HIGH);
}
void sendBase64Page(char base64Page[], WiFiClient client, int packetSize) {
const int base64Page_length = strlen(base64Page);
const int page_length = base64_dec_len(base64Page, base64Page_length);
char page[page_length];
int done = 0;
base64_decode(page, base64Page, base64Page_length);
client.println("HTTP/1.1 200 OK\nConnection: close\nContent-Type: text/html");
if (page[0] == 0x1f && page[1] == 0x8b) {
client.println("Content-Encoding: gzip");
}
client.println("");
while (page_length > done) {
if (page_length - done < packetSize) {
packetSize = page_length - done;
}
client.write(page + done, packetSize * sizeof(char));
done = done + packetSize;
}
}
#ifdef DEBUG
void printWifiStatus() {
Sprint("SSID: "); Sprintln(WiFi.SSID());
Sprint("Signal strength (RSSI): "); Sprint(WiFi.RSSI()); Sprintln(" dBm");
Sprint("IP address: "); Sprintln(WiFi.localIP());
Sprint("Gateway: "); Sprintln(WiFi.gatewayIP());
Sprint("Netmask: "); Sprintln(WiFi.subnetMask());
Sprint("Webserver is at http://"); Sprint(WiFi.localIP()); Sprintln(":" + (String)webPort + "/");
Sprint("Websocket is at http://"); Sprint(WiFi.localIP()); Sprintln(":" + (String)socketPort + "/");
}
#endif