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

Commit b5edd4d

Browse files
authored
v1.0.0 for WT32_ETH01
### Initial Releases v1.0.0 1. Initial coding to support **WT32_ETH01 or ESP32-based boards using LwIP LAN8720 Ethernet**
1 parent 674ffe8 commit b5edd4d

22 files changed

+1609
-674
lines changed

.codespellrc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# See: https://github.com/codespell-project/codespell#using-a-config-file
2+
[codespell]
3+
# In the event of a false positive, add the problematic word, in all lowercase, to a comma-separated list here:
4+
ignore-words-list = ,
5+
check-filenames =
6+
check-hidden =
7+
skip = ./.git,./src,./examples,./Packages_Patches,./LibraryPatches

CONTRIBUTING.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
## Contributing to AsyncDNSServer_WT32_ETH01
2+
3+
### Reporting Bugs
4+
5+
Please report bugs in AsyncDNSServer_WT32_ETH01 if you find them.
6+
7+
However, before reporting a bug please check through the following:
8+
9+
* [Existing Open Issues](https://github.com/khoih-prog/AsyncDNSServer_WT32_ETH01/issues) - someone might have already encountered this.
10+
11+
If you don't find anything, please [open a new issue](https://github.com/khoih-prog/AsyncDNSServer_WT32_ETH01/issues/new).
12+
13+
### How to submit a bug report
14+
15+
Please ensure to specify the following:
16+
17+
* Arduino IDE version (e.g. 1.8.19) or Platform.io version
18+
* `ESP32` Core Version (e.g. ESP32 v2.0.5)
19+
* Contextual information (e.g. what you were trying to achieve)
20+
* Simplest possible steps to reproduce
21+
* Anything that might be relevant in your opinion, such as:
22+
* Operating system (Windows, Ubuntu, etc.) and the output of `uname -a`
23+
* Network configuration
24+
25+
26+
### Example
27+
28+
```
29+
Arduino IDE version: 1.8.19
30+
ESP32 Core Version 2.0.5
31+
OS: Ubuntu 20.04 LTS
32+
Linux xy-Inspiron-3593 5.15.0-53-generic #59~20.04.1-Ubuntu SMP Thu Oct 20 15:10:22 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
33+
34+
Context:
35+
I encountered a crash while using this library
36+
Steps to reproduce:
37+
1. ...
38+
2. ...
39+
3. ...
40+
4. ...
41+
```
42+
43+
### Additional context
44+
45+
Add any other context about the problem here.
46+
47+
---
48+
49+
### Sending Feature Requests
50+
51+
Feel free to post feature requests. It's helpful if you can explain exactly why the feature would be useful.
52+
53+
There are usually some outstanding feature requests in the [existing issues list](https://github.com/khoih-prog/AsyncDNSServer_WT32_ETH01/issues?q=is%3Aopen+is%3Aissue+label%3Aenhancement), feel free to add comments to them.
54+
55+
---
56+
57+
### Sending Pull Requests
58+
59+
Pull Requests with changes and fixes are also welcome!
60+
61+
Please use the `astyle` to reformat the updated library code as follows (demo for Ubuntu Linux)
62+
63+
1. Change directory to the library GitHub
64+
65+
```
66+
xy@xy-Inspiron-3593:~$ cd Arduino/xy/AsyncDNSServer_WT32_ETH01_GitHub/
67+
xy@xy-Inspiron-3593:~/Arduino/xy/AsyncDNSServer_WT32_ETH01_GitHub$
68+
```
69+
70+
2. Issue astyle command
71+
72+
```
73+
xy@xy-Inspiron-3593:~/Arduino/xy/AsyncDNSServer_WT32_ETH01_GitHub$ bash utils/restyle.sh
74+
```
75+

LICENSE

Lines changed: 21 additions & 674 deletions
Large diffs are not rendered by default.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
Server.h - Base class that provides Server
3+
Copyright (c) 2011 Adrian McEwen. All right reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#ifndef server_h
21+
#define server_h
22+
23+
#include "Print.h"
24+
25+
class Server: public Print
26+
{
27+
public:
28+
// KH, change to fix compiler error for EthernetWebServer
29+
// error: cannot declare field 'EthernetWebServer::_server' to be of abstract type 'EthernetServer'
30+
// virtual void begin(uint16_t port=0) =0;
31+
//virtual void begin() = 0;
32+
void begin() {};
33+
};
34+
35+
#endif

changelog.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# AsyncDNSServer_WT32_ETH01 Library
2+
3+
[![arduino-library-badge](https://www.ardu-badge.com/badge/AsyncDNSServer_WT32_ETH01.svg?)](https://www.ardu-badge.com/AsyncDNSServer_WT32_ETH01)
4+
[![GitHub release](https://img.shields.io/github/release/khoih-prog/AsyncDNSServer_WT32_ETH01.svg)](https://github.com/khoih-prog/AsyncDNSServer_WT32_ETH01/releases)
5+
[![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](#Contributing)
6+
[![GitHub issues](https://img.shields.io/github/issues/khoih-prog/AsyncDNSServer_WT32_ETH01.svg)](http://github.com/khoih-prog/AsyncDNSServer_WT32_ETH01/issues)
7+
8+
9+
<a href="https://www.buymeacoffee.com/khoihprog6" title="Donate to my libraries using BuyMeACoffee"><img src="https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png" alt="Donate to my libraries using BuyMeACoffee" style="height: 50px !important;width: 181px !important;" ></a>
10+
<a href="https://www.buymeacoffee.com/khoihprog6" title="Donate to my libraries using BuyMeACoffee"><img src="https://img.shields.io/badge/buy%20me%20a%20coffee-donate-orange.svg?logo=buy-me-a-coffee&logoColor=FFDD00" style="height: 20px !important;width: 200px !important;" ></a>
11+
<a href="https://profile-counter.glitch.me/khoih-prog/count.svg" title="Total khoih-prog Visitor count"><img src="https://profile-counter.glitch.me/khoih-prog/count.svg" style="height: 30px;width: 200px;"></a>
12+
<a href="https://profile-counter.glitch.me/khoih-prog-AsyncDNSServer_WT32_ETH01/count.svg" title="Visitor count"><img src="https://profile-counter.glitch.me/khoih-prog-AsyncDNSServer_WT32_ETH01/count.svg" style="height: 30px;width: 200px;"></a>
13+
14+
---
15+
---
16+
17+
## Table of Contents
18+
19+
* [Changelog](#changelog)
20+
* [Initial Releases v1.0.0](#initial-releases-v100)
21+
22+
---
23+
---
24+
25+
## Changelog
26+
27+
### Initial Releases v1.0.0
28+
29+
1. Initial coding to support **WT32_ETH01 or ESP32-based boards using LwIP LAN8720 Ethernet**
30+
Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
/****************************************************************************************************************************
2+
AsyncCaptivePortalAdvanced_WT32_ETH01.ino
3+
4+
AsyncDNSServer_WT32_ETH01 is a Async DNS Server library for WT32_ETH01 (ESP32 + LAN8720)
5+
6+
Based on and modified from ESPAsyncDNSServer Library (https://github.com/devyte/ESPAsyncDNSServer)
7+
Built by Khoi Hoang https://github.com/khoih-prog/AsyncDNSServer_WT32_ETH01
8+
Licensed under GPLv3 license
9+
*****************************************************************************************************************************/
10+
11+
/*
12+
This example serves a "hello world".
13+
14+
Now the WT32_ETH01 is in your network. You can reach it through http://192.168.x.x/
15+
16+
This is a captive portal because it will redirect any http request to http://192.168.x.x/
17+
*/
18+
19+
#define ASYNC_DNS_WT32_ETH01_DEBUG_PORT Serial
20+
21+
// Use from 0 to 4. Higher number, more debugging messages and memory usage.
22+
#define _ASYNC_DNS_WT32_ETH01_LOGLEVEL_ 4
23+
24+
/////////////////////////////////////////////
25+
26+
// Select the IP address according to your local network
27+
IPAddress myIP(192, 168, 2, 232);
28+
IPAddress myGW(192, 168, 2, 1);
29+
IPAddress mySN(255, 255, 255, 0);
30+
31+
// Google DNS Server IP
32+
IPAddress myDNS(8, 8, 8, 8);
33+
34+
/////////////////////////////////////////////
35+
36+
#include <AsyncDNSServer_WT32_ETH01.h>
37+
#include <ESPAsyncWebServer.h>
38+
39+
40+
// DNS server
41+
const byte DNS_PORT = 53;
42+
43+
AsyncDNSServer dnsServer;
44+
45+
// Web server
46+
AsyncWebServer server(80);
47+
48+
IPAddress apIP;
49+
50+
/** Is this an IP? */
51+
bool isIp(String str)
52+
{
53+
for (size_t i = 0; i < str.length(); i++)
54+
{
55+
int c = str.charAt(i);
56+
57+
if (c != '.' && (c < '0' || c > '9'))
58+
{
59+
return false;
60+
}
61+
}
62+
63+
return true;
64+
}
65+
66+
/** IP to String? */
67+
String toStringIp(IPAddress ip)
68+
{
69+
String res = "";
70+
71+
for (int i = 0; i < 3; i++)
72+
{
73+
res += String((ip >> (8 * i)) & 0xFF) + ".";
74+
}
75+
76+
res += String(((ip >> 8 * 3)) & 0xFF);
77+
78+
return res;
79+
}
80+
81+
/** Handle root or redirect to captive portal */
82+
void handleRoot(AsyncWebServerRequest * request)
83+
{
84+
if (captivePortal(request))
85+
{
86+
// If captive portal redirect instead of displaying the page.
87+
return;
88+
}
89+
90+
String Page = F(
91+
"<!DOCTYPE html><html lang='en'><head>"
92+
"<meta name='viewport' content='width=device-width'>"
93+
"<title>WT32_ETH01-CaptivePortal</title></head><body>"
94+
"<h1>HELLO WORLD!!</h1>");
95+
96+
Page += "<h2>From " + String(BOARD_NAME) + " using LAN8720A</h2>";
97+
98+
AsyncWebServerResponse *response = request->beginResponse(200, "text/html", Page);
99+
response->addHeader("Cache-Control", "no-cache, no-store, must-revalidate");
100+
response->addHeader("Pragma", "no-cache");
101+
response->addHeader("Expires", "-1");
102+
103+
request->send(response);
104+
}
105+
106+
// Redirect to captive portal if we got a request for another domain.
107+
// Return true in that case so the page handler do not try to handle the request again.
108+
bool captivePortal(AsyncWebServerRequest * request)
109+
{
110+
if (!isIp(request->host()))
111+
{
112+
Serial.println("Request redirected to captive portal");
113+
114+
// Empty content inhibits Content-length header so we have to close the socket ourselves.
115+
AsyncWebServerResponse *response = request->beginResponse(302, "text/plain", "");
116+
response->addHeader("Location", String("http://") + toStringIp(request->client()->localIP()));
117+
118+
request->send(response);
119+
120+
request->client()->stop(); // Stop is needed because we sent no content length
121+
122+
return true;
123+
}
124+
125+
return false;
126+
}
127+
128+
void handleNotFound(AsyncWebServerRequest * request)
129+
{
130+
if (captivePortal(request))
131+
{
132+
// If captive portal redirect instead of displaying the error page.
133+
return;
134+
}
135+
136+
String message = F("File Not Found\n\n");
137+
138+
message += F("URI: ");
139+
message += request->url();
140+
message += F("\nMethod: ");
141+
message += (request->method() == HTTP_GET) ? "GET" : "POST";
142+
message += F("\nArguments: ");
143+
message += request->args();
144+
message += F("\n");
145+
146+
for (uint8_t i = 0; i < request->args(); i++)
147+
{
148+
message += String(F(" ")) + request->argName(i) + F(": ") + request->arg(i) + F("\n");
149+
}
150+
151+
AsyncWebServerResponse *response = request->beginResponse(404, "text/plain", message);
152+
response->addHeader("Cache-Control", "no-cache, no-store, must-revalidate");
153+
response->addHeader("Pragma", "no-cache");
154+
response->addHeader("Expires", "-1");
155+
156+
request->send(response);
157+
}
158+
159+
void setup()
160+
{
161+
Serial.begin(115200);
162+
163+
while (!Serial && (millis() < 5000));
164+
165+
Serial.print("\nStarting AsyncCaptivePortalAdvanced_WT32_ETH01 on " + String(ARDUINO_BOARD));
166+
Serial.println(" with " + String(SHIELD_TYPE));
167+
Serial.println(WEBSERVER_WT32_ETH01_VERSION);
168+
Serial.println(ASYNC_UDP_WT32_ETH01_VERSION);
169+
Serial.println(ASYNC_DNS_SERVER_WT32_ETH01_VERSION);
170+
171+
Serial.setDebugOutput(true);
172+
173+
///////////////////////////////////
174+
175+
/// To be called before ETH.begin()
176+
WT32_ETH01_onEvent();
177+
178+
//bool begin(uint8_t phy_addr=ETH_PHY_ADDR, int power=ETH_PHY_POWER, int mdc=ETH_PHY_MDC, int mdio=ETH_PHY_MDIO,
179+
// eth_phy_type_t type=ETH_PHY_TYPE, eth_clock_mode_t clk_mode=ETH_CLK_MODE);
180+
//ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER, ETH_PHY_MDC, ETH_PHY_MDIO, ETH_PHY_TYPE, ETH_CLK_MODE);
181+
ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER);
182+
183+
// Static IP, leave without this line to get IP via DHCP
184+
//bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
185+
ETH.config(myIP, myGW, mySN, myDNS);
186+
187+
WT32_ETH01_waitForConnect();
188+
189+
apIP = ETH.localIP();
190+
191+
///////////////////////////////////
192+
193+
// modify TTL associated with the domain name (in seconds)
194+
// default is 60 seconds
195+
dnsServer.setTTL(300);
196+
// set which return code will be used for all other domains (e.g. sending
197+
// ServerFailure instead of NonExistentDomain will reduce number of queries
198+
// sent by clients)
199+
// default is AsyncDNSReplyCode::NonExistentDomain
200+
dnsServer.setErrorReplyCode(AsyncDNSReplyCode::ServerFailure);
201+
202+
dnsServer.start(DNS_PORT, "*", apIP);
203+
204+
/* Setup web pages: root, wifi config pages, SO captive portal detectors and not found. */
205+
// simple HTTP server to see that DNS server is working
206+
server.on("/", HTTP_GET, [](AsyncWebServerRequest * request)
207+
{
208+
handleRoot(request);
209+
});
210+
211+
//Android captive portal. Maybe not needed. Might be handled by notFound handler.
212+
server.on("/generate_204", HTTP_GET, [](AsyncWebServerRequest * request)
213+
{
214+
handleRoot(request);
215+
});
216+
217+
//Microsoft captive portal. Maybe not needed. Might be handled by notFound handler.
218+
server.on("/fwlink", HTTP_GET, [](AsyncWebServerRequest * request)
219+
{
220+
handleRoot(request);
221+
});
222+
223+
server.onNotFound(handleNotFound);
224+
225+
server.begin(); // Web server start
226+
227+
Serial.print(F("AsyncDNSServer is @ IP : "));
228+
Serial.println(apIP);
229+
}
230+
231+
void loop()
232+
{
233+
}

0 commit comments

Comments
 (0)