This repository has been archived by the owner on Jan 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathEsp32-AdvancedWebServer.ino
191 lines (147 loc) · 4.51 KB
/
Esp32-AdvancedWebServer.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
/****************************************************************************************************************************
Esp32-AdvancedWebServer.ino
For Esp32.
Based on and modified from Gil Maimon's ArduinoWebsockets library https://github.com/gilmaimon/ArduinoWebsockets
to support STM32F/L/H/G/WB/MP1, nRF52 and SAMD21/SAMD51 boards besides ESP8266 and ESP32
The library provides simple and easy interface for websockets (Client and Server).
Example first created on: 10.05.2018
Original Author: Markus Sattler
Built by Khoi Hoang https://github.com/khoih-prog/Websockets2_Generic
Licensed under MIT license
*****************************************************************************************************************************/
#include "defines.h"
#include <WebSockets2_Generic.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <ESPmDNS.h>
using namespace websockets2_generic;
WebsocketsServer SocketsServer;
#define WEBSOCKETS_PORT 8080
WebServer server(80);
void handleRoot()
{
#define BUFFER_SIZE 400
char temp[BUFFER_SIZE];
int sec = millis() / 1000;
int min = sec / 60;
int hr = min / 60;
int day = hr / 24;
snprintf(temp, BUFFER_SIZE - 1,
"<html>\
<head>\
<meta http-equiv='refresh' content='5'/>\
<title>AdvancedWebServer %s</title>\
<style>\
body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
</style>\
</head>\
<body>\
<h2>WebSockets&WebServer!</h2>\
<h3>both on %s</h3>\
<p>Uptime: %d d %02d:%02d:%02d</p>\
<img src=\"/test.svg\" />\
</body>\
</html>", BOARD_NAME, BOARD_NAME, day, hr % 24, min % 60, sec % 60);
server.send(200, "text/html", temp);
}
void handleNotFound()
{
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++)
{
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
}
void drawGraph()
{
String out;
out.reserve(3000);
char temp[70];
out += "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"310\" height=\"150\">\n";
out += "<rect width=\"310\" height=\"150\" fill=\"rgb(250, 230, 210)\" stroke-width=\"5\" stroke=\"rgb(0, 0, 0)\" />\n";
out += "<g stroke=\"blue\">\n";
int y = rand() % 130;
for (int x = 15; x < 300; x += 15)
{
int y2 = rand() % 130;
sprintf(temp, "<line x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" stroke-width=\"1\" />\n", x, 140 - y, x + 15, 140 - y2);
out += temp;
y = y2;
}
out += "</g>\n</svg>\n";
server.send(200, "image/svg+xml", out);
}
void setup()
{
Serial.begin(115200);
while (!Serial && millis() < 5000);
Serial.print("\nStart ESP32-AdvancedWebServer on ");
Serial.println(ARDUINO_BOARD);
Serial.println(WEBSOCKETS2_GENERIC_VERSION);
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.config(serverIP, static_GW, static_SN);
WiFi.begin(ssid, password);
// Wait some time to connect to wifi
for (int i = 0; i < 30 && WiFi.status() != WL_CONNECTED; i++)
{
Serial.print(".");
delay(500);
}
if (WiFi.status() == WL_CONNECTED)
{
Serial.print("Connected to Wifi, IP address: ");
Serial.println(WiFi.localIP());
}
else
{
Serial.println("\nNo WiFi");
return;
}
if (MDNS.begin("esp32"))
{
Serial.println("MDNS responder started");
}
SocketsServer.listen(WEBSOCKETS_PORT);
Serial.print(SocketsServer.available() ? "WebSockets Server Running and Ready on " : "Server Not Running on ");
Serial.println(BOARD_NAME);
Serial.print("IP address: ");
Serial.print(WiFi.localIP());
Serial.print(", Port: ");
Serial.println(WEBSOCKETS_PORT); // Websockets Server Port
server.on("/", handleRoot);
server.on("/test.svg", drawGraph);
server.on("/inline", []()
{
server.send(200, "text/plain", "This works as well");
});
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
}
void loop()
{
server.handleClient();
WebsocketsClient client = SocketsServer.accept();
if (client.available())
{
WebsocketsMessage msg = client.readNonBlocking();
// log
Serial.print("Got Message: ");
Serial.println(msg.data());
// return echo
client.send("Echo: " + msg.data());
// close the connection
client.close();
}
}