Skip to content
This repository was archived by the owner on Jan 31, 2024. It is now read-only.

Commit 38b4626

Browse files
authored
v1.5.0 fixing issue with slow browsers, etc.
### Releases v1.5.0 1. Fix issue with slow browsers or network. Check [Target stops responding after variable time when using Firefox on Windows 10 #3](khoih-prog/AsyncWebServer_RP2040W#3) 2. Add functions and example `Async_AdvancedWebServer_favicon` to support `favicon.ico`
1 parent 775dec9 commit 38b4626

File tree

2 files changed

+325
-0
lines changed

2 files changed

+325
-0
lines changed
Lines changed: 251 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,251 @@
1+
/****************************************************************************************************************************
2+
Async_AdvancedWebServer.ino
3+
4+
For Teensy41 with QNEthernet
5+
6+
AsyncWebServer_Teensy41 is a library for the Teensy41 with QNEthernet
7+
8+
Based on and modified from ESPAsyncWebServer (https://github.com/me-no-dev/ESPAsyncWebServer)
9+
Built by Khoi Hoang https://github.com/khoih-prog/AsyncWebServer_Teensy41
10+
Licensed under GPLv3 license
11+
12+
Copyright (c) 2015, Majenko Technologies
13+
All rights reserved.
14+
15+
Redistribution and use in source and binary forms, with or without modification,
16+
are permitted provided that the following conditions are met:
17+
18+
Redistributions of source code must retain the above copyright notice, this
19+
list of conditions and the following disclaimer.
20+
21+
Redistributions in binary form must reproduce the above copyright notice, this
22+
list of conditions and the following disclaimer in the documentation and/or
23+
other materials provided with the distribution.
24+
25+
Neither the name of Majenko Technologies nor the names of its
26+
contributors may be used to endorse or promote products derived from
27+
this software without specific prior written permission.
28+
29+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
30+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
31+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
32+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
33+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
35+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
36+
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
38+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39+
*****************************************************************************************************************************/
40+
41+
42+
#if !( defined(CORE_TEENSY) && defined(__IMXRT1062__) && defined(ARDUINO_TEENSY41) )
43+
#error Only Teensy 4.1 supported
44+
#endif
45+
46+
// Debug Level from 0 to 4
47+
#define _TEENSY41_ASYNC_TCP_LOGLEVEL_ 1
48+
#define _AWS_TEENSY41_LOGLEVEL_ 1
49+
50+
#define SHIELD_TYPE "Teensy4.1 QNEthernet"
51+
52+
#if (_AWS_TEENSY41_LOGLEVEL_ > 3)
53+
#warning Using QNEthernet lib for Teensy 4.1. Must also use Teensy Packages Patch or error
54+
#endif
55+
56+
#define USING_DHCP true
57+
//#define USING_DHCP false
58+
59+
#if !USING_DHCP
60+
// Set the static IP address to use if the DHCP fails to assign
61+
IPAddress myIP(192, 168, 2, 222);
62+
IPAddress myNetmask(255, 255, 255, 0);
63+
IPAddress myGW(192, 168, 2, 1);
64+
//IPAddress mydnsServer(192, 168, 2, 1);
65+
IPAddress mydnsServer(8, 8, 8, 8);
66+
#endif
67+
68+
#include "QNEthernet.h" // https://github.com/ssilverman/QNEthernet
69+
using namespace qindesign::network;
70+
71+
#include "favicon.h"
72+
#include <AsyncWebServer_Teensy41.h>
73+
74+
AsyncWebServer server(80);
75+
76+
int reqCount = 0; // number of requests received
77+
78+
const int led = 13;
79+
80+
#define BUFFER_SIZE 512
81+
char temp[BUFFER_SIZE];
82+
83+
void handleRoot(AsyncWebServerRequest *request)
84+
{
85+
digitalWrite(led, 1);
86+
87+
int sec = millis() / 1000;
88+
int min = sec / 60;
89+
int hr = min / 60;
90+
int day = hr / 24;
91+
92+
snprintf(temp, BUFFER_SIZE - 1,
93+
"<html>\
94+
<head>\
95+
<meta http-equiv='refresh' content='5'/>\
96+
<title>AsyncWebServer-%s</title>\
97+
<style>\
98+
body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
99+
</style>\
100+
</head>\
101+
<body>\
102+
<h2>AsyncWebServer_Teensy41!</h2>\
103+
<h3>running on %s</h3>\
104+
<p>Uptime: %d d %02d:%02d:%02d</p>\
105+
<img src=\"/test.svg\" />\
106+
</body>\
107+
</html>", BOARD_NAME, BOARD_NAME, day, hr % 24, min % 60, sec % 60);
108+
109+
request->send(200, "text/html", temp);
110+
111+
digitalWrite(led, 0);
112+
}
113+
void drawFavicon(AsyncWebServerRequest *request)
114+
{
115+
AsyncWebServerResponse *response = request->beginResponse(200, "image/x-icon", favicon_ico_gz, favicon_ico_gz_len);
116+
117+
response->addHeader("Content-Encoding", "gzip");
118+
request->send(response);
119+
}
120+
121+
void handleNotFound(AsyncWebServerRequest *request)
122+
{
123+
digitalWrite(led, 1);
124+
String message = "File Not Found\n\n";
125+
126+
message += "URI: ";
127+
message += request->url();
128+
message += "\nMethod: ";
129+
message += (request->method() == HTTP_GET) ? "GET" : "POST";
130+
message += "\nArguments: ";
131+
message += request->args();
132+
message += "\n";
133+
134+
for (uint8_t i = 0; i < request->args(); i++)
135+
{
136+
message += " " + request->argName(i) + ": " + request->arg(i) + "\n";
137+
}
138+
139+
request->send(404, "text/plain", message);
140+
digitalWrite(led, 0);
141+
}
142+
143+
void drawGraph(AsyncWebServerRequest *request)
144+
{
145+
String out;
146+
147+
out.reserve(4000);
148+
char temp[70];
149+
150+
out += "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"310\" height=\"150\">\n";
151+
out += "<rect width=\"310\" height=\"150\" fill=\"rgb(250, 230, 210)\" stroke-width=\"2\" stroke=\"rgb(0, 0, 0)\" />\n";
152+
out += "<g stroke=\"blue\">\n";
153+
int y = rand() % 130;
154+
155+
for (int x = 10; x < 300; x += 10)
156+
{
157+
int y2 = rand() % 130;
158+
sprintf(temp, "<line x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" stroke-width=\"2\" />\n", x, 140 - y, x + 10, 140 - y2);
159+
out += temp;
160+
y = y2;
161+
}
162+
out += "</g>\n</svg>\n";
163+
164+
request->send(200, "image/svg+xml", out);
165+
}
166+
167+
168+
void setup()
169+
{
170+
pinMode(led, OUTPUT);
171+
digitalWrite(led, 0);
172+
173+
Serial.begin(115200);
174+
while (!Serial && millis() < 5000);
175+
176+
delay(200);
177+
178+
Serial.print("\nStart Async_AdvancedWebServer_favicon on "); Serial.print(BOARD_NAME);
179+
Serial.print(" with "); Serial.println(SHIELD_TYPE);
180+
Serial.println(ASYNC_WEBSERVER_TEENSY41_VERSION);
181+
182+
delay(500);
183+
184+
#if USING_DHCP
185+
// Start the Ethernet connection, using DHCP
186+
Serial.print("Initialize Ethernet using DHCP => ");
187+
Ethernet.begin();
188+
#else
189+
// Start the Ethernet connection, using static IP
190+
Serial.print("Initialize Ethernet using static IP => ");
191+
Ethernet.begin(myIP, myNetmask, myGW);
192+
Ethernet.setDNSServerIP(mydnsServer);
193+
#endif
194+
195+
if (!Ethernet.waitForLocalIP(5000))
196+
{
197+
Serial.println(F("Failed to configure Ethernet"));
198+
199+
if (!Ethernet.linkStatus())
200+
{
201+
Serial.println(F("Ethernet cable is not connected."));
202+
}
203+
204+
// Stay here forever
205+
while (true)
206+
{
207+
delay(1);
208+
}
209+
}
210+
else
211+
{
212+
Serial.print(F("Connected! IP address:")); Serial.println(Ethernet.localIP());
213+
}
214+
215+
#if USING_DHCP
216+
delay(1000);
217+
#else
218+
delay(2000);
219+
#endif
220+
221+
server.on("/", HTTP_GET, [](AsyncWebServerRequest * request)
222+
{
223+
handleRoot(request);
224+
});
225+
226+
server.on("/test.svg", HTTP_GET, [](AsyncWebServerRequest * request)
227+
{
228+
drawGraph(request);
229+
});
230+
231+
server.on("/favicon.ico", HTTP_GET, [](AsyncWebServerRequest * request)
232+
{
233+
drawFavicon(request);
234+
});
235+
236+
server.on("/inline", [](AsyncWebServerRequest * request)
237+
{
238+
request->send(200, "text/plain", "This works as well");
239+
});
240+
241+
server.onNotFound(handleNotFound);
242+
243+
server.begin();
244+
245+
Serial.print(F("HTTP EthernetWebServer is @ IP : "));
246+
Serial.println(Ethernet.localIP());
247+
}
248+
249+
void loop()
250+
{
251+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*! \file
2+
\brief Favicon stored in progmem.
3+
4+
`#include "favicon.h"` then
5+
@code
6+
// Serve favicon from PROGMEM: #include <favicon.h>
7+
HTTP.on("/favicon.ico", HTTP_GET, [](AsyncWebServerRequest * request) {
8+
AsyncWebServerResponse *response = request->beginResponse_P(200, "image/x-icon", favicon_ico_gz, favicon_ico_gz_len);
9+
response->addHeader("Content-Encoding", "gzip");
10+
request->send(response);
11+
});
12+
@endcode
13+
*/
14+
15+
#pragma once
16+
17+
/*
18+
19+
Provided from me-no-dev Async WebServer for ESP8266
20+
https://github.com/me-no-dev/ESPAsyncWebServer
21+
22+
*/
23+
24+
//File: favicon.ico.gz, Size: 726
25+
#define favicon_ico_gz_len 726
26+
const uint8_t favicon_ico_gz[] /*PROGMEM*/ =
27+
{
28+
0x1F, 0x8B, 0x08, 0x08, 0x0B, 0x87, 0x90, 0x57, 0x00, 0x03, 0x66, 0x61, 0x76, 0x69, 0x63, 0x6F,
29+
0x6E, 0x2E, 0x69, 0x63, 0x6F, 0x00, 0xCD, 0x53, 0x5F, 0x48, 0x9A, 0x51, 0x14, 0xBF, 0x62, 0x6D,
30+
0x86, 0x96, 0xA9, 0x64, 0xD3, 0xFE, 0xA8, 0x99, 0x65, 0x1A, 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