68 |
69 | WebServer server(80);
70 |
71 | // Enter a MAC address and IP address for your controller below.
72 | #define NUMBER_OF_MAC 20
73 |
74 | byte mac[][NUMBER_OF_MAC] =
75 | {
76 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01 },
77 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x02 },
78 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x03 },
79 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x04 },
80 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x05 },
81 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x06 },
82 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x07 },
83 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x08 },
84 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x09 },
85 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0A },
86 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0B },
87 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0C },
88 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0D },
89 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0E },
90 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0F },
91 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x10 },
92 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x11 },
93 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x12 },
94 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x13 },
95 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x14 },
96 | };
97 |
98 | // Select the IP address according to your local network
99 | IPAddress myIP(192, 168, 2, 232);
100 | IPAddress myGW(192, 168, 2, 1);
101 | IPAddress mySN(255, 255, 255, 0);
102 |
103 | // Google DNS Server IP
104 | IPAddress myDNS(8, 8, 8, 8);
105 |
106 | int reqCount = 0; // number of requests received
107 |
108 | void handleRoot()
109 | {
110 | #define BUFFER_SIZE 400
111 |
112 | char temp[BUFFER_SIZE];
113 | int sec = millis() / 1000;
114 | int min = sec / 60;
115 | int hr = min / 60;
116 | int day = hr / 24;
117 |
118 | hr = hr % 24;
119 |
120 | snprintf(temp, BUFFER_SIZE - 1,
121 | "\
122 | \
123 | \
124 | AdvancedWebServer %s\
125 | \
128 | \
129 | \
130 | Hi from WebServer_ESP32_W5500!
\
131 | on %s
\
132 | Uptime: %d d %02d:%02d:%02d
\
133 |
\
134 | \
135 | ", BOARD_NAME, BOARD_NAME, day, hr % 24, min % 60, sec % 60);
136 |
137 | server.send(200, F("text/html"), temp);
138 | }
139 |
140 | void handleNotFound()
141 | {
142 | String message = F("File Not Found\n\n");
143 |
144 | message += F("URI: ");
145 | message += server.uri();
146 | message += F("\nMethod: ");
147 | message += (server.method() == HTTP_GET) ? F("GET") : F("POST");
148 | message += F("\nArguments: ");
149 | message += server.args();
150 | message += F("\n");
151 |
152 | for (uint8_t i = 0; i < server.args(); i++)
153 | {
154 | message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
155 | }
156 |
157 | server.send(404, F("text/plain"), message);
158 | }
159 |
160 | void drawGraph()
161 | {
162 | String out;
163 | out.reserve(3000);
164 | char temp[70];
165 |
166 | out += F("\n");
180 |
181 | server.send(200, F("image/svg+xml"), out);
182 | }
183 |
184 | void setup()
185 | {
186 | Serial.begin(115200);
187 |
188 | while (!Serial && (millis() < 5000));
189 |
190 | Serial.print(F("\nStart AdvancedWebServer on "));
191 | Serial.print(ARDUINO_BOARD);
192 | Serial.print(F(" with "));
193 | Serial.println(SHIELD_TYPE);
194 | Serial.println(WEBSERVER_ESP32_W5500_VERSION);
195 |
196 | ET_LOGWARN(F("Default SPI pinout:"));
197 | ET_LOGWARN1(F("SPI_HOST:"), ETH_SPI_HOST);
198 | ET_LOGWARN1(F("MOSI:"), MOSI_GPIO);
199 | ET_LOGWARN1(F("MISO:"), MISO_GPIO);
200 | ET_LOGWARN1(F("SCK:"), SCK_GPIO);
201 | ET_LOGWARN1(F("CS:"), CS_GPIO);
202 | ET_LOGWARN1(F("INT:"), INT_GPIO);
203 | ET_LOGWARN1(F("SPI Clock (MHz):"), SPI_CLOCK_MHZ);
204 | ET_LOGWARN(F("========================="));
205 |
206 | ///////////////////////////////////
207 |
208 | // To be called before ETH.begin()
209 | ESP32_W5500_onEvent();
210 |
211 | // start the ethernet connection and the server:
212 | // Use DHCP dynamic IP and random mac
213 | //bool begin(int MISO_GPIO, int MOSI_GPIO, int SCLK_GPIO, int CS_GPIO, int INT_GPIO, int SPI_CLOCK_MHZ,
214 | // int SPI_HOST, uint8_t *W6100_Mac = W6100_Default_Mac);
215 | ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST );
216 | //ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST, mac[millis() % NUMBER_OF_MAC] );
217 |
218 | // Static IP, leave without this line to get IP via DHCP
219 | //bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
220 | //ETH.config(myIP, myGW, mySN, myDNS);
221 |
222 | ESP32_W5500_waitForConnect();
223 |
224 | ///////////////////////////////////
225 |
226 | server.on(F("/"), handleRoot);
227 | server.on(F("/test.svg"), drawGraph);
228 | server.on(F("/inline"), []()
229 | {
230 | server.send(200, F("text/plain"), F("This works as well"));
231 | });
232 |
233 | server.onNotFound(handleNotFound);
234 | server.begin();
235 |
236 | Serial.print(F("HTTP EthernetWebServer is @ IP : "));
237 | Serial.println(ETH.localIP());
238 | }
239 |
240 | void heartBeatPrint()
241 | {
242 | static int num = 1;
243 |
244 | Serial.print(F("."));
245 |
246 | if (num == 80)
247 | {
248 | Serial.println();
249 | num = 1;
250 | }
251 | else if (num++ % 10 == 0)
252 | {
253 | Serial.print(F(" "));
254 | }
255 | }
256 |
257 | void check_status()
258 | {
259 | static unsigned long checkstatus_timeout = 0;
260 |
261 | #define STATUS_CHECK_INTERVAL 10000L
262 |
263 | // Send status report every STATUS_REPORT_INTERVAL (60) seconds: we don't need to send updates frequently if there is no status change.
264 | if ((millis() > checkstatus_timeout) || (checkstatus_timeout == 0))
265 | {
266 | heartBeatPrint();
267 | checkstatus_timeout = millis() + STATUS_CHECK_INTERVAL;
268 | }
269 | }
270 |
271 | void loop()
272 | {
273 | server.handleClient();
274 | check_status();
275 | }
276 |
--------------------------------------------------------------------------------
/examples/HelloServer/HelloServer.ino:
--------------------------------------------------------------------------------
1 | /****************************************************************************************************************************
2 | HelloServer.ino - Dead simple web-server for Ethernet shields
3 |
4 | For Ethernet shields using ESP32_W5500 (ESP32 + W5500)
5 |
6 | WebServer_ESP32_W5500 is a library for the ESP32 with Ethernet W5500 to run WebServer
7 |
8 | Based on and modified from ESP32-IDF https://github.com/espressif/esp-idf
9 | Built by Khoi Hoang https://github.com/khoih-prog/WebServer_ESP32_W5500
10 | Licensed under GPLv3 license
11 | *****************************************************************************************************************************/
12 |
13 | #if !( defined(ESP32) )
14 | #error This code is designed for (ESP32 + W5500) to run on ESP32 platform! Please check your Tools->Board setting.
15 | #endif
16 |
17 | #define DEBUG_ETHERNET_WEBSERVER_PORT Serial
18 |
19 | // Debug Level from 0 to 4
20 | #define _ETHERNET_WEBSERVER_LOGLEVEL_ 3
21 |
22 | //////////////////////////////////////////////////////////
23 |
24 | // Optional values to override default settings
25 | // Don't change unless you know what you're doing
26 | //#define ETH_SPI_HOST SPI3_HOST
27 | //#define SPI_CLOCK_MHZ 25
28 |
29 | // Must connect INT to GPIOxx or not working
30 | //#define INT_GPIO 4
31 |
32 | //#define MISO_GPIO 19
33 | //#define MOSI_GPIO 23
34 | //#define SCK_GPIO 18
35 | //#define CS_GPIO 5
36 |
37 | //////////////////////////////////////////////////////////
38 |
39 | #include
40 |
41 | WebServer server(80);
42 |
43 | // Enter a MAC address and IP address for your controller below.
44 | #define NUMBER_OF_MAC 20
45 |
46 | byte mac[][NUMBER_OF_MAC] =
47 | {
48 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01 },
49 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x02 },
50 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x03 },
51 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x04 },
52 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x05 },
53 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x06 },
54 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x07 },
55 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x08 },
56 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x09 },
57 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0A },
58 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0B },
59 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0C },
60 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0D },
61 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0E },
62 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0F },
63 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x10 },
64 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x11 },
65 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x12 },
66 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x13 },
67 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x14 },
68 | };
69 |
70 | // Select the IP address according to your local network
71 | IPAddress myIP(192, 168, 2, 232);
72 | IPAddress myGW(192, 168, 2, 1);
73 | IPAddress mySN(255, 255, 255, 0);
74 |
75 | // Google DNS Server IP
76 | IPAddress myDNS(8, 8, 8, 8);
77 |
78 | void handleRoot()
79 | {
80 | String html = F("Hello from HelloServer running on ");
81 |
82 | html += String(BOARD_NAME);
83 |
84 | server.send(200, F("text/plain"), html);
85 | }
86 |
87 | void handleNotFound()
88 | {
89 | String message = F("File Not Found\n\n");
90 |
91 | message += F("URI: ");
92 | message += server.uri();
93 | message += F("\nMethod: ");
94 | message += (server.method() == HTTP_GET) ? F("GET") : F("POST");
95 | message += F("\nArguments: ");
96 | message += server.args();
97 | message += F("\n");
98 |
99 | for (uint8_t i = 0; i < server.args(); i++)
100 | {
101 | message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
102 | }
103 |
104 | server.send(404, F("text/plain"), message);
105 | }
106 |
107 | void setup()
108 | {
109 | Serial.begin(115200);
110 |
111 | while (!Serial && (millis() < 5000));
112 |
113 | Serial.print(F("\nStart HelloServer on "));
114 | Serial.print(ARDUINO_BOARD);
115 | Serial.print(F(" with "));
116 | Serial.println(SHIELD_TYPE);
117 | Serial.println(WEBSERVER_ESP32_W5500_VERSION);
118 |
119 | ET_LOGWARN(F("Default SPI pinout:"));
120 | ET_LOGWARN1(F("SPI_HOST:"), ETH_SPI_HOST);
121 | ET_LOGWARN1(F("MOSI:"), MOSI_GPIO);
122 | ET_LOGWARN1(F("MISO:"), MISO_GPIO);
123 | ET_LOGWARN1(F("SCK:"), SCK_GPIO);
124 | ET_LOGWARN1(F("CS:"), CS_GPIO);
125 | ET_LOGWARN1(F("INT:"), INT_GPIO);
126 | ET_LOGWARN1(F("SPI Clock (MHz):"), SPI_CLOCK_MHZ);
127 | ET_LOGWARN(F("========================="));
128 |
129 | ///////////////////////////////////
130 |
131 | // To be called before ETH.begin()
132 | ESP32_W5500_onEvent();
133 |
134 | // start the ethernet connection and the server:
135 | // Use DHCP dynamic IP and random mac
136 | //bool begin(int MISO_GPIO, int MOSI_GPIO, int SCLK_GPIO, int CS_GPIO, int INT_GPIO, int SPI_CLOCK_MHZ,
137 | // int SPI_HOST, uint8_t *W6100_Mac = W6100_Default_Mac);
138 | ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST );
139 | //ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST, mac[millis() % NUMBER_OF_MAC] );
140 |
141 | // Static IP, leave without this line to get IP via DHCP
142 | //bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
143 | //ETH.config(myIP, myGW, mySN, myDNS);
144 |
145 | ESP32_W5500_waitForConnect();
146 |
147 | ///////////////////////////////////
148 |
149 | server.on(F("/"), handleRoot);
150 |
151 | server.on(F("/inline"), []()
152 | {
153 | server.send(200, F("text/plain"), F("This works as well"));
154 | });
155 |
156 | server.onNotFound(handleNotFound);
157 |
158 | server.begin();
159 |
160 | Serial.print(F("HTTP EthernetWebServer is @ IP : "));
161 | Serial.println(ETH.localIP());
162 | }
163 |
164 | void loop()
165 | {
166 | server.handleClient();
167 | }
168 |
--------------------------------------------------------------------------------
/examples/HelloServer2/HelloServer2.ino:
--------------------------------------------------------------------------------
1 | /****************************************************************************************************************************
2 | HelloServer2.h - Dead simple web-server for Ethernet shields
3 |
4 | For Ethernet shields using ESP32_W5500 (ESP32 + W5500)
5 |
6 | WebServer_ESP32_W5500 is a library for the ESP32 with Ethernet W5500 to run WebServer
7 |
8 | Based on and modified from ESP32-IDF https://github.com/espressif/esp-idf
9 | Built by Khoi Hoang https://github.com/khoih-prog/WebServer_ESP32_W5500
10 | Licensed under GPLv3 license
11 | *****************************************************************************************************************************/
12 |
13 | #if !( defined(ESP32) )
14 | #error This code is designed for (ESP32 + W5500) to run on ESP32 platform! Please check your Tools->Board setting.
15 | #endif
16 |
17 | #define DEBUG_ETHERNET_WEBSERVER_PORT Serial
18 |
19 | // Debug Level from 0 to 4
20 | #define _ETHERNET_WEBSERVER_LOGLEVEL_ 3
21 |
22 | //////////////////////////////////////////////////////////
23 |
24 | // Optional values to override default settings
25 | // Don't change unless you know what you're doing
26 | //#define ETH_SPI_HOST SPI3_HOST
27 | //#define SPI_CLOCK_MHZ 25
28 |
29 | // Must connect INT to GPIOxx or not working
30 | //#define INT_GPIO 4
31 |
32 | //#define MISO_GPIO 19
33 | //#define MOSI_GPIO 23
34 | //#define SCK_GPIO 18
35 | //#define CS_GPIO 5
36 |
37 | //////////////////////////////////////////////////////////
38 |
39 | #include
40 |
41 | WebServer server(80);
42 |
43 | // Enter a MAC address and IP address for your controller below.
44 | #define NUMBER_OF_MAC 20
45 |
46 | byte mac[][NUMBER_OF_MAC] =
47 | {
48 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01 },
49 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x02 },
50 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x03 },
51 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x04 },
52 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x05 },
53 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x06 },
54 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x07 },
55 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x08 },
56 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x09 },
57 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0A },
58 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0B },
59 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0C },
60 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0D },
61 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0E },
62 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0F },
63 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x10 },
64 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x11 },
65 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x12 },
66 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x13 },
67 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x14 },
68 | };
69 |
70 | // Select the IP address according to your local network
71 | IPAddress myIP(192, 168, 2, 232);
72 | IPAddress myGW(192, 168, 2, 1);
73 | IPAddress mySN(255, 255, 255, 0);
74 |
75 | // Google DNS Server IP
76 | IPAddress myDNS(8, 8, 8, 8);
77 |
78 | void handleRoot()
79 | {
80 | String html = F("Hello from HelloServer2 running on ");
81 |
82 | html += String(BOARD_NAME);
83 |
84 | server.send(200, F("text/plain"), html);
85 | }
86 |
87 | void handleNotFound()
88 | {
89 | String message = F("File Not Found\n\n");
90 |
91 | message += F("URI: ");
92 | message += server.uri();
93 | message += F("\nMethod: ");
94 | message += (server.method() == HTTP_GET) ? F("GET") : F("POST");
95 | message += F("\nArguments: ");
96 | message += server.args();
97 | message += F("\n");
98 |
99 | for (uint8_t i = 0; i < server.args(); i++)
100 | {
101 | message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
102 | }
103 |
104 | server.send(404, F("text/plain"), message);
105 | }
106 |
107 | void setup()
108 | {
109 | Serial.begin(115200);
110 |
111 | while (!Serial && (millis() < 5000));
112 |
113 | Serial.print(F("\nStart HelloServer2 on "));
114 | Serial.print(ARDUINO_BOARD);
115 | Serial.print(F(" with "));
116 | Serial.println(SHIELD_TYPE);
117 | Serial.println(WEBSERVER_ESP32_W5500_VERSION);
118 |
119 | ET_LOGWARN(F("Default SPI pinout:"));
120 | ET_LOGWARN1(F("SPI_HOST:"), ETH_SPI_HOST);
121 | ET_LOGWARN1(F("MOSI:"), MOSI_GPIO);
122 | ET_LOGWARN1(F("MISO:"), MISO_GPIO);
123 | ET_LOGWARN1(F("SCK:"), SCK_GPIO);
124 | ET_LOGWARN1(F("CS:"), CS_GPIO);
125 | ET_LOGWARN1(F("INT:"), INT_GPIO);
126 | ET_LOGWARN1(F("SPI Clock (MHz):"), SPI_CLOCK_MHZ);
127 | ET_LOGWARN(F("========================="));
128 |
129 | ///////////////////////////////////
130 |
131 | // To be called before ETH.begin()
132 | ESP32_W5500_onEvent();
133 |
134 | // start the ethernet connection and the server:
135 | // Use DHCP dynamic IP and random mac
136 | //bool begin(int MISO_GPIO, int MOSI_GPIO, int SCLK_GPIO, int CS_GPIO, int INT_GPIO, int SPI_CLOCK_MHZ,
137 | // int SPI_HOST, uint8_t *W6100_Mac = W6100_Default_Mac);
138 | ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST );
139 | //ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST, mac[millis() % NUMBER_OF_MAC] );
140 |
141 | // Static IP, leave without this line to get IP via DHCP
142 | //bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
143 | //ETH.config(myIP, myGW, mySN, myDNS);
144 |
145 | ESP32_W5500_waitForConnect();
146 |
147 | ///////////////////////////////////
148 |
149 | server.on(F("/"), handleRoot);
150 |
151 | server.on(F("/inline"), []()
152 | {
153 | server.send(200, F("text/plain"), F("This works as well"));
154 | });
155 |
156 | server.on(F("/gif"), []()
157 | {
158 | static const uint8_t gif[] PROGMEM =
159 | {
160 | 0x47, 0x49, 0x46, 0x38, 0x37, 0x61, 0x10, 0x00, 0x10, 0x00, 0x80, 0x01,
161 | 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0x2c, 0x00, 0x00, 0x00, 0x00,
162 | 0x10, 0x00, 0x10, 0x00, 0x00, 0x02, 0x19, 0x8c, 0x8f, 0xa9, 0xcb, 0x9d,
163 | 0x00, 0x5f, 0x74, 0xb4, 0x56, 0xb0, 0xb0, 0xd2, 0xf2, 0x35, 0x1e, 0x4c,
164 | 0x0c, 0x24, 0x5a, 0xe6, 0x89, 0xa6, 0x4d, 0x01, 0x00, 0x3b
165 | };
166 |
167 | char gif_colored[sizeof(gif)];
168 |
169 | memcpy_P(gif_colored, gif, sizeof(gif));
170 |
171 | // Set the background to a random set of colors
172 | gif_colored[16] = millis() % 256;
173 | gif_colored[17] = millis() % 256;
174 | gif_colored[18] = millis() % 256;
175 |
176 | server.send_P(200, "image/gif", gif_colored, sizeof(gif_colored));
177 | });
178 |
179 | server.onNotFound(handleNotFound);
180 |
181 | server.begin();
182 |
183 | Serial.print(F("HTTP HelloServer2 started @ IP : "));
184 | Serial.println(ETH.localIP());
185 | }
186 |
187 | void loop()
188 | {
189 | server.handleClient();
190 | }
191 |
--------------------------------------------------------------------------------
/examples/HttpBasicAuth/HttpBasicAuth.ino:
--------------------------------------------------------------------------------
1 | /****************************************************************************************************************************
2 | HTTPBasicAuth.h - Dead simple web-server for Ethernet shields
3 |
4 | For Ethernet shields using ESP32_W5500 (ESP32 + W5500)
5 |
6 | WebServer_ESP32_W5500 is a library for the ESP32 with Ethernet W5500 to run WebServer
7 |
8 | Based on and modified from ESP32-IDF https://github.com/espressif/esp-idf
9 | Built by Khoi Hoang https://github.com/khoih-prog/WebServer_ESP32_W5500
10 | Licensed under GPLv3 license
11 | *****************************************************************************************************************************/
12 |
13 | #if !( defined(ESP32) )
14 | #error This code is designed for (ESP32 + W5500) to run on ESP32 platform! Please check your Tools->Board setting.
15 | #endif
16 |
17 | #define DEBUG_ETHERNET_WEBSERVER_PORT Serial
18 |
19 | // Debug Level from 0 to 4
20 | #define _ETHERNET_WEBSERVER_LOGLEVEL_ 3
21 |
22 | //////////////////////////////////////////////////////////
23 |
24 | // Optional values to override default settings
25 | // Don't change unless you know what you're doing
26 | //#define ETH_SPI_HOST SPI3_HOST
27 | //#define SPI_CLOCK_MHZ 25
28 |
29 | // Must connect INT to GPIOxx or not working
30 | //#define INT_GPIO 4
31 |
32 | //#define MISO_GPIO 19
33 | //#define MOSI_GPIO 23
34 | //#define SCK_GPIO 18
35 | //#define CS_GPIO 5
36 |
37 | //////////////////////////////////////////////////////////
38 |
39 | #include
40 |
41 | WebServer server(80);
42 |
43 | // Enter a MAC address and IP address for your controller below.
44 | #define NUMBER_OF_MAC 20
45 |
46 | byte mac[][NUMBER_OF_MAC] =
47 | {
48 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01 },
49 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x02 },
50 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x03 },
51 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x04 },
52 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x05 },
53 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x06 },
54 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x07 },
55 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x08 },
56 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x09 },
57 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0A },
58 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0B },
59 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0C },
60 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0D },
61 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0E },
62 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0F },
63 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x10 },
64 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x11 },
65 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x12 },
66 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x13 },
67 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x14 },
68 | };
69 |
70 | // Select the IP address according to your local network
71 | IPAddress myIP(192, 168, 2, 232);
72 | IPAddress myGW(192, 168, 2, 1);
73 | IPAddress mySN(255, 255, 255, 0);
74 |
75 | // Google DNS Server IP
76 | IPAddress myDNS(8, 8, 8, 8);
77 |
78 | const char* www_username = "admin";
79 | const char* www_password = "esp32_w5500";
80 |
81 | void setup()
82 | {
83 | Serial.begin(115200);
84 |
85 | while (!Serial && (millis() < 5000));
86 |
87 | Serial.print(F("\nStart HTTPBasicAuth on "));
88 | Serial.print(ARDUINO_BOARD);
89 | Serial.print(F(" with "));
90 | Serial.println(SHIELD_TYPE);
91 | Serial.println(WEBSERVER_ESP32_W5500_VERSION);
92 |
93 | ET_LOGWARN(F("Default SPI pinout:"));
94 | ET_LOGWARN1(F("SPI_HOST:"), ETH_SPI_HOST);
95 | ET_LOGWARN1(F("MOSI:"), MOSI_GPIO);
96 | ET_LOGWARN1(F("MISO:"), MISO_GPIO);
97 | ET_LOGWARN1(F("SCK:"), SCK_GPIO);
98 | ET_LOGWARN1(F("CS:"), CS_GPIO);
99 | ET_LOGWARN1(F("INT:"), INT_GPIO);
100 | ET_LOGWARN1(F("SPI Clock (MHz):"), SPI_CLOCK_MHZ);
101 | ET_LOGWARN(F("========================="));
102 |
103 | ///////////////////////////////////
104 |
105 | // To be called before ETH.begin()
106 | ESP32_W5500_onEvent();
107 |
108 | // start the ethernet connection and the server:
109 | // Use DHCP dynamic IP and random mac
110 | //bool begin(int MISO_GPIO, int MOSI_GPIO, int SCLK_GPIO, int CS_GPIO, int INT_GPIO, int SPI_CLOCK_MHZ,
111 | // int SPI_HOST, uint8_t *W6100_Mac = W6100_Default_Mac);
112 | ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST );
113 | //ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST, mac[millis() % NUMBER_OF_MAC] );
114 |
115 | // Static IP, leave without this line to get IP via DHCP
116 | //bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
117 | //ETH.config(myIP, myGW, mySN, myDNS);
118 |
119 | ESP32_W5500_waitForConnect();
120 |
121 | ///////////////////////////////////
122 |
123 | server.on(F("/"), []()
124 | {
125 | if (!server.authenticate(www_username, www_password))
126 | {
127 | return server.requestAuthentication();
128 | }
129 |
130 | server.send(200, F("text/plain"), F("Login OK"));
131 | });
132 |
133 | server.begin();
134 |
135 | Serial.print(F("HTTP HTTPBasicAuth started @ IP : "));
136 | Serial.println(ETH.localIP());
137 |
138 | Serial.print(F("Open http://"));
139 | Serial.print(ETH.localIP());
140 | Serial.println(F("/ in your browser to see it working"));
141 | Serial.print(F("Using username : "));
142 | Serial.print(www_username);
143 | Serial.print(F(" and password : "));
144 | Serial.println(www_password);
145 | }
146 |
147 | void loop()
148 | {
149 | server.handleClient();
150 | }
151 |
--------------------------------------------------------------------------------
/examples/MQTTClient_Auth/MQTTClient_Auth.ino:
--------------------------------------------------------------------------------
1 | /****************************************************************************************************************************
2 | MQTTClient_Auth.ino - Dead simple MQTT Client for Ethernet shields
3 |
4 | For Ethernet shields using ESP32_W5500 (ESP32 + W5500)
5 |
6 | WebServer_ESP32_W5500 is a library for the ESP32 with Ethernet W5500 to run WebServer
7 |
8 | Based on and modified from ESP32-IDF https://github.com/espressif/esp-idf
9 | Built by Khoi Hoang https://github.com/khoih-prog/WebServer_ESP32_W5500
10 | Licensed under GPLv3 license
11 | *****************************************************************************************************************************/
12 |
13 | /*
14 | Basic MQTT example (without SSL!) with Authentication
15 | This sketch demonstrates the basic capabilities of the library.
16 | It connects to an MQTT server then:
17 | - providing username and password
18 | - publishes "hello world" to the topic "outTopic"
19 | - subscribes to the topic "inTopic", printing out any messages
20 | it receives. NB - it assumes the received payloads are strings not binary
21 | It will reconnect to the server if the connection is lost using a blocking
22 | reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to
23 | achieve the same result without blocking the main loop.
24 | */
25 |
26 | #if !( defined(ESP32) )
27 | #error This code is designed for (ESP32 + W5500) to run on ESP32 platform! Please check your Tools->Board setting.
28 | #endif
29 |
30 | #define DEBUG_ETHERNET_WEBSERVER_PORT Serial
31 |
32 | // Debug Level from 0 to 4
33 | #define _ETHERNET_WEBSERVER_LOGLEVEL_ 3
34 |
35 | //////////////////////////////////////////////////////////
36 |
37 | // Optional values to override default settings
38 | // Don't change unless you know what you're doing
39 | //#define ETH_SPI_HOST SPI3_HOST
40 | //#define SPI_CLOCK_MHZ 25
41 |
42 | // Must connect INT to GPIOxx or not working
43 | //#define INT_GPIO 4
44 |
45 | //#define MISO_GPIO 19
46 | //#define MOSI_GPIO 23
47 | //#define SCK_GPIO 18
48 | //#define CS_GPIO 5
49 |
50 | //////////////////////////////////////////////////////////
51 |
52 | #include
53 |
54 | WebServer server(80);
55 |
56 | // Enter a MAC address and IP address for your controller below.
57 | #define NUMBER_OF_MAC 20
58 |
59 | byte mac[][NUMBER_OF_MAC] =
60 | {
61 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01 },
62 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x02 },
63 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x03 },
64 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x04 },
65 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x05 },
66 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x06 },
67 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x07 },
68 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x08 },
69 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x09 },
70 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0A },
71 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0B },
72 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0C },
73 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0D },
74 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0E },
75 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0F },
76 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x10 },
77 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x11 },
78 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x12 },
79 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x13 },
80 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x14 },
81 | };
82 |
83 | // Select the IP address according to your local network
84 | IPAddress myIP(192, 168, 2, 232);
85 | IPAddress myGW(192, 168, 2, 1);
86 | IPAddress mySN(255, 255, 255, 0);
87 |
88 | // Google DNS Server IP
89 | IPAddress myDNS(8, 8, 8, 8);
90 |
91 | #include
92 |
93 | // Update these with values suitable for your network.
94 | //const char* mqttServer = "broker.example"; // Broker address
95 | const char* mqttServer = "broker.emqx.io"; // Broker address
96 | //const char* mqttServer = "broker.shiftr.io"; // Broker address
97 |
98 | const char *ID = "MQTTClient_SSL-Client"; // Name of our device, must be unique
99 | const char *TOPIC = "MQTT_Pub"; // Topic to subscribe to
100 | const char *subTopic = "MQTT_Sub"; // Topic to subscribe to
101 |
102 | //IPAddress mqttServer(172, 16, 0, 2);
103 |
104 | void callback(char* topic, byte* payload, unsigned int length)
105 | {
106 | Serial.print("Message arrived [");
107 | Serial.print(topic);
108 | Serial.print("] ");
109 |
110 | for (unsigned int i = 0; i < length; i++)
111 | {
112 | Serial.print((char)payload[i]);
113 | }
114 |
115 | Serial.println();
116 | }
117 |
118 | WiFiClient ethClient;
119 | PubSubClient client(mqttServer, 1883, callback, ethClient);
120 |
121 | void reconnect()
122 | {
123 | // Loop until we're reconnected
124 | while (!client.connected())
125 | {
126 | Serial.print("Attempting MQTT connection to ");
127 | Serial.print(mqttServer);
128 |
129 | // Attempt to connect
130 | if (client.connect("arduino", "try", "try"))
131 | {
132 | Serial.println("...connected");
133 |
134 | // Once connected, publish an announcement...
135 | String data = "Hello from MQTTClient_SSL on " + String(BOARD_NAME);
136 |
137 | client.publish(TOPIC, data.c_str());
138 |
139 | //Serial.println("Published connection message successfully!");
140 | //Serial.print("Subscribed to: ");
141 | //Serial.println(subTopic);
142 |
143 | // This is a workaround to address https://github.com/OPEnSLab-OSU/SSLClient/issues/9
144 | //ethClientSSL.flush();
145 | // ... and resubscribe
146 | client.subscribe(subTopic);
147 | // for loopback testing
148 | client.subscribe(TOPIC);
149 | // This is a workaround to address https://github.com/OPEnSLab-OSU/SSLClient/issues/9
150 | //ethClientSSL.flush();
151 | }
152 | else
153 | {
154 | Serial.print("...failed, rc=");
155 | Serial.print(client.state());
156 | Serial.println(" try again in 5 seconds");
157 |
158 | // Wait 5 seconds before retrying
159 | delay(5000);
160 | }
161 | }
162 | }
163 |
164 | void setup()
165 | {
166 | Serial.begin(115200);
167 |
168 | while (!Serial && (millis() < 5000));
169 |
170 | Serial.print(F("\nStart MQTTClient_Auth on "));
171 | Serial.print(ARDUINO_BOARD);
172 | Serial.print(F(" with "));
173 | Serial.println(SHIELD_TYPE);
174 | Serial.println(WEBSERVER_ESP32_W5500_VERSION);
175 |
176 | ET_LOGWARN(F("Default SPI pinout:"));
177 | ET_LOGWARN1(F("SPI_HOST:"), ETH_SPI_HOST);
178 | ET_LOGWARN1(F("MOSI:"), MOSI_GPIO);
179 | ET_LOGWARN1(F("MISO:"), MISO_GPIO);
180 | ET_LOGWARN1(F("SCK:"), SCK_GPIO);
181 | ET_LOGWARN1(F("CS:"), CS_GPIO);
182 | ET_LOGWARN1(F("INT:"), INT_GPIO);
183 | ET_LOGWARN1(F("SPI Clock (MHz):"), SPI_CLOCK_MHZ);
184 | ET_LOGWARN(F("========================="));
185 |
186 | ///////////////////////////////////
187 |
188 | // To be called before ETH.begin()
189 | ESP32_W5500_onEvent();
190 |
191 | // start the ethernet connection and the server:
192 | // Use DHCP dynamic IP and random mac
193 | //bool begin(int MISO_GPIO, int MOSI_GPIO, int SCLK_GPIO, int CS_GPIO, int INT_GPIO, int SPI_CLOCK_MHZ,
194 | // int SPI_HOST, uint8_t *W6100_Mac = W6100_Default_Mac);
195 | ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST );
196 | //ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST, mac[millis() % NUMBER_OF_MAC] );
197 |
198 | // Static IP, leave without this line to get IP via DHCP
199 | //bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
200 | //ETH.config(myIP, myGW, mySN, myDNS);
201 |
202 | ESP32_W5500_waitForConnect();
203 |
204 | ///////////////////////////////////
205 |
206 | // Note - the default maximum packet size is 128 bytes. If the
207 | // combined length of clientId, username and password exceed this use the
208 | // following to increase the buffer size:
209 | // client.setBufferSize(255);
210 | }
211 |
212 | #define MQTT_PUBLISH_INTERVAL_MS 5000L
213 |
214 | String data = "Hello from MQTTClient_Auth on " + String(ARDUINO_BOARD) + " with " + String(SHIELD_TYPE);
215 | const char *pubData = data.c_str();
216 |
217 | unsigned long lastMsg = 0;
218 |
219 | void loop()
220 | {
221 | static unsigned long now;
222 |
223 | if (!client.connected())
224 | {
225 | reconnect();
226 | }
227 |
228 | // Sending Data
229 | now = millis();
230 |
231 | if (now - lastMsg > MQTT_PUBLISH_INTERVAL_MS)
232 | {
233 | lastMsg = now;
234 |
235 | if (!client.publish(TOPIC, pubData))
236 | {
237 | Serial.println("Message failed to send.");
238 | }
239 |
240 | Serial.print("Message Send : " + String(TOPIC) + " => ");
241 | Serial.println(data);
242 | }
243 |
244 | client.loop();
245 | }
246 |
--------------------------------------------------------------------------------
/examples/MQTTClient_Basic/MQTTClient_Basic.ino:
--------------------------------------------------------------------------------
1 | /****************************************************************************************************************************
2 | MQTTClient_Basic.ino - Dead simple MQTT Client for Ethernet shields
3 |
4 | For Ethernet shields using ESP32_W5500 (ESP32 + W5500)
5 |
6 | WebServer_ESP32_W5500 is a library for the ESP32 with Ethernet W5500 to run WebServer
7 |
8 | Based on and modified from ESP32-IDF https://github.com/espressif/esp-idf
9 | Built by Khoi Hoang https://github.com/khoih-prog/WebServer_ESP32_W5500
10 | Licensed under GPLv3 license
11 | *****************************************************************************************************************************/
12 |
13 | /*
14 | Basic MQTT example (without SSL!) with Authentication
15 | This sketch demonstrates the basic capabilities of the library.
16 | It connects to an MQTT server then:
17 | - providing username and password
18 | - publishes "hello world" to the topic "outTopic"
19 | - subscribes to the topic "inTopic", printing out any messages
20 | it receives. NB - it assumes the received payloads are strings not binary
21 |
22 | It will reconnect to the server if the connection is lost using a blocking
23 | reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to
24 | achieve the same result without blocking the main loop.
25 | */
26 |
27 | #if !( defined(ESP32) )
28 | #error This code is designed for (ESP32 + W5500) to run on ESP32 platform! Please check your Tools->Board setting.
29 | #endif
30 |
31 | #define DEBUG_ETHERNET_WEBSERVER_PORT Serial
32 |
33 | // Debug Level from 0 to 4
34 | #define _ETHERNET_WEBSERVER_LOGLEVEL_ 3
35 |
36 | //////////////////////////////////////////////////////////
37 |
38 | // Optional values to override default settings
39 | // Don't change unless you know what you're doing
40 | //#define ETH_SPI_HOST SPI3_HOST
41 | //#define SPI_CLOCK_MHZ 25
42 |
43 | // Must connect INT to GPIOxx or not working
44 | //#define INT_GPIO 4
45 |
46 | //#define MISO_GPIO 19
47 | //#define MOSI_GPIO 23
48 | //#define SCK_GPIO 18
49 | //#define CS_GPIO 5
50 |
51 | //////////////////////////////////////////////////////////
52 |
53 | #include
54 |
55 | WebServer server(80);
56 |
57 | // Enter a MAC address and IP address for your controller below.
58 | #define NUMBER_OF_MAC 20
59 |
60 | byte mac[][NUMBER_OF_MAC] =
61 | {
62 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01 },
63 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x02 },
64 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x03 },
65 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x04 },
66 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x05 },
67 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x06 },
68 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x07 },
69 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x08 },
70 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x09 },
71 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0A },
72 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0B },
73 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0C },
74 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0D },
75 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0E },
76 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0F },
77 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x10 },
78 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x11 },
79 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x12 },
80 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x13 },
81 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x14 },
82 | };
83 |
84 | // Select the IP address according to your local network
85 | IPAddress myIP(192, 168, 2, 232);
86 | IPAddress myGW(192, 168, 2, 1);
87 | IPAddress mySN(255, 255, 255, 0);
88 |
89 | // Google DNS Server IP
90 | IPAddress myDNS(8, 8, 8, 8);
91 |
92 | #include
93 |
94 | // Update these with values suitable for your network.
95 | //const char* mqttServer = "broker.example"; // Broker address
96 | const char* mqttServer = "broker.emqx.io"; // Broker address
97 | //const char* mqttServer = "broker.shiftr.io"; // Broker address
98 |
99 | const char *ID = "MQTTClient_SSL-Client"; // Name of our device, must be unique
100 | const char *TOPIC = "MQTT_Pub"; // Topic to subscribe to
101 | const char *subTopic = "MQTT_Sub"; // Topic to subscribe to
102 |
103 | //IPAddress mqttServer(172, 16, 0, 2);
104 |
105 | void callback(char* topic, byte* payload, unsigned int length)
106 | {
107 | Serial.print("Message arrived [");
108 | Serial.print(topic);
109 | Serial.print("] ");
110 |
111 | for (unsigned int i = 0; i < length; i++)
112 | {
113 | Serial.print((char)payload[i]);
114 | }
115 |
116 | Serial.println();
117 | }
118 |
119 | WiFiClient ethClient;
120 | PubSubClient client(mqttServer, 1883, callback, ethClient);
121 |
122 | void reconnect()
123 | {
124 | // Loop until we're reconnected
125 | while (!client.connected())
126 | {
127 | //Serial.print("Attempting MQTT connection to ");
128 | //Serial.print(mqttServer);
129 |
130 | // Attempt to connect
131 | if (client.connect(ID, "try", "try"))
132 | {
133 | //Serial.println("...connected");
134 |
135 | // Once connected, publish an announcement...
136 | String data = "Hello from MQTTClient_SSL on " + String(BOARD_NAME);
137 |
138 | client.publish(TOPIC, data.c_str());
139 |
140 | //Serial.println("Published connection message successfully!");
141 | //Serial.print("Subscribed to: ");
142 | //Serial.println(subTopic);
143 |
144 | // This is a workaround to address https://github.com/OPEnSLab-OSU/SSLClient/issues/9
145 | //ethClientSSL.flush();
146 | // ... and resubscribe
147 | client.subscribe(subTopic);
148 | // for loopback testing
149 | client.subscribe(TOPIC);
150 | // This is a workaround to address https://github.com/OPEnSLab-OSU/SSLClient/issues/9
151 | //ethClientSSL.flush();
152 | }
153 | else
154 | {
155 | Serial.print("...failed, rc=");
156 | Serial.print(client.state());
157 | Serial.println(" try again in 5 seconds");
158 |
159 | // Wait 5 seconds before retrying
160 | delay(5000);
161 | }
162 | }
163 | }
164 |
165 | void setup()
166 | {
167 | Serial.begin(115200);
168 |
169 | while (!Serial && (millis() < 5000));
170 |
171 | Serial.print(F("\nStart MQTTClient_Basic on "));
172 | Serial.print(ARDUINO_BOARD);
173 | Serial.print(F(" with "));
174 | Serial.println(SHIELD_TYPE);
175 | Serial.println(WEBSERVER_ESP32_W5500_VERSION);
176 |
177 | ET_LOGWARN(F("Default SPI pinout:"));
178 | ET_LOGWARN1(F("SPI_HOST:"), ETH_SPI_HOST);
179 | ET_LOGWARN1(F("MOSI:"), MOSI_GPIO);
180 | ET_LOGWARN1(F("MISO:"), MISO_GPIO);
181 | ET_LOGWARN1(F("SCK:"), SCK_GPIO);
182 | ET_LOGWARN1(F("CS:"), CS_GPIO);
183 | ET_LOGWARN1(F("INT:"), INT_GPIO);
184 | ET_LOGWARN1(F("SPI Clock (MHz):"), SPI_CLOCK_MHZ);
185 | ET_LOGWARN(F("========================="));
186 |
187 | ///////////////////////////////////
188 |
189 | // To be called before ETH.begin()
190 | ESP32_W5500_onEvent();
191 |
192 | // start the ethernet connection and the server:
193 | // Use DHCP dynamic IP and random mac
194 | //bool begin(int MISO_GPIO, int MOSI_GPIO, int SCLK_GPIO, int CS_GPIO, int INT_GPIO, int SPI_CLOCK_MHZ,
195 | // int SPI_HOST, uint8_t *W6100_Mac = W6100_Default_Mac);
196 | ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST );
197 | //ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST, mac[millis() % NUMBER_OF_MAC] );
198 |
199 | // Static IP, leave without this line to get IP via DHCP
200 | //bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
201 | //ETH.config(myIP, myGW, mySN, myDNS);
202 |
203 | ESP32_W5500_waitForConnect();
204 |
205 | ///////////////////////////////////
206 |
207 | client.setServer(mqttServer, 1883);
208 | client.setCallback(callback);
209 |
210 | // Allow the hardware to sort itself out
211 | delay(1500);
212 | }
213 |
214 | #define MQTT_PUBLISH_INTERVAL_MS 5000L
215 |
216 | String data = "Hello from MQTTClient_Basic on " + String(ARDUINO_BOARD) + " with " + String(SHIELD_TYPE);
217 | const char *pubData = data.c_str();
218 |
219 | unsigned long lastMsg = 0;
220 |
221 | void loop()
222 | {
223 | static unsigned long now;
224 |
225 | if (!client.connected())
226 | {
227 | reconnect();
228 | }
229 |
230 | // Sending Data
231 | now = millis();
232 |
233 | if (now - lastMsg > MQTT_PUBLISH_INTERVAL_MS)
234 | {
235 | lastMsg = now;
236 |
237 | if (!client.publish(TOPIC, pubData))
238 | {
239 | Serial.println("Message failed to send.");
240 | }
241 |
242 | Serial.print("Message Send : " + String(TOPIC) + " => ");
243 | Serial.println(data);
244 | }
245 |
246 | client.loop();
247 | }
248 |
--------------------------------------------------------------------------------
/examples/MQTT_ThingStream/MQTT_ThingStream.ino:
--------------------------------------------------------------------------------
1 | /****************************************************************************************************************************
2 | MQTT_ThingStream.ino - Dead simple MQTT Client for Ethernet shields
3 |
4 | For Ethernet shields using ESP32_W5500 (ESP32 + W5500)
5 |
6 | WebServer_ESP32_W5500 is a library for the ESP32 with Ethernet W5500 to run WebServer
7 |
8 | Based on and modified from ESP32-IDF https://github.com/espressif/esp-idf
9 | Built by Khoi Hoang https://github.com/khoih-prog/WebServer_ESP32_W5500
10 | Licensed under GPLv3 license
11 | *****************************************************************************************************************************/
12 | /*
13 | Basic MQTT example (without SSL!)
14 | This sketch demonstrates the basic capabilities of the library.
15 | It connects to an MQTT server then:
16 | - publishes {Hello from MQTTClient_SSL on NUCLEO_F767ZI} to the topic [STM32_Pub]
17 | - subscribes to the topic [STM32_Sub], printing out any messages
18 | it receives. NB - it assumes the received payloads are strings not binary
19 | It will reconnect to the server if the connection is lost using a blocking
20 | reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to
21 | achieve the same result without blocking the main loop.
22 |
23 | You will need to populate "certificates.h" with your trust anchors
24 | (see https://github.com/OPEnSLab-OSU/SSLClient/blob/master/TrustAnchors.md)
25 | and my_cert/my_key with your certificate/private key pair
26 | (see https://github.com/OPEnSLab-OSU/SSLClient#mtls).
27 | */
28 |
29 | #if !( defined(ESP32) )
30 | #error This code is designed for (ESP32 + W5500) to run on ESP32 platform! Please check your Tools->Board setting.
31 | #endif
32 |
33 | #define DEBUG_ETHERNET_WEBSERVER_PORT Serial
34 |
35 | // Debug Level from 0 to 4
36 | #define _ETHERNET_WEBSERVER_LOGLEVEL_ 3
37 |
38 | //////////////////////////////////////////////////////////
39 |
40 | // Optional values to override default settings
41 | // Don't change unless you know what you're doing
42 | //#define ETH_SPI_HOST SPI3_HOST
43 | //#define SPI_CLOCK_MHZ 25
44 |
45 | // Must connect INT to GPIOxx or not working
46 | //#define INT_GPIO 4
47 |
48 | //#define MISO_GPIO 19
49 | //#define MOSI_GPIO 23
50 | //#define SCK_GPIO 18
51 | //#define CS_GPIO 5
52 |
53 | //////////////////////////////////////////////////////////
54 |
55 | #include
56 |
57 | WebServer server(80);
58 |
59 | // Enter a MAC address and IP address for your controller below.
60 | #define NUMBER_OF_MAC 20
61 |
62 | byte mac[][NUMBER_OF_MAC] =
63 | {
64 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01 },
65 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x02 },
66 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x03 },
67 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x04 },
68 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x05 },
69 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x06 },
70 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x07 },
71 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x08 },
72 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x09 },
73 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0A },
74 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0B },
75 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0C },
76 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0D },
77 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0E },
78 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0F },
79 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x10 },
80 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x11 },
81 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x12 },
82 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x13 },
83 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x14 },
84 | };
85 |
86 | // Select the IP address according to your local network
87 | IPAddress myIP(192, 168, 2, 232);
88 | IPAddress myGW(192, 168, 2, 1);
89 | IPAddress mySN(255, 255, 255, 0);
90 |
91 | // Google DNS Server IP
92 | IPAddress myDNS(8, 8, 8, 8);
93 |
94 | #include
95 |
96 | const char my_cert[] = "FIXME";
97 | const char my_key[] = "FIXME";
98 |
99 | #define USING_THINGSTREAM_IO false //true
100 |
101 | #if USING_THINGSTREAM_IO
102 |
103 | const char *MQTT_PREFIX_TOPIC = "esp32-sniffer/";
104 | const char *MQTT_ANNOUNCE_TOPIC = "/status";
105 | const char *MQTT_CONTROL_TOPIC = "/control";
106 | const char *MQTT_BLE_TOPIC = "/ble";
107 |
108 |
109 | // GOT FROM ThingsStream!
110 | const char *MQTT_SERVER = "mqtt.thingstream.io";
111 | const char *MQTT_USER = "MQTT_USER";
112 | const char *MQTT_PASS = "MQTT_PASS";
113 | const char *MQTT_CLIENT_ID = "MQTT_CLIENT_ID";
114 |
115 | String topic = MQTT_PREFIX_TOPIC + String("12345678") + MQTT_BLE_TOPIC;
116 | String subTopic = MQTT_PREFIX_TOPIC + String("12345678") + MQTT_BLE_TOPIC;
117 |
118 | #else
119 |
120 | const char* MQTT_SERVER = "broker.emqx.io"; // Broker address
121 |
122 | const char* ID = "MQTTClient_SSL-Client"; // Name of our device, must be unique
123 | String topic = "ESP32_Pub"; // Topic to subscribe to
124 | String subTopic = "ESP32_Sub"; // Topic to subscribe to
125 |
126 | #endif
127 |
128 | void mqtt_receive_callback(char* topic, byte* payload, unsigned int length);
129 |
130 | const int MQTT_PORT = 1883; //if you use SSL //1883 no SSL
131 |
132 | unsigned long lastMsg = 0;
133 |
134 | // Initialize the SSL client library
135 | // Arguments: EthernetClient, our trust anchors
136 |
137 |
138 | WiFiClient ethClient;
139 |
140 | PubSubClient client(MQTT_SERVER, MQTT_PORT, mqtt_receive_callback, ethClient);
141 |
142 | /*
143 | Called whenever a payload is received from a subscribed MQTT topic
144 | */
145 | void mqtt_receive_callback(char* topic, byte* payload, unsigned int length)
146 | {
147 | Serial.print("MQTT Message receive [");
148 | Serial.print(topic);
149 | Serial.print("] ");
150 |
151 | for (unsigned int i = 0; i < length; i++)
152 | {
153 | Serial.print((char)payload[i]);
154 | }
155 |
156 | Serial.println();
157 | }
158 |
159 | void reconnect()
160 | {
161 | // Loop until we're reconnected
162 | while (!client.connected())
163 | {
164 | //Serial.print("Attempting MQTT connection to ");
165 | //Serial.println(MQTT_SERVER);
166 |
167 | // Attempt to connect
168 |
169 | #if USING_THINGSTREAM_IO
170 | int connect_status = client.connect(MQTT_CLIENT_ID, MQTT_USER, MQTT_PASS, topic.c_str(), 2, false, "");
171 | #else
172 | int connect_status = client.connect(ID);
173 | #endif
174 |
175 | if (connect_status)
176 | {
177 | //Serial.println("...connected");
178 |
179 | // Once connected, publish an announcement...
180 | String data = "Hello from MQTTClient_SSL on " + String(BOARD_NAME);
181 |
182 | client.publish(topic.c_str(), data.c_str());
183 |
184 | //Serial.println("Published connection message successfully!");
185 |
186 | //Serial.print("Subscribed to: ");
187 | //Serial.println(subTopic);
188 |
189 | // This is a workaround to address https://github.com/OPEnSLab-OSU/SSLClient/issues/9
190 | //ethClientSSL.flush();
191 | // ... and resubscribe
192 | client.subscribe(subTopic.c_str());
193 | // for loopback testing
194 | client.subscribe(topic.c_str());
195 | // This is a workaround to address https://github.com/OPEnSLab-OSU/SSLClient/issues/9
196 | //ethClientSSL.flush();
197 | }
198 | else
199 | {
200 | Serial.print("failed, rc=");
201 | Serial.print(client.state());
202 | Serial.println(" try again in 5 seconds");
203 |
204 | // Wait 5 seconds before retrying
205 | delay(5000);
206 | }
207 | }
208 | }
209 |
210 | void setup()
211 | {
212 | Serial.begin(115200);
213 |
214 | while (!Serial && (millis() < 5000));
215 |
216 | Serial.print(F("\nStart MQTT_ThingStream on "));
217 | Serial.print(ARDUINO_BOARD);
218 | Serial.print(F(" with "));
219 | Serial.println(SHIELD_TYPE);
220 | Serial.println(WEBSERVER_ESP32_W5500_VERSION);
221 |
222 | ET_LOGWARN(F("Default SPI pinout:"));
223 | ET_LOGWARN1(F("SPI_HOST:"), ETH_SPI_HOST);
224 | ET_LOGWARN1(F("MOSI:"), MOSI_GPIO);
225 | ET_LOGWARN1(F("MISO:"), MISO_GPIO);
226 | ET_LOGWARN1(F("SCK:"), SCK_GPIO);
227 | ET_LOGWARN1(F("CS:"), CS_GPIO);
228 | ET_LOGWARN1(F("INT:"), INT_GPIO);
229 | ET_LOGWARN1(F("SPI Clock (MHz):"), SPI_CLOCK_MHZ);
230 | ET_LOGWARN(F("========================="));
231 |
232 | ///////////////////////////////////
233 |
234 | // To be called before ETH.begin()
235 | ESP32_W5500_onEvent();
236 |
237 | // start the ethernet connection and the server:
238 | // Use DHCP dynamic IP and random mac
239 | //bool begin(int MISO_GPIO, int MOSI_GPIO, int SCLK_GPIO, int CS_GPIO, int INT_GPIO, int SPI_CLOCK_MHZ,
240 | // int SPI_HOST, uint8_t *W6100_Mac = W6100_Default_Mac);
241 | ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST );
242 | //ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST, mac[millis() % NUMBER_OF_MAC] );
243 |
244 | // Static IP, leave without this line to get IP via DHCP
245 | //bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
246 | //ETH.config(myIP, myGW, mySN, myDNS);
247 |
248 | ESP32_W5500_waitForConnect();
249 |
250 | ///////////////////////////////////
251 |
252 | // Note - the default maximum packet size is 256 bytes. If the
253 | // combined length of clientId, username and password exceed this use the
254 | // following to increase the buffer size:
255 | //client.setBufferSize(256);
256 |
257 | Serial.println("***************************************");
258 | Serial.println(topic);
259 | Serial.println("***************************************");
260 | }
261 |
262 | #define MQTT_PUBLISH_INTERVAL_MS 5000L
263 |
264 | String data = "Hello from MQTT_ThingStream on " + String(BOARD_NAME) + " with " + String(SHIELD_TYPE);
265 | const char *pubData = data.c_str();
266 |
267 | void loop()
268 | {
269 | static unsigned long now;
270 |
271 | if (!client.connected())
272 | {
273 | reconnect();
274 | }
275 |
276 | // Sending Data
277 | now = millis();
278 |
279 | if (now - lastMsg > MQTT_PUBLISH_INTERVAL_MS)
280 | {
281 | lastMsg = now;
282 |
283 | if (!client.publish(topic.c_str(), pubData))
284 | {
285 | Serial.println("Message failed to send.");
286 | }
287 |
288 | Serial.print("MQTT Message Send : " + topic + " => ");
289 | Serial.println(data);
290 | }
291 |
292 | client.loop();
293 | }
294 |
--------------------------------------------------------------------------------
/examples/PostServer/PostServer.ino:
--------------------------------------------------------------------------------
1 | /****************************************************************************************************************************
2 | PostServer.h - Dead simple web-server for Ethernet shields
3 |
4 | For Ethernet shields using ESP32_W5500 (ESP32 + W5500)
5 |
6 | WebServer_ESP32_W5500 is a library for the ESP32 with Ethernet W5500 to run WebServer
7 |
8 | Based on and modified from ESP32-IDF https://github.com/espressif/esp-idf
9 | Built by Khoi Hoang https://github.com/khoih-prog/WebServer_ESP32_W5500
10 | Licensed under GPLv3 license
11 | *****************************************************************************************************************************/
12 |
13 | #if !( defined(ESP32) )
14 | #error This code is designed for (ESP32 + W5500) to run on ESP32 platform! Please check your Tools->Board setting.
15 | #endif
16 |
17 | #define DEBUG_ETHERNET_WEBSERVER_PORT Serial
18 |
19 | // Debug Level from 0 to 4
20 | #define _ETHERNET_WEBSERVER_LOGLEVEL_ 3
21 |
22 | //////////////////////////////////////////////////////////
23 |
24 | // Optional values to override default settings
25 | // Don't change unless you know what you're doing
26 | //#define ETH_SPI_HOST SPI3_HOST
27 | //#define SPI_CLOCK_MHZ 25
28 |
29 | // Must connect INT to GPIOxx or not working
30 | //#define INT_GPIO 4
31 |
32 | //#define MISO_GPIO 19
33 | //#define MOSI_GPIO 23
34 | //#define SCK_GPIO 18
35 | //#define CS_GPIO 5
36 |
37 | //////////////////////////////////////////////////////////
38 |
39 | #include
40 |
41 | WebServer server(80);
42 |
43 | // Enter a MAC address and IP address for your controller below.
44 | #define NUMBER_OF_MAC 20
45 |
46 | byte mac[][NUMBER_OF_MAC] =
47 | {
48 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01 },
49 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x02 },
50 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x03 },
51 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x04 },
52 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x05 },
53 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x06 },
54 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x07 },
55 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x08 },
56 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x09 },
57 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0A },
58 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0B },
59 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0C },
60 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0D },
61 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0E },
62 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0F },
63 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x10 },
64 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x11 },
65 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x12 },
66 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x13 },
67 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x14 },
68 | };
69 |
70 | // Select the IP address according to your local network
71 | IPAddress myIP(192, 168, 2, 232);
72 | IPAddress myGW(192, 168, 2, 1);
73 | IPAddress mySN(255, 255, 255, 0);
74 |
75 | // Google DNS Server IP
76 | IPAddress myDNS(8, 8, 8, 8);
77 |
78 | const String postForms =
79 | F("\
80 | \
81 | WebServer_ESP32_ENC POST handling\
82 | \
85 | \
86 | \
87 | POST plain text to /postplain/
\
88 | \
92 | POST form data to /postform/
\
93 | \
97 | \
98 | ");
99 |
100 | void handleRoot()
101 | {
102 | server.send(200, F("text/html"), postForms);
103 | }
104 |
105 | void handlePlain()
106 | {
107 | if (server.method() != HTTP_POST)
108 | {
109 | server.send(405, F("text/plain"), F("Method Not Allowed"));
110 | }
111 | else
112 | {
113 | server.send(200, F("text/plain"), "POST body was:\n" + server.arg("plain"));
114 | }
115 | }
116 |
117 | void handleForm()
118 | {
119 | if (server.method() != HTTP_POST)
120 | {
121 | server.send(405, F("text/plain"), F("Method Not Allowed"));
122 | }
123 | else
124 | {
125 | String message = F("POST form was:\n");
126 |
127 | for (uint8_t i = 0; i < server.args(); i++)
128 | {
129 | message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
130 | }
131 |
132 | server.send(200, F("text/plain"), message);
133 | }
134 | }
135 |
136 | void handleNotFound()
137 | {
138 | String message = F("File Not Found\n\n");
139 |
140 | message += F("URI: ");
141 | message += server.uri();
142 | message += F("\nMethod: ");
143 | message += (server.method() == HTTP_GET) ? F("GET") : F("POST");
144 | message += F("\nArguments: ");
145 | message += server.args();
146 | message += F("\n");
147 |
148 | for (uint8_t i = 0; i < server.args(); i++)
149 | {
150 | message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
151 | }
152 |
153 | server.send(404, F("text/plain"), message);
154 | }
155 |
156 | void setup()
157 | {
158 | Serial.begin(115200);
159 |
160 | while (!Serial && (millis() < 5000));
161 |
162 | Serial.print(F("\nStart POSTServer on "));
163 | Serial.print(ARDUINO_BOARD);
164 | Serial.print(F(" with "));
165 | Serial.println(SHIELD_TYPE);
166 | Serial.println(WEBSERVER_ESP32_W5500_VERSION);
167 |
168 | ET_LOGWARN(F("Default SPI pinout:"));
169 | ET_LOGWARN1(F("SPI_HOST:"), ETH_SPI_HOST);
170 | ET_LOGWARN1(F("MOSI:"), MOSI_GPIO);
171 | ET_LOGWARN1(F("MISO:"), MISO_GPIO);
172 | ET_LOGWARN1(F("SCK:"), SCK_GPIO);
173 | ET_LOGWARN1(F("CS:"), CS_GPIO);
174 | ET_LOGWARN1(F("INT:"), INT_GPIO);
175 | ET_LOGWARN1(F("SPI Clock (MHz):"), SPI_CLOCK_MHZ);
176 | ET_LOGWARN(F("========================="));
177 |
178 | ///////////////////////////////////
179 |
180 | // To be called before ETH.begin()
181 | ESP32_W5500_onEvent();
182 |
183 | // start the ethernet connection and the server:
184 | // Use DHCP dynamic IP and random mac
185 | //bool begin(int MISO_GPIO, int MOSI_GPIO, int SCLK_GPIO, int CS_GPIO, int INT_GPIO, int SPI_CLOCK_MHZ,
186 | // int SPI_HOST, uint8_t *W6100_Mac = W6100_Default_Mac);
187 | ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST );
188 | //ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST, mac[millis() % NUMBER_OF_MAC] );
189 |
190 | // Static IP, leave without this line to get IP via DHCP
191 | //bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
192 | //ETH.config(myIP, myGW, mySN, myDNS);
193 |
194 | ESP32_W5500_waitForConnect();
195 |
196 | ///////////////////////////////////
197 |
198 | server.on(F("/"), handleRoot);
199 |
200 | server.on(F("/postplain/"), handlePlain);
201 |
202 | server.on(F("/postform/"), handleForm);
203 |
204 | server.onNotFound(handleNotFound);
205 |
206 | server.begin();
207 |
208 | Serial.print(F("HTTP POSTServer started @ IP : "));
209 | Serial.println(ETH.localIP());
210 | }
211 |
212 | void loop()
213 | {
214 | server.handleClient();
215 | }
216 |
--------------------------------------------------------------------------------
/examples/SimpleAuthentication/SimpleAuthentication.ino:
--------------------------------------------------------------------------------
1 | /****************************************************************************************************************************
2 | SimpleAuthentication.ino - Dead simple web-server for Ethernet shields
3 |
4 | For Ethernet shields using ESP32_W5500 (ESP32 + W5500)
5 |
6 | WebServer_ESP32_W5500 is a library for the ESP32 with Ethernet W5500 to run WebServer
7 |
8 | Based on and modified from ESP32-IDF https://github.com/espressif/esp-idf
9 | Built by Khoi Hoang https://github.com/khoih-prog/WebServer_ESP32_W5500
10 | Licensed under GPLv3 license
11 | *****************************************************************************************************************************/
12 |
13 | #if !( defined(ESP32) )
14 | #error This code is designed for (ESP32 + W5500) to run on ESP32 platform! Please check your Tools->Board setting.
15 | #endif
16 |
17 | #define DEBUG_ETHERNET_WEBSERVER_PORT Serial
18 |
19 | // Debug Level from 0 to 4
20 | #define _ETHERNET_WEBSERVER_LOGLEVEL_ 3
21 |
22 | //////////////////////////////////////////////////////////
23 |
24 | // Optional values to override default settings
25 | // Don't change unless you know what you're doing
26 | //#define ETH_SPI_HOST SPI3_HOST
27 | //#define SPI_CLOCK_MHZ 25
28 |
29 | // Must connect INT to GPIOxx or not working
30 | //#define INT_GPIO 4
31 |
32 | //#define MISO_GPIO 19
33 | //#define MOSI_GPIO 23
34 | //#define SCK_GPIO 18
35 | //#define CS_GPIO 5
36 |
37 | //////////////////////////////////////////////////////////
38 |
39 | #include
40 |
41 | WebServer server(80);
42 |
43 | // Enter a MAC address and IP address for your controller below.
44 | #define NUMBER_OF_MAC 20
45 |
46 | byte mac[][NUMBER_OF_MAC] =
47 | {
48 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01 },
49 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x02 },
50 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x03 },
51 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x04 },
52 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x05 },
53 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x06 },
54 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x07 },
55 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x08 },
56 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x09 },
57 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0A },
58 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0B },
59 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0C },
60 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0D },
61 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0E },
62 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0F },
63 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x10 },
64 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x11 },
65 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x12 },
66 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x13 },
67 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x14 },
68 | };
69 |
70 | // Select the IP address according to your local network
71 | IPAddress myIP(192, 168, 2, 232);
72 | IPAddress myGW(192, 168, 2, 1);
73 | IPAddress mySN(255, 255, 255, 0);
74 |
75 | // Google DNS Server IP
76 | IPAddress myDNS(8, 8, 8, 8);
77 |
78 | //Check if header is present and correct
79 | bool is_authenticated()
80 | {
81 | Serial.println(F("Enter is_authenticated"));
82 |
83 | if (server.hasHeader(F("Cookie")))
84 | {
85 | Serial.print(F("Found cookie: "));
86 | String cookie = server.header(F("Cookie"));
87 | Serial.println(cookie);
88 |
89 | if (cookie.indexOf(F("ESPSESSIONID=1")) != -1)
90 | {
91 | Serial.println(F("Authentication Successful"));
92 | return true;
93 | }
94 | }
95 |
96 | Serial.println(F("Authentication Failed"));
97 | return false;
98 | }
99 |
100 | //login page, also called for disconnect
101 | void handleLogin()
102 | {
103 | String msg;
104 |
105 | if (server.hasHeader(F("Cookie")))
106 | {
107 | Serial.print(F("Found cookie: "));
108 | String cookie = server.header(F("Cookie"));
109 | Serial.println(cookie);
110 | }
111 |
112 | if (server.hasArg(F("DISCONNECT")))
113 | {
114 | Serial.println(F("Disconnection"));
115 | server.sendHeader(F("Location"), F("/login"));
116 | server.sendHeader(F("Cache-Control"), F("no-cache"));
117 | server.sendHeader(F("Set-Cookie"), F("ESPSESSIONID=0"));
118 | server.send(301);
119 | return;
120 | }
121 |
122 | if (server.hasArg(F("USERNAME")) && server.hasArg(F("PASSWORD")))
123 | {
124 | if (server.arg(F("USERNAME")) == F("admin") && server.arg(F("PASSWORD")) == F("password"))
125 | {
126 | server.sendHeader(F("Location"), F("/"));
127 | server.sendHeader(F("Cache-Control"), F("no-cache"));
128 | server.sendHeader(F("Set-Cookie"), F("ESPSESSIONID=1"));
129 | server.send(301);
130 | Serial.println(F("Log in Successful"));
131 | return;
132 | }
133 |
134 | msg = F("Wrong username/password! try again.");
135 | Serial.println(F("Log in Failed"));
136 | }
137 |
138 | String content = F("");
142 | content += msg;
143 | content += F("
");
144 | content += F("You also can go here");
145 | server.send(200, F("text/html"), content);
146 | }
147 |
148 | //root page can be accessed only if authentication is ok
149 | void handleRoot()
150 | {
151 | String header;
152 |
153 | Serial.println(F("Enter handleRoot"));
154 |
155 | if (!is_authenticated())
156 | {
157 | server.sendHeader(F("Location"), F("/login"));
158 | server.sendHeader(F("Cache-Control"), F("no-cache"));
159 | server.send(301);
160 | return;
161 | }
162 |
163 | String content = F("Hello, you're connected to WebServer_ESP32_W5500 running on ");
164 |
165 | content += String(ARDUINO_BOARD);
166 | content += F("!
");
167 |
168 | if (server.hasHeader(F("User-Agent")))
169 | {
170 | content += F("the user agent used is : ");
171 | content += server.header(F("User-Agent"));
172 | content += F("
");
173 | }
174 |
175 | content += F("You can access this page until you disconnect");
176 | server.send(200, F("text/html"), content);
177 | }
178 |
179 | //no need authentication
180 | void handleNotFound()
181 | {
182 | String message = F("File Not Found\n\n");
183 |
184 | message += F("URI: ");
185 | message += server.uri();
186 | message += F("\nMethod: ");
187 | message += (server.method() == HTTP_GET) ? F("GET") : F("POST");
188 | message += F("\nArguments: ");
189 | message += server.args();
190 | message += F("\n");
191 |
192 | for (uint8_t i = 0; i < server.args(); i++)
193 | {
194 | message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
195 | }
196 |
197 | server.send(404, F("text/plain"), message);
198 | }
199 |
200 | void setup()
201 | {
202 | Serial.begin(115200);
203 |
204 | while (!Serial && (millis() < 5000));
205 |
206 | Serial.print(F("\nStart SimpleAuthentication on "));
207 | Serial.print(ARDUINO_BOARD);
208 | Serial.print(F(" with "));
209 | Serial.println(SHIELD_TYPE);
210 | Serial.println(WEBSERVER_ESP32_W5500_VERSION);
211 |
212 | ET_LOGWARN(F("Default SPI pinout:"));
213 | ET_LOGWARN1(F("SPI_HOST:"), ETH_SPI_HOST);
214 | ET_LOGWARN1(F("MOSI:"), MOSI_GPIO);
215 | ET_LOGWARN1(F("MISO:"), MISO_GPIO);
216 | ET_LOGWARN1(F("SCK:"), SCK_GPIO);
217 | ET_LOGWARN1(F("CS:"), CS_GPIO);
218 | ET_LOGWARN1(F("INT:"), INT_GPIO);
219 | ET_LOGWARN1(F("SPI Clock (MHz):"), SPI_CLOCK_MHZ);
220 | ET_LOGWARN(F("========================="));
221 |
222 | ///////////////////////////////////
223 |
224 | // To be called before ETH.begin()
225 | ESP32_W5500_onEvent();
226 |
227 | // start the ethernet connection and the server:
228 | // Use DHCP dynamic IP and random mac
229 | //bool begin(int MISO_GPIO, int MOSI_GPIO, int SCLK_GPIO, int CS_GPIO, int INT_GPIO, int SPI_CLOCK_MHZ,
230 | // int SPI_HOST, uint8_t *W6100_Mac = W6100_Default_Mac);
231 | ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST );
232 | //ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST, mac[millis() % NUMBER_OF_MAC] );
233 |
234 | // Static IP, leave without this line to get IP via DHCP
235 | //bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
236 | //ETH.config(myIP, myGW, mySN, myDNS);
237 |
238 | ESP32_W5500_waitForConnect();
239 |
240 | ///////////////////////////////////
241 |
242 | server.on(F("/"), handleRoot);
243 |
244 | server.on(F("/login"), handleLogin);
245 |
246 | server.on(F("/inline"), []()
247 | {
248 | server.send(200, F("text/plain"), F("This works without need of authentication"));
249 | });
250 |
251 | server.onNotFound(handleNotFound);
252 |
253 | //here the list of headers to be recorded
254 | const char * headerkeys[] = {"User-Agent", "Cookie"} ;
255 | size_t headerkeyssize = sizeof(headerkeys) / sizeof(char*);
256 |
257 | //ask server to track these headers
258 | server.collectHeaders(headerkeys, headerkeyssize);
259 | server.begin();
260 |
261 | Serial.print(F("HTTP SimpleAuthentication is @ IP : "));
262 | Serial.println(ETH.localIP());
263 | }
264 |
265 | void loop()
266 | {
267 | server.handleClient();
268 | }
269 |
--------------------------------------------------------------------------------
/examples/UdpNTPClient/UdpNTPClient.ino:
--------------------------------------------------------------------------------
1 | /****************************************************************************************************************************
2 | UdpNTPClient.ino - Simple Arduino web server sample for ESP8266 AT-command shield
3 |
4 | For Ethernet shields using ESP32_W5500 (ESP32 + W5500)
5 |
6 | WebServer_ESP32_W5500 is a library for the ESP32 with Ethernet W5500 to run WebServer
7 |
8 | Based on and modified from ESP32-IDF https://github.com/espressif/esp-idf
9 | Built by Khoi Hoang https://github.com/khoih-prog/WebServer_ESP32_W5500
10 | Licensed under GPLv3 license
11 | *****************************************************************************************************************************/
12 | /*
13 | The Arduino board communicates with the shield using the SPI bus. This is on digital pins 11, 12, and 13 on the Uno
14 | and pins 50, 51, and 52 on the Mega. On both boards, pin 10 is used as SS. On the Mega, the hardware SS pin, 53,
15 | is not used to select the Ethernet controller chip, but it must be kept as an output or the SPI interface won't work.
16 | */
17 |
18 | #if !( defined(ESP32) )
19 | #error This code is designed for (ESP32 + W5500) to run on ESP32 platform! Please check your Tools->Board setting.
20 | #endif
21 |
22 | #define DEBUG_ETHERNET_WEBSERVER_PORT Serial
23 |
24 | // Debug Level from 0 to 4
25 | #define _ETHERNET_WEBSERVER_LOGLEVEL_ 3
26 |
27 | //////////////////////////////////////////////////////////
28 |
29 | // Optional values to override default settings
30 | // Don't change unless you know what you're doing
31 | //#define ETH_SPI_HOST SPI3_HOST
32 | //#define SPI_CLOCK_MHZ 25
33 |
34 | // Must connect INT to GPIOxx or not working
35 | //#define INT_GPIO 4
36 |
37 | //#define MISO_GPIO 19
38 | //#define MOSI_GPIO 23
39 | //#define SCK_GPIO 18
40 | //#define CS_GPIO 5
41 |
42 | //////////////////////////////////////////////////////////
43 |
44 | #include
45 |
46 | // Enter a MAC address and IP address for your controller below.
47 | #define NUMBER_OF_MAC 20
48 |
49 | byte mac[][NUMBER_OF_MAC] =
50 | {
51 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01 },
52 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x02 },
53 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x03 },
54 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x04 },
55 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x05 },
56 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x06 },
57 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x07 },
58 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x08 },
59 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x09 },
60 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0A },
61 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0B },
62 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0C },
63 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0D },
64 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0E },
65 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0F },
66 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x10 },
67 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x11 },
68 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x12 },
69 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x13 },
70 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x14 },
71 | };
72 |
73 | // Select the IP address according to your local network
74 | IPAddress myIP(192, 168, 2, 232);
75 | IPAddress myGW(192, 168, 2, 1);
76 | IPAddress mySN(255, 255, 255, 0);
77 |
78 | // Google DNS Server IP
79 | IPAddress myDNS(8, 8, 8, 8);
80 |
81 | char timeServer[] = "time.nist.gov"; // NTP server
82 | unsigned int localPort = 2390; // local port to listen for UDP packets
83 |
84 | const int NTP_PACKET_SIZE = 48; // NTP timestamp is in the first 48 bytes of the message
85 | const int UDP_TIMEOUT = 2000; // timeout in milliseconds to wait for an UDP packet to arrive
86 |
87 | byte packetBuffer[NTP_PACKET_SIZE]; // buffer to hold incoming and outgoing packets
88 |
89 | // A UDP instance to let us send and receive packets over UDP
90 | WiFiUDP Udp;
91 |
92 | // send an NTP request to the time server at the given address
93 | void sendNTPpacket(char *ntpSrv)
94 | {
95 | // set all bytes in the buffer to 0
96 | memset(packetBuffer, 0, NTP_PACKET_SIZE);
97 | // Initialize values needed to form NTP request
98 | // (see URL above for details on the packets)
99 |
100 | packetBuffer[0] = 0b11100011; // LI, Version, Mode
101 | packetBuffer[1] = 0; // Stratum, or type of clock
102 | packetBuffer[2] = 6; // Polling Interval
103 | packetBuffer[3] = 0xEC; // Peer Clock Precision
104 | // 8 bytes of zero for Root Delay & Root Dispersion
105 | packetBuffer[12] = 49;
106 | packetBuffer[13] = 0x4E;
107 | packetBuffer[14] = 49;
108 | packetBuffer[15] = 52;
109 |
110 | // all NTP fields have been given values, now
111 | // you can send a packet requesting a timestamp:
112 | Udp.beginPacket(ntpSrv, 123); //NTP requests are to port 123
113 |
114 | Udp.write(packetBuffer, NTP_PACKET_SIZE);
115 |
116 | Udp.endPacket();
117 | }
118 |
119 | void setup()
120 | {
121 | Serial.begin(115200);
122 |
123 | while (!Serial && (millis() < 5000));
124 |
125 | Serial.print(F("\nStart UdpNTPClient on "));
126 | Serial.print(ARDUINO_BOARD);
127 | Serial.print(F(" with "));
128 | Serial.println(SHIELD_TYPE);
129 | Serial.println(WEBSERVER_ESP32_W5500_VERSION);
130 |
131 | ET_LOGWARN(F("Default SPI pinout:"));
132 | ET_LOGWARN1(F("SPI_HOST:"), ETH_SPI_HOST);
133 | ET_LOGWARN1(F("MOSI:"), MOSI_GPIO);
134 | ET_LOGWARN1(F("MISO:"), MISO_GPIO);
135 | ET_LOGWARN1(F("SCK:"), SCK_GPIO);
136 | ET_LOGWARN1(F("CS:"), CS_GPIO);
137 | ET_LOGWARN1(F("INT:"), INT_GPIO);
138 | ET_LOGWARN1(F("SPI Clock (MHz):"), SPI_CLOCK_MHZ);
139 | ET_LOGWARN(F("========================="));
140 |
141 | ///////////////////////////////////
142 |
143 | // To be called before ETH.begin()
144 | ESP32_W5500_onEvent();
145 |
146 | // start the ethernet connection and the server:
147 | // Use DHCP dynamic IP and random mac
148 | //bool begin(int MISO_GPIO, int MOSI_GPIO, int SCLK_GPIO, int CS_GPIO, int INT_GPIO, int SPI_CLOCK_MHZ,
149 | // int SPI_HOST, uint8_t *W6100_Mac = W6100_Default_Mac);
150 | ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST );
151 | //ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST, mac[millis() % NUMBER_OF_MAC] );
152 |
153 | // Static IP, leave without this line to get IP via DHCP
154 | //bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
155 | //ETH.config(myIP, myGW, mySN, myDNS);
156 |
157 | ESP32_W5500_waitForConnect();
158 |
159 | ///////////////////////////////////
160 |
161 | Udp.begin(localPort);
162 | }
163 |
164 | void loop()
165 | {
166 | sendNTPpacket(timeServer); // send an NTP packet to a time server
167 |
168 | // wait for a reply for UDP_TIMEOUT milliseconds
169 | unsigned long startMs = millis();
170 |
171 | while (!Udp.available() && (millis() - startMs) < UDP_TIMEOUT) {}
172 |
173 | // if there's data available, read a packet
174 | int packetSize = Udp.parsePacket();
175 |
176 | if (packetSize)
177 | {
178 | Serial.print(F("UDP Packet received, size "));
179 | Serial.println(packetSize);
180 | Serial.print(F("From "));
181 | IPAddress remoteIp = Udp.remoteIP();
182 | Serial.print(remoteIp);
183 | Serial.print(F(", port "));
184 | Serial.println(Udp.remotePort());
185 |
186 | // We've received a packet, read the data from it into the buffer
187 | Udp.read(packetBuffer, NTP_PACKET_SIZE);
188 |
189 | // the timestamp starts at byte 40 of the received packet and is four bytes,
190 | // or two words, long. First, esxtract the two words:
191 |
192 | unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
193 | unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
194 |
195 | // combine the four bytes (two words) into a long integer
196 | // this is NTP time (seconds since Jan 1 1900):
197 | unsigned long secsSince1900 = highWord << 16 | lowWord;
198 |
199 | Serial.print(F("Seconds since Jan 1 1900 = "));
200 | Serial.println(secsSince1900);
201 |
202 | // now convert NTP time into )everyday time:
203 | Serial.print(F("Unix time = "));
204 | // Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
205 | const unsigned long seventyYears = 2208988800UL;
206 | // subtract seventy years:
207 | unsigned long epoch = secsSince1900 - seventyYears;
208 | // print Unix time:
209 | Serial.println(epoch);
210 |
211 | // print the hour, minute and second:
212 | Serial.print(F("The UTC time is ")); // UTC is the time at Greenwich Meridian (GMT)
213 | Serial.print((epoch % 86400L) / 3600); // print the hour (86400 equals secs per day)
214 | Serial.print(F(":"));
215 |
216 | if (((epoch % 3600) / 60) < 10)
217 | {
218 | // In the first 10 minutes of each hour, we'll want a leading '0'
219 | Serial.print(F("0"));
220 | }
221 |
222 | Serial.print((epoch % 3600) / 60); // print the minute (3600 equals secs per minute)
223 | Serial.print(F(":"));
224 |
225 | if ((epoch % 60) < 10)
226 | {
227 | // In the first 10 seconds of each minute, we'll want a leading '0'
228 | Serial.print(F("0"));
229 | }
230 |
231 | Serial.println(epoch % 60); // print the second
232 | }
233 |
234 | // wait ten seconds before asking for the time again
235 | delay(10000);
236 | }
237 |
--------------------------------------------------------------------------------
/examples/UdpSendReceive/UdpSendReceive.ino:
--------------------------------------------------------------------------------
1 | /****************************************************************************************************************************
2 | UDPSendReceive.ino - Simple Arduino web server sample for ESP8266/ESP32 AT-command shield
3 |
4 | For Ethernet shields using ESP32_W5500 (ESP32 + W5500)
5 |
6 | WebServer_ESP32_W5500 is a library for the ESP32 with Ethernet W5500 to run WebServer
7 |
8 | Based on and modified from ESP32-IDF https://github.com/espressif/esp-idf
9 | Built by Khoi Hoang https://github.com/khoih-prog/WebServer_ESP32_W5500
10 | Licensed under GPLv3 license
11 | *****************************************************************************************************************************/
12 |
13 | #if !( defined(ESP32) )
14 | #error This code is designed for (ESP32 + W5500) to run on ESP32 platform! Please check your Tools->Board setting.
15 | #endif
16 |
17 | #define DEBUG_ETHERNET_WEBSERVER_PORT Serial
18 |
19 | // Debug Level from 0 to 4
20 | #define _ETHERNET_WEBSERVER_LOGLEVEL_ 3
21 |
22 | //////////////////////////////////////////////////////////
23 |
24 | // Optional values to override default settings
25 | // Don't change unless you know what you're doing
26 | //#define ETH_SPI_HOST SPI3_HOST
27 | //#define SPI_CLOCK_MHZ 25
28 |
29 | // Must connect INT to GPIOxx or not working
30 | //#define INT_GPIO 4
31 |
32 | //#define MISO_GPIO 19
33 | //#define MOSI_GPIO 23
34 | //#define SCK_GPIO 18
35 | //#define CS_GPIO 5
36 |
37 | //////////////////////////////////////////////////////////
38 |
39 | #include
40 |
41 | // Enter a MAC address and IP address for your controller below.
42 | #define NUMBER_OF_MAC 20
43 |
44 | byte mac[][NUMBER_OF_MAC] =
45 | {
46 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01 },
47 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x02 },
48 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x03 },
49 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x04 },
50 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x05 },
51 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x06 },
52 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x07 },
53 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x08 },
54 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x09 },
55 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0A },
56 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0B },
57 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0C },
58 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0D },
59 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0E },
60 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0F },
61 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x10 },
62 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x11 },
63 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x12 },
64 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x13 },
65 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x14 },
66 | };
67 |
68 | // Select the IP address according to your local network
69 | IPAddress myIP(192, 168, 2, 232);
70 | IPAddress myGW(192, 168, 2, 1);
71 | IPAddress mySN(255, 255, 255, 0);
72 |
73 | // Google DNS Server IP
74 | IPAddress myDNS(8, 8, 8, 8);
75 |
76 | char timeServer[] = "time.nist.gov"; // NTP server
77 | unsigned int localPort = 2390; // local port to listen for UDP packets
78 |
79 | const int NTP_PACKET_SIZE = 48; // NTP timestamp is in the first 48 bytes of the message
80 | const int UDP_TIMEOUT = 2000; // timeout in milliseconds to wait for an UDP packet to arrive
81 |
82 | byte packetBuffer[NTP_PACKET_SIZE]; // buffer to hold incoming packet
83 | byte ReplyBuffer[] = "ACK"; // a string to send back
84 |
85 | // A UDP instance to let us send and receive packets over UDP
86 | WiFiUDP Udp;
87 |
88 | // send an NTP request to the time server at the given address
89 | void sendNTPpacket(char *ntpSrv)
90 | {
91 | // set all bytes in the buffer to 0
92 | memset(packetBuffer, 0, NTP_PACKET_SIZE);
93 | // Initialize values needed to form NTP request
94 | // (see URL above for details on the packets)
95 |
96 | packetBuffer[0] = 0b11100011; // LI, Version, Mode
97 | packetBuffer[1] = 0; // Stratum, or type of clock
98 | packetBuffer[2] = 6; // Polling Interval
99 | packetBuffer[3] = 0xEC; // Peer Clock Precision
100 | // 8 bytes of zero for Root Delay & Root Dispersion
101 | packetBuffer[12] = 49;
102 | packetBuffer[13] = 0x4E;
103 | packetBuffer[14] = 49;
104 | packetBuffer[15] = 52;
105 |
106 | // all NTP fields have been given values, now
107 | // you can send a packet requesting a timestamp:
108 | Udp.beginPacket(ntpSrv, 123); //NTP requests are to port 123
109 |
110 | Udp.write(packetBuffer, NTP_PACKET_SIZE);
111 |
112 | Udp.endPacket();
113 | }
114 |
115 | void setup()
116 | {
117 | Serial.begin(115200);
118 |
119 | while (!Serial && (millis() < 5000));
120 |
121 | Serial.print(F("\nStart UDPSendReceive on "));
122 | Serial.print(ARDUINO_BOARD);
123 | Serial.print(F(" with "));
124 | Serial.println(SHIELD_TYPE);
125 | Serial.println(WEBSERVER_ESP32_W5500_VERSION);
126 |
127 | ET_LOGWARN(F("Default SPI pinout:"));
128 | ET_LOGWARN1(F("SPI_HOST:"), ETH_SPI_HOST);
129 | ET_LOGWARN1(F("MOSI:"), MOSI_GPIO);
130 | ET_LOGWARN1(F("MISO:"), MISO_GPIO);
131 | ET_LOGWARN1(F("SCK:"), SCK_GPIO);
132 | ET_LOGWARN1(F("CS:"), CS_GPIO);
133 | ET_LOGWARN1(F("INT:"), INT_GPIO);
134 | ET_LOGWARN1(F("SPI Clock (MHz):"), SPI_CLOCK_MHZ);
135 | ET_LOGWARN(F("========================="));
136 |
137 | ///////////////////////////////////
138 |
139 | // To be called before ETH.begin()
140 | ESP32_W5500_onEvent();
141 |
142 | // start the ethernet connection and the server:
143 | // Use DHCP dynamic IP and random mac
144 | //bool begin(int MISO_GPIO, int MOSI_GPIO, int SCLK_GPIO, int CS_GPIO, int INT_GPIO, int SPI_CLOCK_MHZ,
145 | // int SPI_HOST, uint8_t *W6100_Mac = W6100_Default_Mac);
146 | ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST );
147 | //ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST, mac[millis() % NUMBER_OF_MAC] );
148 |
149 | // Static IP, leave without this line to get IP via DHCP
150 | //bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
151 | //ETH.config(myIP, myGW, mySN, myDNS);
152 |
153 | ESP32_W5500_waitForConnect();
154 |
155 | ///////////////////////////////////
156 |
157 | Serial.println(F("\nStarting connection to server..."));
158 | // if you get a connection, report back via serial:
159 | Udp.begin(localPort);
160 |
161 | Serial.print(F("Listening on port "));
162 | Serial.println(localPort);
163 | }
164 |
165 | void loop()
166 | {
167 | sendNTPpacket(timeServer); // send an NTP packet to a time server
168 |
169 | // wait for a reply for UDP_TIMEOUT milliseconds
170 | unsigned long startMs = millis();
171 |
172 | while (!Udp.available() && (millis() - startMs) < UDP_TIMEOUT) {}
173 |
174 | // if there's data available, read a packet
175 | int packetSize = Udp.parsePacket();
176 |
177 | if (packetSize)
178 | {
179 | Serial.print(F("UDP Packet received, size "));
180 | Serial.println(packetSize);
181 | Serial.print(F("From "));
182 | IPAddress remoteIp = Udp.remoteIP();
183 | Serial.print(remoteIp);
184 | Serial.print(F(", port "));
185 | Serial.println(Udp.remotePort());
186 |
187 | // We've received a packet, read the data from it into the buffer
188 | Udp.read(packetBuffer, NTP_PACKET_SIZE);
189 |
190 | // the timestamp starts at byte 40 of the received packet and is four bytes,
191 | // or two words, long. First, esxtract the two words:
192 |
193 | unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
194 | unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
195 |
196 | // combine the four bytes (two words) into a long integer
197 | // this is NTP time (seconds since Jan 1 1900):
198 | unsigned long secsSince1900 = highWord << 16 | lowWord;
199 |
200 | Serial.print(F("Seconds since Jan 1 1900 = "));
201 | Serial.println(secsSince1900);
202 |
203 | // now convert NTP time into )everyday time:
204 | Serial.print(F("Unix time = "));
205 | // Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
206 | const unsigned long seventyYears = 2208988800UL;
207 | // subtract seventy years:
208 | unsigned long epoch = secsSince1900 - seventyYears;
209 | // print Unix time:
210 | Serial.println(epoch);
211 |
212 | // print the hour, minute and second:
213 | Serial.print(F("The UTC time is ")); // UTC is the time at Greenwich Meridian (GMT)
214 | Serial.print((epoch % 86400L) / 3600); // print the hour (86400 equals secs per day)
215 | Serial.print(F(":"));
216 |
217 | if (((epoch % 3600) / 60) < 10)
218 | {
219 | // In the first 10 minutes of each hour, we'll want a leading '0'
220 | Serial.print(F("0"));
221 | }
222 |
223 | Serial.print((epoch % 3600) / 60); // print the minute (3600 equals secs per minute)
224 | Serial.print(F(":"));
225 |
226 | if ((epoch % 60) < 10)
227 | {
228 | // In the first 10 seconds of each minute, we'll want a leading '0'
229 | Serial.print(F("0"));
230 | }
231 |
232 | Serial.println(epoch % 60); // print the second
233 | }
234 |
235 | // wait ten seconds before asking for the time again
236 | delay(10000);
237 | }
238 |
--------------------------------------------------------------------------------
/examples/WebClient/WebClient.ino:
--------------------------------------------------------------------------------
1 | /****************************************************************************************************************************
2 | WebClient.ino - Simple Arduino web server sample for Ethernet shield
3 |
4 | For Ethernet shields using ESP32_W5500 (ESP32 + W5500)
5 |
6 | WebServer_ESP32_W5500 is a library for the ESP32 with Ethernet W5500 to run WebServer
7 |
8 | Based on and modified from ESP32-IDF https://github.com/espressif/esp-idf
9 | Built by Khoi Hoang https://github.com/khoih-prog/WebServer_ESP32_W5500
10 | Licensed under GPLv3 license
11 | *****************************************************************************************************************************/
12 |
13 | #if !( defined(ESP32) )
14 | #error This code is designed for (ESP32 + W5500) to run on ESP32 platform! Please check your Tools->Board setting.
15 | #endif
16 |
17 | #define DEBUG_ETHERNET_WEBSERVER_PORT Serial
18 |
19 | // Debug Level from 0 to 4
20 | #define _ETHERNET_WEBSERVER_LOGLEVEL_ 3
21 |
22 | //////////////////////////////////////////////////////////
23 |
24 | // Optional values to override default settings
25 | // Don't change unless you know what you're doing
26 | //#define ETH_SPI_HOST SPI3_HOST
27 | //#define SPI_CLOCK_MHZ 25
28 |
29 | // Must connect INT to GPIOxx or not working
30 | //#define INT_GPIO 4
31 |
32 | //#define MISO_GPIO 19
33 | //#define MOSI_GPIO 23
34 | //#define SCK_GPIO 18
35 | //#define CS_GPIO 5
36 |
37 | //////////////////////////////////////////////////////////
38 |
39 | #include
40 |
41 | // Enter a MAC address and IP address for your controller below.
42 | #define NUMBER_OF_MAC 20
43 |
44 | byte mac[][NUMBER_OF_MAC] =
45 | {
46 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01 },
47 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x02 },
48 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x03 },
49 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x04 },
50 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x05 },
51 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x06 },
52 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x07 },
53 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x08 },
54 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x09 },
55 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0A },
56 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0B },
57 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0C },
58 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0D },
59 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0E },
60 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0F },
61 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x10 },
62 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x11 },
63 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x12 },
64 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x13 },
65 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x14 },
66 | };
67 |
68 | // Select the IP address according to your local network
69 | IPAddress myIP(192, 168, 2, 232);
70 | IPAddress myGW(192, 168, 2, 1);
71 | IPAddress mySN(255, 255, 255, 0);
72 |
73 | // Google DNS Server IP
74 | IPAddress myDNS(8, 8, 8, 8);
75 |
76 | char server[] = "arduino.tips";
77 |
78 | // Initialize the Ethernet client object
79 | WiFiClient client;
80 |
81 | void setup()
82 | {
83 | Serial.begin(115200);
84 |
85 | while (!Serial && (millis() < 5000));
86 |
87 | Serial.print(F("\nStart WebClient on "));
88 | Serial.print(ARDUINO_BOARD);
89 | Serial.print(F(" with "));
90 | Serial.println(SHIELD_TYPE);
91 | Serial.println(WEBSERVER_ESP32_W5500_VERSION);
92 |
93 | ET_LOGWARN(F("Default SPI pinout:"));
94 | ET_LOGWARN1(F("SPI_HOST:"), ETH_SPI_HOST);
95 | ET_LOGWARN1(F("MOSI:"), MOSI_GPIO);
96 | ET_LOGWARN1(F("MISO:"), MISO_GPIO);
97 | ET_LOGWARN1(F("SCK:"), SCK_GPIO);
98 | ET_LOGWARN1(F("CS:"), CS_GPIO);
99 | ET_LOGWARN1(F("INT:"), INT_GPIO);
100 | ET_LOGWARN1(F("SPI Clock (MHz):"), SPI_CLOCK_MHZ);
101 | ET_LOGWARN(F("========================="));
102 |
103 | ///////////////////////////////////
104 |
105 | // To be called before ETH.begin()
106 | ESP32_W5500_onEvent();
107 |
108 | // start the ethernet connection and the server:
109 | // Use DHCP dynamic IP and random mac
110 | //bool begin(int MISO_GPIO, int MOSI_GPIO, int SCLK_GPIO, int CS_GPIO, int INT_GPIO, int SPI_CLOCK_MHZ,
111 | // int SPI_HOST, uint8_t *W6100_Mac = W6100_Default_Mac);
112 | ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST );
113 | //ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST, mac[millis() % NUMBER_OF_MAC] );
114 |
115 | // Static IP, leave without this line to get IP via DHCP
116 | //bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
117 | // KH, Dynamic not OK yet
118 | //ETH.config(myIP, myGW, mySN, myDNS);
119 |
120 | ESP32_W5500_waitForConnect();
121 |
122 | ///////////////////////////////////
123 |
124 | Serial.println();
125 | Serial.println(F("Starting connection to server..."));
126 |
127 | // if you get a connection, report back via serial
128 | if (client.connect(server, 80))
129 | {
130 | Serial.println(F("Connected to server"));
131 | // Make a HTTP request
132 | client.println(F("GET /asciilogo.txt HTTP/1.1"));
133 | client.println(F("Host: arduino.tips"));
134 | client.println(F("Connection: close"));
135 | client.println();
136 | }
137 | }
138 |
139 | void printoutData()
140 | {
141 | // if there are incoming bytes available
142 | // from the server, read them and print them
143 | while (client.available())
144 | {
145 | char c = client.read();
146 | Serial.write(c);
147 | Serial.flush();
148 | }
149 | }
150 |
151 | void loop()
152 | {
153 | printoutData();
154 |
155 | // if the server's disconnected, stop the client
156 | if (!client.connected())
157 | {
158 | Serial.println();
159 | Serial.println(F("Disconnecting from server..."));
160 | client.stop();
161 |
162 | // do nothing forever
163 | while (true)
164 | yield();
165 | }
166 | }
167 |
--------------------------------------------------------------------------------
/examples/WebClientRepeating/WebClientRepeating.ino:
--------------------------------------------------------------------------------
1 | /****************************************************************************************************************************
2 | WebClientRepeating.ino - Simple Arduino web server sample for Ethernet shield
3 |
4 | For Ethernet shields using ESP32_W5500 (ESP32 + W5500)
5 |
6 | WebServer_ESP32_W5500 is a library for the ESP32 with Ethernet W5500 to run WebServer
7 |
8 | Based on and modified from ESP32-IDF https://github.com/espressif/esp-idf
9 | Built by Khoi Hoang https://github.com/khoih-prog/WebServer_ESP32_W5500
10 | Licensed under GPLv3 license
11 | *****************************************************************************************************************************/
12 | /*
13 | The Arduino board communicates with the shield using the SPI bus. This is on digital pins 11, 12, and 13 on the Uno
14 | and pins 50, 51, and 52 on the Mega. On both boards, pin 10 is used as SS. On the Mega, the hardware SS pin, 53,
15 | is not used to select the Ethernet controller chip, but it must be kept as an output or the SPI interface won't work.
16 | */
17 |
18 | #if !( defined(ESP32) )
19 | #error This code is designed for (ESP32 + W5500) to run on ESP32 platform! Please check your Tools->Board setting.
20 | #endif
21 |
22 | #define DEBUG_ETHERNET_WEBSERVER_PORT Serial
23 |
24 | // Debug Level from 0 to 4
25 | #define _ETHERNET_WEBSERVER_LOGLEVEL_ 3
26 |
27 | //////////////////////////////////////////////////////////
28 |
29 | // Optional values to override default settings
30 | // Don't change unless you know what you're doing
31 | //#define ETH_SPI_HOST SPI3_HOST
32 | //#define SPI_CLOCK_MHZ 25
33 |
34 | // Must connect INT to GPIOxx or not working
35 | //#define INT_GPIO 4
36 |
37 | //#define MISO_GPIO 19
38 | //#define MOSI_GPIO 23
39 | //#define SCK_GPIO 18
40 | //#define CS_GPIO 5
41 |
42 | //////////////////////////////////////////////////////////
43 |
44 | #include
45 |
46 | // Enter a MAC address and IP address for your controller below.
47 | #define NUMBER_OF_MAC 20
48 |
49 | byte mac[][NUMBER_OF_MAC] =
50 | {
51 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01 },
52 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x02 },
53 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x03 },
54 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x04 },
55 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x05 },
56 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x06 },
57 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x07 },
58 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x08 },
59 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x09 },
60 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0A },
61 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0B },
62 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0C },
63 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0D },
64 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0E },
65 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0F },
66 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x10 },
67 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x11 },
68 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x12 },
69 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x13 },
70 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x14 },
71 | };
72 |
73 | // Select the IP address according to your local network
74 | IPAddress myIP(192, 168, 2, 232);
75 | IPAddress myGW(192, 168, 2, 1);
76 | IPAddress mySN(255, 255, 255, 0);
77 |
78 | // Google DNS Server IP
79 | IPAddress myDNS(8, 8, 8, 8);
80 |
81 | char server[] = "arduino.tips";
82 |
83 | unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
84 | const unsigned long postingInterval = 10000L; // delay between updates, in milliseconds
85 |
86 | // Initialize the Web client object
87 | WiFiClient client;
88 |
89 | // this method makes a HTTP connection to the server
90 | void httpRequest()
91 | {
92 | Serial.println();
93 |
94 | // close any connection before send a new request
95 | // this will free the socket on the WiFi shield
96 | client.stop();
97 |
98 | // if there's a successful connection
99 | if (client.connect(server, 80))
100 | {
101 | Serial.println(F("Connecting..."));
102 |
103 | // send the HTTP PUT request
104 | client.println(F("GET /asciilogo.txt HTTP/1.1"));
105 | client.println(F("Host: arduino.tips"));
106 | client.println(F("Connection: close"));
107 | client.println();
108 |
109 | // note the time that the connection was made
110 | lastConnectionTime = millis();
111 | }
112 | else
113 | {
114 | // if you couldn't make a connection
115 | Serial.println(F("Connection failed"));
116 | }
117 | }
118 |
119 | void setup()
120 | {
121 | Serial.begin(115200);
122 |
123 | while (!Serial && (millis() < 5000));
124 |
125 | Serial.print(F("\nStart WebClientRepeating on "));
126 | Serial.print(ARDUINO_BOARD);
127 | Serial.print(F(" with "));
128 | Serial.println(SHIELD_TYPE);
129 | Serial.println(WEBSERVER_ESP32_W5500_VERSION);
130 |
131 | ET_LOGWARN(F("Default SPI pinout:"));
132 | ET_LOGWARN1(F("SPI_HOST:"), ETH_SPI_HOST);
133 | ET_LOGWARN1(F("MOSI:"), MOSI_GPIO);
134 | ET_LOGWARN1(F("MISO:"), MISO_GPIO);
135 | ET_LOGWARN1(F("SCK:"), SCK_GPIO);
136 | ET_LOGWARN1(F("CS:"), CS_GPIO);
137 | ET_LOGWARN1(F("INT:"), INT_GPIO);
138 | ET_LOGWARN1(F("SPI Clock (MHz):"), SPI_CLOCK_MHZ);
139 | ET_LOGWARN(F("========================="));
140 |
141 | ///////////////////////////////////
142 |
143 | // To be called before ETH.begin()
144 | ESP32_W5500_onEvent();
145 |
146 | // start the ethernet connection and the server:
147 | // Use DHCP dynamic IP and random mac
148 | //bool begin(int MISO_GPIO, int MOSI_GPIO, int SCLK_GPIO, int CS_GPIO, int INT_GPIO, int SPI_CLOCK_MHZ,
149 | // int SPI_HOST, uint8_t *W6100_Mac = W6100_Default_Mac);
150 | ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST );
151 | //ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST, mac[millis() % NUMBER_OF_MAC] );
152 |
153 | // Static IP, leave without this line to get IP via DHCP
154 | //bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
155 | //ETH.config(myIP, myGW, mySN, myDNS);
156 |
157 | ESP32_W5500_waitForConnect();
158 |
159 | ///////////////////////////////////
160 | }
161 |
162 | void loop()
163 | {
164 | // if there's incoming data from the net connection send it out the serial port
165 | // this is for debugging purposes only
166 | while (client.available())
167 | {
168 | char c = client.read();
169 | Serial.write(c);
170 | Serial.flush();
171 | }
172 |
173 | // if 10 seconds have passed since your last connection,
174 | // then connect again and send data
175 | if (millis() - lastConnectionTime > postingInterval)
176 | {
177 | httpRequest();
178 | }
179 | }
180 |
--------------------------------------------------------------------------------
/examples/WebServer/WebServer.ino:
--------------------------------------------------------------------------------
1 | /****************************************************************************************************************************
2 | WebServer.ino - Simple Arduino web server sample for Ethernet shield
3 |
4 | For Ethernet shields using ESP32_W5500 (ESP32 + W5500)
5 |
6 | WebServer_ESP32_W5500 is a library for the ESP32 with Ethernet W5500 to run WebServer
7 |
8 | Based on and modified from ESP32-IDF https://github.com/espressif/esp-idf
9 | Built by Khoi Hoang https://github.com/khoih-prog/WebServer_ESP32_W5500
10 | Licensed under GPLv3 license
11 | *****************************************************************************************************************************/
12 |
13 | #if !( defined(ESP32) )
14 | #error This code is designed for (ESP32 + W5500) to run on ESP32 platform! Please check your Tools->Board setting.
15 | #endif
16 |
17 | #define DEBUG_ETHERNET_WEBSERVER_PORT Serial
18 |
19 | // Debug Level from 0 to 4
20 | #define _ETHERNET_WEBSERVER_LOGLEVEL_ 3
21 |
22 | //////////////////////////////////////////////////////////
23 |
24 | // Optional values to override default settings
25 | // Don't change unless you know what you're doing
26 | //#define ETH_SPI_HOST SPI3_HOST
27 | //#define SPI_CLOCK_MHZ 25
28 |
29 | // Must connect INT to GPIOxx or not working
30 | //#define INT_GPIO 4
31 |
32 | //#define MISO_GPIO 19
33 | //#define MOSI_GPIO 23
34 | //#define SCK_GPIO 18
35 | //#define CS_GPIO 5
36 |
37 | //////////////////////////////////////////////////////////
38 |
39 | #include
40 |
41 | WiFiServer server(80);
42 |
43 | // Enter a MAC address and IP address for your controller below.
44 | #define NUMBER_OF_MAC 20
45 |
46 | byte mac[][NUMBER_OF_MAC] =
47 | {
48 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01 },
49 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x02 },
50 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x03 },
51 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x04 },
52 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x05 },
53 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x06 },
54 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x07 },
55 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x08 },
56 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x09 },
57 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0A },
58 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0B },
59 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0C },
60 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0D },
61 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0E },
62 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0F },
63 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x10 },
64 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x11 },
65 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x12 },
66 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x13 },
67 | { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x14 },
68 | };
69 |
70 | // Select the IP address according to your local network
71 | IPAddress myIP(192, 168, 2, 232);
72 | IPAddress myGW(192, 168, 2, 1);
73 | IPAddress mySN(255, 255, 255, 0);
74 |
75 | // Google DNS Server IP
76 | IPAddress myDNS(8, 8, 8, 8);
77 |
78 | int reqCount = 0; // number of requests received
79 |
80 | void setup()
81 | {
82 | Serial.begin(115200);
83 |
84 | while (!Serial && (millis() < 5000));
85 |
86 | Serial.print(F("\nStart WebServer on "));
87 | Serial.print(ARDUINO_BOARD);
88 | Serial.print(F(" with "));
89 | Serial.println(SHIELD_TYPE);
90 | Serial.println(WEBSERVER_ESP32_W5500_VERSION);
91 |
92 | ET_LOGWARN(F("Default SPI pinout:"));
93 | ET_LOGWARN1(F("SPI_HOST:"), ETH_SPI_HOST);
94 | ET_LOGWARN1(F("MOSI:"), MOSI_GPIO);
95 | ET_LOGWARN1(F("MISO:"), MISO_GPIO);
96 | ET_LOGWARN1(F("SCK:"), SCK_GPIO);
97 | ET_LOGWARN1(F("CS:"), CS_GPIO);
98 | ET_LOGWARN1(F("INT:"), INT_GPIO);
99 | ET_LOGWARN1(F("SPI Clock (MHz):"), SPI_CLOCK_MHZ);
100 | ET_LOGWARN(F("========================="));
101 |
102 | ///////////////////////////////////
103 |
104 | // To be called before ETH.begin()
105 | ESP32_W5500_onEvent();
106 |
107 | // start the ethernet connection and the server:
108 | // Use DHCP dynamic IP and random mac
109 | //bool begin(int MISO_GPIO, int MOSI_GPIO, int SCLK_GPIO, int CS_GPIO, int INT_GPIO, int SPI_CLOCK_MHZ,
110 | // int SPI_HOST, uint8_t *W6100_Mac = W6100_Default_Mac);
111 | ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST );
112 | //ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST, mac[millis() % NUMBER_OF_MAC] );
113 |
114 | // Static IP, leave without this line to get IP via DHCP
115 | //bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
116 | //ETH.config(myIP, myGW, mySN, myDNS);
117 |
118 | ESP32_W5500_waitForConnect();
119 |
120 | ///////////////////////////////////
121 |
122 | // start the web server on port 80
123 | server.begin();
124 | }
125 |
126 | void loop()
127 | {
128 | // listen for incoming clients
129 | WiFiClient client = server.available();
130 |
131 | if (client)
132 | {
133 | Serial.println(F("New client"));
134 | // an http request ends with a blank line
135 | bool currentLineIsBlank = true;
136 |
137 | while (client.connected())
138 | {
139 | if (client.available())
140 | {
141 | char c = client.read();
142 | Serial.write(c);
143 |
144 | // if you've gotten to the end of the line (received a newline
145 | // character) and the line is blank, the http request has ended,
146 | // so you can send a reply
147 | if (c == '\n' && currentLineIsBlank)
148 | {
149 | Serial.println(F("Sending response"));
150 |
151 | // send a standard http response header
152 | // use \r\n instead of many println statements to speedup data send
153 | client.print(
154 | "HTTP/1.1 200 OK\r\n"
155 | "Content-Type: text/html\r\n"
156 | "Connection: close\r\n" // the connection will be closed after completion of the response
157 | "Refresh: 20\r\n" // refresh the page automatically every 20 sec
158 | "\r\n");
159 | client.print("\r\n");
160 | client.print("\r\n");
161 | client.print(String("Hello World from ") + BOARD_NAME + "!
\r\n");
162 | client.print("Requests received: ");
163 | client.print(++reqCount);
164 | client.print("
\r\n");
165 | client.print("
\r\n");
166 | client.print("\r\n");
167 | break;
168 | }
169 |
170 | if (c == '\n')
171 | {
172 | // you're starting a new line
173 | currentLineIsBlank = true;
174 | }
175 | else if (c != '\r')
176 | {
177 | // you've gotten a character on the current line
178 | currentLineIsBlank = false;
179 | }
180 | }
181 | }
182 |
183 | // give the web browser time to receive the data
184 | delay(10);
185 |
186 | // close the connection:
187 | client.stop();
188 | Serial.println(F("Client disconnected"));
189 | }
190 | }
191 |
--------------------------------------------------------------------------------
/examples/multiFileProject/multiFileProject.cpp:
--------------------------------------------------------------------------------
1 | /****************************************************************************************************************************
2 | multiFileProject.cpp
3 | For Ethernet shields using ESP32_W5500 (ESP32 + W5500)
4 |
5 | WebServer_ESP32_W5500 is a library for the ESP32 with Ethernet W5500 to run WebServer
6 |
7 | Based on and modified from ESP32-IDF https://github.com/espressif/esp-idf
8 | Built by Khoi Hoang https://github.com/khoih-prog/WebServer_ESP32_W5500
9 | Licensed under GPLv3 license
10 | *****************************************************************************************************************************/
11 |
12 | // To demo how to include files in multi-file Projects
13 |
14 | #include "multiFileProject.h"
15 |
--------------------------------------------------------------------------------
/examples/multiFileProject/multiFileProject.h:
--------------------------------------------------------------------------------
1 | /****************************************************************************************************************************
2 | multiFileProject.h
3 | For Ethernet shields using ESP32_W5500 (ESP32 + W5500)
4 |
5 | WebServer_ESP32_W5500 is a library for the ESP32 with Ethernet W5500 to run WebServer
6 |
7 | Based on and modified from ESP32-IDF https://github.com/espressif/esp-idf
8 | Built by Khoi Hoang https://github.com/khoih-prog/WebServer_ESP32_W5500
9 | Licensed under GPLv3 license
10 | *****************************************************************************************************************************/
11 |
12 | // To demo how to include files in multi-file Projects
13 |
14 | #pragma once
15 |
16 | #define _ETHERNET_WEBSERVER_LOGLEVEL_ 1
17 |
18 | //////////////////////////////////////////////////////////
19 |
20 | // Optional values to override default settings
21 | // Don't change unless you know what you're doing
22 | //#define ETH_SPI_HOST SPI3_HOST
23 | //#define SPI_CLOCK_MHZ 25
24 |
25 | // Must connect INT to GPIOxx or not working
26 | //#define INT_GPIO 4
27 |
28 | //#define MISO_GPIO 19
29 | //#define MOSI_GPIO 23
30 | //#define SCK_GPIO 18
31 | //#define CS_GPIO 5
32 |
33 | //////////////////////////////////////////////////////////
34 |
35 | // Can be included as many times as necessary, without `Multiple Definitions` Linker Error
36 | #include "WebServer_ESP32_W5500.hpp"
37 |
--------------------------------------------------------------------------------
/examples/multiFileProject/multiFileProject.ino:
--------------------------------------------------------------------------------
1 | /****************************************************************************************************************************
2 | multiFileProject.ino
3 | For Ethernet shields using ESP32_W5500 (ESP32 + W5500)
4 |
5 | WebServer_ESP32_W5500 is a library for the ESP32 with Ethernet W5500 to run WebServer
6 |
7 | Based on and modified from ESP32-IDF https://github.com/espressif/esp-idf
8 | Built by Khoi Hoang https://github.com/khoih-prog/WebServer_ESP32_W5500
9 | Licensed under GPLv3 license
10 | *****************************************************************************************************************************/
11 |
12 | // To demo how to include files in multi-file Projects
13 |
14 | #include "multiFileProject.h"
15 |
16 | // Can be included as many times as necessary, without `Multiple Definitions` Linker Error
17 | #include "WebServer_ESP32_W5500.h"
18 |
19 | void setup()
20 | {
21 | Serial.begin(115200);
22 |
23 | while (!Serial);
24 |
25 | delay(500);
26 |
27 | Serial.println("\nStart multiFileProject");
28 | Serial.println(WEBSERVER_ESP32_W5500_VERSION);
29 |
30 |
31 | Serial.print("You're OK now");
32 | }
33 |
34 | void loop()
35 | {
36 | // put your main code here, to run repeatedly:
37 | }
38 |
--------------------------------------------------------------------------------
/library.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "WebServer_ESP32_W5500",
3 | "version": "1.5.3",
4 | "keywords": "WebServer, Ethernet, MQTT, MQTTS, HTTP, HTTPS, SSL, Arduino, ESP32, W5500, HTTP-Client, WebSocket-Client, MQTT-Client, server, client, websocket, LittleFS, SPIFFS, ThingStream",
5 | "description": "Simple Ethernet WebServer, HTTP/HTTPS Client wrapper library for ESP32 boards using W5500 with LwIP Ethernet library. The WebServer supports HTTP(S) GET and POST requests, provides argument parsing, handles one client at a time. It provides HTTP(S), MQTT(S) Client and supports WebServer serving from LittleFS/SPIFFS",
6 | "authors":
7 | {
8 | "name": "Khoi Hoang",
9 | "url": "https://github.com/khoih-prog",
10 | "maintainer": true
11 | },
12 | "repository":
13 | {
14 | "type": "git",
15 | "url": "https://github.com/khoih-prog/WebServer_ESP32_W5500"
16 | },
17 | "homepage": "https://github.com/khoih-prog/WebServer_ESP32_W5500",
18 | "export": {
19 | "exclude": [
20 | "linux",
21 | "extras",
22 | "tests"
23 | ]
24 | },
25 | "license": "GPL-3.0",
26 | "frameworks": "*",
27 | "platforms": "espressif32",
28 | "examples": "examples/*/*/*.ino",
29 | "headers": ["WebServer_ESP32_W5500.h", "WebServer_ESP32_W5500.hpp"]
30 | }
31 |
--------------------------------------------------------------------------------
/library.properties:
--------------------------------------------------------------------------------
1 | name=WebServer_ESP32_W5500
2 | version=1.5.3
3 | author=Khoi Hoang
4 | license=GPLv3
5 | maintainer=Khoi Hoang
6 | sentence=Simple Ethernet WebServer, HTTP/HTTPS Client wrapper library for ESP32 boards using W5500 with LwIP Ethernet library.
7 | paragraph=The WebServer supports HTTP(S) GET and POST requests, provides argument parsing, handles one client at a time. It provides HTTP(S), MQTT(S) Client and supports WebServer serving from LittleFS/SPIFFS
8 | category=Communication
9 | url=https://github.com/khoih-prog/WebServer_ESP32_W5500
10 | architectures=esp32
11 | includes=WebServer_ESP32_W5500.h, WebServer_ESP32_W5500.hpp
12 |
--------------------------------------------------------------------------------
/pics/AdvancedWebServer.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/khoih-prog/WebServer_ESP32_W5500/05494bfc3c5c0a28bb1dc330dfd856f5c6b5c97a/pics/AdvancedWebServer.png
--------------------------------------------------------------------------------
/pics/W5500.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/khoih-prog/WebServer_ESP32_W5500/05494bfc3c5c0a28bb1dc330dfd856f5c6b5c97a/pics/W5500.png
--------------------------------------------------------------------------------
/pics/W5500_small.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/khoih-prog/WebServer_ESP32_W5500/05494bfc3c5c0a28bb1dc330dfd856f5c6b5c97a/pics/W5500_small.png
--------------------------------------------------------------------------------
/platformio/platformio.ini:
--------------------------------------------------------------------------------
1 | ;PlatformIO Project Configuration File
2 | ;
3 | ; Build options: build flags, source filter
4 | ; Upload options: custom upload port, speed and extra flags
5 | ; Library options: dependencies, extra library storages
6 | ; Advanced options: extra scripting
7 | ;
8 | ; Please visit documentation for the other options and examples
9 | ; https://docs.platformio.org/page/projectconf.html
10 |
11 | [platformio]
12 | ; ============================================================
13 | ; chose environment:
14 | ; ESP32
15 | ; esp32s2
16 | ; esp32s3
17 | ; esp32c3
18 |
19 | ; ============================================================
20 | default_envs = ESP32
21 |
22 | [env]
23 | ; ============================================================
24 | ; Serial configuration
25 | ; choose upload speed, serial-monitor speed
26 | ; ============================================================
27 | upload_speed = 921600
28 | ;upload_port = COM11
29 | ;monitor_speed = 9600
30 | ;monitor_port = COM11
31 |
32 | ; ============================================================
33 | ; Checks for the compatibility with frameworks and dev/platforms
34 | lib_compat_mode = strict
35 | lib_ldf_mode = chain+
36 | ;lib_ldf_mode = deep+
37 |
38 | ; ============================================================
39 | lib_deps =
40 |
41 | ; ============================================================
42 | build_flags =
43 | ; set your debug output (default=Serial)
44 | -D DEBUG_ESP_PORT=Serial
45 | ; comment the following line to enable WiFi debugging
46 | -D NDEBUG
47 |
48 | ; ============================================================
49 | ; ============================================================
50 | [env:ESP32]
51 | platform = espressif32
52 | framework = arduino
53 |
54 | ; ============================================================
55 | ; Board configuration
56 | ; choose your board by uncommenting one of the following lines
57 | ; ============================================================
58 | ;board = esp32cam
59 | ;board = alksesp32
60 | ;board = featheresp32
61 | ;board = espea32
62 | ;board = bpi-bit
63 | ;board = d-duino-32
64 | board = esp32doit-devkit-v1
65 | ;board = pocket_32
66 | ;board = fm-devkit
67 | ;board = pico32
68 | ;board = esp32-evb
69 | ;board = esp32-gateway
70 | ;board = esp32-pro
71 | ;board = esp32-poe
72 | ;board = oroca_edubot
73 | ;board = onehorse32dev
74 | ;board = lopy
75 | ;board = lopy4
76 | ;board = wesp32
77 | ;board = esp32thing
78 | ;board = sparkfun_lora_gateway_1-channel
79 | ;board = ttgo-lora32-v1
80 | ;board = ttgo-t-beam
81 | ;board = turta_iot_node
82 | ;board = lolin_d32
83 | ;board = lolin_d32_pro
84 | ;board = lolin32
85 | ;board = wemosbat
86 | ;board = widora-air
87 | ;board = xinabox_cw02
88 | ;board = iotbusio
89 | ;board = iotbusproteus
90 | ;board = nina_w10
91 |
92 | ; ============================================================
93 | ; ============================================================
94 | [env:esp32s2]
95 | platform = espressif32
96 | framework = arduino
97 |
98 | ; toolchain download links see
99 | ; refer "name": "xtensa-esp32s2-elf-gcc","version": "gcc8_4_0-esp-2021r1" section of
100 | ; https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_dev_index.json
101 | ; e.g. Windows: https://github.com/espressif/crosstool-NG/releases/download/esp-2021r1/xtensa-esp32s2-elf-gcc8_4_0-esp-2021r1-win32.zip
102 | platform_packages =
103 | toolchain-xtensa32s2@file://C:\Users\Max\Downloads\xtensa-esp32s2-elf
104 | framework-arduinoespressif32@https://github.com/espressif/arduino-esp32.git#a4118ea88987c28aac3a49bcb9cc5d6c0acc6f3f
105 | platformio/tool-esptoolpy @ ~1.30100
106 | framework = arduino
107 | board = esp32dev
108 | board_build.mcu = esp32s2
109 | board_build.partitions = huge_app.csv
110 | board_build.variant = esp32s2
111 | board_build.f_cpu = 240000000L
112 | board_build.f_flash = 80000000L
113 | board_build.flash_mode = qio
114 | board_build.arduino.ldscript = esp32s2_out.ld
115 | build_unflags =
116 | -DARDUINO_ESP32_DEV
117 | -DARDUINO_VARIANT="esp32"
118 | build_flags =
119 | -DARDUINO_ESP32S2_DEV
120 | -DARDUINO_VARIANT="esp32s2"
121 |
122 | ; ============================================================
123 | ; ============================================================
124 | [env:esp32s3]
125 | platform = espressif32
126 | framework = arduino
127 |
128 | board_build.mcu = esp32s3
129 | board_build.partitions = huge_app.csv
130 | board_build.variant = esp32s3
131 | board_build.f_cpu = 240000000L
132 | board_build.f_flash = 80000000L
133 | board_build.flash_mode = qio
134 | board_build.arduino.ldscript = esp32s3_out.ld
135 | build_unflags =
136 | -DARDUINO_ESP32_DEV
137 | -DARDUINO_VARIANT="esp32"
138 | build_flags =
139 | -DARDUINO_ESP32S3_DEV
140 | -DARDUINO_VARIANT="esp32s3"
141 |
142 | ; ============================================================
143 | ; ============================================================
144 | [env:esp32sc3]
145 | platform = espressif32
146 | framework = arduino
147 |
148 | board_build.mcu = esp32c3
149 | board_build.partitions = huge_app.csv
150 | board_build.variant = esp32c3
151 | board_build.f_cpu = 160000000L
152 | board_build.f_flash = 80000000L
153 | board_build.flash_mode = qio
154 | board_build.arduino.ldscript = esp32c3_out.ld
155 | build_unflags =
156 | -DARDUINO_ESP32_DEV
157 | -DARDUINO_VARIANT="esp32"
158 | build_flags =
159 | -DARDUINO_ESP32S3_DEV
160 | -DARDUINO_VARIANT="esp32c3"
161 |
162 | ; ============================================================
163 | ; ============================================================
164 |
--------------------------------------------------------------------------------
/src/WebServer_ESP32_W5500.h:
--------------------------------------------------------------------------------
1 | /****************************************************************************************************************************
2 | WebServer_ESP32_W5500.h
3 |
4 | For Ethernet shields using ESP32_W5500 (ESP32 + W5500)
5 |
6 | WebServer_ESP32_W5500 is a library for the ESP32 with Ethernet W5500 to run WebServer
7 |
8 | Based on and modified from ESP32-IDF https://github.com/espressif/esp-idf
9 | Built by Khoi Hoang https://github.com/khoih-prog/WebServer_ESP32_W5500
10 | Licensed under GPLv3 license
11 |
12 | Version: 1.5.3
13 |
14 | Version Modified By Date Comments
15 | ------- ----------- ---------- -----------
16 | 1.5.1 K Hoang 29/11/2022 Initial coding for ESP32_W5500 (ESP32 + W5500). Sync with WebServer_WT32_ETH01 v1.5.1
17 | 1.5.2 K Hoang 06/01/2023 Suppress compile error when using aggressive compile settings
18 | 1.5.3 K Hoang 11/01/2023 Using `SPI_DMA_CH_AUTO` and built-in ESP32 MAC
19 | *****************************************************************************************************************************/
20 |
21 | #pragma once
22 |
23 | #ifndef WEBSERVER_ESP32_W5500_H
24 | #define WEBSERVER_ESP32_W5500_H
25 |
26 | //////////////////////////////////////////////////////////////
27 |
28 | //#if !defined(USING_CORE_ESP32_CORE_V200_PLUS)
29 | #if ( ( defined(ESP_ARDUINO_VERSION_MAJOR) && (ESP_ARDUINO_VERSION_MAJOR >= 2) ) && ( ARDUINO_ESP32_GIT_VER != 0x46d5afb1 ) )
30 | #define USING_CORE_ESP32_CORE_V200_PLUS true
31 |
32 | #if (_ETHERNET_WEBSERVER_LOGLEVEL_ > 3)
33 | #warning Using code for ESP32 core v2.0.0+ in WebServer_ESP32_W5500.h
34 | #endif
35 |
36 | #define WEBSERVER_ESP32_W5500_VERSION "WebServer_ESP32_W5500 v1.5.3 for core v2.0.0+"
37 | #else
38 | #if (_ETHERNET_WEBSERVER_LOGLEVEL_ > 3)
39 | #warning Using code for ESP32 core v1.0.6- in WebServer_ESP32_W5500.h
40 | #endif
41 |
42 | #define WEBSERVER_ESP32_W5500_VERSION "WebServer_ESP32_W5500 v1.5.3 for core v1.0.6-"
43 | #endif
44 |
45 | #define WEBSERVER_ESP32_W5500_VERSION_MAJOR 1
46 | #define WEBSERVER_ESP32_W5500_VERSION_MINOR 5
47 | #define WEBSERVER_ESP32_W5500_VERSION_PATCH 3
48 |
49 | #define WEBSERVER_ESP32_W5500_VERSION_INT 1005003
50 |
51 | //////////////////////////////////////////////////////////////
52 |
53 | #if ( ARDUINO_ESP32S2_DEV || ARDUINO_FEATHERS2 || ARDUINO_ESP32S2_THING_PLUS || ARDUINO_MICROS2 || \
54 | ARDUINO_METRO_ESP32S2 || ARDUINO_MAGTAG29_ESP32S2 || ARDUINO_FUNHOUSE_ESP32S2 || \
55 | ARDUINO_ADAFRUIT_FEATHER_ESP32S2_NOPSRAM )
56 |
57 | #error ESP32_S2 not supported. Use WebServer_ESP32_SC_W5500 library
58 |
59 | #elif ( ARDUINO_ESP32C3_DEV )
60 |
61 | #error ESP32_C3 not supported. Use WebServer_ESP32_SC_W5500 library
62 |
63 | #elif ( defined(ARDUINO_ESP32S3_DEV) || defined(ARDUINO_ESP32_S3_BOX) || defined(ARDUINO_TINYS3) || \
64 | defined(ARDUINO_PROS3) || defined(ARDUINO_FEATHERS3) )
65 |
66 | #error ESP32_S3 not supported. Use WebServer_ESP32_SC_W5500 library
67 |
68 | #elif ESP32
69 |
70 | #if (_ETHERNET_WEBSERVER_LOGLEVEL_ > 3)
71 | #warning Using ESP32 architecture for WebServer_ESP32_W5500
72 | #endif
73 |
74 | #define BOARD_NAME ARDUINO_BOARD
75 |
76 | #else
77 | #error This code is designed to run on ESP32 platform! Please check your Tools->Board setting.
78 | #endif
79 |
80 | //////////////////////////////////////////////////////////////
81 |
82 | #include
83 |
84 | #include "WebServer_ESP32_W5500_Debug.h"
85 |
86 | //////////////////////////////////////////////////////////////
87 |
88 | #include "w5500/esp32_w5500.h"
89 |
90 | #include "WebServer_ESP32_W5500.hpp"
91 | #include "WebServer_ESP32_W5500_Impl.h"
92 |
93 | #endif // WEBSERVER_ESP32_W5500_H
94 |
--------------------------------------------------------------------------------
/src/WebServer_ESP32_W5500.hpp:
--------------------------------------------------------------------------------
1 | /****************************************************************************************************************************
2 | WebServer_ESP32_W5500.h
3 |
4 | For Ethernet shields using ESP32_W5500 (ESP32 + W5500)
5 |
6 | WebServer_ESP32_W5500 is a library for the ESP32 with Ethernet W5500 to run WebServer
7 |
8 | Based on and modified from ESP32-IDF https://github.com/espressif/esp-idf
9 | Built by Khoi Hoang https://github.com/khoih-prog/WebServer_ESP32_W5500
10 | Licensed under GPLv3 license
11 |
12 | Version: 1.5.3
13 |
14 | Version Modified By Date Comments
15 | ------- ----------- ---------- -----------
16 | 1.5.1 K Hoang 29/11/2022 Initial coding for ESP32_W5500 (ESP32 + W5500). Sync with WebServer_WT32_ETH01 v1.5.1
17 | 1.5.2 K Hoang 06/01/2023 Suppress compile error when using aggressive compile settings
18 | 1.5.3 K Hoang 11/01/2023 Using `SPI_DMA_CH_AUTO` and built-in ESP32 MAC
19 | *****************************************************************************************************************************/
20 |
21 | #pragma once
22 |
23 | #ifndef WEBSERVER_ESP32_W5500_HPP
24 | #define WEBSERVER_ESP32_W5500_HPP
25 |
26 | //////////////////////////////////////////////////////////////
27 |
28 | //#define CONFIG_ETH_SPI_ETHERNET_W5500 true
29 |
30 | //////////////////////////////////////////////////////////////
31 |
32 | #include
33 | #include // Introduce corresponding libraries
34 |
35 | #include
36 |
37 | //////////////////////////////////////////////////////////////
38 |
39 | #if !defined(ETH_SPI_HOST)
40 | #define ETH_SPI_HOST SPI3_HOST
41 | #endif
42 |
43 | #if !defined(SPI_CLOCK_MHZ)
44 | // Using 25MHz for W5500, 14MHz for W5100
45 | #define SPI_CLOCK_MHZ 25
46 | #endif
47 |
48 | #if !defined(INT_GPIO)
49 | #define INT_GPIO 4
50 | #endif
51 |
52 | #if !defined(MISO_GPIO)
53 | #define MISO_GPIO 19
54 | #endif
55 |
56 | #if !defined(MOSI_GPIO)
57 | #define MOSI_GPIO 23
58 | #endif
59 |
60 | #if !defined(SCK_GPIO)
61 | #define SCK_GPIO 18
62 | #endif
63 |
64 | #if !defined(CS_GPIO)
65 | #define CS_GPIO 5
66 | #endif
67 |
68 | //////////////////////////////////////////////////////////////
69 |
70 | #ifndef SHIELD_TYPE
71 | #define SHIELD_TYPE "ESP32_W5500"
72 | #endif
73 |
74 | //////////////////////////////////////////////////////////////
75 |
76 | extern bool ESP32_W5500_eth_connected;
77 |
78 | extern void ESP32_W5500_onEvent();
79 |
80 | extern void ESP32_W5500_waitForConnect();
81 |
82 | extern bool ESP32_W5500_isConnected();
83 |
84 | extern void ESP32_W5500_event(WiFiEvent_t event);
85 |
86 | //////////////////////////////////////////////////////////////
87 |
88 | #endif // WEBSERVER_ESP32_W5500_HPP
89 |
--------------------------------------------------------------------------------
/src/WebServer_ESP32_W5500_Debug.h:
--------------------------------------------------------------------------------
1 | /****************************************************************************************************************************
2 | WebServer_ESP32_W5500_Debug.h
3 |
4 | For Ethernet shields using ESP32_W5500 (ESP32 + W5500)
5 |
6 | WebServer_ESP32_W5500 is a library for the ESP32 with Ethernet W5500 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_ESP32_W5500
10 | Licensed under GPLv3 license
11 |
12 | Version: 1.5.3
13 |
14 | Version Modified By Date Comments
15 | ------- ----------- ---------- -----------
16 | 1.5.1 K Hoang 29/11/2022 Initial coding for ESP32_W5500 (ESP32 + W5500). Sync with WebServer_WT32_ETH01 v1.5.1
17 | 1.5.2 K Hoang 06/01/2023 Suppress compile error when using aggressive compile settings
18 | 1.5.3 K Hoang 11/01/2023 Using `SPI_DMA_CH_AUTO` and built-in ESP32 MAC
19 | *****************************************************************************************************************************/
20 |
21 | #pragma once
22 |
23 | #ifndef WEBSERVER_ESP32_W5500_DEBUG_H
24 | #define WEBSERVER_ESP32_W5500_DEBUG_H
25 |
26 | #include
27 | #include
28 |
29 | ///////////////////////////////////////
30 |
31 | #ifdef DEBUG_ETHERNET_WEBSERVER_PORT
32 | #define ET_DEBUG_OUTPUT DEBUG_ETHERNET_WEBSERVER_PORT
33 | #else
34 | #define ET_DEBUG_OUTPUT Serial
35 | #endif
36 |
37 | ///////////////////////////////////////
38 |
39 | // Change _ETHERNET_WEBSERVER_LOGLEVEL_ to set tracing and logging verbosity
40 | // 0: DISABLED: no logging
41 | // 1: ERROR: errors
42 | // 2: WARN: errors and warnings
43 | // 3: INFO: errors, warnings and informational (default)
44 | // 4: DEBUG: errors, warnings, informational and debug
45 |
46 | #ifndef _ETHERNET_WEBSERVER_LOGLEVEL_
47 | #define _ETHERNET_WEBSERVER_LOGLEVEL_ 0
48 | #endif
49 |
50 | ///////////////////////////////////////
51 |
52 | const char EWS_MARK[] = "[EWS] ";
53 | const char EWS_SPACE[] = " ";
54 | const char EWS_LINE[] = "========================================\n";
55 |
56 | #define EWS_PRINT_MARK EWS_PRINT(EWS_MARK)
57 | #define EWS_PRINT_SP EWS_PRINT(EWS_SPACE)
58 | #define EWS_PRINT_LINE EWS_PRINT(EWS_LINE)
59 |
60 | #define EWS_PRINT ET_DEBUG_OUTPUT.print
61 | #define EWS_PRINTLN ET_DEBUG_OUTPUT.println
62 |
63 | ///////////////////////////////////////
64 |
65 | #define ET_LOG(x) { EWS_PRINTLN(x); }
66 | #define ET_LOG0(x) { EWS_PRINT(x); }
67 | #define ET_LOG1(x,y) { EWS_PRINT(x); EWS_PRINTLN(y); }
68 | #define ET_LOG2(x,y,z) { EWS_PRINT(x); EWS_PRINT(y); EWS_PRINTLN(z); }
69 | #define ET_LOG3(x,y,z,w) { EWS_PRINT(x); EWS_PRINT(y); EWS_PRINT(z); EWS_PRINTLN(w); }
70 |
71 | ///////////////////////////////////////
72 |
73 | #define ET_LOGERROR(x) if(_ETHERNET_WEBSERVER_LOGLEVEL_>0) { EWS_PRINT_MARK; EWS_PRINTLN(x); }
74 | #define ET_LOGERROR_LINE(x) if(_ETHERNET_WEBSERVER_LOGLEVEL_>0) { EWS_PRINT_MARK; EWS_PRINTLN(x); EWS_PRINT_LINE; }
75 | #define ET_LOGERROR0(x) if(_ETHERNET_WEBSERVER_LOGLEVEL_>0) { EWS_PRINT(x); }
76 | #define ET_LOGERROR1(x,y) if(_ETHERNET_WEBSERVER_LOGLEVEL_>0) { EWS_PRINT_MARK; EWS_PRINT(x); EWS_PRINT_SP; EWS_PRINTLN(y); }
77 | #define ET_LOGERROR2(x,y,z) if(_ETHERNET_WEBSERVER_LOGLEVEL_>0) { EWS_PRINT_MARK; EWS_PRINT(x); EWS_PRINT_SP; EWS_PRINT(y); EWS_PRINT_SP; EWS_PRINTLN(z); }
78 | #define ET_LOGERROR3(x,y,z,w) if(_ETHERNET_WEBSERVER_LOGLEVEL_>0) { EWS_PRINT_MARK; EWS_PRINT(x); EWS_PRINT_SP; EWS_PRINT(y); EWS_PRINT_SP; EWS_PRINT(z); EWS_PRINT_SP; EWS_PRINTLN(w); }
79 |
80 | ///////////////////////////////////////
81 |
82 | #define ET_LOGWARN(x) if(_ETHERNET_WEBSERVER_LOGLEVEL_>1) { EWS_PRINT_MARK; EWS_PRINTLN(x); }
83 | #define ET_LOGWARN_LINE(x) if(_ETHERNET_WEBSERVER_LOGLEVEL_>1) { EWS_PRINT_MARK; EWS_PRINTLN(x); EWS_PRINT_LINE; }
84 | #define ET_LOGWARN0(x) if(_ETHERNET_WEBSERVER_LOGLEVEL_>1) { EWS_PRINT(x); }
85 | #define ET_LOGWARN1(x,y) if(_ETHERNET_WEBSERVER_LOGLEVEL_>1) { EWS_PRINT_MARK; EWS_PRINT(x); EWS_PRINT_SP; EWS_PRINTLN(y); }
86 | #define ET_LOGWARN2(x,y,z) if(_ETHERNET_WEBSERVER_LOGLEVEL_>1) { EWS_PRINT_MARK; EWS_PRINT(x); EWS_PRINT_SP; EWS_PRINT(y); EWS_PRINT_SP; EWS_PRINTLN(z); }
87 | #define ET_LOGWARN3(x,y,z,w) if(_ETHERNET_WEBSERVER_LOGLEVEL_>1) { EWS_PRINT_MARK; EWS_PRINT(x); EWS_PRINT_SP; EWS_PRINT(y); EWS_PRINT_SP; EWS_PRINT(z); EWS_PRINT_SP; EWS_PRINTLN(w); }
88 |
89 | ///////////////////////////////////////
90 |
91 | #define ET_LOGINFO(x) if(_ETHERNET_WEBSERVER_LOGLEVEL_>2) { EWS_PRINT_MARK; EWS_PRINTLN(x); }
92 | #define ET_LOGINFO_LINE(x) if(_ETHERNET_WEBSERVER_LOGLEVEL_>2) { EWS_PRINT_MARK; EWS_PRINTLN(x); EWS_PRINT_LINE; }
93 | #define ET_LOGINFO0(x) if(_ETHERNET_WEBSERVER_LOGLEVEL_>2) { EWS_PRINT(x); }
94 | #define ET_LOGINFO1(x,y) if(_ETHERNET_WEBSERVER_LOGLEVEL_>2) { EWS_PRINT_MARK; EWS_PRINT(x); EWS_PRINT_SP; EWS_PRINTLN(y); }
95 | #define ET_LOGINFO2(x,y,z) if(_ETHERNET_WEBSERVER_LOGLEVEL_>2) { EWS_PRINT_MARK; EWS_PRINT(x); EWS_PRINT_SP; EWS_PRINT(y); EWS_PRINT_SP; EWS_PRINTLN(z); }
96 | #define ET_LOGINFO3(x,y,z,w) if(_ETHERNET_WEBSERVER_LOGLEVEL_>2) { EWS_PRINT_MARK; EWS_PRINT(x); EWS_PRINT_SP; EWS_PRINT(y); EWS_PRINT_SP; EWS_PRINT(z); EWS_PRINT_SP; EWS_PRINTLN(w); }
97 |
98 | ///////////////////////////////////////
99 |
100 | #define ET_LOGDEBUG(x) if(_ETHERNET_WEBSERVER_LOGLEVEL_>3) { EWS_PRINT_MARK; EWS_PRINTLN(x); }
101 | #define ET_LOGDEBUG_LINE(x) if(_ETHERNET_WEBSERVER_LOGLEVEL_>3) { EWS_PRINT_MARK; EWS_PRINTLN(x); EWS_PRINT_LINE; }
102 | #define ET_LOGDEBUG0(x) if(_ETHERNET_WEBSERVER_LOGLEVEL_>3) { EWS_PRINT(x); }
103 | #define ET_LOGDEBUG1(x,y) if(_ETHERNET_WEBSERVER_LOGLEVEL_>3) { EWS_PRINT_MARK; EWS_PRINT(x); EWS_PRINT_SP; EWS_PRINTLN(y); }
104 | #define ET_LOGDEBUG2(x,y,z) if(_ETHERNET_WEBSERVER_LOGLEVEL_>3) { EWS_PRINT_MARK; EWS_PRINT(x); EWS_PRINT_SP; EWS_PRINT(y); EWS_PRINT_SP; EWS_PRINTLN(z); }
105 | #define ET_LOGDEBUG3(x,y,z,w) if(_ETHERNET_WEBSERVER_LOGLEVEL_>3) { EWS_PRINT_MARK; EWS_PRINT(x); EWS_PRINT_SP; EWS_PRINT(y); EWS_PRINT_SP; EWS_PRINT(z); EWS_PRINT_SP; EWS_PRINTLN(w); }
106 |
107 | ///////////////////////////////////////
108 |
109 | #endif // WEBSERVER_ESP32_W5500_DEBUG_H
110 |
111 |
112 |
--------------------------------------------------------------------------------
/src/WebServer_ESP32_W5500_Impl.h:
--------------------------------------------------------------------------------
1 | /****************************************************************************************************************************
2 | WebServer_ESP32_W5500_Impl.h
3 |
4 | For Ethernet shields using ESP32_W5500 (ESP32 + W5500)
5 |
6 | WebServer_ESP32_W5500 is a library for the ESP32 with Ethernet W5500 to run WebServer
7 |
8 | Based on and modified from ESP32-IDF https://github.com/espressif/esp-idf
9 | Built by Khoi Hoang https://github.com/khoih-prog/WebServer_ESP32_W5500
10 | Licensed under GPLv3 license
11 |
12 | Version: 1.5.3
13 |
14 | Version Modified By Date Comments
15 | ------- ----------- ---------- -----------
16 | 1.5.1 K Hoang 29/11/2022 Initial coding for ESP32_W5500 (ESP32 + W5500). Sync with WebServer_WT32_ETH01 v1.5.1
17 | 1.5.2 K Hoang 06/01/2023 Suppress compile error when using aggressive compile settings
18 | 1.5.3 K Hoang 11/01/2023 Using `SPI_DMA_CH_AUTO` and built-in ESP32 MAC
19 | *****************************************************************************************************************************/
20 |
21 | #pragma once
22 |
23 | #ifndef WEBSERVER_ESP32_W5500_IMPL_H
24 | #define WEBSERVER_ESP32_W5500_IMPL_H
25 |
26 | //////////////////////////////////////////////////////////////
27 |
28 | bool ESP32_W5500_eth_connected = false;
29 |
30 | //////////////////////////////////////////////////////////////
31 |
32 | void ESP32_W5500_onEvent()
33 | {
34 | WiFi.onEvent(ESP32_W5500_event);
35 | }
36 |
37 | //////////////////////////////////////////////////////////////
38 |
39 | void ESP32_W5500_waitForConnect()
40 | {
41 | while (!ESP32_W5500_eth_connected)
42 | delay(100);
43 | }
44 |
45 | //////////////////////////////////////////////////////////////
46 |
47 | bool ESP32_W5500_isConnected()
48 | {
49 | return ESP32_W5500_eth_connected;
50 | }
51 |
52 | //////////////////////////////////////////////////////////////
53 |
54 | void ESP32_W5500_event(WiFiEvent_t event)
55 | {
56 | switch (event)
57 | {
58 | //#if USING_CORE_ESP32_CORE_V200_PLUS
59 | #if ( ( defined(ESP_ARDUINO_VERSION_MAJOR) && (ESP_ARDUINO_VERSION_MAJOR >= 2) ) && ( ARDUINO_ESP32_GIT_VER != 0x46d5afb1 ) )
60 | // For breaking core v2.0.0
61 | // Why so strange to define a breaking enum arduino_event_id_t in WiFiGeneric.h
62 | // compared to the old system_event_id_t, now in tools/sdk/esp32/include/esp_event/include/esp_event_legacy.h
63 | // You can preserve the old enum order and just adding new items to do no harm
64 | case ARDUINO_EVENT_ETH_START:
65 | ET_LOG(F("\nETH Started"));
66 | //set eth hostname here
67 | ETH.setHostname("ESP32_W5500");
68 | break;
69 |
70 | case ARDUINO_EVENT_ETH_CONNECTED:
71 | ET_LOG(F("ETH Connected"));
72 | break;
73 |
74 | case ARDUINO_EVENT_ETH_GOT_IP:
75 | if (!ESP32_W5500_eth_connected)
76 | {
77 | ET_LOG3(F("ETH MAC: "), ETH.macAddress(), F(", IPv4: "), ETH.localIP());
78 |
79 | if (ETH.fullDuplex())
80 | {
81 | ET_LOG0(F("FULL_DUPLEX, "));
82 | }
83 | else
84 | {
85 | ET_LOG0(F("HALF_DUPLEX, "));
86 | }
87 |
88 | ET_LOG1(ETH.linkSpeed(), F("Mbps"));
89 |
90 | ESP32_W5500_eth_connected = true;
91 | }
92 |
93 | break;
94 |
95 | case ARDUINO_EVENT_ETH_DISCONNECTED:
96 | ET_LOG("ETH Disconnected");
97 | ESP32_W5500_eth_connected = false;
98 | break;
99 |
100 | case ARDUINO_EVENT_ETH_STOP:
101 | ET_LOG("\nETH Stopped");
102 | ESP32_W5500_eth_connected = false;
103 | break;
104 |
105 | #else
106 |
107 | // For old core v1.0.6-
108 | // Core v2.0.0 defines a stupid enum arduino_event_id_t, breaking any code for ESP32_W5500 written for previous core
109 | // Why so strange to define a breaking enum arduino_event_id_t in WiFiGeneric.h
110 | // compared to the old system_event_id_t, now in tools/sdk/esp32/include/esp_event/include/esp_event_legacy.h
111 | // You can preserve the old enum order and just adding new items to do no harm
112 | case SYSTEM_EVENT_ETH_START:
113 | ET_LOG(F("\nETH Started"));
114 | //set eth hostname here
115 | ETH.setHostname("ESP32_W5500");
116 | break;
117 |
118 | case SYSTEM_EVENT_ETH_CONNECTED:
119 | ET_LOG(F("ETH Connected"));
120 | break;
121 |
122 | case SYSTEM_EVENT_ETH_GOT_IP:
123 | if (!ESP32_W5500_eth_connected)
124 | {
125 | ET_LOG3(F("ETH MAC: "), ETH.macAddress(), F(", IPv4: "), ETH.localIP());
126 |
127 | if (ETH.fullDuplex())
128 | {
129 | ET_LOG0(F("FULL_DUPLEX, "));
130 | }
131 | else
132 | {
133 | ET_LOG0(F("HALF_DUPLEX, "));
134 | }
135 |
136 | ET_LOG1(ETH.linkSpeed(), F("Mbps"));
137 |
138 | ESP32_W5500_eth_connected = true;
139 | }
140 |
141 | break;
142 |
143 | case SYSTEM_EVENT_ETH_DISCONNECTED:
144 | ET_LOG("ETH Disconnected");
145 | ESP32_W5500_eth_connected = false;
146 | break;
147 |
148 | case SYSTEM_EVENT_ETH_STOP:
149 | ET_LOG("\nETH Stopped");
150 | ESP32_W5500_eth_connected = false;
151 | break;
152 | #endif
153 |
154 | default:
155 | break;
156 | }
157 | }
158 |
159 | //////////////////////////////////////////////////////////////
160 |
161 | #endif // WEBSERVER_ESP32_W5500_IMPL_H
162 |
--------------------------------------------------------------------------------
/src/w5500/esp32_w5500.cpp:
--------------------------------------------------------------------------------
1 | /****************************************************************************************************************************
2 | esp32_w5500.cpp
3 |
4 | For Ethernet shields using ESP32_W5500 (ESP32 + W5500)
5 |
6 | WebServer_ESP32_W5500 is a library for the ESP32 with Ethernet W5500 to run WebServer
7 |
8 | Based on and modified from ESP32-IDF https://github.com/espressif/esp-idf
9 | Built by Khoi Hoang https://github.com/khoih-prog/WebServer_ESP32_W5500
10 | Licensed under GPLv3 license
11 |
12 | Version: 1.5.3
13 |
14 | Version Modified By Date Comments
15 | ------- ----------- ---------- -----------
16 | 1.5.1 K Hoang 29/11/2022 Initial coding for ESP32_W5500 (ESP32 + W5500). Sync with WebServer_WT32_ETH01 v1.5.1
17 | 1.5.2 K Hoang 06/01/2023 Suppress compile error when using aggressive compile settings
18 | 1.5.3 K Hoang 11/01/2023 Using `SPI_DMA_CH_AUTO` and built-in ESP32 MAC
19 | *****************************************************************************************************************************/
20 |
21 | #include "WebServer_ESP32_W5500_Debug.h"
22 | #include "esp32_w5500.h"
23 |
24 | extern "C"
25 | {
26 | esp_eth_mac_t* w5500_begin(int MISO, int MOSI, int SCLK, int CS, int INT, int SPICLOCK_MHZ,
27 | int SPIHOST);
28 | #include "esp_eth/esp_eth_w5500.h"
29 | }
30 |
31 | #include "esp_event.h"
32 | #include "esp_eth_phy.h"
33 | #include "esp_eth_mac.h"
34 | #include "esp_eth_com.h"
35 |
36 | #if CONFIG_IDF_TARGET_ESP32
37 | #include "soc/emac_ext_struct.h"
38 | #include "soc/rtc.h"
39 | #endif
40 |
41 | #include "lwip/err.h"
42 | #include "lwip/dns.h"
43 |
44 | extern void tcpipInit();
45 |
46 | ////////////////////////////////////////
47 |
48 | ESP32_W5500::ESP32_W5500()
49 | : initialized(false)
50 | , staticIP(false)
51 | , eth_handle(NULL)
52 | , started(false)
53 | , eth_link(ETH_LINK_DOWN)
54 | {
55 | }
56 |
57 | ////////////////////////////////////////
58 |
59 | ESP32_W5500::~ESP32_W5500()
60 | {}
61 |
62 | ////////////////////////////////////////
63 |
64 | bool ESP32_W5500::begin(int MISO, int MOSI, int SCLK, int CS, int INT, int SPICLOCK_MHZ, int SPIHOST,
65 | uint8_t *W5500_Mac)
66 | {
67 | tcpipInit();
68 |
69 | //esp_base_mac_addr_set( W5500_Mac );
70 |
71 | if ( esp_read_mac(mac_eth, ESP_MAC_ETH) == ESP_OK )
72 | {
73 | char macStr[18] = { 0 };
74 |
75 | sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", mac_eth[0], mac_eth[1], mac_eth[2],
76 | mac_eth[3], mac_eth[4], mac_eth[5]);
77 |
78 | ET_LOGINFO1("Using built-in mac_eth =", macStr);
79 |
80 | esp_base_mac_addr_set( mac_eth );
81 | }
82 | else
83 | {
84 | ET_LOGINFO("Using user mac_eth");
85 | memcpy(mac_eth, W5500_Mac, sizeof(mac_eth));
86 |
87 | esp_base_mac_addr_set( W5500_Mac );
88 | }
89 |
90 | tcpip_adapter_set_default_eth_handlers();
91 |
92 | esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH();
93 | esp_netif_t *eth_netif = esp_netif_new(&cfg);
94 |
95 | esp_eth_mac_t *eth_mac = w5500_begin(MISO, MOSI, SCLK, CS, INT, SPICLOCK_MHZ, SPIHOST);
96 |
97 | if (eth_mac == NULL)
98 | {
99 | ET_LOGERROR("esp_eth_mac_new_esp32 failed");
100 |
101 | return false;
102 | }
103 |
104 | eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
105 | phy_config.autonego_timeout_ms = 0; // W5500 doesn't support auto-negotiation
106 | phy_config.reset_gpio_num = -1; // W5500 doesn't have a pin to reset internal PHY
107 | esp_eth_phy_t *eth_phy = esp_eth_phy_new_w5500(&phy_config);
108 |
109 | if (eth_phy == NULL)
110 | {
111 | ET_LOGERROR("esp_eth_phy_new failed");
112 |
113 | return false;
114 | }
115 |
116 | eth_handle = NULL;
117 | esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(eth_mac, eth_phy);
118 |
119 | if (esp_eth_driver_install(ð_config, ð_handle) != ESP_OK || eth_handle == NULL)
120 | {
121 | ET_LOG("esp_eth_driver_install failed");
122 |
123 | return false;
124 | }
125 |
126 | eth_mac->set_addr(eth_mac, mac_eth);
127 |
128 | #if 1
129 |
130 | if ( (SPICLOCK_MHZ < 14) || (SPICLOCK_MHZ > 25) )
131 | {
132 | ET_LOGERROR("SPI Clock must be >= 8 and <= 25 MHz for W5500");
133 | ESP_ERROR_CHECK(ESP_FAIL);
134 | }
135 |
136 | #endif
137 |
138 | /* attach Ethernet driver to TCP/IP stack */
139 | if (esp_netif_attach(eth_netif, esp_eth_new_netif_glue(eth_handle)) != ESP_OK)
140 | {
141 | ET_LOGERROR("esp_netif_attach failed");
142 |
143 | return false;
144 | }
145 |
146 | if (esp_eth_start(eth_handle) != ESP_OK)
147 | {
148 | ET_LOG("esp_eth_start failed");
149 |
150 | return false;
151 | }
152 |
153 | // holds a few microseconds to let DHCP start and enter into a good state
154 | // FIX ME -- addresses issue https://github.com/espressif/arduino-esp32/issues/5733
155 | delay(50);
156 |
157 | return true;
158 | }
159 |
160 | ////////////////////////////////////////
161 |
162 | bool ESP32_W5500::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1, IPAddress dns2)
163 | {
164 | esp_err_t err = ESP_OK;
165 | tcpip_adapter_ip_info_t info;
166 |
167 | if (static_cast(local_ip) != 0)
168 | {
169 | info.ip.addr = static_cast(local_ip);
170 | info.gw.addr = static_cast(gateway);
171 | info.netmask.addr = static_cast(subnet);
172 | }
173 | else
174 | {
175 | info.ip.addr = 0;
176 | info.gw.addr = 0;
177 | info.netmask.addr = 0;
178 | }
179 |
180 | err = tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_ETH);
181 |
182 | if (err != ESP_OK && err != ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STOPPED)
183 | {
184 | ET_LOGERROR1("DHCP could not be stopped! Error =", err);
185 | return false;
186 | }
187 |
188 | err = tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_ETH, &info);
189 |
190 | if (err != ERR_OK)
191 | {
192 | ET_LOGERROR1("STA IP could not be configured! Error = ", err);
193 | return false;
194 | }
195 |
196 | if (info.ip.addr)
197 | {
198 | staticIP = true;
199 | }
200 | else
201 | {
202 | err = tcpip_adapter_dhcpc_start(TCPIP_ADAPTER_IF_ETH);
203 |
204 | if (err != ESP_OK && err != ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STARTED)
205 | {
206 | ET_LOGWARN1("DHCP could not be started! Error =", err);
207 | return false;
208 | }
209 |
210 | staticIP = false;
211 | }
212 |
213 | ip_addr_t d;
214 | d.type = IPADDR_TYPE_V4;
215 |
216 | if (static_cast(dns1) != 0)
217 | {
218 | // Set DNS1-Server
219 | d.u_addr.ip4.addr = static_cast(dns1);
220 | dns_setserver(0, &d);
221 | }
222 |
223 | if (static_cast(dns2) != 0)
224 | {
225 | // Set DNS2-Server
226 | d.u_addr.ip4.addr = static_cast(dns2);
227 | dns_setserver(1, &d);
228 | }
229 |
230 | return true;
231 | }
232 |
233 | ////////////////////////////////////////
234 |
235 | IPAddress ESP32_W5500::localIP()
236 | {
237 | tcpip_adapter_ip_info_t ip;
238 |
239 | if (tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_ETH, &ip))
240 | {
241 | ET_LOGDEBUG("localIP NULL");
242 |
243 | return IPAddress();
244 | }
245 |
246 | ET_LOGDEBUG1("localIP =", IPAddress(ip.ip.addr));
247 |
248 | return IPAddress(ip.ip.addr);
249 | }
250 |
251 | ////////////////////////////////////////
252 |
253 | IPAddress ESP32_W5500::subnetMask()
254 | {
255 | tcpip_adapter_ip_info_t ip;
256 |
257 | if (tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_ETH, &ip))
258 | {
259 | return IPAddress();
260 | }
261 |
262 | return IPAddress(ip.netmask.addr);
263 | }
264 |
265 | ////////////////////////////////////////
266 |
267 | IPAddress ESP32_W5500::gatewayIP()
268 | {
269 | tcpip_adapter_ip_info_t ip;
270 |
271 | if (tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_ETH, &ip))
272 | {
273 | return IPAddress();
274 | }
275 |
276 | return IPAddress(ip.gw.addr);
277 | }
278 |
279 | ////////////////////////////////////////
280 |
281 | IPAddress ESP32_W5500::dnsIP(uint8_t dns_no)
282 | {
283 | const ip_addr_t * dns_ip = dns_getserver(dns_no);
284 |
285 | return IPAddress(dns_ip->u_addr.ip4.addr);
286 | }
287 |
288 | ////////////////////////////////////////
289 |
290 | IPAddress ESP32_W5500::broadcastIP()
291 | {
292 | tcpip_adapter_ip_info_t ip;
293 |
294 | if (tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_ETH, &ip))
295 | {
296 | return IPAddress();
297 | }
298 |
299 | return WiFiGenericClass::calculateBroadcast(IPAddress(ip.gw.addr), IPAddress(ip.netmask.addr));
300 | }
301 |
302 | ////////////////////////////////////////
303 |
304 | IPAddress ESP32_W5500::networkID()
305 | {
306 | tcpip_adapter_ip_info_t ip;
307 |
308 | if (tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_ETH, &ip))
309 | {
310 | return IPAddress();
311 | }
312 |
313 | return WiFiGenericClass::calculateNetworkID(IPAddress(ip.gw.addr), IPAddress(ip.netmask.addr));
314 | }
315 |
316 | ////////////////////////////////////////
317 |
318 | uint8_t ESP32_W5500::subnetCIDR()
319 | {
320 | tcpip_adapter_ip_info_t ip;
321 |
322 | if (tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_ETH, &ip))
323 | {
324 | return (uint8_t)0;
325 | }
326 |
327 | return WiFiGenericClass::calculateSubnetCIDR(IPAddress(ip.netmask.addr));
328 | }
329 |
330 | ////////////////////////////////////////
331 |
332 | const char * ESP32_W5500::getHostname()
333 | {
334 | const char * hostname;
335 |
336 | if (tcpip_adapter_get_hostname(TCPIP_ADAPTER_IF_ETH, &hostname))
337 | {
338 | return NULL;
339 | }
340 |
341 | return hostname;
342 | }
343 |
344 | ////////////////////////////////////////
345 |
346 | bool ESP32_W5500::setHostname(const char * hostname)
347 | {
348 | return tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_ETH, hostname) == 0;
349 | }
350 |
351 | ////////////////////////////////////////
352 |
353 | bool ESP32_W5500::fullDuplex()
354 | {
355 | #ifdef ESP_IDF_VERSION_MAJOR
356 | return true;//todo: do not see an API for this
357 | #else
358 | return eth_config.phy_get_duplex_mode();
359 | #endif
360 | }
361 |
362 | ////////////////////////////////////////
363 |
364 | bool ESP32_W5500::linkUp()
365 | {
366 | #ifdef ESP_IDF_VERSION_MAJOR
367 | return eth_link == ETH_LINK_UP;
368 | #else
369 | return eth_config.phy_check_link();
370 | #endif
371 | }
372 |
373 | ////////////////////////////////////////
374 |
375 | uint8_t ESP32_W5500::linkSpeed()
376 | {
377 | #ifdef ESP_IDF_VERSION_MAJOR
378 | eth_speed_t link_speed;
379 | esp_eth_ioctl(eth_handle, ETH_CMD_G_SPEED, &link_speed);
380 | return (link_speed == ETH_SPEED_10M) ? 10 : 100;
381 | #else
382 | return eth_config.phy_get_speed_mode() ? 100 : 10;
383 | #endif
384 | }
385 |
386 | ////////////////////////////////////////
387 |
388 | bool ESP32_W5500::enableIpV6()
389 | {
390 | return tcpip_adapter_create_ip6_linklocal(TCPIP_ADAPTER_IF_ETH) == 0;
391 | }
392 |
393 | ////////////////////////////////////////
394 |
395 | IPv6Address ESP32_W5500::localIPv6()
396 | {
397 | static ip6_addr_t addr;
398 |
399 | if (tcpip_adapter_get_ip6_linklocal(TCPIP_ADAPTER_IF_ETH, &addr))
400 | {
401 | return IPv6Address();
402 | }
403 |
404 | return IPv6Address(addr.addr);
405 | }
406 |
407 | ////////////////////////////////////////
408 |
409 | uint8_t * ESP32_W5500::macAddress(uint8_t* mac)
410 | {
411 | if (!mac)
412 | {
413 | return NULL;
414 | }
415 |
416 | #ifdef ESP_IDF_VERSION_MAJOR
417 | esp_eth_ioctl(eth_handle, ETH_CMD_G_MAC_ADDR, mac);
418 | #else
419 | esp_eth_get_mac(mac);
420 | #endif
421 |
422 | return mac;
423 | }
424 |
425 | ////////////////////////////////////////
426 |
427 | String ESP32_W5500::macAddress()
428 | {
429 | uint8_t mac[6] = {0, 0, 0, 0, 0, 0};
430 | char macStr[18] = { 0 };
431 | macAddress(mac);
432 | sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
433 |
434 | return String(macStr);
435 | }
436 |
437 | ////////////////////////////////////////
438 |
439 | ESP32_W5500 ETH;
440 |
--------------------------------------------------------------------------------
/src/w5500/esp32_w5500.h:
--------------------------------------------------------------------------------
1 | /****************************************************************************************************************************
2 | esp32_w5500.h
3 |
4 | For Ethernet shields using ESP32_W5500 (ESP32 + W5500)
5 |
6 | WebServer_ESP32_W5500 is a library for the ESP32 with Ethernet W5500 to run WebServer
7 |
8 | Based on and modified from ESP32-IDF https://github.com/espressif/esp-idf
9 | Built by Khoi Hoang https://github.com/khoih-prog/WebServer_ESP32_W5500
10 | Licensed under GPLv3 license
11 |
12 | Version: 1.5.3
13 |
14 | Version Modified By Date Comments
15 | ------- ----------- ---------- -----------
16 | 1.5.1 K Hoang 29/11/2022 Initial coding for ESP32_W5500 (ESP32 + W5500). Sync with WebServer_WT32_ETH01 v1.5.1
17 | 1.5.2 K Hoang 06/01/2023 Suppress compile error when using aggressive compile settings
18 | 1.5.3 K Hoang 11/01/2023 Using `SPI_DMA_CH_AUTO` and built-in ESP32 MAC
19 | *****************************************************************************************************************************/
20 |
21 | #ifndef _ESP32_W5500_H_
22 | #define _ESP32_W5500_H_
23 |
24 | #include "WiFi.h"
25 | #include "esp_system.h"
26 | #include "esp_eth.h"
27 |
28 | #include
29 |
30 | ////////////////////////////////////////
31 |
32 | #if ESP_IDF_VERSION_MAJOR < 4 || ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(4,4,0)
33 | #error "This version of Arduino is too old"
34 | #endif
35 |
36 | ////////////////////////////////////////
37 |
38 | static uint8_t W5500_Default_Mac[] = { 0xFE, 0xED, 0xDE, 0xAD, 0xBE, 0xEF };
39 |
40 | ////////////////////////////////////////
41 |
42 | class ESP32_W5500
43 | {
44 | private:
45 | bool initialized;
46 | bool staticIP;
47 |
48 | uint8_t mac_eth[6] = { 0xFE, 0xED, 0xDE, 0xAD, 0xBE, 0xEF };
49 |
50 | #if ESP_IDF_VERSION_MAJOR > 3
51 | esp_eth_handle_t eth_handle;
52 |
53 | protected:
54 | bool started;
55 | eth_link_t eth_link;
56 | static void eth_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
57 | #else
58 | bool started;
59 | eth_config_t eth_config;
60 | #endif
61 |
62 | public:
63 | ESP32_W5500();
64 | ~ESP32_W5500();
65 |
66 | bool begin(int MISO, int MOSI, int SCLK, int CS, int INT, int SPICLOCK_MHZ = 25, int SPIHOST = SPI3_HOST,
67 | uint8_t *W5500_Mac = W5500_Default_Mac);
68 |
69 | bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = (uint32_t)0x00000000,
70 | IPAddress dns2 = (uint32_t)0x00000000);
71 |
72 | const char * getHostname();
73 | bool setHostname(const char * hostname);
74 |
75 | bool fullDuplex();
76 | bool linkUp();
77 | uint8_t linkSpeed();
78 |
79 | bool enableIpV6();
80 | IPv6Address localIPv6();
81 |
82 | IPAddress localIP();
83 | IPAddress subnetMask();
84 | IPAddress gatewayIP();
85 | IPAddress dnsIP(uint8_t dns_no = 0);
86 |
87 | IPAddress broadcastIP();
88 | IPAddress networkID();
89 | uint8_t subnetCIDR();
90 |
91 | uint8_t * macAddress(uint8_t* mac);
92 | String macAddress();
93 |
94 | friend class WiFiClient;
95 | friend class WiFiServer;
96 | };
97 |
98 | ////////////////////////////////////////
99 |
100 | extern ESP32_W5500 ETH;
101 |
102 | ////////////////////////////////////////
103 |
104 | #endif /* _ESP32_W5500_H_ */
105 |
--------------------------------------------------------------------------------
/src/w5500/esp_eth/esp_eth_phy_w5500.c:
--------------------------------------------------------------------------------
1 | /****************************************************************************************************************************
2 | esp_eth_phy_w5500.c
3 |
4 | For Ethernet shields using ESP32_W5500 (ESP32 + W5500)
5 |
6 | WebServer_ESP32_W5500 is a library for the ESP32 with Ethernet W5500 to run WebServer
7 |
8 | Based on and modified from ESP32-IDF https://github.com/espressif/esp-idf
9 | Built by Khoi Hoang https://github.com/khoih-prog/WebServer_ESP32_W5500
10 | Licensed under GPLv3 license
11 |
12 | Version: 1.5.3
13 |
14 | Version Modified By Date Comments
15 | ------- ----------- ---------- -----------
16 | 1.5.1 K Hoang 29/11/2022 Initial coding for ESP32_W5500 (ESP32 + W5500). Sync with WebServer_WT32_ETH01 v1.5.1
17 | 1.5.2 K Hoang 06/01/2023 Suppress compile error when using aggressive compile settings
18 | 1.5.3 K Hoang 11/01/2023 Using `SPI_DMA_CH_AUTO` and built-in ESP32 MAC
19 | *****************************************************************************************************************************/
20 | // Copyright 2020 Espressif Systems (Shanghai) PTE LTD
21 | //
22 | // Licensed under the Apache License, Version 2.0 (the "License");
23 | // you may not use this file except in compliance with the License.
24 | // You may obtain a copy of the License at
25 | //
26 | // http://www.apache.org/licenses/LICENSE-2.0
27 | //
28 | // Unless required by applicable law or agreed to in writing, software
29 | // distributed under the License is distributed on an "AS IS" BASIS,
30 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 | // See the License for the specific language governing permissions and
32 | // limitations under the License.
33 |
34 | ////////////////////////////////////////
35 |
36 | #include
37 | #include
38 | #include
39 | #include "esp_log.h"
40 | #include "esp_check.h"
41 | #include "esp_eth.h"
42 | #include "freertos/FreeRTOS.h"
43 | #include "freertos/task.h"
44 | #include "driver/gpio.h"
45 | #include "esp_rom_gpio.h"
46 | #include "esp_rom_sys.h"
47 | #include "w5500.h"
48 |
49 | ////////////////////////////////////////
50 |
51 | static const char *TAG = "w5500.phy";
52 |
53 | ////////////////////////////////////////
54 |
55 | /***************Vendor Specific Register***************/
56 | /**
57 | @brief PHYCFGR(PHY Configuration Register)
58 |
59 | */
60 | typedef union
61 | {
62 | struct
63 | {
64 | uint8_t link: 1; /*!< Link status */
65 | uint8_t speed: 1; /*!< Speed status */
66 | uint8_t duplex: 1; /*!< Duplex status */
67 | uint8_t opmode: 3; /*!< Operation mode */
68 | uint8_t opsel: 1; /*!< Operation select */
69 | uint8_t reset: 1; /*!< Reset, when this bit is '0', PHY will get reset */
70 | };
71 |
72 | uint8_t val;
73 | } phycfg_reg_t;
74 |
75 | ////////////////////////////////////////
76 |
77 | typedef struct
78 | {
79 | esp_eth_phy_t parent;
80 | esp_eth_mediator_t *eth;
81 | int addr;
82 | uint32_t reset_timeout_ms;
83 | uint32_t autonego_timeout_ms;
84 | eth_link_t link_status;
85 | int reset_gpio_num;
86 | } phy_w5500_t;
87 |
88 | ////////////////////////////////////////
89 |
90 | static esp_err_t w5500_update_link_duplex_speed(phy_w5500_t *w5500)
91 | {
92 | esp_err_t ret = ESP_OK;
93 |
94 | esp_eth_mediator_t *eth = w5500->eth;
95 | eth_speed_t speed = ETH_SPEED_10M;
96 | eth_duplex_t duplex = ETH_DUPLEX_HALF;
97 | phycfg_reg_t phycfg;
98 |
99 | ESP_GOTO_ON_ERROR(eth->phy_reg_read(eth, w5500->addr, W5500_REG_PHYCFGR, (uint32_t *) & (phycfg.val)), err, TAG,
100 | "Read PHYCFG failed");
101 | eth_link_t link = phycfg.link ? ETH_LINK_UP : ETH_LINK_DOWN;
102 |
103 | /* check if link status changed */
104 | if (w5500->link_status != link)
105 | {
106 | /* when link up, read negotiation result */
107 | if (link == ETH_LINK_UP)
108 | {
109 | if (phycfg.speed)
110 | {
111 | speed = ETH_SPEED_100M;
112 | }
113 | else
114 | {
115 | speed = ETH_SPEED_10M;
116 | }
117 |
118 | if (phycfg.duplex)
119 | {
120 | duplex = ETH_DUPLEX_FULL;
121 | }
122 | else
123 | {
124 | duplex = ETH_DUPLEX_HALF;
125 | }
126 |
127 | ESP_GOTO_ON_ERROR(eth->on_state_changed(eth, ETH_STATE_SPEED, (void *)speed), err, TAG, "Change speed failed");
128 | ESP_GOTO_ON_ERROR(eth->on_state_changed(eth, ETH_STATE_DUPLEX, (void *)duplex), err, TAG, "Change duplex failed");
129 | }
130 |
131 | ESP_GOTO_ON_ERROR(eth->on_state_changed(eth, ETH_STATE_LINK, (void *)link), err, TAG, "Change link failed");
132 | w5500->link_status = link;
133 | }
134 |
135 | return ESP_OK;
136 |
137 | err:
138 | return ret;
139 | }
140 |
141 | ////////////////////////////////////////
142 |
143 | static esp_err_t w5500_set_mediator(esp_eth_phy_t *phy, esp_eth_mediator_t *eth)
144 | {
145 | esp_err_t ret = ESP_OK;
146 |
147 | ESP_GOTO_ON_FALSE(eth, ESP_ERR_INVALID_ARG, err, TAG, "Can't set mediator to null");
148 | phy_w5500_t *w5500 = __containerof(phy, phy_w5500_t, parent);
149 | w5500->eth = eth;
150 |
151 | return ESP_OK;
152 |
153 | err:
154 | return ret;
155 | }
156 |
157 | ////////////////////////////////////////
158 |
159 | static esp_err_t w5500_get_link(esp_eth_phy_t *phy)
160 | {
161 | esp_err_t ret = ESP_OK;
162 |
163 | phy_w5500_t *w5500 = __containerof(phy, phy_w5500_t, parent);
164 |
165 | /* Updata information about link, speed, duplex */
166 | ESP_GOTO_ON_ERROR(w5500_update_link_duplex_speed(w5500), err, TAG, "Update link duplex speed failed");
167 |
168 | return ESP_OK;
169 |
170 | err:
171 | return ret;
172 | }
173 |
174 | ////////////////////////////////////////
175 |
176 | static esp_err_t w5500_reset(esp_eth_phy_t *phy)
177 | {
178 | esp_err_t ret = ESP_OK;
179 |
180 | phy_w5500_t *w5500 = __containerof(phy, phy_w5500_t, parent);
181 |
182 | w5500->link_status = ETH_LINK_DOWN;
183 | esp_eth_mediator_t *eth = w5500->eth;
184 |
185 | phycfg_reg_t phycfg;
186 | ESP_GOTO_ON_ERROR(eth->phy_reg_read(eth, w5500->addr, W5500_REG_PHYCFGR, (uint32_t *) & (phycfg.val)), err, TAG,
187 | "Read PHYCFG failed");
188 |
189 | phycfg.reset = 0; // set to '0' will reset internal PHY
190 | ESP_GOTO_ON_ERROR(eth->phy_reg_write(eth, w5500->addr, W5500_REG_PHYCFGR, phycfg.val), err, TAG, "Write PHYCFG failed");
191 |
192 | vTaskDelay(pdMS_TO_TICKS(10));
193 |
194 | phycfg.reset = 1; // set to '1' after reset
195 | ESP_GOTO_ON_ERROR(eth->phy_reg_write(eth, w5500->addr, W5500_REG_PHYCFGR, phycfg.val), err, TAG, "Write PHYCFG failed");
196 |
197 | return ESP_OK;
198 |
199 | err:
200 | return ret;
201 | }
202 |
203 | ////////////////////////////////////////
204 |
205 | static esp_err_t w5500_reset_hw(esp_eth_phy_t *phy)
206 | {
207 | phy_w5500_t *w5500 = __containerof(phy, phy_w5500_t, parent);
208 |
209 | // set reset_gpio_num to a negative value can skip hardware reset phy chip
210 | if (w5500->reset_gpio_num >= 0)
211 | {
212 | esp_rom_gpio_pad_select_gpio(w5500->reset_gpio_num);
213 | gpio_set_direction(w5500->reset_gpio_num, GPIO_MODE_OUTPUT);
214 | gpio_set_level(w5500->reset_gpio_num, 0);
215 | esp_rom_delay_us(100); // insert min input assert time
216 | gpio_set_level(w5500->reset_gpio_num, 1);
217 | }
218 |
219 | return ESP_OK;
220 | }
221 |
222 | ////////////////////////////////////////
223 |
224 | static esp_err_t w5500_negotiate(esp_eth_phy_t *phy)
225 | {
226 | esp_err_t ret = ESP_OK;
227 |
228 | phy_w5500_t *w5500 = __containerof(phy, phy_w5500_t, parent);
229 |
230 | esp_eth_mediator_t *eth = w5500->eth;
231 |
232 | /* in case any link status has changed, let's assume we're in link down status */
233 | w5500->link_status = ETH_LINK_DOWN;
234 | phycfg_reg_t phycfg;
235 | ESP_GOTO_ON_ERROR(eth->phy_reg_read(eth, w5500->addr, W5500_REG_PHYCFGR, (uint32_t *) & (phycfg.val)), err, TAG,
236 | "Read PHYCFG failed");
237 |
238 | phycfg.opsel = 1; // PHY working mode configured by register
239 | phycfg.opmode = 7; // all capable, auto-negotiation enabled
240 | ESP_GOTO_ON_ERROR(eth->phy_reg_write(eth, w5500->addr, W5500_REG_PHYCFGR, phycfg.val), err, TAG, "Write PHYCFG failed");
241 |
242 | return ESP_OK;
243 |
244 | err:
245 | return ret;
246 | }
247 |
248 | ////////////////////////////////////////
249 |
250 | static esp_err_t w5500_pwrctl(esp_eth_phy_t *phy, bool enable)
251 | {
252 | // power control is not supported for W5500 internal PHY
253 | return ESP_OK;
254 | }
255 |
256 | ////////////////////////////////////////
257 |
258 | static esp_err_t w5500_set_addr(esp_eth_phy_t *phy, uint32_t addr)
259 | {
260 | phy_w5500_t *w5500 = __containerof(phy, phy_w5500_t, parent);
261 | w5500->addr = addr;
262 |
263 | return ESP_OK;
264 | }
265 |
266 | ////////////////////////////////////////
267 |
268 | static esp_err_t w5500_get_addr(esp_eth_phy_t *phy, uint32_t *addr)
269 | {
270 | esp_err_t ret = ESP_OK;
271 |
272 | ESP_GOTO_ON_FALSE(addr, ESP_ERR_INVALID_ARG, err, TAG, "Addr can't be null");
273 | phy_w5500_t *w5500 = __containerof(phy, phy_w5500_t, parent);
274 | *addr = w5500->addr;
275 |
276 | return ESP_OK;
277 |
278 | err:
279 | return ret;
280 | }
281 |
282 | ////////////////////////////////////////
283 |
284 | static esp_err_t w5500_del(esp_eth_phy_t *phy)
285 | {
286 | phy_w5500_t *w5500 = __containerof(phy, phy_w5500_t, parent);
287 | free(w5500);
288 |
289 | return ESP_OK;
290 | }
291 |
292 | ////////////////////////////////////////
293 |
294 | static esp_err_t w5500_advertise_pause_ability(esp_eth_phy_t *phy, uint32_t ability)
295 | {
296 | // pause ability advertisement is not supported for W5500 internal PHY
297 | return ESP_OK;
298 | }
299 |
300 | ////////////////////////////////////////
301 |
302 | static esp_err_t w5500_loopback(esp_eth_phy_t *phy, bool enable)
303 | {
304 | // Loopback is not supported for W5500 internal PHY
305 | return ESP_ERR_NOT_SUPPORTED;
306 | }
307 |
308 | ////////////////////////////////////////
309 |
310 | static esp_err_t w5500_init(esp_eth_phy_t *phy)
311 | {
312 | esp_err_t ret = ESP_OK;
313 |
314 | /* Power on Ethernet PHY */
315 | ESP_GOTO_ON_ERROR(w5500_pwrctl(phy, true), err, TAG, "Power control failed");
316 |
317 | /* Reset Ethernet PHY */
318 | ESP_GOTO_ON_ERROR(w5500_reset(phy), err, TAG, "Reset failed");
319 |
320 | return ESP_OK;
321 |
322 | err:
323 | return ret;
324 | }
325 |
326 | ////////////////////////////////////////
327 |
328 | static esp_err_t w5500_deinit(esp_eth_phy_t *phy)
329 | {
330 | esp_err_t ret = ESP_OK;
331 |
332 | /* Power off Ethernet PHY */
333 | ESP_GOTO_ON_ERROR(w5500_pwrctl(phy, false), err, TAG, "Power control failed");
334 |
335 | return ESP_OK;
336 |
337 | err:
338 | return ret;
339 | }
340 |
341 | ////////////////////////////////////////
342 |
343 | esp_eth_phy_t *esp_eth_phy_new_w5500(const eth_phy_config_t *config)
344 | {
345 | esp_eth_phy_t *ret = NULL;
346 |
347 | ESP_GOTO_ON_FALSE(config, NULL, err, TAG, "Invalid arguments");
348 |
349 | phy_w5500_t *w5500 = calloc(1, sizeof(phy_w5500_t));
350 | ESP_GOTO_ON_FALSE(w5500, NULL, err, TAG, "No mem for PHY instance");
351 |
352 | /* bind methods and attributes */
353 | w5500->addr = config->phy_addr;
354 | w5500->reset_timeout_ms = config->reset_timeout_ms;
355 | w5500->reset_gpio_num = config->reset_gpio_num;
356 | w5500->link_status = ETH_LINK_DOWN;
357 | w5500->autonego_timeout_ms = config->autonego_timeout_ms;
358 | w5500->parent.reset = w5500_reset;
359 | w5500->parent.reset_hw = w5500_reset_hw;
360 | w5500->parent.init = w5500_init;
361 | w5500->parent.deinit = w5500_deinit;
362 | w5500->parent.set_mediator = w5500_set_mediator;
363 | w5500->parent.negotiate = w5500_negotiate;
364 | w5500->parent.get_link = w5500_get_link;
365 | w5500->parent.pwrctl = w5500_pwrctl;
366 | w5500->parent.get_addr = w5500_get_addr;
367 | w5500->parent.set_addr = w5500_set_addr;
368 | w5500->parent.advertise_pause_ability = w5500_advertise_pause_ability;
369 | w5500->parent.loopback = w5500_loopback;
370 | w5500->parent.del = w5500_del;
371 |
372 | return &(w5500->parent);
373 |
374 | err:
375 | return ret;
376 | }
377 |
378 | ////////////////////////////////////////
379 |
380 |
--------------------------------------------------------------------------------
/src/w5500/esp_eth/esp_eth_spi_w5500.c:
--------------------------------------------------------------------------------
1 | /****************************************************************************************************************************
2 | esp_eth_spi_w5500.c
3 |
4 | For Ethernet shields using ESP32_W5500 (ESP32 + W5500)
5 |
6 | WebServer_ESP32_W5500 is a library for the ESP32 with Ethernet W5500 to run WebServer
7 |
8 | Based on and modified from ESP32-IDF https://github.com/espressif/esp-idf
9 | Built by Khoi Hoang https://github.com/khoih-prog/WebServer_ESP32_W5500
10 | Licensed under GPLv3 license
11 |
12 | Version: 1.5.3
13 |
14 | Version Modified By Date Comments
15 | ------- ----------- ---------- -----------
16 | 1.5.1 K Hoang 29/11/2022 Initial coding for ESP32_W5500 (ESP32 + W5500). Sync with WebServer_WT32_ETH01 v1.5.1
17 | 1.5.2 K Hoang 06/01/2023 Suppress compile error when using aggressive compile settings
18 | 1.5.3 K Hoang 11/01/2023 Using `SPI_DMA_CH_AUTO` and built-in ESP32 MAC
19 | *****************************************************************************************************************************/
20 |
21 | #include
22 | #include
23 | #include "sdkconfig.h"
24 | #include "freertos/FreeRTOS.h"
25 | #include "freertos/task.h"
26 | #include "esp_netif.h"
27 | #include "esp_eth.h"
28 | #include "esp_event.h"
29 | #include "driver/gpio.h"
30 | #include "esp_eth_w5500.h"
31 | #include "driver/spi_master.h"
32 |
33 | #include "esp_log.h"
34 | #include "esp_check.h"
35 |
36 | static const char *TAG = "w5500.spi";
37 |
38 | ////////////////////////////////////////
39 |
40 | esp_eth_mac_t* w5500_new_mac( spi_device_handle_t *spi_handle, int INT_GPIO )
41 | {
42 | eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG( *spi_handle );
43 | w5500_config.int_gpio_num = INT_GPIO;
44 |
45 | eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
46 |
47 | //eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
48 | //phy_config.reset_gpio_num = -1;
49 |
50 | mac_config.smi_mdc_gpio_num = -1; // w5500 doesn't have SMI interface
51 | mac_config.smi_mdio_gpio_num = -1;
52 | mac_config.rx_task_prio = 1;
53 |
54 | return esp_eth_mac_new_w5500( &w5500_config, &mac_config );
55 | }
56 |
57 | ////////////////////////////////////////
58 |
59 | esp_eth_mac_t* w5500_begin(int MISO_GPIO, int MOSI_GPIO, int SCLK_GPIO, int CS_GPIO, int INT_GPIO, int SPICLOCK_MHZ,
60 | int SPIHOST)
61 | {
62 | if (ESP_OK != gpio_install_isr_service(0))
63 | {
64 | ESP_LOGE(TAG, "%s(%d): Error gpio_install_isr_service", __FUNCTION__, __LINE__);
65 |
66 | return NULL;
67 | }
68 |
69 | /* w5500 ethernet driver is based on spi driver */
70 | spi_bus_config_t buscfg =
71 | {
72 | .miso_io_num = MISO_GPIO,
73 | .mosi_io_num = MOSI_GPIO,
74 | .sclk_io_num = SCLK_GPIO,
75 | .quadwp_io_num = -1,
76 | .quadhd_io_num = -1,
77 | };
78 |
79 | if ( ESP_OK != spi_bus_initialize( SPIHOST, &buscfg, SPI_DMA_CH_AUTO ))
80 | {
81 | ESP_LOGE(TAG, "%s(%d): Error spi_bus_initialize", __FUNCTION__, __LINE__);
82 |
83 | return NULL;
84 | }
85 |
86 | spi_device_interface_config_t devcfg =
87 | {
88 | .command_bits = 16,
89 | .address_bits = 8,
90 | .mode = 0,
91 | .clock_speed_hz = SPICLOCK_MHZ * 1000 * 1000,
92 | .spics_io_num = CS_GPIO,
93 | .queue_size = 20,
94 | .cs_ena_posttrans = w5500_cal_spi_cs_hold_time(SPICLOCK_MHZ),
95 | };
96 |
97 | spi_device_handle_t spi_handle = NULL;
98 |
99 | if (ESP_OK != spi_bus_add_device( SPIHOST, &devcfg, &spi_handle ))
100 | {
101 | ESP_LOGE(TAG, "%s(%d): Error spi_bus_add_device", __FUNCTION__, __LINE__);
102 |
103 | return NULL;
104 | }
105 |
106 | return w5500_new_mac( &spi_handle, INT_GPIO );
107 | }
108 |
109 | ////////////////////////////////////////
110 |
111 |
--------------------------------------------------------------------------------
/src/w5500/esp_eth/esp_eth_w5500.h:
--------------------------------------------------------------------------------
1 | /****************************************************************************************************************************
2 | esp_eth_w5500.h
3 |
4 | For Ethernet shields using ESP32_W5500 (ESP32 + W5500)
5 |
6 | WebServer_ESP32_W5500 is a library for the ESP32 with Ethernet W5500 to run WebServer
7 |
8 | Based on and modified from ESP32-IDF https://github.com/espressif/esp-idf
9 | Built by Khoi Hoang https://github.com/khoih-prog/WebServer_ESP32_W5500
10 | Licensed under GPLv3 license
11 |
12 | Version: 1.5.3
13 |
14 | Version Modified By Date Comments
15 | ------- ----------- ---------- -----------
16 | 1.5.1 K Hoang 29/11/2022 Initial coding for ESP32_W5500 (ESP32 + W5500). Sync with WebServer_WT32_ETH01 v1.5.1
17 | 1.5.2 K Hoang 06/01/2023 Suppress compile error when using aggressive compile settings
18 | 1.5.3 K Hoang 11/01/2023 Using `SPI_DMA_CH_AUTO` and built-in ESP32 MAC
19 | *****************************************************************************************************************************/
20 |
21 | // Copyright 2021 Espressif Systems (Shanghai) PTE LTD
22 | //
23 | // Licensed under the Apache License, Version 2.0 (the "License");
24 | // you may not use this file except in compliance with the License.
25 | // You may obtain a copy of the License at
26 | //
27 | // http://www.apache.org/licenses/LICENSE-2.0
28 | //
29 | // Unless required by applicable law or agreed to in writing, software
30 | // distributed under the License is distributed on an "AS IS" BASIS,
31 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
32 | // See the License for the specific language governing permissions and
33 | // limitations under the License.
34 |
35 | #pragma once
36 |
37 | #ifdef __cplusplus
38 | extern "C" {
39 | #endif
40 |
41 | ////////////////////////////////////////
42 |
43 | #include "esp_eth_phy.h"
44 | #include "esp_eth_mac.h"
45 | #include "driver/spi_master.h"
46 |
47 | ////////////////////////////////////////
48 |
49 | #define CS_HOLD_TIME_MIN_NS 210
50 |
51 | ////////////////////////////////////////
52 |
53 | /*
54 | // From tools/sdk/esp32/include/esp_eth/include/esp_eth_mac.h
55 |
56 | typedef struct
57 | {
58 | void *spi_hdl; //!< Handle of SPI device driver
59 | int int_gpio_num; //!< Interrupt GPIO number
60 | } eth_w5500_config_t;
61 |
62 |
63 | #define ETH_W5500_DEFAULT_CONFIG(spi_device) \
64 | { \
65 | .spi_hdl = spi_device, \
66 | .int_gpio_num = 4, \
67 | }
68 |
69 | */
70 |
71 |
72 | ////////////////////////////////////////
73 |
74 | /**
75 | @brief Compute amount of SPI bit-cycles the CS should stay active after the transmission
76 | to meet w5500 CS Hold Time specification.
77 |
78 | @param clock_speed_mhz SPI Clock frequency in MHz (valid range is <1, 20>)
79 | @return uint8_t
80 | */
81 | static inline uint8_t w5500_cal_spi_cs_hold_time(int clock_speed_mhz)
82 | {
83 | if (clock_speed_mhz <= 0 || clock_speed_mhz > 20)
84 | {
85 | return 0;
86 | }
87 |
88 | int temp = clock_speed_mhz * CS_HOLD_TIME_MIN_NS;
89 | uint8_t cs_posttrans = temp / 1000;
90 |
91 | if (temp % 1000)
92 | {
93 | cs_posttrans += 1;
94 | }
95 |
96 | return cs_posttrans;
97 | }
98 |
99 | ////////////////////////////////////////
100 |
101 | /**
102 | @brief Create w5500 Ethernet MAC instance
103 |
104 | @param[in] w5500_config: w5500 specific configuration
105 | @param[in] mac_config: Ethernet MAC configuration
106 |
107 | @return
108 | - instance: create MAC instance successfully
109 | - NULL: create MAC instance failed because some error occurred
110 | */
111 | esp_eth_mac_t *esp_eth_mac_new_w5500(const eth_w5500_config_t *w5500_config,
112 | const eth_mac_config_t *mac_config);
113 |
114 | ////////////////////////////////////////
115 |
116 | /**
117 | @brief Create a PHY instance of w5500
118 |
119 | @param[in] config: configuration of PHY
120 |
121 | @return
122 | - instance: create PHY instance successfully
123 | - NULL: create PHY instance failed because some error occurred
124 | */
125 | esp_eth_phy_t *esp_eth_phy_new_w5500(const eth_phy_config_t *config);
126 |
127 | ////////////////////////////////////////
128 |
129 | // todo: the below functions should be accessed through ioctl in the future
130 | /**
131 | @brief Set w5500 Duplex mode. It sets Duplex mode first to the PHY and then
132 | MAC is set based on what PHY indicates.
133 |
134 | @param phy w5500 PHY Handle
135 | @param duplex Duplex mode
136 |
137 | @return esp_err_t
138 | - ESP_OK when PHY registers were correctly written.
139 | */
140 | esp_err_t w5500_set_phy_duplex(esp_eth_phy_t *phy, eth_duplex_t duplex);
141 |
142 | ////////////////////////////////////////
143 |
144 | #ifdef __cplusplus
145 | }
146 | #endif
147 |
--------------------------------------------------------------------------------
/src/w5500/esp_eth/w5500.h:
--------------------------------------------------------------------------------
1 | /****************************************************************************************************************************
2 | w5500.h
3 |
4 | For Ethernet shields using ESP32_W5500 (ESP32 + W5500)
5 |
6 | WebServer_ESP32_W5500 is a library for the ESP32 with Ethernet W5500 to run WebServer
7 |
8 | Based on and modified from ESP32-IDF https://github.com/espressif/esp-idf
9 | Built by Khoi Hoang https://github.com/khoih-prog/WebServer_ESP32_W5500
10 | Licensed under GPLv3 license
11 |
12 | Version: 1.5.3
13 |
14 | Version Modified By Date Comments
15 | ------- ----------- ---------- -----------
16 | 1.5.1 K Hoang 29/11/2022 Initial coding for ESP32_W5500 (ESP32 + W5500). Sync with WebServer_WT32_ETH01 v1.5.1
17 | 1.5.2 K Hoang 06/01/2023 Suppress compile error when using aggressive compile settings
18 | 1.5.3 K Hoang 11/01/2023 Using `SPI_DMA_CH_AUTO` and built-in ESP32 MAC
19 | *****************************************************************************************************************************/
20 |
21 | // Copyright 2020 Espressif Systems (Shanghai) PTE LTD
22 | //
23 | // Licensed under the Apache License, Version 2.0 (the "License");
24 | // you may not use this file except in compliance with the License.
25 | // You may obtain a copy of the License at
26 | //
27 | // http://www.apache.org/licenses/LICENSE-2.0
28 | //
29 | // Unless required by applicable law or agreed to in writing, software
30 | // distributed under the License is distributed on an "AS IS" BASIS,
31 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
32 | // See the License for the specific language governing permissions and
33 | // limitations under the License.
34 |
35 | ////////////////////////////////////////
36 |
37 | #define W5500_ADDR_OFFSET (16) // Address length
38 | #define W5500_BSB_OFFSET (3) // Block Select Bits offset
39 | #define W5500_RWB_OFFSET (2) // Read Write Bits offset
40 |
41 | ////////////////////////////////////////
42 |
43 | #define W5500_BSB_COM_REG (0x00) // Common Register
44 | #define W5500_BSB_SOCK_REG(s) ((s)*4+1) // Socket Register
45 | #define W5500_BSB_SOCK_TX_BUF(s) ((s)*4+2) // Socket TX Buffer
46 | #define W5500_BSB_SOCK_RX_BUF(s) ((s)*4+3) // Socket RX Buffer
47 |
48 | ////////////////////////////////////////
49 |
50 | #define W5500_ACCESS_MODE_READ (0) // Read Mode
51 | #define W5500_ACCESS_MODE_WRITE (1) // Write Mode
52 |
53 | ////////////////////////////////////////
54 |
55 | #define W5500_SPI_OP_MODE_VDM (0x00) // Variable Data Length Mode (SPI frame is controlled by CS line)
56 | #define W5500_SPI_OP_MODE_FDM_1 (0x01) // Fixed Data Length Mode, 1 Byte Length
57 | #define W5500_SPI_OP_MODE_FDM_2 (0x02) // Fixed Data Length Mode, 2 Bytes Length
58 | #define W5500_SPI_OP_MODE_FDM_4 (0x03) // Fixed Data Length Mode, 4 Bytes Length
59 |
60 | ////////////////////////////////////////
61 |
62 | #define W5500_MAKE_MAP(offset, bsb) ((offset) << W5500_ADDR_OFFSET | (bsb) << W5500_BSB_OFFSET)
63 |
64 | ////////////////////////////////////////
65 |
66 | #define W5500_REG_MR W5500_MAKE_MAP(0x0000, W5500_BSB_COM_REG) // Mode
67 | #define W5500_REG_MAC W5500_MAKE_MAP(0x0009, W5500_BSB_COM_REG) // MAC Address
68 | #define W5500_REG_INTLEVEL W5500_MAKE_MAP(0x0013, W5500_BSB_COM_REG) // Interrupt Level Timeout
69 | #define W5500_REG_IR W5500_MAKE_MAP(0x0015, W5500_BSB_COM_REG) // Interrupt
70 | #define W5500_REG_IMR W5500_MAKE_MAP(0x0016, W5500_BSB_COM_REG) // Interrupt Mask
71 | #define W5500_REG_SIR W5500_MAKE_MAP(0x0017, W5500_BSB_COM_REG) // Socket Interrupt
72 | #define W5500_REG_SIMR W5500_MAKE_MAP(0x0018, W5500_BSB_COM_REG) // Socket Interrupt Mask
73 | #define W5500_REG_RTR W5500_MAKE_MAP(0x0019, W5500_BSB_COM_REG) // Retry Time
74 | #define W5500_REG_RCR W5500_MAKE_MAP(0x001B, W5500_BSB_COM_REG) // Retry Count
75 | #define W5500_REG_PHYCFGR W5500_MAKE_MAP(0x002E, W5500_BSB_COM_REG) // PHY Configuration
76 | #define W5500_REG_VERSIONR W5500_MAKE_MAP(0x0039, W5500_BSB_COM_REG) // Chip version
77 |
78 | ////////////////////////////////////////
79 |
80 | #define W5500_REG_SOCK_MR(s) W5500_MAKE_MAP(0x0000, W5500_BSB_SOCK_REG(s)) // Socket Mode
81 | #define W5500_REG_SOCK_CR(s) W5500_MAKE_MAP(0x0001, W5500_BSB_SOCK_REG(s)) // Socket Command
82 | #define W5500_REG_SOCK_IR(s) W5500_MAKE_MAP(0x0002, W5500_BSB_SOCK_REG(s)) // Socket Interrupt
83 | #define W5500_REG_SOCK_SR(s) W5500_MAKE_MAP(0x0004, W5500_BSB_SOCK_REG(s)) // Socket Status
84 | #define W5500_REG_SOCK_RXBUF_SIZE(s) W5500_MAKE_MAP(0x001E, W5500_BSB_SOCK_REG(s)) // Socket Receive Buffer Size
85 | #define W5500_REG_SOCK_TXBUF_SIZE(s) W5500_MAKE_MAP(0x001F, W5500_BSB_SOCK_REG(s)) // Socket Transmit Buffer Size
86 | #define W5500_REG_SOCK_TX_FSR(s) W5500_MAKE_MAP(0x0020, W5500_BSB_SOCK_REG(s)) // Socket TX Free Size
87 | #define W5500_REG_SOCK_TX_RD(s) W5500_MAKE_MAP(0x0022, W5500_BSB_SOCK_REG(s)) // Socket TX Read Pointer
88 | #define W5500_REG_SOCK_TX_WR(s) W5500_MAKE_MAP(0x0024, W5500_BSB_SOCK_REG(s)) // Socket TX Write Pointer
89 | #define W5500_REG_SOCK_RX_RSR(s) W5500_MAKE_MAP(0x0026, W5500_BSB_SOCK_REG(s)) // Socket RX Received Size
90 | #define W5500_REG_SOCK_RX_RD(s) W5500_MAKE_MAP(0x0028, W5500_BSB_SOCK_REG(s)) // Socket RX Read Pointer
91 | #define W5500_REG_SOCK_RX_WR(s) W5500_MAKE_MAP(0x002A, W5500_BSB_SOCK_REG(s)) // Socket RX Write Pointer
92 | #define W5500_REG_SOCK_IMR(s) W5500_MAKE_MAP(0x002C, W5500_BSB_SOCK_REG(s)) // Socket Interrupt Mask
93 |
94 | ////////////////////////////////////////
95 |
96 | #define W5500_MEM_SOCK_TX(s,addr) W5500_MAKE_MAP(addr, W5500_BSB_SOCK_TX_BUF(s)) // Socket TX buffer address
97 | #define W5500_MEM_SOCK_RX(s,addr) W5500_MAKE_MAP(addr, W5500_BSB_SOCK_RX_BUF(s)) // Socket RX buffer address
98 |
99 | ////////////////////////////////////////
100 |
101 | #define W5500_MR_RST (1<<7) // Software reset
102 | #define W5500_MR_PB (1<<4) // Ping block (block the response to a ping request)
103 |
104 | ////////////////////////////////////////
105 |
106 | #define W5500_SIMR_SOCK0 (1<<0) // Socket 0 interrupt
107 |
108 | ////////////////////////////////////////
109 |
110 | #define W5500_SMR_MAC_RAW (1<<2) // MAC RAW mode
111 | #define W5500_SMR_MAC_FILTER (1<<7) // MAC filter
112 |
113 | ////////////////////////////////////////
114 |
115 | #define W5500_SCR_OPEN (0x01) // Open command
116 | #define W5500_SCR_CLOSE (0x10) // Close command
117 | #define W5500_SCR_SEND (0x20) // Send command
118 | #define W5500_SCR_RECV (0x40) // Recv command
119 |
120 | ////////////////////////////////////////
121 |
122 | #define W5500_SIR_RECV (1<<2) // Receive done
123 | #define W5500_SIR_SEND (1<<4) // Send done
124 |
125 | ////////////////////////////////////////
126 |
127 |
--------------------------------------------------------------------------------
/utils/astyle_library.conf:
--------------------------------------------------------------------------------
1 | # Code formatting rules for Arduino libraries, modified from for KH libraries:
2 | #
3 | # https://github.com/arduino/Arduino/blob/master/build/shared/examples_formatter.conf
4 | #
5 |
6 | # astyle --style=allman -s2 -t2 -C -S -xW -Y -M120 -f -p -xg -H -xb -c --xC120 -xL *.h *.cpp *.ino
7 |
8 | --mode=c
9 | --lineend=linux
10 | --style=allman
11 |
12 | # -r or -R
13 | #--recursive
14 |
15 | # -c => Converts tabs into spaces
16 | convert-tabs
17 |
18 | # -s2 => 2 spaces indentation
19 | --indent=spaces=2
20 |
21 | # -t2 => tab =2 spaces
22 | #--indent=tab=2
23 |
24 | # -C
25 | --indent-classes
26 |
27 | # -S
28 | --indent-switches
29 |
30 | # -xW
31 | --indent-preproc-block
32 |
33 | # -Y => indent classes, switches (and cases), comments starting at column 1
34 | --indent-col1-comments
35 |
36 | # -M120 => maximum of 120 spaces to indent a continuation line
37 | --max-continuation-indent=120
38 |
39 | # -xC120 => max‑code‑length will break a line if the code exceeds # characters
40 | --max-code-length=120
41 |
42 | # -f =>
43 | --break-blocks
44 |
45 | # -p => put a space around operators
46 | --pad-oper
47 |
48 | # -xg => Insert space padding after commas
49 | --pad-comma
50 |
51 | # -H => put a space after if/for/while
52 | pad-header
53 |
54 | # -xb => Break one line headers (e.g. if/for/while)
55 | --break-one-line-headers
56 |
57 | # -c => Converts tabs into spaces
58 | #--convert-tabs
59 |
60 | # if you like one-liners, keep them
61 | #keep-one-line-statements
62 |
63 | # -xV
64 | --attach-closing-while
65 |
66 | #unpad-paren
67 |
68 | # -xp
69 | remove-comment-prefix
70 |
71 |
--------------------------------------------------------------------------------
/utils/restyle.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | for dir in . ; do
4 | find $dir -type f \( -name "*.c" -o -name "*.h" -o -name "*.cpp" -o -name "*.ino" \) -exec astyle --suffix=none --options=./utils/astyle_library.conf \{\} \;
5 | done
6 |
7 |
--------------------------------------------------------------------------------