This repository has been archived by the owner on Feb 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
MultiServers.ino
287 lines (231 loc) · 12 KB
/
MultiServers.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
/****************************************************************************************************************************
MultiServers.ino
For boards with WiFiNINA module/shield.
by Oscar den Uijl (https://github.com/ocrdu) in (https://github.com/arduino-libraries/WiFiNINA/issues/87)
Based on and modified from WiFiNINA library https://www.arduino.cc/en/Reference/WiFiNINA
to support nRF52, SAMD21/SAMD51, STM32F/L/H/G/WB/MP1, Teensy, etc. boards besides Nano-33 IoT, MKRWIFI1010, MKRVIDOR400, etc.
Built by Khoi Hoang https://github.com/khoih-prog/WiFiNINA_Generic
Licensed under MIT license
Copyright (c) 2018 Arduino SA. All rights reserved.
Copyright (c) 2011-2014 Arduino LLC. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*****************************************************************************************************************************/
#define _WIFININA_LOGLEVEL_ 1
// To eliminate FW warning when using not latest nina-fw version
// To use whenever WiFi101-FirmwareUpdater-Plugin is not sync'ed with nina-fw version
#define WIFI_FIRMWARE_LATEST_VERSION "1.4.8"
#include "defines.h"
#include <SPI.h>
#if USING_WIFI101
#include <WiFi101_Generic.h>
#else
#include <WiFiNINA_Generic.h>
#endif
// https://github.com/ocrdu/NINA-Websocket
#include <WebSocketServer.h>
//////
#include <Base64.h>
#include "arduino_secrets.h"
char ssid[] = SECRET_SSID;
char pass[] = SECRET_PASS;
#define Sprint(a) (Serial.print(a))
#define Sprintln(a) (Serial.println(a))
const int IPLastByte = 99;
const int webPort = 80;
const int socketPort = 8080;
WiFiServer webServer(webPort);
WiFiServer socketServer(socketPort);
WebSocketServer webSocketServer;
WiFiClient socketClient;
boolean on = false;
char webpage_base64[] =
"H4sICEGBKl8CAFdlYnBhZ2VTZXJ2ZWRCeUFyZHVpbm8uaHRtbADsWelu00AQ/o/EO5ggLoHvIzZQpHVuoBBDufrPsdcH8RFsN06DeHdmvRvshKucEhJWa8/MfjM7M7vjdaf3rwyfDk7ezEdcVKXJg8uX7pMnl7hZeNTDWQ8kHHc/wq7fUEBXcZXgB7OswkXgevi+SAVsNMWVy3mRW5S4OuqdVQFv9nZjSZwtuQInR70yyovKO6u42MuzHhcVODjq+W7l3o1TN8TihicD9xZuiQ3tDkLIRmiERnAnzxmyc0KOQ7gNyA058Avy3Xjnso8fw/1kUKMfvk7rmWynzvT4sfUc2amNfuaavqmeO2bLT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uogOIzFULH3ZB9CvxoeNuvNyH2Kbd2nbNM4cuZD92G2q8nTh80Wzexs0IIC9lf+mzjXAG4foI6Ss9kOIe9NUmhxXYrxVIsjxkL3UKt/OoRwTqCe2PRCn2fDPoO+q7PZZgPnP/hY1YvRj8AlQ+Ph9xPdHSYJ8bOJOxP/H6PfAJuWEF5BOgAA";
void printWifiStatus()
{
Sprint(F("SSID: "));
Sprintln(WiFi.SSID());
Sprint(F("Signal strength (RSSI): "));
Sprint(WiFi.RSSI());
Sprintln(F(" dBm"));
Sprint(F("IP address: "));
Sprintln(WiFi.localIP());
Sprint(F("Gateway: "));
Sprintln(WiFi.gatewayIP());
Sprint(F("Netmask: "));
Sprintln(WiFi.subnetMask());
Sprint(F("Webpage is at http://"));
Sprint(WiFi.localIP());
Sprintln(F("/"));
Sprint(F("Websocket is at http://"));
Sprint(WiFi.localIP());
Sprintln(":" + (String)socketPort + "/");
}
void WiFiConnect()
{
while (WiFi.status() != WL_CONNECTED)
{
Sprint(F("Connecting to "));
Sprintln(ssid);
WiFi.begin(ssid, pass);
delay(5000);
}
IPAddress IP = WiFi.localIP();
IP[3] = IPLastByte;
WiFi.config(IP, WiFi.gatewayIP(), WiFi.gatewayIP(), WiFi.subnetMask());
Sprint(F("Connected to "));
Sprintln(ssid);
webServer.begin();
socketServer.begin();
printWifiStatus();
WiFi.lowPowerMode();
digitalWrite(LED_BUILTIN, HIGH);
}
void setup()
{
//Initialize serial and wait for port to open:
Serial.begin(115200);
while (!Serial && millis() < 5000);
Serial.print(F("\nStart MultiServers on "));
Serial.println(BOARD_TYPE);
// check for the WiFi module:
#if USING_WIFI101
if (WiFi.status() == WL_NO_SHIELD)
#else
Serial.println(WIFININA_GENERIC_VERSION);
if (WiFi.status() == WL_NO_MODULE)
#endif
{
Serial.println(F("Communication with WiFi module failed!"));
// don't continue
while (true);
}
String fv = WiFi.firmwareVersion();
if (fv < WIFI_FIRMWARE_LATEST_VERSION)
{
Serial.print(F("Your current firmware NINA FW v"));
Serial.println(fv);
Serial.print(F("Please upgrade the firmware to NINA FW v"));
Serial.println(WIFI_FIRMWARE_LATEST_VERSION);
}
pinMode(LED_BUILTIN, OUTPUT);
WiFiConnect();
}
void loop()
{
if (WiFi.status() != WL_CONNECTED)
{
digitalWrite(LED_BUILTIN, LOW);
Sprintln(F("\n--Lost WiFi connection"));
WiFi.end();
WiFiConnect();
}
//if (!socketClient.connected()) {
WiFiClient webClient = webServer.available();
if (webClient.connected())
{
Sprint(F("\n--New client: "));
Sprint(webClient.remoteIP());
Sprint(F(":"));
Sprintln(webClient.remotePort());
String header = "";
while (webClient.available())
{
char c = webClient.read();
if (c != '\r')
{
header += c;
}
if (header.substring(header.length() - 2) == "\n\n")
{
Sprint(header.substring(0, header.length() - 1));
if (header.indexOf("GET / HTTP") > -1)
{
const int webpage_base64_length = sizeof(webpage_base64);
const int webpage_gz_length = base64_dec_len(webpage_base64, webpage_base64_length);
char webpage_gz[webpage_gz_length];
base64_decode(webpage_gz, webpage_base64, webpage_base64_length);
int packetsize = 1024;
int done = 0;
webClient.println("HTTP/1.1 200 OK\nContent-Type: text/html\nContent-Encoding: gzip\n");
while (webpage_gz_length > done)
{
if (webpage_gz_length - done < packetsize)
{
packetsize = webpage_gz_length - done;
}
webClient.write(webpage_gz + done, packetsize * sizeof(char));
done = done + packetsize;
}
Sprintln(F("--Interface webpage sent"));
}
else
{
webClient.println("HTTP/1.1 404 Not Found\nContent-Type: text/plain; charset=utf-8\n\n404 Not Found\n");
Sprintln(F("--Page not found"));
}
webClient.stop();
Sprintln(F("--Client disconnected"));
}
}
}
//}
if (!socketClient.connected())
{
socketClient = socketServer.available();
if (socketClient.connected() && webSocketServer.handshake(socketClient))
{
Sprint(F("\n--Websocket connected to: "));
Sprint(socketClient.remoteIP());
Sprint(F(":"));
Sprintln(socketClient.remotePort());
if (on)
{
webSocketServer.sendData("sw:true");
}
else
{
webSocketServer.sendData("sw:false");
}
Sprintln(F("\n--Settings sent"));
}
else
{
//Sprintln(F("\n--Couldn't connect websocket"));
socketClient.stop();
delay(100);
}
}
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
{
goodSettings = false;
webSocketServer.sendData("message:Bad data; ignored");
}
if (goodSettings)
{
webSocketServer.sendData("message:" + name + " set to " + value);
}
}
}
}