-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathT002_LaserTankBase64.ino
273 lines (239 loc) · 11.2 KB
/
T002_LaserTankBase64.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
#include <global.h>
#include <base64.h>
#include <WebSocketClient.h>
#include <WebSocketServer.h>
#define _WIFININA_LOGLEVEL_ 1
#include <WiFiNINA_Generic.h>
#include "arduino_secrets_private.h"
#include "network_parameters.h"
// global var
char ssid[] = SECRET_SSID;
char pass[] = SECRET_PASS;
const short webPort = 80;
const short socketPort = 8080;
// Background
boolean Backgroundinit = true;
boolean BackgroundHearBeat = false;
// WIFI objects
WiFiServer webServer(webPort);
WiFiServer socketServer(socketPort);
WebSocketServer webSocketServer;
WiFiClient socketClient;
// Laser Tank webpage, gzipped and base64 encoding to save space
char webpage_base64[] = "H4sICACdJmAEAHdlYnBhZ2UuaHRtbADtXGtv0zAU/c6vMEGCTrC16QNGm1UCBgKJAaIDhAChNHG7iDSOEocxEP+d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FrRQgAA";
void printWifiStatus()
{
Serial.print("SSID: "); Serial.println(WiFi.SSID());
Serial.print("Signal strength (RSSI): "); Serial.print(WiFi.RSSI()); Serial.println(" dBm");
Serial.print("IP address: "); Serial.println(WiFi.localIP());
Serial.print("Gateway: "); Serial.println(WiFi.gatewayIP());
Serial.print("Netmask: "); Serial.println(WiFi.subnetMask());
Serial.print("Webpage is at http://"); Serial.print(WiFi.localIP()); Serial.println("/");
Serial.print("Websocket is at http://"); Serial.print(WiFi.localIP()); Serial.println(":" + (String)socketPort + "/");
}
void WiFiConnect()
{
while (WiFi.status() != WL_CONNECTED)
{
Serial.println("Connecting to " + (String)ssid + " ...");
WiFi.begin(ssid, pass);
delay(5000);
}
IPAddress IP = WiFi.localIP();
IP[0] = MYHOME_IP_OCT1;
IP[1] = MYHOME_IP_OCT2;
IP[2] = MYHOME_IP_OCT3;
IP[3] = MYHOME_IP_OCT4;
WiFi.config(IP, WiFi.gatewayIP(), WiFi.gatewayIP(), WiFi.subnetMask());
Serial.println("Connected to " + (String)ssid);
webServer.begin();
socketServer.begin();
printWifiStatus();
WiFi.lowPowerMode();
}
void setup()
{
// we use this led to show the background is running
// flash on and off every time we go through the background loop
pinMode(LED_BUILTIN, OUTPUT);
// Serial port initialization
Serial.begin(57600);
Serial.println("\nStart MultiServers");
Serial.println("Version " + String(WIFININA_GENERIC_VERSION));
String fv = WiFi.firmwareVersion();
if (fv < WIFI_FIRMWARE_LATEST_VERSION)
{
Serial.print("Your current firmware NINA FW v");
Serial.println(fv);
Serial.print("Please upgrade the firmware to NINA FW v");
Serial.println(WIFI_FIRMWARE_LATEST_VERSION);
}
// setup complete
digitalWrite(LED_BUILTIN, HIGH);
}
void loop()
{
if(WiFi.status() != WL_CONNECTED)
{
Serial.println("Lost WiFi connection");
WiFi.end();
WiFiConnect();
}
WiFiClient webClient = webServer.available();
if(webClient.connected())
{
Serial.print("New client: "); Serial.print(webClient.remoteIP()); Serial.print(":"); Serial.println(webClient.remotePort());
String header = "";
while(webClient.available())
{
char ch = webClient.read();
if (ch != '\r')
{
header += ch;
}
if (header.substring(header.length() - 2) == "\n\n")
{
Serial.print(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;
}
Serial.println("--Interface webpage sent");
}
else
{
webClient.println("HTTP/1.1 404 Not Found\nContent-Type: text/plain; charset=utf-8\n\n404 Not Found\n");
Serial.println("--Page not found");
}
webClient.stop();
Serial.println("--Client disconnected");
}
}
}
if(!socketClient.connected())
{
socketClient = socketServer.available();
if (socketClient.connected() && webSocketServer.handshake(socketClient))
{
Serial.print("Websocket connected to: ");
Serial.print(socketClient.remoteIP());
Serial.print(":");
Serial.println(socketClient.remotePort());
}
else
{
socketClient.stop();
delay(100);
}
}
if(socketClient.connected())
{
// Background Init - setup the background tasks, runs only once
if(Backgroundinit == true) {
Backgroundinit = false;
String data = webSocketServer.getData();
Serial.println("Websocket Flushed");
Serial.println("Background Init Complete");
}
// Background Process 1
// see if we have a command/request from the user
String data = webSocketServer.getData();
if (data.length() > 0)
{
String cmd = data.substring(0, data.indexOf(":"));
String setting = data.substring(data.indexOf(":") + 1);
// process command
switch (cmd.toInt()) {
case 1:
// Forward
break;
case 2:
// Reverse
break;
case 3:
// Max Forward
break;
case 4:
// Stop
break;
case 5:
// Max Reverse
break;
case 6:
// Left
break;
case 7:
// Right
break;
case 8:
// Max Left
break;
case 9:
// Straight
break;
case 10:
// Max Right
break;
case 11:
// Target LED
webSocketServer.sendData("R:Laser Targeting");
break;
case 12:
// Laser Left
break;
case 13:
// Laser Right
break;
case 14:
// Laser Up
break;
case 15:
// Laser Down
break;
case 16:
// Fire
webSocketServer.sendData("R:Laser Fired");
break;
case 17:
// Send request/command to secondary processor
break;
case 18:
// This is the -Battery+ button
break;
case 19:
// Process a Primary command
break;
case 20:
// Speed UP
break;
case 21:
// Slow Down
break;
default:
webSocketServer.sendData("E:" + cmd + " - " + setting);
break;
}
}
}
// Background Process 2
// show the background heart
BackgroundHearBeat = !BackgroundHearBeat;
if(BackgroundHearBeat) {
digitalWrite(LED_BUILTIN, LOW);
} else {
digitalWrite(LED_BUILTIN, HIGH);
}
}