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

Commit 0fe3f92

Browse files
authored
v1.2.0 to add common code to library
#### Releases v1.2.0 1. Add common code to library 2. Renew examples
1 parent 1163c5c commit 0fe3f92

File tree

2 files changed

+818
-0
lines changed

2 files changed

+818
-0
lines changed
Lines changed: 261 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,261 @@
1+
/****************************************************************************************************************************
2+
AdvancedWebServer.h - Dead simple web-server for Ethernet shields
3+
4+
For Ethernet shields using WT32_ETH01 (ESP32 + LAN8720)
5+
6+
WebServer_WT32_ETH01 is a library for the Ethernet LAN8720 in WT32_ETH01 to run WebServer
7+
8+
Based on and modified from ESP8266 https://github.com/esp8266/Arduino/releases
9+
Built by Khoi Hoang https://github.com/khoih-prog/WebServer_WT32_ETH01
10+
Licensed under MIT 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+
#define DEBUG_ETHERNET_WEBSERVER_PORT Serial
42+
43+
// Debug Level from 0 to 4
44+
#define _ETHERNET_WEBSERVER_LOGLEVEL_ 3
45+
46+
#include <WebServer_WT32_ETH01.h>
47+
48+
WebServer server(80);
49+
50+
// Select the IP address according to your local network
51+
IPAddress myIP(192, 168, 2, 232);
52+
IPAddress myGW(192, 168, 2, 1);
53+
IPAddress mySN(255, 255, 255, 0);
54+
55+
// Google DNS Server IP
56+
IPAddress myDNS(8, 8, 8, 8);
57+
58+
bool eth_connected = false;
59+
60+
int reqCount = 0; // number of requests received
61+
62+
void handleRoot()
63+
{
64+
#define BUFFER_SIZE 400
65+
66+
char temp[BUFFER_SIZE];
67+
int sec = millis() / 1000;
68+
int min = sec / 60;
69+
int hr = min / 60;
70+
int day = hr / 24;
71+
72+
hr = hr % 24;
73+
74+
snprintf(temp, BUFFER_SIZE - 1,
75+
"<html>\
76+
<head>\
77+
<meta http-equiv='refresh' content='5'/>\
78+
<title>AdvancedWebServer %s</title>\
79+
<style>\
80+
body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
81+
</style>\
82+
</head>\
83+
<body>\
84+
<h2>Hi from WebServer_WT32_ETH01!</h2>\
85+
<h3>on %s</h3>\
86+
<p>Uptime: %d d %02d:%02d:%02d</p>\
87+
<img src=\"/test.svg\" />\
88+
</body>\
89+
</html>", BOARD_NAME, BOARD_NAME, day, hr % 24, min % 60, sec % 60);
90+
91+
server.send(200, F("text/html"), temp);
92+
}
93+
94+
void handleNotFound()
95+
{
96+
String message = F("File Not Found\n\n");
97+
98+
message += F("URI: ");
99+
message += server.uri();
100+
message += F("\nMethod: ");
101+
message += (server.method() == HTTP_GET) ? F("GET") : F("POST");
102+
message += F("\nArguments: ");
103+
message += server.args();
104+
message += F("\n");
105+
106+
for (uint8_t i = 0; i < server.args(); i++)
107+
{
108+
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
109+
}
110+
111+
server.send(404, F("text/plain"), message);
112+
}
113+
114+
void drawGraph()
115+
{
116+
String out;
117+
out.reserve(3000);
118+
char temp[70];
119+
120+
out += F("<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"310\" height=\"150\">\n");
121+
out += F("<rect width=\"310\" height=\"150\" fill=\"rgb(250, 230, 210)\" stroke-width=\"1\" stroke=\"rgb(0, 0, 0)\" />\n");
122+
out += F("<g stroke=\"black\">\n");
123+
int y = rand() % 130;
124+
125+
for (int x = 10; x < 300; x += 10)
126+
{
127+
int y2 = rand() % 130;
128+
sprintf(temp, "<line x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" stroke-width=\"1\" />\n", x, 140 - y, x + 10, 140 - y2);
129+
out += temp;
130+
y = y2;
131+
}
132+
out += F("</g>\n</svg>\n");
133+
134+
server.send(200, F("image/svg+xml"), out);
135+
}
136+
137+
void WiFiEvent(WiFiEvent_t event)
138+
{
139+
switch (event)
140+
{
141+
case SYSTEM_EVENT_ETH_START:
142+
Serial.println("\nETH Started");
143+
//set eth hostname here
144+
ETH.setHostname("WT32-ETH01");
145+
break;
146+
case SYSTEM_EVENT_ETH_CONNECTED:
147+
Serial.println("ETH Connected");
148+
break;
149+
150+
case SYSTEM_EVENT_ETH_GOT_IP:
151+
if (!eth_connected)
152+
{
153+
Serial.print("ETH MAC: ");
154+
Serial.print(ETH.macAddress());
155+
Serial.print(", IPv4: ");
156+
Serial.print(ETH.localIP());
157+
158+
if (ETH.fullDuplex())
159+
{
160+
Serial.print(", FULL_DUPLEX");
161+
}
162+
163+
Serial.print(", ");
164+
Serial.print(ETH.linkSpeed());
165+
Serial.println("Mbps");
166+
eth_connected = true;
167+
}
168+
169+
break;
170+
171+
case SYSTEM_EVENT_ETH_DISCONNECTED:
172+
Serial.println("ETH Disconnected");
173+
eth_connected = false;
174+
break;
175+
176+
case SYSTEM_EVENT_ETH_STOP:
177+
Serial.println("\nETH Stopped");
178+
eth_connected = false;
179+
break;
180+
181+
default:
182+
break;
183+
}
184+
}
185+
186+
void setup()
187+
{
188+
Serial.begin(115200);
189+
while (!Serial);
190+
191+
// Using this if Serial debugging is not necessary or not using Serial port
192+
//while (!Serial && (millis() < 3000));
193+
194+
Serial.print("\nStarting AdvancedWebServer on " + String(ARDUINO_BOARD));
195+
Serial.println(" with " + String(SHIELD_TYPE));
196+
Serial.println(WEBSERVER_WT32_ETH01_VERSION);
197+
198+
//bool begin(uint8_t phy_addr=ETH_PHY_ADDR, int power=ETH_PHY_POWER, int mdc=ETH_PHY_MDC, int mdio=ETH_PHY_MDIO,
199+
// eth_phy_type_t type=ETH_PHY_TYPE, eth_clock_mode_t clk_mode=ETH_CLK_MODE);
200+
//ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER, ETH_PHY_MDC, ETH_PHY_MDIO, ETH_PHY_TYPE, ETH_CLK_MODE);
201+
ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER);
202+
203+
// Static IP, leave without this line to get IP via DHCP
204+
//bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
205+
ETH.config(myIP, myGW, mySN, myDNS);
206+
207+
WiFi.onEvent(WiFiEvent);
208+
209+
while (!eth_connected)
210+
delay(100);
211+
212+
server.on(F("/"), handleRoot);
213+
server.on(F("/test.svg"), drawGraph);
214+
server.on(F("/inline"), []()
215+
{
216+
server.send(200, F("text/plain"), F("This works as well"));
217+
});
218+
219+
server.onNotFound(handleNotFound);
220+
server.begin();
221+
222+
Serial.print(F("HTTP EthernetWebServer is @ IP : "));
223+
Serial.println(ETH.localIP());
224+
}
225+
226+
void heartBeatPrint()
227+
{
228+
static int num = 1;
229+
230+
Serial.print(F("."));
231+
232+
if (num == 80)
233+
{
234+
Serial.println();
235+
num = 1;
236+
}
237+
else if (num++ % 10 == 0)
238+
{
239+
Serial.print(F(" "));
240+
}
241+
}
242+
243+
void check_status()
244+
{
245+
static unsigned long checkstatus_timeout = 0;
246+
247+
#define STATUS_CHECK_INTERVAL 10000L
248+
249+
// Send status report every STATUS_REPORT_INTERVAL (60) seconds: we don't need to send updates frequently if there is no status change.
250+
if ((millis() > checkstatus_timeout) || (checkstatus_timeout == 0))
251+
{
252+
heartBeatPrint();
253+
checkstatus_timeout = millis() + STATUS_CHECK_INTERVAL;
254+
}
255+
}
256+
257+
void loop()
258+
{
259+
server.handleClient();
260+
check_status();
261+
}

0 commit comments

Comments
 (0)