93 |
94 |
95 |
96 |
97 |
--------------------------------------------------------------------------------
/examples/HTTPClient/Authorization/Authorization.ino:
--------------------------------------------------------------------------------
1 | /****************************************************************************************************************************
2 | Authorization.ino - Simple Arduino web server sample for Ethernet shield
3 |
4 | For Ethernet shields using WT32_ETH01 (ESP32 + LAN8720)
5 |
6 | WebServer_WT32_ETH01 is a library for the Ethernet LAN8720 in WT32_ETH01 to run WebServer
7 |
8 | Based on and modified from ESP8266 https://github.com/esp8266/Arduino/releases
9 | Built by Khoi Hoang https://github.com/khoih-prog/WebServer_WT32_ETH01
10 | Licensed under MIT license
11 | *****************************************************************************************************************************/
12 |
13 | #define DEBUG_ETHERNET_WEBSERVER_PORT Serial
14 |
15 | // Debug Level from 0 to 4
16 | #define _ETHERNET_WEBSERVER_LOGLEVEL_ 3
17 |
18 | #include
19 | #include
20 |
21 | // Select the IP address according to your local network
22 | IPAddress myIP(192, 168, 2, 232);
23 | IPAddress myGW(192, 168, 2, 1);
24 | IPAddress mySN(255, 255, 255, 0);
25 |
26 | // Google DNS Server IP
27 | IPAddress myDNS(8, 8, 8, 8);
28 |
29 | void setup()
30 | {
31 | Serial.begin(115200);
32 |
33 | while (!Serial);
34 |
35 | // Using this if Serial debugging is not necessary or not using Serial port
36 | //while (!Serial && (millis() < 3000));
37 |
38 | Serial.print("\nStarting Authorization on " + String(ARDUINO_BOARD));
39 | Serial.println(" with " + String(SHIELD_TYPE));
40 | Serial.println(WEBSERVER_WT32_ETH01_VERSION);
41 |
42 | // To be called before ETH.begin()
43 | WT32_ETH01_onEvent();
44 |
45 | //bool begin(uint8_t phy_addr=ETH_PHY_ADDR, int power=ETH_PHY_POWER, int mdc=ETH_PHY_MDC, int mdio=ETH_PHY_MDIO,
46 | // eth_phy_type_t type=ETH_PHY_TYPE, eth_clock_mode_t clk_mode=ETH_CLK_MODE);
47 | //ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER, ETH_PHY_MDC, ETH_PHY_MDIO, ETH_PHY_TYPE, ETH_CLK_MODE);
48 | ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER);
49 |
50 | // Static IP, leave without this line to get IP via DHCP
51 | //bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
52 | ETH.config(myIP, myGW, mySN, myDNS);
53 |
54 | WT32_ETH01_waitForConnect();
55 | }
56 |
57 | void loop()
58 | {
59 | if (WT32_ETH01_isConnected())
60 | {
61 | HTTPClient http;
62 |
63 | Serial.print("[HTTP] begin...\n");
64 |
65 | http.begin("http://user:password@192.168.2.112/test.html");
66 |
67 | /*
68 | // or
69 | http.begin("http://192.168.2.112/test.html");
70 | http.setAuthorization("user", "password");
71 | // or
72 | http.begin("http://192.168.2.112/test.html");
73 | http.setAuthorization("dXNlcjpwYXN3b3Jk");
74 | */
75 |
76 | Serial.print("[HTTP] GET...\n");
77 | // start connection and send HTTP header
78 | int httpCode = http.GET();
79 |
80 | // httpCode will be negative on error
81 | if (httpCode > 0)
82 | {
83 | // HTTP header has been send and Server response header has been handled
84 | Serial.printf("[HTTP] GET... code: %d\n", httpCode);
85 |
86 | // file found at server
87 | if (httpCode == HTTP_CODE_OK)
88 | {
89 | String payload = http.getString();
90 | Serial.println(payload);
91 | }
92 | }
93 | else
94 | {
95 | Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
96 | }
97 |
98 | http.end();
99 | }
100 |
101 | delay(10000);
102 | }
103 |
--------------------------------------------------------------------------------
/examples/HTTPClient/BasicHttpClient/BasicHttpClient.ino:
--------------------------------------------------------------------------------
1 | /****************************************************************************************************************************
2 | BasicHttpClient.ino - Simple Arduino web server sample for Ethernet shield
3 |
4 | For Ethernet shields using WT32_ETH01 (ESP32 + LAN8720)
5 |
6 | WebServer_WT32_ETH01 is a library for the Ethernet LAN8720 in WT32_ETH01 to run WebServer
7 |
8 | Based on and modified from ESP8266 https://github.com/esp8266/Arduino/releases
9 | Built by Khoi Hoang https://github.com/khoih-prog/WebServer_WT32_ETH01
10 | Licensed under MIT license
11 | *****************************************************************************************************************************/
12 |
13 | #define DEBUG_ETHERNET_WEBSERVER_PORT Serial
14 |
15 | // Debug Level from 0 to 4
16 | #define _ETHERNET_WEBSERVER_LOGLEVEL_ 3
17 |
18 | #include
19 | #include
20 |
21 | // Select the IP address according to your local network
22 | IPAddress myIP(192, 168, 2, 232);
23 | IPAddress myGW(192, 168, 2, 1);
24 | IPAddress mySN(255, 255, 255, 0);
25 |
26 | // Google DNS Server IP
27 | IPAddress myDNS(8, 8, 8, 8);
28 |
29 | void setup()
30 | {
31 | Serial.begin(115200);
32 |
33 | while (!Serial);
34 |
35 | // Using this if Serial debugging is not necessary or not using Serial port
36 | //while (!Serial && (millis() < 3000));
37 |
38 | Serial.print("\nStarting BasicHttpClient on " + String(ARDUINO_BOARD));
39 | Serial.println(" with " + String(SHIELD_TYPE));
40 | Serial.println(WEBSERVER_WT32_ETH01_VERSION);
41 |
42 | // To be called before ETH.begin()
43 | WT32_ETH01_onEvent();
44 |
45 | //bool begin(uint8_t phy_addr=ETH_PHY_ADDR, int power=ETH_PHY_POWER, int mdc=ETH_PHY_MDC, int mdio=ETH_PHY_MDIO,
46 | // eth_phy_type_t type=ETH_PHY_TYPE, eth_clock_mode_t clk_mode=ETH_CLK_MODE);
47 | //ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER, ETH_PHY_MDC, ETH_PHY_MDIO, ETH_PHY_TYPE, ETH_CLK_MODE);
48 | ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER);
49 |
50 | // Static IP, leave without this line to get IP via DHCP
51 | //bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
52 | ETH.config(myIP, myGW, mySN, myDNS);
53 |
54 | WT32_ETH01_waitForConnect();
55 | }
56 |
57 | void loop()
58 | {
59 | if (WT32_ETH01_isConnected())
60 | {
61 | HTTPClient http;
62 |
63 | Serial.print("[HTTP] begin...\n");
64 | // configure traged server and url
65 | //http.begin("https://www.howsmyssl.com/a/check", ca); //HTTPS
66 | http.begin("http://example.com/index.html"); //HTTP
67 |
68 | Serial.print("[HTTP] GET...\n");
69 | // start connection and send HTTP header
70 | int httpCode = http.GET();
71 |
72 | // httpCode will be negative on error
73 | if (httpCode > 0)
74 | {
75 | // HTTP header has been send and Server response header has been handled
76 | Serial.printf("[HTTP] GET... code: %d\n", httpCode);
77 |
78 | // file found at server
79 | if (httpCode == HTTP_CODE_OK)
80 | {
81 | String payload = http.getString();
82 | Serial.println(payload);
83 | }
84 | }
85 | else
86 | {
87 | Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
88 | }
89 |
90 | http.end();
91 | }
92 |
93 | delay(5000);
94 | }
95 |
--------------------------------------------------------------------------------
/examples/HTTPClient/BasicHttpsClient/BasicHttpsClient.ino:
--------------------------------------------------------------------------------
1 | /****************************************************************************************************************************
2 | BasicHttpsClient.ino - Simple Arduino web server sample for Ethernet shield
3 |
4 | For Ethernet shields using WT32_ETH01 (ESP32 + LAN8720)
5 |
6 | WebServer_WT32_ETH01 is a library for the Ethernet LAN8720 in WT32_ETH01 to run WebServer
7 |
8 | Based on and modified from ESP8266 https://github.com/esp8266/Arduino/releases
9 | Built by Khoi Hoang https://github.com/khoih-prog/WebServer_WT32_ETH01
10 | Licensed under MIT license
11 | *****************************************************************************************************************************/
12 |
13 | #define DEBUG_ETHERNET_WEBSERVER_PORT Serial
14 |
15 | // Debug Level from 0 to 4
16 | #define _ETHERNET_WEBSERVER_LOGLEVEL_ 3
17 |
18 | #include
19 | #include
20 |
21 | #include
22 |
23 | // Select the IP address according to your local network
24 | IPAddress myIP(192, 168, 2, 232);
25 | IPAddress myGW(192, 168, 2, 1);
26 | IPAddress mySN(255, 255, 255, 0);
27 |
28 | // Google DNS Server IP
29 | IPAddress myDNS(8, 8, 8, 8);
30 |
31 | // This is GandiStandardSSLCA2.pem, the root Certificate Authority that signed
32 | // the server certifcate for the demo server https://jigsaw.w3.org in this
33 | // example. This certificate is valid until Sep 11 23:59:59 2024 GMT
34 | const char* rootCACertificate =
35 | "-----BEGIN CERTIFICATE-----\n" \
36 | "MIIF6TCCA9GgAwIBAgIQBeTcO5Q4qzuFl8umoZhQ4zANBgkqhkiG9w0BAQwFADCB\n" \
37 | "iDELMAkGA1UEBhMCVVMxEzARBgNVBAgTCk5ldyBKZXJzZXkxFDASBgNVBAcTC0pl\n" \
38 | "cnNleSBDaXR5MR4wHAYDVQQKExVUaGUgVVNFUlRSVVNUIE5ldHdvcmsxLjAsBgNV\n" \
39 | "BAMTJVVTRVJUcnVzdCBSU0EgQ2VydGlmaWNhdGlvbiBBdXRob3JpdHkwHhcNMTQw\n" \
40 | "OTEyMDAwMDAwWhcNMjQwOTExMjM1OTU5WjBfMQswCQYDVQQGEwJGUjEOMAwGA1UE\n" \
41 | "CBMFUGFyaXMxDjAMBgNVBAcTBVBhcmlzMQ4wDAYDVQQKEwVHYW5kaTEgMB4GA1UE\n" \
42 | "AxMXR2FuZGkgU3RhbmRhcmQgU1NMIENBIDIwggEiMA0GCSqGSIb3DQEBAQUAA4IB\n" \
43 | "DwAwggEKAoIBAQCUBC2meZV0/9UAPPWu2JSxKXzAjwsLibmCg5duNyj1ohrP0pIL\n" \
44 | "m6jTh5RzhBCf3DXLwi2SrCG5yzv8QMHBgyHwv/j2nPqcghDA0I5O5Q1MsJFckLSk\n" \
45 | "QFEW2uSEEi0FXKEfFxkkUap66uEHG4aNAXLy59SDIzme4OFMH2sio7QQZrDtgpbX\n" \
46 | "bmq08j+1QvzdirWrui0dOnWbMdw+naxb00ENbLAb9Tr1eeohovj0M1JLJC0epJmx\n" \
47 | "bUi8uBL+cnB89/sCdfSN3tbawKAyGlLfOGsuRTg/PwSWAP2h9KK71RfWJ3wbWFmV\n" \
48 | "XooS/ZyrgT5SKEhRhWvzkbKGPym1bgNi7tYFAgMBAAGjggF1MIIBcTAfBgNVHSME\n" \
49 | "GDAWgBRTeb9aqitKz1SA4dibwJ3ysgNmyzAdBgNVHQ4EFgQUs5Cn2MmvTs1hPJ98\n" \
50 | "rV1/Qf1pMOowDgYDVR0PAQH/BAQDAgGGMBIGA1UdEwEB/wQIMAYBAf8CAQAwHQYD\n" \
51 | "VR0lBBYwFAYIKwYBBQUHAwEGCCsGAQUFBwMCMCIGA1UdIAQbMBkwDQYLKwYBBAGy\n" \
52 | "MQECAhowCAYGZ4EMAQIBMFAGA1UdHwRJMEcwRaBDoEGGP2h0dHA6Ly9jcmwudXNl\n" \
53 | "cnRydXN0LmNvbS9VU0VSVHJ1c3RSU0FDZXJ0aWZpY2F0aW9uQXV0aG9yaXR5LmNy\n" \
54 | "bDB2BggrBgEFBQcBAQRqMGgwPwYIKwYBBQUHMAKGM2h0dHA6Ly9jcnQudXNlcnRy\n" \
55 | "dXN0LmNvbS9VU0VSVHJ1c3RSU0FBZGRUcnVzdENBLmNydDAlBggrBgEFBQcwAYYZ\n" \
56 | "aHR0cDovL29jc3AudXNlcnRydXN0LmNvbTANBgkqhkiG9w0BAQwFAAOCAgEAWGf9\n" \
57 | "crJq13xhlhl+2UNG0SZ9yFP6ZrBrLafTqlb3OojQO3LJUP33WbKqaPWMcwO7lWUX\n" \
58 | "zi8c3ZgTopHJ7qFAbjyY1lzzsiI8Le4bpOHeICQW8owRc5E69vrOJAKHypPstLbI\n" \
59 | "FhfFcvwnQPYT/pOmnVHvPCvYd1ebjGU6NSU2t7WKY28HJ5OxYI2A25bUeo8tqxyI\n" \
60 | "yW5+1mUfr13KFj8oRtygNeX56eXVlogMT8a3d2dIhCe2H7Bo26y/d7CQuKLJHDJd\n" \
61 | "ArolQ4FCR7vY4Y8MDEZf7kYzawMUgtN+zY+vkNaOJH1AQrRqahfGlZfh8jjNp+20\n" \
62 | "J0CT33KpuMZmYzc4ZCIwojvxuch7yPspOqsactIGEk72gtQjbz7Dk+XYtsDe3CMW\n" \
63 | "1hMwt6CaDixVBgBwAc/qOR2A24j3pSC4W/0xJmmPLQphgzpHphNULB7j7UTKvGof\n" \
64 | "KA5R2d4On3XNDgOVyvnFqSot/kGkoUeuDcL5OWYzSlvhhChZbH2UF3bkRYKtcCD9\n" \
65 | "0m9jqNf6oDP6N8v3smWe2lBvP+Sn845dWDKXcCMu5/3EFZucJ48y7RetWIExKREa\n" \
66 | "m9T8bJUox04FB6b9HbwZ4ui3uRGKLXASUoWNjDNKD/yZkuBjcNqllEdjB+dYxzFf\n" \
67 | "BT02Vf6Dsuimrdfp5gJ0iHRc2jTbkNJtUQoj1iM=\n" \
68 | "-----END CERTIFICATE-----\n";
69 |
70 | // Not sure if WiFiClientSecure checks the validity date of the certificate.
71 | // Setting clock just to be sure...
72 | void setClock()
73 | {
74 | configTime(0, 0, "pool.ntp.org");
75 |
76 | Serial.print(F("Waiting for NTP time sync: "));
77 | time_t nowSecs = time(nullptr);
78 |
79 | while (nowSecs < 8 * 3600 * 2)
80 | {
81 | delay(500);
82 | Serial.print(F("."));
83 | yield();
84 | nowSecs = time(nullptr);
85 | }
86 |
87 | Serial.println();
88 | struct tm timeinfo;
89 | gmtime_r(&nowSecs, &timeinfo);
90 | Serial.print(F("Current time: "));
91 | Serial.print(asctime(&timeinfo));
92 | }
93 |
94 | void setup()
95 | {
96 | Serial.begin(115200);
97 |
98 | while (!Serial);
99 |
100 | // Using this if Serial debugging is not necessary or not using Serial port
101 | //while (!Serial && (millis() < 3000));
102 |
103 | Serial.print("\nStarting BasicHttpsClient on " + String(ARDUINO_BOARD));
104 | Serial.println(" with " + String(SHIELD_TYPE));
105 | Serial.println(WEBSERVER_WT32_ETH01_VERSION);
106 |
107 | // To be called before ETH.begin()
108 | WT32_ETH01_onEvent();
109 |
110 | //bool begin(uint8_t phy_addr=ETH_PHY_ADDR, int power=ETH_PHY_POWER, int mdc=ETH_PHY_MDC, int mdio=ETH_PHY_MDIO,
111 | // eth_phy_type_t type=ETH_PHY_TYPE, eth_clock_mode_t clk_mode=ETH_CLK_MODE);
112 | //ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER, ETH_PHY_MDC, ETH_PHY_MDIO, ETH_PHY_TYPE, ETH_CLK_MODE);
113 | ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER);
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 | WT32_ETH01_waitForConnect();
120 |
121 | setClock();
122 | }
123 |
124 | void loop()
125 | {
126 | if (WT32_ETH01_isConnected())
127 | {
128 | WiFiClientSecure *client = new WiFiClientSecure;
129 |
130 | if (client)
131 | {
132 | client -> setCACert(rootCACertificate);
133 |
134 | {
135 | // Add a scoping block for HTTPClient https to make sure it is destroyed before WiFiClientSecure *client is
136 | HTTPClient https;
137 |
138 | Serial.print("[HTTPS] begin...\n");
139 |
140 | if (https.begin(*client, "https://jigsaw.w3.org/HTTP/connection.html"))
141 | {
142 | // HTTPS
143 | Serial.print("[HTTPS] GET...\n");
144 | // start connection and send HTTP header
145 | int httpCode = https.GET();
146 |
147 | // httpCode will be negative on error
148 | if (httpCode > 0)
149 | {
150 | // HTTP header has been send and Server response header has been handled
151 | Serial.printf("[HTTPS] GET... code: %d\n", httpCode);
152 |
153 | // file found at server
154 | if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY)
155 | {
156 | String payload = https.getString();
157 | Serial.println(payload);
158 | }
159 | }
160 | else
161 | {
162 | Serial.printf("[HTTPS] GET... failed, error: %s\n", https.errorToString(httpCode).c_str());
163 | }
164 |
165 | https.end();
166 | }
167 | else
168 | {
169 | Serial.printf("[HTTPS] Unable to connect\n");
170 | }
171 |
172 | // End extra scoping block
173 | }
174 |
175 | delete client;
176 | }
177 | else
178 | {
179 | Serial.println("Unable to create client");
180 | }
181 |
182 | Serial.println();
183 | Serial.println("Waiting 10s before the next round...");
184 | delay(10000);
185 | }
186 | }
187 |
--------------------------------------------------------------------------------
/examples/HTTPClient/StreamHttpClient/StreamHttpClient.ino:
--------------------------------------------------------------------------------
1 | /****************************************************************************************************************************
2 | StreamHttpClient.ino - Simple Arduino web server sample for Ethernet shield
3 |
4 | For Ethernet shields using WT32_ETH01 (ESP32 + LAN8720)
5 |
6 | WebServer_WT32_ETH01 is a library for the Ethernet LAN8720 in WT32_ETH01 to run WebServer
7 |
8 | Based on and modified from ESP8266 https://github.com/esp8266/Arduino/releases
9 | Built by Khoi Hoang https://github.com/khoih-prog/WebServer_WT32_ETH01
10 | Licensed under MIT license
11 | *****************************************************************************************************************************/
12 |
13 | #define DEBUG_ETHERNET_WEBSERVER_PORT Serial
14 |
15 | // Debug Level from 0 to 4
16 | #define _ETHERNET_WEBSERVER_LOGLEVEL_ 3
17 |
18 | #include
19 | #include
20 |
21 | // Select the IP address according to your local network
22 | IPAddress myIP(192, 168, 2, 232);
23 | IPAddress myGW(192, 168, 2, 1);
24 | IPAddress mySN(255, 255, 255, 0);
25 |
26 | // Google DNS Server IP
27 | IPAddress myDNS(8, 8, 8, 8);
28 |
29 | void setup()
30 | {
31 | Serial.begin(115200);
32 |
33 | while (!Serial);
34 |
35 | // Using this if Serial debugging is not necessary or not using Serial port
36 | //while (!Serial && (millis() < 3000));
37 |
38 | Serial.print("\nStarting StreamHttpClient on " + String(ARDUINO_BOARD));
39 | Serial.println(" with " + String(SHIELD_TYPE));
40 | Serial.println(WEBSERVER_WT32_ETH01_VERSION);
41 |
42 | // To be called before ETH.begin()
43 | WT32_ETH01_onEvent();
44 |
45 | //bool begin(uint8_t phy_addr=ETH_PHY_ADDR, int power=ETH_PHY_POWER, int mdc=ETH_PHY_MDC, int mdio=ETH_PHY_MDIO,
46 | // eth_phy_type_t type=ETH_PHY_TYPE, eth_clock_mode_t clk_mode=ETH_CLK_MODE);
47 | //ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER, ETH_PHY_MDC, ETH_PHY_MDIO, ETH_PHY_TYPE, ETH_CLK_MODE);
48 | ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER);
49 |
50 | // Static IP, leave without this line to get IP via DHCP
51 | //bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
52 | ETH.config(myIP, myGW, mySN, myDNS);
53 |
54 | WT32_ETH01_waitForConnect();
55 | }
56 |
57 | void loop()
58 | {
59 | if (WT32_ETH01_isConnected())
60 | {
61 | HTTPClient http;
62 |
63 | Serial.print("[HTTP] begin...\n");
64 |
65 | // configure server and url
66 | http.begin("http://192.168.2.112/test.html");
67 | //http.begin("192.168.2.112", 80, "/test.html");
68 |
69 | Serial.print("[HTTP] GET...\n");
70 | // start connection and send HTTP header
71 | int httpCode = http.GET();
72 |
73 | if (httpCode > 0)
74 | {
75 | // HTTP header has been send and Server response header has been handled
76 | Serial.printf("[HTTP] GET... code: %d\n", httpCode);
77 |
78 | // file found at server
79 | if (httpCode == HTTP_CODE_OK)
80 | {
81 | // get length of document (is -1 when Server sends no Content-Length header)
82 | int len = http.getSize();
83 |
84 | // create buffer for read
85 | uint8_t buff[128] = { 0 };
86 |
87 | // get tcp stream
88 | WiFiClient * stream = http.getStreamPtr();
89 |
90 | // read all data from server
91 | while (http.connected() && (len > 0 || len == -1))
92 | {
93 | // get available data size
94 | size_t size = stream->available();
95 |
96 | if (size)
97 | {
98 | // read up to 128 byte
99 | int c = stream->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size));
100 |
101 | // write it to Serial
102 | Serial.write(buff, c);
103 |
104 | if (len > 0)
105 | {
106 | len -= c;
107 | }
108 | }
109 |
110 | delay(1);
111 | }
112 |
113 | Serial.println();
114 | Serial.print("[HTTP] connection closed or file end.\n");
115 | }
116 | }
117 | else
118 | {
119 | Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
120 | }
121 |
122 | http.end();
123 | }
124 |
125 | delay(10000);
126 | }
127 |
--------------------------------------------------------------------------------
/examples/HTTPClient/node_test_server/getPostPutDelete.js:
--------------------------------------------------------------------------------
1 | /*
2 | Express.js GET/POST example
3 | Shows how handle GET, POST, PUT, DELETE
4 | in Express.js 4.0
5 |
6 | created 14 Feb 2016
7 | by Tom Igoe
8 | */
9 |
10 | var express = require('express'); // include express.js
11 | var app = express(); // a local instance of it
12 | var bodyParser = require('body-parser'); // include body-parser
13 | var WebSocketServer = require('ws').Server // include Web Socket server
14 |
15 | // you need a body parser:
16 | app.use(bodyParser.urlencoded({extended: false})); // for application/x-www-form-urlencoded
17 |
18 | // this runs after the server successfully starts:
19 | function serverStart() {
20 | var port = server.address().port;
21 | console.log('Server listening on port '+ port);
22 | }
23 |
24 | app.get('/chunked', function(request, response) {
25 | response.write('\n');
26 | response.write(' `:;;;,` .:;;:. \n');
27 | response.write(' .;;;;;;;;;;;` :;;;;;;;;;;: TM \n');
28 | response.write(' `;;;;;;;;;;;;;;;` :;;;;;;;;;;;;;;; \n');
29 | response.write(' :;;;;;;;;;;;;;;;;;; `;;;;;;;;;;;;;;;;;; \n');
30 | response.write(' ;;;;;;;;;;;;;;;;;;;;; .;;;;;;;;;;;;;;;;;;;; \n');
31 | response.write(' ;;;;;;;;:` `;;;;;;;;; ,;;;;;;;;.` .;;;;;;;; \n');
32 | response.write(' .;;;;;;, :;;;;;;; .;;;;;;; ;;;;;;; \n');
33 | response.write(' ;;;;;; ;;;;;;; ;;;;;;, ;;;;;;. \n');
34 | response.write(' ,;;;;; ;;;;;;.;;;;;;` ;;;;;; \n');
35 | response.write(' ;;;;;. ;;;;;;;;;;;` ``` ;;;;;`\n');
36 | response.write(' ;;;;; ;;;;;;;;;, ;;; .;;;;;\n');
37 | response.write('`;;;;: `;;;;;;;; ;;; ;;;;;\n');
38 | response.write(',;;;;` `,,,,,,,, ;;;;;;; .,,;;;,,, ;;;;;\n');
39 | response.write(':;;;;` .;;;;;;;; ;;;;;, :;;;;;;;; ;;;;;\n');
40 | response.write(':;;;;` .;;;;;;;; `;;;;;; :;;;;;;;; ;;;;;\n');
41 | response.write('.;;;;. ;;;;;;;. ;;; ;;;;;\n');
42 | response.write(' ;;;;; ;;;;;;;;; ;;; ;;;;;\n');
43 | response.write(' ;;;;; .;;;;;;;;;; ;;; ;;;;;,\n');
44 | response.write(' ;;;;;; `;;;;;;;;;;;; ;;;;; \n');
45 | response.write(' `;;;;;, .;;;;;; ;;;;;;; ;;;;;; \n');
46 | response.write(' ;;;;;;: :;;;;;;. ;;;;;;; ;;;;;; \n');
47 | response.write(' ;;;;;;;` .;;;;;;;, ;;;;;;;; ;;;;;;;: \n');
48 | response.write(' ;;;;;;;;;:,:;;;;;;;;;: ;;;;;;;;;;:,;;;;;;;;;; \n');
49 | response.write(' `;;;;;;;;;;;;;;;;;;;. ;;;;;;;;;;;;;;;;;;;; \n');
50 | response.write(' ;;;;;;;;;;;;;;;;; :;;;;;;;;;;;;;;;;: \n');
51 | response.write(' ,;;;;;;;;;;;;;, ;;;;;;;;;;;;;; \n');
52 | response.write(' .;;;;;;;;;` ,;;;;;;;;: \n');
53 | response.write(' \n');
54 | response.write(' \n');
55 | response.write(' \n');
56 | response.write(' \n');
57 | response.write(' ;;; ;;;;;` ;;;;: .;; ;; ,;;;;;, ;;. `;, ;;;; \n');
58 | response.write(' ;;; ;;:;;; ;;;;;; .;; ;; ,;;;;;: ;;; `;, ;;;:;; \n');
59 | response.write(' ,;:; ;; ;; ;; ;; .;; ;; ,;, ;;;,`;, ;; ;; \n');
60 | response.write(' ;; ;: ;; ;; ;; ;; .;; ;; ,;, ;;;;`;, ;; ;;. \n');
61 | response.write(' ;: ;; ;;;;;: ;; ;; .;; ;; ,;, ;;`;;;, ;; ;;` \n');
62 | response.write(' ,;;;;; ;;`;; ;; ;; .;; ;; ,;, ;; ;;;, ;; ;; \n');
63 | response.write(' ;; ,;, ;; .;; ;;;;;: ;;;;;: ,;;;;;: ;; ;;, ;;;;;; \n');
64 | response.write(' ;; ;; ;; ;;` ;;;;. `;;;: ,;;;;;, ;; ;;, ;;;; \n');
65 | response.write('\n');
66 | response.end();
67 | });
68 |
69 | // this is the POST handler:
70 | app.all('/*', function (request, response) {
71 | console.log('Got a ' + request.method + ' request');
72 | // the parameters of a GET request are passed in
73 | // request.body. Pass that to formatResponse()
74 | // for formatting:
75 | console.log(request.headers);
76 | if (request.method == 'GET') {
77 | console.log(request.query);
78 | } else {
79 | console.log(request.body);
80 | }
81 |
82 | // send the response:
83 | response.send('OK');
84 | response.end();
85 | });
86 |
87 | // start the server:
88 | var server = app.listen(8080, serverStart);
89 |
90 | // create a WebSocket server and attach it to the server
91 | var wss = new WebSocketServer({server: server});
92 |
93 | wss.on('connection', function connection(ws) {
94 | // new connection, add message listener
95 | ws.on('message', function incoming(message) {
96 | // received a message
97 | console.log('received: %s', message);
98 |
99 | // echo it back
100 | ws.send(message);
101 | });
102 | });
103 |
--------------------------------------------------------------------------------
/examples/HTTPClient/node_test_server/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "node_test_server",
3 | "version": "0.0.1",
4 | "author": {
5 | "name": "Tom Igoe"
6 | },
7 | "dependencies": {
8 | "body-parser": ">=1.11.0",
9 | "express": ">=4.0.0",
10 | "multer": "*",
11 | "ws": ">=5.2.3"
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/examples/HelloServer/HelloServer.ino:
--------------------------------------------------------------------------------
1 | /****************************************************************************************************************************
2 | HelloServer.ino - Dead simple web-server for Ethernet shields
3 |
4 | For Ethernet shields using WT32_ETH01 (ESP32 + LAN8720)
5 |
6 | WebServer_WT32_ETH01 is a library for the Ethernet LAN8720 in WT32_ETH01 to run WebServer
7 |
8 | Based on and modified from ESP8266 https://github.com/esp8266/Arduino/releases
9 | Built by Khoi Hoang https://github.com/khoih-prog/WebServer_WT32_ETH01
10 | Licensed under MIT license
11 | *****************************************************************************************************************************/
12 |
13 | #define DEBUG_ETHERNET_WEBSERVER_PORT Serial
14 |
15 | // Debug Level from 0 to 4
16 | #define _ETHERNET_WEBSERVER_LOGLEVEL_ 3
17 |
18 | #include
19 |
20 | WebServer server(80);
21 |
22 | // Select the IP address according to your local network
23 | IPAddress myIP(192, 168, 2, 232);
24 | IPAddress myGW(192, 168, 2, 1);
25 | IPAddress mySN(255, 255, 255, 0);
26 |
27 | // Google DNS Server IP
28 | IPAddress myDNS(8, 8, 8, 8);
29 |
30 | void handleRoot()
31 | {
32 | String html = F("Hello from HelloServer running on ");
33 |
34 | html += String(BOARD_NAME);
35 |
36 | server.send(200, F("text/plain"), html);
37 | }
38 |
39 | void handleNotFound()
40 | {
41 | String message = F("File Not Found\n\n");
42 |
43 | message += F("URI: ");
44 | message += server.uri();
45 | message += F("\nMethod: ");
46 | message += (server.method() == HTTP_GET) ? F("GET") : F("POST");
47 | message += F("\nArguments: ");
48 | message += server.args();
49 | message += F("\n");
50 |
51 | for (uint8_t i = 0; i < server.args(); i++)
52 | {
53 | message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
54 | }
55 |
56 | server.send(404, F("text/plain"), message);
57 | }
58 |
59 | void setup()
60 | {
61 | // Open serial communications and wait for port to open:
62 | Serial.begin(115200);
63 |
64 | while (!Serial);
65 |
66 | // Using this if Serial debugging is not necessary or not using Serial port
67 | //while (!Serial && (millis() < 3000));
68 |
69 | Serial.print("\nStarting HelloServer on " + String(ARDUINO_BOARD));
70 | Serial.println(" with " + String(SHIELD_TYPE));
71 | Serial.println(WEBSERVER_WT32_ETH01_VERSION);
72 |
73 | // To be called before ETH.begin()
74 | WT32_ETH01_onEvent();
75 |
76 | //bool begin(uint8_t phy_addr=ETH_PHY_ADDR, int power=ETH_PHY_POWER, int mdc=ETH_PHY_MDC, int mdio=ETH_PHY_MDIO,
77 | // eth_phy_type_t type=ETH_PHY_TYPE, eth_clock_mode_t clk_mode=ETH_CLK_MODE);
78 | //ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER, ETH_PHY_MDC, ETH_PHY_MDIO, ETH_PHY_TYPE, ETH_CLK_MODE);
79 | ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER);
80 |
81 | // Static IP, leave without this line to get IP via DHCP
82 | //bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
83 | ETH.config(myIP, myGW, mySN, myDNS);
84 |
85 | WT32_ETH01_waitForConnect();
86 |
87 | server.on(F("/"), handleRoot);
88 |
89 | server.on(F("/inline"), []()
90 | {
91 | server.send(200, F("text/plain"), F("This works as well"));
92 | });
93 |
94 | server.onNotFound(handleNotFound);
95 |
96 | server.begin();
97 |
98 | Serial.print(F("HTTP EthernetWebServer is @ IP : "));
99 | Serial.println(ETH.localIP());
100 | }
101 |
102 | void loop()
103 | {
104 | server.handleClient();
105 | }
106 |
--------------------------------------------------------------------------------
/examples/HelloServer2/HelloServer2.ino:
--------------------------------------------------------------------------------
1 | /****************************************************************************************************************************
2 | HelloServer2.h - Dead simple web-server for Ethernet shields
3 |
4 | For Ethernet shields using WT32_ETH01 (ESP32 + LAN8720)
5 |
6 | WebServer_WT32_ETH01 is a library for the Ethernet LAN8720 in WT32_ETH01 to run WebServer
7 |
8 | Based on and modified from ESP8266 https://github.com/esp8266/Arduino/releases
9 | Built by Khoi Hoang https://github.com/khoih-prog/WebServer_WT32_ETH01
10 | Licensed under MIT license
11 | *****************************************************************************************************************************/
12 |
13 | #define DEBUG_ETHERNET_WEBSERVER_PORT Serial
14 |
15 | // Debug Level from 0 to 4
16 | #define _ETHERNET_WEBSERVER_LOGLEVEL_ 3
17 |
18 | #include
19 |
20 | WebServer server(80);
21 |
22 | // Select the IP address according to your local network
23 | IPAddress myIP(192, 168, 2, 232);
24 | IPAddress myGW(192, 168, 2, 1);
25 | IPAddress mySN(255, 255, 255, 0);
26 |
27 | // Google DNS Server IP
28 | IPAddress myDNS(8, 8, 8, 8);
29 |
30 | void handleRoot()
31 | {
32 | String html = F("Hello from HelloServer2 running on ");
33 |
34 | html += String(BOARD_NAME);
35 |
36 | server.send(200, F("text/plain"), html);
37 | }
38 |
39 | void handleNotFound()
40 | {
41 | String message = F("File Not Found\n\n");
42 |
43 | message += F("URI: ");
44 | message += server.uri();
45 | message += F("\nMethod: ");
46 | message += (server.method() == HTTP_GET) ? F("GET") : F("POST");
47 | message += F("\nArguments: ");
48 | message += server.args();
49 | message += F("\n");
50 |
51 | for (uint8_t i = 0; i < server.args(); i++)
52 | {
53 | message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
54 | }
55 |
56 | server.send(404, F("text/plain"), message);
57 | }
58 |
59 | void setup()
60 | {
61 | Serial.begin(115200);
62 |
63 | while (!Serial);
64 |
65 | // Using this if Serial debugging is not necessary or not using Serial port
66 | //while (!Serial && (millis() < 3000));
67 |
68 | Serial.print("\nStarting HelloServer2 on " + String(ARDUINO_BOARD));
69 | Serial.println(" with " + String(SHIELD_TYPE));
70 | Serial.println(WEBSERVER_WT32_ETH01_VERSION);
71 |
72 | // To be called before ETH.begin()
73 | WT32_ETH01_onEvent();
74 |
75 | //bool begin(uint8_t phy_addr=ETH_PHY_ADDR, int power=ETH_PHY_POWER, int mdc=ETH_PHY_MDC, int mdio=ETH_PHY_MDIO,
76 | // eth_phy_type_t type=ETH_PHY_TYPE, eth_clock_mode_t clk_mode=ETH_CLK_MODE);
77 | //ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER, ETH_PHY_MDC, ETH_PHY_MDIO, ETH_PHY_TYPE, ETH_CLK_MODE);
78 | ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER);
79 |
80 | // Static IP, leave without this line to get IP via DHCP
81 | //bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
82 | ETH.config(myIP, myGW, mySN, myDNS);
83 |
84 | WT32_ETH01_waitForConnect();
85 |
86 | server.on(F("/"), handleRoot);
87 |
88 | server.on(F("/inline"), []()
89 | {
90 | server.send(200, F("text/plain"), F("This works as well"));
91 | });
92 |
93 | server.on(F("/gif"), []()
94 | {
95 | static const uint8_t gif[] PROGMEM =
96 | {
97 | 0x47, 0x49, 0x46, 0x38, 0x37, 0x61, 0x10, 0x00, 0x10, 0x00, 0x80, 0x01,
98 | 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0x2c, 0x00, 0x00, 0x00, 0x00,
99 | 0x10, 0x00, 0x10, 0x00, 0x00, 0x02, 0x19, 0x8c, 0x8f, 0xa9, 0xcb, 0x9d,
100 | 0x00, 0x5f, 0x74, 0xb4, 0x56, 0xb0, 0xb0, 0xd2, 0xf2, 0x35, 0x1e, 0x4c,
101 | 0x0c, 0x24, 0x5a, 0xe6, 0x89, 0xa6, 0x4d, 0x01, 0x00, 0x3b
102 | };
103 |
104 | char gif_colored[sizeof(gif)];
105 |
106 | memcpy_P(gif_colored, gif, sizeof(gif));
107 |
108 | // Set the background to a random set of colors
109 | gif_colored[16] = millis() % 256;
110 | gif_colored[17] = millis() % 256;
111 | gif_colored[18] = millis() % 256;
112 |
113 | server.send_P(200, "image/gif", gif_colored, sizeof(gif_colored));
114 | });
115 |
116 | server.onNotFound(handleNotFound);
117 |
118 | server.begin();
119 |
120 | Serial.print(F("HTTP HelloServer2 started @ IP : "));
121 | Serial.println(ETH.localIP());
122 | }
123 |
124 | void loop()
125 | {
126 | server.handleClient();
127 | }
128 |
--------------------------------------------------------------------------------
/examples/HttpBasicAuth/HttpBasicAuth.ino:
--------------------------------------------------------------------------------
1 | /****************************************************************************************************************************
2 | HTTPBasicAuth.h - Dead simple web-server for Ethernet shields
3 |
4 | For Ethernet shields using WT32_ETH01 (ESP32 + LAN8720)
5 |
6 | WebServer_WT32_ETH01 is a library for the Ethernet LAN8720 in WT32_ETH01 to run WebServer
7 |
8 | Based on and modified from ESP8266 https://github.com/esp8266/Arduino/releases
9 | Built by Khoi Hoang https://github.com/khoih-prog/WebServer_WT32_ETH01
10 | Licensed under MIT license
11 | *****************************************************************************************************************************/
12 |
13 | #define DEBUG_ETHERNET_WEBSERVER_PORT Serial
14 |
15 | // Debug Level from 0 to 4
16 | #define _ETHERNET_WEBSERVER_LOGLEVEL_ 3
17 |
18 | #include
19 |
20 | WebServer server(80);
21 |
22 | // Select the IP address according to your local network
23 | IPAddress myIP(192, 168, 2, 232);
24 | IPAddress myGW(192, 168, 2, 1);
25 | IPAddress mySN(255, 255, 255, 0);
26 |
27 | // Google DNS Server IP
28 | IPAddress myDNS(8, 8, 8, 8);
29 |
30 | const char* www_username = "admin";
31 | const char* www_password = "wt32_eth01";
32 |
33 | void setup()
34 | {
35 | Serial.begin(115200);
36 |
37 | while (!Serial);
38 |
39 | // Using this if Serial debugging is not necessary or not using Serial port
40 | //while (!Serial && (millis() < 3000));
41 |
42 | Serial.print("\nStarting HTTPBasicAuth on " + String(ARDUINO_BOARD));
43 | Serial.println(" with " + String(SHIELD_TYPE));
44 | Serial.println(WEBSERVER_WT32_ETH01_VERSION);
45 |
46 | // To be called before ETH.begin()
47 | WT32_ETH01_onEvent();
48 |
49 | //bool begin(uint8_t phy_addr=ETH_PHY_ADDR, int power=ETH_PHY_POWER, int mdc=ETH_PHY_MDC, int mdio=ETH_PHY_MDIO,
50 | // eth_phy_type_t type=ETH_PHY_TYPE, eth_clock_mode_t clk_mode=ETH_CLK_MODE);
51 | //ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER, ETH_PHY_MDC, ETH_PHY_MDIO, ETH_PHY_TYPE, ETH_CLK_MODE);
52 | ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER);
53 |
54 | // Static IP, leave without this line to get IP via DHCP
55 | //bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
56 | ETH.config(myIP, myGW, mySN, myDNS);
57 |
58 | WT32_ETH01_waitForConnect();
59 |
60 | server.on(F("/"), []()
61 | {
62 | if (!server.authenticate(www_username, www_password))
63 | {
64 | return server.requestAuthentication();
65 | }
66 |
67 | server.send(200, F("text/plain"), F("Login OK"));
68 | });
69 |
70 | server.begin();
71 |
72 | Serial.print(F("HTTP HTTPBasicAuth started @ IP : "));
73 | Serial.println(ETH.localIP());
74 |
75 | Serial.print(F("Open http://"));
76 | Serial.print(ETH.localIP());
77 | Serial.println(F("/ in your browser to see it working"));
78 | Serial.print(F("Using username : "));
79 | Serial.print(www_username);
80 | Serial.print(F(" and password : "));
81 | Serial.println(www_password);
82 | }
83 |
84 | void loop()
85 | {
86 | server.handleClient();
87 | }
88 |
--------------------------------------------------------------------------------
/examples/MQTTClient_Auth/MQTTClient_Auth.ino:
--------------------------------------------------------------------------------
1 | /****************************************************************************************************************************
2 | MQTTClient_Auth.ino - Dead simple MQTT Client for Ethernet shields
3 |
4 | MQTTClient_Auth
5 | *****************************************************************************************************************************/
6 |
7 | /*
8 | Basic MQTT example (without SSL!) with Authentication
9 | This sketch demonstrates the basic capabilities of the library.
10 | It connects to an MQTT server then:
11 | - providing username and password
12 | - publishes "hello world" to the topic "outTopic"
13 | - subscribes to the topic "inTopic", printing out any messages
14 | it receives. NB - it assumes the received payloads are strings not binary
15 | It will reconnect to the server if the connection is lost using a blocking
16 | reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to
17 | achieve the same result without blocking the main loop.
18 | */
19 |
20 | #define DEBUG_ETHERNET_WEBSERVER_PORT Serial
21 |
22 | // Debug Level from 0 to 4
23 | #define _ETHERNET_WEBSERVER_LOGLEVEL_ 3
24 |
25 | #include
26 |
27 | WebServer server(80);
28 |
29 | // Select the IP address according to your local network
30 | IPAddress myIP(192, 168, 2, 232);
31 | IPAddress myGW(192, 168, 2, 1);
32 | IPAddress mySN(255, 255, 255, 0);
33 |
34 | // Google DNS Server IP
35 | IPAddress myDNS(8, 8, 8, 8);
36 |
37 | #include
38 |
39 | // Update these with values suitable for your network.
40 | //const char* mqttServer = "broker.example"; // Broker address
41 | const char* mqttServer = "broker.emqx.io"; // Broker address
42 | //const char* mqttServer = "broker.shiftr.io"; // Broker address
43 |
44 | const char *ID = "MQTTClient_SSL-Client"; // Name of our device, must be unique
45 | const char *TOPIC = "MQTT_Pub"; // Topic to subcribe to
46 | const char *subTopic = "MQTT_Sub"; // Topic to subcribe to
47 |
48 | //IPAddress mqttServer(172, 16, 0, 2);
49 |
50 | void callback(char* topic, byte* payload, unsigned int length)
51 | {
52 | Serial.print("Message arrived [");
53 | Serial.print(topic);
54 | Serial.print("] ");
55 |
56 | for (unsigned int i = 0; i < length; i++)
57 | {
58 | Serial.print((char)payload[i]);
59 | }
60 |
61 | Serial.println();
62 | }
63 |
64 | WiFiClient ethClient;
65 | PubSubClient client(mqttServer, 1883, callback, ethClient);
66 |
67 | void reconnect()
68 | {
69 | // Loop until we're reconnected
70 | while (!client.connected())
71 | {
72 | Serial.print("Attempting MQTT connection to ");
73 | Serial.print(mqttServer);
74 |
75 | // Attempt to connect
76 | if (client.connect("arduino", "try", "try"))
77 | {
78 | Serial.println("...connected");
79 |
80 | // Once connected, publish an announcement...
81 | String data = "Hello from MQTTClient_SSL on " + String(BOARD_NAME);
82 |
83 | client.publish(TOPIC, data.c_str());
84 |
85 | //Serial.println("Published connection message successfully!");
86 | //Serial.print("Subcribed to: ");
87 | //Serial.println(subTopic);
88 |
89 | // This is a workaround to address https://github.com/OPEnSLab-OSU/SSLClient/issues/9
90 | //ethClientSSL.flush();
91 | // ... and resubscribe
92 | client.subscribe(subTopic);
93 | // for loopback testing
94 | client.subscribe(TOPIC);
95 | // This is a workaround to address https://github.com/OPEnSLab-OSU/SSLClient/issues/9
96 | //ethClientSSL.flush();
97 | }
98 | else
99 | {
100 | Serial.print("...failed, rc=");
101 | Serial.print(client.state());
102 | Serial.println(" try again in 5 seconds");
103 |
104 | // Wait 5 seconds before retrying
105 | delay(5000);
106 | }
107 | }
108 | }
109 |
110 | void setup()
111 | {
112 | // Open serial communications and wait for port to open:
113 | Serial.begin(115200);
114 |
115 | while (!Serial);
116 |
117 | // Using this if Serial debugging is not necessary or not using Serial port
118 | //while (!Serial && (millis() < 3000));
119 |
120 | Serial.print("\nStarting MQTTClient_Auth on " + String(ARDUINO_BOARD));
121 | Serial.println(" with " + String(SHIELD_TYPE));
122 | Serial.println(WEBSERVER_WT32_ETH01_VERSION);
123 |
124 | // To be called before ETH.begin()
125 | WT32_ETH01_onEvent();
126 |
127 | //bool begin(uint8_t phy_addr=ETH_PHY_ADDR, int power=ETH_PHY_POWER, int mdc=ETH_PHY_MDC, int mdio=ETH_PHY_MDIO,
128 | // eth_phy_type_t type=ETH_PHY_TYPE, eth_clock_mode_t clk_mode=ETH_CLK_MODE);
129 | //ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER, ETH_PHY_MDC, ETH_PHY_MDIO, ETH_PHY_TYPE, ETH_CLK_MODE);
130 | ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER);
131 |
132 | // Static IP, leave without this line to get IP via DHCP
133 | //bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
134 | ETH.config(myIP, myGW, mySN, myDNS);
135 |
136 | WT32_ETH01_waitForConnect();
137 |
138 | // Note - the default maximum packet size is 128 bytes. If the
139 | // combined length of clientId, username and password exceed this use the
140 | // following to increase the buffer size:
141 | // client.setBufferSize(255);
142 | }
143 |
144 | #define MQTT_PUBLISH_INTERVAL_MS 5000L
145 |
146 | String data = "Hello from MQTTClient_Auth on " + String(BOARD_NAME) + " with " + String(SHIELD_TYPE);
147 | const char *pubData = data.c_str();
148 |
149 | unsigned long lastMsg = 0;
150 |
151 | void loop()
152 | {
153 | static unsigned long now;
154 |
155 | if (!client.connected())
156 | {
157 | reconnect();
158 | }
159 |
160 | // Sending Data
161 | now = millis();
162 |
163 | if (now - lastMsg > MQTT_PUBLISH_INTERVAL_MS)
164 | {
165 | lastMsg = now;
166 |
167 | if (!client.publish(TOPIC, pubData))
168 | {
169 | Serial.println("Message failed to send.");
170 | }
171 |
172 | Serial.print("Message Send : " + String(TOPIC) + " => ");
173 | Serial.println(data);
174 | }
175 |
176 | client.loop();
177 | }
178 |
--------------------------------------------------------------------------------
/examples/MQTTClient_Basic/MQTTClient_Basic.ino:
--------------------------------------------------------------------------------
1 | /****************************************************************************************************************************
2 | MQTTClient_Basic.ino - Dead simple MQTT Client for Ethernet shields
3 |
4 | MQTTClient_Auth
5 | *****************************************************************************************************************************/
6 |
7 | /*
8 | Basic MQTT example (without SSL!) with Authentication
9 | This sketch demonstrates the basic capabilities of the library.
10 | It connects to an MQTT server then:
11 | - providing username and password
12 | - publishes "hello world" to the topic "outTopic"
13 | - subscribes to the topic "inTopic", printing out any messages
14 | it receives. NB - it assumes the received payloads are strings not binary
15 |
16 | It will reconnect to the server if the connection is lost using a blocking
17 | reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to
18 | achieve the same result without blocking the main loop.
19 | */
20 |
21 | #define DEBUG_ETHERNET_WEBSERVER_PORT Serial
22 |
23 | // Debug Level from 0 to 4
24 | #define _ETHERNET_WEBSERVER_LOGLEVEL_ 3
25 |
26 | #include
27 |
28 | WebServer server(80);
29 |
30 | // Select the IP address according to your local network
31 | IPAddress myIP(192, 168, 2, 232);
32 | IPAddress myGW(192, 168, 2, 1);
33 | IPAddress mySN(255, 255, 255, 0);
34 |
35 | // Google DNS Server IP
36 | IPAddress myDNS(8, 8, 8, 8);
37 |
38 | #include
39 |
40 | // Update these with values suitable for your network.
41 | //const char* mqttServer = "broker.example"; // Broker address
42 | const char* mqttServer = "broker.emqx.io"; // Broker address
43 | //const char* mqttServer = "broker.shiftr.io"; // Broker address
44 |
45 | const char *ID = "MQTTClient_SSL-Client"; // Name of our device, must be unique
46 | const char *TOPIC = "MQTT_Pub"; // Topic to subcribe to
47 | const char *subTopic = "MQTT_Sub"; // Topic to subcribe to
48 |
49 | //IPAddress mqttServer(172, 16, 0, 2);
50 |
51 | void callback(char* topic, byte* payload, unsigned int length)
52 | {
53 | Serial.print("Message arrived [");
54 | Serial.print(topic);
55 | Serial.print("] ");
56 |
57 | for (unsigned int i = 0; i < length; i++)
58 | {
59 | Serial.print((char)payload[i]);
60 | }
61 |
62 | Serial.println();
63 | }
64 |
65 | WiFiClient ethClient;
66 | PubSubClient client(mqttServer, 1883, callback, ethClient);
67 |
68 | void reconnect()
69 | {
70 | // Loop until we're reconnected
71 | while (!client.connected())
72 | {
73 | Serial.print("Attempting MQTT connection to ");
74 | Serial.print(mqttServer);
75 |
76 | // Attempt to connect
77 | if (client.connect(ID, "try", "try"))
78 | {
79 | Serial.println("...connected");
80 |
81 | // Once connected, publish an announcement...
82 | String data = "Hello from MQTTClient_SSL on " + String(BOARD_NAME);
83 |
84 | client.publish(TOPIC, data.c_str());
85 |
86 | //Serial.println("Published connection message successfully!");
87 | //Serial.print("Subcribed to: ");
88 | //Serial.println(subTopic);
89 |
90 | // This is a workaround to address https://github.com/OPEnSLab-OSU/SSLClient/issues/9
91 | //ethClientSSL.flush();
92 | // ... and resubscribe
93 | client.subscribe(subTopic);
94 | // for loopback testing
95 | client.subscribe(TOPIC);
96 | // This is a workaround to address https://github.com/OPEnSLab-OSU/SSLClient/issues/9
97 | //ethClientSSL.flush();
98 | }
99 | else
100 | {
101 | Serial.print("...failed, rc=");
102 | Serial.print(client.state());
103 | Serial.println(" try again in 5 seconds");
104 |
105 | // Wait 5 seconds before retrying
106 | delay(5000);
107 | }
108 | }
109 | }
110 |
111 | void setup()
112 | {
113 | // Open serial communications and wait for port to open:
114 | Serial.begin(115200);
115 |
116 | while (!Serial);
117 |
118 | // Using this if Serial debugging is not necessary or not using Serial port
119 | //while (!Serial && (millis() < 3000));
120 |
121 | Serial.print("\nStarting MQTTClient_Basic on " + String(ARDUINO_BOARD));
122 | Serial.println(" with " + String(SHIELD_TYPE));
123 | Serial.println(WEBSERVER_WT32_ETH01_VERSION);
124 |
125 | // To be called before ETH.begin()
126 | WT32_ETH01_onEvent();
127 |
128 | //bool begin(uint8_t phy_addr=ETH_PHY_ADDR, int power=ETH_PHY_POWER, int mdc=ETH_PHY_MDC, int mdio=ETH_PHY_MDIO,
129 | // eth_phy_type_t type=ETH_PHY_TYPE, eth_clock_mode_t clk_mode=ETH_CLK_MODE);
130 | //ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER, ETH_PHY_MDC, ETH_PHY_MDIO, ETH_PHY_TYPE, ETH_CLK_MODE);
131 | ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER);
132 |
133 | // Static IP, leave without this line to get IP via DHCP
134 | //bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
135 | ETH.config(myIP, myGW, mySN, myDNS);
136 |
137 | WT32_ETH01_waitForConnect();
138 |
139 | client.setServer(mqttServer, 1883);
140 | client.setCallback(callback);
141 |
142 | // Allow the hardware to sort itself out
143 | delay(1500);
144 | }
145 |
146 | #define MQTT_PUBLISH_INTERVAL_MS 5000L
147 |
148 | String data = "Hello from MQTTClient_Basic on " + String(BOARD_NAME) + " with " + String(SHIELD_TYPE);
149 | const char *pubData = data.c_str();
150 |
151 | unsigned long lastMsg = 0;
152 |
153 | void loop()
154 | {
155 | static unsigned long now;
156 |
157 | if (!client.connected())
158 | {
159 | reconnect();
160 | }
161 |
162 | // Sending Data
163 | now = millis();
164 |
165 | if (now - lastMsg > MQTT_PUBLISH_INTERVAL_MS)
166 | {
167 | lastMsg = now;
168 |
169 | if (!client.publish(TOPIC, pubData))
170 | {
171 | Serial.println("Message failed to send.");
172 | }
173 |
174 | Serial.print("Message Send : " + String(TOPIC) + " => ");
175 | Serial.println(data);
176 | }
177 |
178 | client.loop();
179 | }
180 |
--------------------------------------------------------------------------------
/examples/MQTT_And_OTA_Ethernet/MQTT_And_OTA_Ethernet.ino:
--------------------------------------------------------------------------------
1 | /****************************************************************************************************************************
2 | MQTT_And_OTA_Ethernet.ino
3 | - Dead simple MQTT Client for Ethernet shields
4 | - Allows for new sketch compiled bin to be uploaded over HTTP using AsyncElegantOTA
5 |
6 | Based on MQTTClient_Auth
7 | *****************************************************************************************************************************/
8 |
9 | /*
10 | Basic MQTT example (without SSL!) with Authentication
11 | This sketch demonstrates the basic capabilities of the library.
12 | It connects to an MQTT server then:
13 | - providing username and password
14 | - publishes "hello world" to the topic "outTopic"
15 | - subscribes to the topic "inTopic", printing out any messages
16 | it receives. NB - it assumes the received payloads are strings not binary
17 | It will reconnect to the server if the connection is lost using a blocking
18 | reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to
19 | achieve the same result without blocking the main loop.
20 |
21 | To use OTA:
22 | - Take note of what IP the board is on, you should be able to see this both in the serial monitor and in the MQTT messages when the board first starts.
23 | - In the Arduino IDE, go to Sketch > Export compiled binary
24 | - You should now see a build folder next to the MQTT_And_OTA_Ethernet.ino file
25 | - Go into build\esp32.esp32.esp32 folder
26 | - The file you will be uploading to do an OTA update is MQTT_And_OTA_Ethernet.ino.bin in that folder
27 | - Go to http://192.168.X.X/update with a browser (whatever your board IP is that you noted earlier.)
28 | - Click "Choose file" and browse to the MQTT_And_OTA_Ethernet.ino.bin that you made earlier when you clicked Export compiled binary
29 | */
30 |
31 | #define DEBUG_ETHERNET_WEBSERVER_PORT Serial
32 | #define _ETHERNET_WEBSERVER_LOGLEVEL_ 3 // Debug Level from 0 to 4
33 |
34 | #include // https://github.com/khoih-prog/WebServer_WT32_ETH01/
35 | #include // https://github.com/me-no-dev/AsyncTCP
36 | #include // https://github.com/me-no-dev/ESPAsyncWebServer
37 | #include // https://github.com/ayushsharma82/AsyncElegantOTA
38 |
39 | // Libraries also needed: https://github.com/knolleary/pubsubclient
40 |
41 | AsyncWebServer server(80);
42 |
43 | // Set according to your local network if you need static IP
44 | // IPAddress myIP(192, 168, 1, 232);
45 | // IPAddress myGW(192, 168, 1, 1);
46 | // IPAddress mySN(255, 255, 255, 0);
47 | // IPAddress myDNS(8, 8, 8, 8);
48 |
49 | #include
50 |
51 | // MQTT Settings
52 | const char *mqttServer = "192.168.1.25"; // Broker address
53 | const char *mqttBrokerUser = "YOUR_MQTT_USERNAME"; // Username to connect to your MQTT broker
54 | const char *mqttBrokerPass = "YOUR_MQTT_PASSWORD"; // Password to connect to your MQTT broker
55 | const char *ID = "MQTTClient_SSL-Client"; // Name of our device, must be unique
56 | const char *TOPIC = "topics/test/esp32"; // Topic to publish to
57 | const char *subTopic = "topics/MQTT_Sub"; // Topic to subcribe to
58 |
59 | WiFiClient ethClient;
60 |
61 | void setup()
62 | {
63 | Serial.begin(115200);
64 |
65 | while (!Serial);
66 |
67 | // Using this if Serial debugging is not necessary or not using Serial port
68 | //while (!Serial && (millis() < 3000));
69 |
70 | Serial.print("\nStarting MQTT_And_OTA_Ethernet on " + String(ARDUINO_BOARD));
71 | Serial.println(" with " + String(SHIELD_TYPE));
72 | Serial.println(WEBSERVER_WT32_ETH01_VERSION);
73 |
74 | // To be called before ETH.begin()
75 | WT32_ETH01_onEvent();
76 |
77 | ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER);
78 |
79 | // Static IP, leave without this line to get IP via DHCP
80 | // ETH.config(myIP, myGW, mySN, myDNS);
81 |
82 | WT32_ETH01_waitForConnect();
83 |
84 | // Note - the default maximum packet size is 128 bytes. If the
85 | // combined length of clientId, username and password exceed this use the
86 | // following to increase the buffer size:
87 | // client.setBufferSize(255);
88 |
89 | server.on("/", HTTP_GET, [](AsyncWebServerRequest * request)
90 | {
91 | request->send(200, "text/plain", "Hi! I am ESP32.");
92 | });
93 |
94 | AsyncElegantOTA.begin(&server); // Start ElegantOTA
95 | server.begin();
96 |
97 | Serial.println();
98 | Serial.println("HTTP server started with MAC: " + ETH.macAddress() + ", at IPv4: " + ETH.localIP().toString());
99 | Serial.println();
100 | }
101 |
102 | void callback(char* topic, byte* payload, unsigned int length)
103 | {
104 | Serial.print("Message arrived [");
105 | Serial.print(topic);
106 | Serial.print("] ");
107 |
108 | for (unsigned int i = 0; i < length; i++)
109 | {
110 | Serial.print((char)payload[i]);
111 | }
112 |
113 | Serial.println();
114 | }
115 |
116 | PubSubClient client(mqttServer, 1883, callback, ethClient);
117 |
118 | void reconnect()
119 | {
120 | // Loop until we're reconnected
121 | while (!client.connected())
122 | {
123 | Serial.print("Attempting MQTT connection to ");
124 | Serial.print(mqttServer);
125 |
126 | // Attempt to connect
127 | if (client.connect("arduino", mqttBrokerUser, mqttBrokerPass))
128 | {
129 | Serial.println("...connected");
130 |
131 | // Once connected, publish an announcement...
132 | String data = "Hello from MQTTClient_SSL on " + String(BOARD_NAME) + ", at IPv4: " + ETH.localIP().toString();
133 |
134 | client.publish(TOPIC, data.c_str());
135 |
136 | //Serial.println("Published connection message successfully!");
137 | //Serial.print("Subcribed to: ");
138 | //Serial.println(subTopic);
139 |
140 | // ... and resubscribe
141 | client.subscribe(subTopic);
142 | // for loopback testing
143 | client.subscribe(TOPIC);
144 | }
145 | else
146 | {
147 | Serial.print("...failed, rc=");
148 | Serial.print(client.state());
149 | Serial.println(" try again in 5 seconds");
150 |
151 | // Wait 5 seconds before retrying
152 | delay(5000);
153 | }
154 | }
155 | }
156 |
157 | #define MQTT_PUBLISH_INTERVAL_MS 5000L
158 |
159 | unsigned long lastMsg = 0;
160 |
161 | void loop()
162 | {
163 | String data = "Hello from v00 MQTT_And_OTA_Ethernet on: " + String(BOARD_NAME) + " with " + String(SHIELD_TYPE);
164 | const char *pubData = data.c_str();
165 |
166 | static unsigned long now;
167 |
168 | if (!client.connected())
169 | {
170 | reconnect();
171 | }
172 |
173 | // Sending Data
174 | now = millis();
175 |
176 | if (now - lastMsg > MQTT_PUBLISH_INTERVAL_MS)
177 | {
178 | lastMsg = now;
179 |
180 | if (!client.publish(TOPIC, pubData))
181 | {
182 | Serial.println("Message failed to send.");
183 | }
184 |
185 | Serial.print("Message Send : " + String(TOPIC) + " => ");
186 | Serial.println(data);
187 | }
188 |
189 | client.loop();
190 | }
191 |
--------------------------------------------------------------------------------
/examples/MQTT_ThingStream/MQTT_ThingStream.ino:
--------------------------------------------------------------------------------
1 | /****************************************************************************************************************************
2 | MQTT_ThingStream.ino - Dead simple MQTT Client for Ethernet shields
3 |
4 | For Ethernet shields using WT32_ETH01 (ESP32 + LAN8720)
5 |
6 | WebServer_WT32_ETH01 is a library for the Ethernet LAN8720 in WT32_ETH01 to run WebServer
7 |
8 | Based on and modified from ESP8266 https://github.com/esp8266/Arduino/releases
9 | Built by Khoi Hoang https://github.com/khoih-prog/WebServer_WT32_ETH01
10 | Licensed under MIT license
11 | *****************************************************************************************************************************/
12 | /*
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 | #define DEBUG_ETHERNET_WEBSERVER_PORT Serial
30 |
31 | // Debug Level from 0 to 4
32 | #define _ETHERNET_WEBSERVER_LOGLEVEL_ 3
33 |
34 | #include
35 |
36 | WebServer server(80);
37 |
38 | // Select the IP address according to your local network
39 | IPAddress myIP(192, 168, 2, 232);
40 | IPAddress myGW(192, 168, 2, 1);
41 | IPAddress mySN(255, 255, 255, 0);
42 |
43 | // Google DNS Server IP
44 | IPAddress myDNS(8, 8, 8, 8);
45 |
46 | #include
47 |
48 | const char my_cert[] = "FIXME";
49 | const char my_key[] = "FIXME";
50 |
51 | #define USING_THINGSTREAM_IO true
52 |
53 | #if USING_THINGSTREAM_IO
54 |
55 | const char *MQTT_PREFIX_TOPIC = "esp32-sniffer/";
56 | const char *MQTT_ANNOUNCE_TOPIC = "/status";
57 | const char *MQTT_CONTROL_TOPIC = "/control";
58 | const char *MQTT_BLE_TOPIC = "/ble";
59 |
60 |
61 | // GOT FROM ThingsStream!
62 | const char *MQTT_SERVER = "mqtt.thingstream.io";
63 | const char *MQTT_USER = "MQTT_USER";
64 | const char *MQTT_PASS = "MQTT_PASS";
65 | const char *MQTT_CLIENT_ID = "MQTT_CLIENT_ID";
66 |
67 | String topic = MQTT_PREFIX_TOPIC + String("12345678") + MQTT_BLE_TOPIC;
68 | String subTopic = MQTT_PREFIX_TOPIC + String("12345678") + MQTT_BLE_TOPIC;
69 |
70 | #else
71 |
72 | const char* MQTT_SERVER = "broker.emqx.io"; // Broker address
73 |
74 | const char* ID = "MQTTClient_SSL-Client"; // Name of our device, must be unique
75 | String topic = "STM32_Pub"; // Topic to subcribe to
76 | String subTopic = "STM32_Sub"; // Topic to subcribe to
77 |
78 | #endif
79 |
80 | void mqtt_receive_callback(char* topic, byte* payload, unsigned int length);
81 |
82 | const int MQTT_PORT = 1883; //if you use SSL //1883 no SSL
83 |
84 | unsigned long lastMsg = 0;
85 |
86 | // Initialize the SSL client library
87 | // Arguments: EthernetClient, our trust anchors
88 |
89 |
90 | WiFiClient ethClient;
91 |
92 | PubSubClient client(MQTT_SERVER, MQTT_PORT, mqtt_receive_callback, ethClient);
93 |
94 | /*
95 | Called whenever a payload is received from a subscribed MQTT topic
96 | */
97 | void mqtt_receive_callback(char* topic, byte* payload, unsigned int length)
98 | {
99 | Serial.print("MQTT Message receive [");
100 | Serial.print(topic);
101 | Serial.print("] ");
102 |
103 | for (unsigned int i = 0; i < length; i++)
104 | {
105 | Serial.print((char)payload[i]);
106 | }
107 |
108 | Serial.println();
109 | }
110 |
111 | void reconnect()
112 | {
113 | // Loop until we're reconnected
114 | while (!client.connected())
115 | {
116 | Serial.print("Attempting MQTT connection to ");
117 | Serial.println(MQTT_SERVER);
118 |
119 | // Attempt to connect
120 |
121 | #if USING_THINGSTREAM_IO
122 | int connect_status = client.connect(MQTT_CLIENT_ID, MQTT_USER, MQTT_PASS, topic.c_str(), 2, false, "");
123 | #else
124 | int connect_status = client.connect(ID);
125 | #endif
126 |
127 | if (connect_status)
128 | {
129 | Serial.println("...connected");
130 |
131 | // Once connected, publish an announcement...
132 | String data = "Hello from MQTTClient_SSL on " + String(BOARD_NAME);
133 |
134 | client.publish(topic.c_str(), data.c_str());
135 |
136 | Serial.println("Published connection message successfully!");
137 |
138 | Serial.print("Subcribed to: ");
139 | Serial.println(subTopic);
140 |
141 | // This is a workaround to address https://github.com/OPEnSLab-OSU/SSLClient/issues/9
142 | //ethClientSSL.flush();
143 | // ... and resubscribe
144 | client.subscribe(subTopic.c_str());
145 | // for loopback testing
146 | client.subscribe(topic.c_str());
147 | // This is a workaround to address https://github.com/OPEnSLab-OSU/SSLClient/issues/9
148 | //ethClientSSL.flush();
149 | }
150 | else
151 | {
152 | Serial.print("failed, rc=");
153 | Serial.print(client.state());
154 | Serial.println(" try again in 5 seconds");
155 |
156 | // Wait 5 seconds before retrying
157 | delay(5000);
158 | }
159 | }
160 | }
161 |
162 | void setup()
163 | {
164 | // Open serial communications and wait for port to open:
165 | Serial.begin(115200);
166 |
167 | while (!Serial);
168 |
169 | // Using this if Serial debugging is not necessary or not using Serial port
170 | //while (!Serial && (millis() < 3000));
171 |
172 | Serial.print("\nStarting MQTT_ThingStream on " + String(ARDUINO_BOARD));
173 | Serial.println(" with " + String(SHIELD_TYPE));
174 | Serial.println(WEBSERVER_WT32_ETH01_VERSION);
175 |
176 | // To be called before ETH.begin()
177 | WT32_ETH01_onEvent();
178 |
179 | //bool begin(uint8_t phy_addr=ETH_PHY_ADDR, int power=ETH_PHY_POWER, int mdc=ETH_PHY_MDC, int mdio=ETH_PHY_MDIO,
180 | // eth_phy_type_t type=ETH_PHY_TYPE, eth_clock_mode_t clk_mode=ETH_CLK_MODE);
181 | //ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER, ETH_PHY_MDC, ETH_PHY_MDIO, ETH_PHY_TYPE, ETH_CLK_MODE);
182 | ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER);
183 |
184 | // Static IP, leave without this line to get IP via DHCP
185 | //bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
186 | ETH.config(myIP, myGW, mySN, myDNS);
187 |
188 | WT32_ETH01_waitForConnect();
189 |
190 | // Note - the default maximum packet size is 256 bytes. If the
191 | // combined length of clientId, username and password exceed this use the
192 | // following to increase the buffer size:
193 | //client.setBufferSize(256);
194 |
195 | Serial.println("***************************************");
196 | Serial.println(topic);
197 | Serial.println("***************************************");
198 | }
199 |
200 | #define MQTT_PUBLISH_INTERVAL_MS 5000L
201 |
202 | String data = "Hello from MQTT_ThingStream on " + String(BOARD_NAME) + " with " + String(SHIELD_TYPE);
203 | const char *pubData = data.c_str();
204 |
205 | void loop()
206 | {
207 | static unsigned long now;
208 |
209 | if (!client.connected())
210 | {
211 | reconnect();
212 | }
213 |
214 | // Sending Data
215 | now = millis();
216 |
217 | if (now - lastMsg > MQTT_PUBLISH_INTERVAL_MS)
218 | {
219 | lastMsg = now;
220 |
221 | if (!client.publish(topic.c_str(), pubData))
222 | {
223 | Serial.println("Message failed to send.");
224 | }
225 |
226 | Serial.print("MQTT Message Send : " + topic + " => ");
227 | Serial.println(data);
228 | }
229 |
230 | client.loop();
231 | }
232 |
--------------------------------------------------------------------------------
/examples/PostServer/PostServer.ino:
--------------------------------------------------------------------------------
1 | /****************************************************************************************************************************
2 | PostServer.h - Dead simple web-server for Ethernet shields
3 |
4 | For Ethernet shields using WT32_ETH01 (ESP32 + LAN8720)
5 |
6 | WebServer_WT32_ETH01 is a library for the Ethernet LAN8720 in WT32_ETH01 to run WebServer
7 |
8 | Based on and modified from ESP8266 https://github.com/esp8266/Arduino/releases
9 | Built by Khoi Hoang https://github.com/khoih-prog/WebServer_WT32_ETH01
10 | Licensed under MIT license
11 | *****************************************************************************************************************************/
12 |
13 | #define DEBUG_ETHERNET_WEBSERVER_PORT Serial
14 |
15 | // Debug Level from 0 to 4
16 | #define _ETHERNET_WEBSERVER_LOGLEVEL_ 3
17 |
18 | #include
19 |
20 | WebServer server(80);
21 |
22 | // Select the IP address according to your local network
23 | IPAddress myIP(192, 168, 2, 232);
24 | IPAddress myGW(192, 168, 2, 1);
25 | IPAddress mySN(255, 255, 255, 0);
26 |
27 | // Google DNS Server IP
28 | IPAddress myDNS(8, 8, 8, 8);
29 |
30 | const String postForms =
31 | F("\
32 | \
33 | WebServer_WT32_ETH01 POST handling\
34 | \
37 | \
38 | \
39 |
POST plain text to /postplain/
\
40 | \
44 |
POST form data to /postform/
\
45 | \
49 | \
50 | ");
51 |
52 | void handleRoot()
53 | {
54 | server.send(200, F("text/html"), postForms);
55 | }
56 |
57 | void handlePlain()
58 | {
59 | if (server.method() != HTTP_POST)
60 | {
61 | server.send(405, F("text/plain"), F("Method Not Allowed"));
62 | }
63 | else
64 | {
65 | server.send(200, F("text/plain"), "POST body was:\n" + server.arg("plain"));
66 | }
67 | }
68 |
69 | void handleForm()
70 | {
71 | if (server.method() != HTTP_POST)
72 | {
73 | server.send(405, F("text/plain"), F("Method Not Allowed"));
74 | }
75 | else
76 | {
77 | String message = F("POST form was:\n");
78 |
79 | for (uint8_t i = 0; i < server.args(); i++)
80 | {
81 | message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
82 | }
83 |
84 | server.send(200, F("text/plain"), message);
85 | }
86 | }
87 |
88 | void handleNotFound()
89 | {
90 | String message = F("File Not Found\n\n");
91 |
92 | message += F("URI: ");
93 | message += server.uri();
94 | message += F("\nMethod: ");
95 | message += (server.method() == HTTP_GET) ? F("GET") : F("POST");
96 | message += F("\nArguments: ");
97 | message += server.args();
98 | message += F("\n");
99 |
100 | for (uint8_t i = 0; i < server.args(); i++)
101 | {
102 | message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
103 | }
104 |
105 | server.send(404, F("text/plain"), message);
106 | }
107 |
108 | void setup()
109 | {
110 | Serial.begin(115200);
111 |
112 | while (!Serial);
113 |
114 | // Using this if Serial debugging is not necessary or not using Serial port
115 | //while (!Serial && (millis() < 3000));
116 |
117 | Serial.print("\nStarting POSTServer on " + String(ARDUINO_BOARD));
118 | Serial.println(" with " + String(SHIELD_TYPE));
119 | Serial.println(WEBSERVER_WT32_ETH01_VERSION);
120 |
121 | // To be called before ETH.begin()
122 | WT32_ETH01_onEvent();
123 |
124 | //bool begin(uint8_t phy_addr=ETH_PHY_ADDR, int power=ETH_PHY_POWER, int mdc=ETH_PHY_MDC, int mdio=ETH_PHY_MDIO,
125 | // eth_phy_type_t type=ETH_PHY_TYPE, eth_clock_mode_t clk_mode=ETH_CLK_MODE);
126 | //ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER, ETH_PHY_MDC, ETH_PHY_MDIO, ETH_PHY_TYPE, ETH_CLK_MODE);
127 | ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER);
128 |
129 | // Static IP, leave without this line to get IP via DHCP
130 | //bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
131 | ETH.config(myIP, myGW, mySN, myDNS);
132 |
133 | WT32_ETH01_waitForConnect();
134 |
135 | server.on(F("/"), handleRoot);
136 |
137 | server.on(F("/postplain/"), handlePlain);
138 |
139 | server.on(F("/postform/"), handleForm);
140 |
141 | server.onNotFound(handleNotFound);
142 |
143 | server.begin();
144 |
145 | Serial.print(F("HTTP POSTServer started @ IP : "));
146 | Serial.println(ETH.localIP());
147 | }
148 |
149 | void loop()
150 | {
151 | server.handleClient();
152 | }
153 |
--------------------------------------------------------------------------------
/examples/SSL/MQTTClient_SSL/certificates.h:
--------------------------------------------------------------------------------
1 | /****************************************************************************************************************************
2 | certificates.h
3 |
4 | For Ethernet shields using WT32_ETH01 (ESP32 + LAN8720)
5 |
6 | WebServer_WT32_ETH01 is a library for the Ethernet LAN8720 in WT32_ETH01 to run WebServer
7 |
8 | Based on and modified from ESP8266 https://github.com/esp8266/Arduino/releases
9 | Built by Khoi Hoang https://github.com/khoih-prog/WebServer_WT32_ETH01
10 | Licensed under MIT license
11 | *****************************************************************************************************************************/
12 |
13 | #ifndef _CERTIFICATES_H_
14 | #define _CERTIFICATES_H_
15 |
16 | // This is the root Certificate Authority that signed the server certifcate
17 | // for the demo server broker.emqx.io in this example.
18 | // To generate the Root CA Cert, use the following command (in Linux / Ubuntu)
19 | // $ openssl s_client -showcerts -connect broker.emqx.io:8883
20 |
21 | const char* rootCACertificate =
22 | "-----BEGIN CERTIFICATE-----\n" \
23 | "MIIGEzCCA/ugAwIBAgIQfVtRJrR2uhHbdBYLvFMNpzANBgkqhkiG9w0BAQwFADCB\n" \
24 | "iDELMAkGA1UEBhMCVVMxEzARBgNVBAgTCk5ldyBKZXJzZXkxFDASBgNVBAcTC0pl\n" \
25 | "cnNleSBDaXR5MR4wHAYDVQQKExVUaGUgVVNFUlRSVVNUIE5ldHdvcmsxLjAsBgNV\n" \
26 | "BAMTJVVTRVJUcnVzdCBSU0EgQ2VydGlmaWNhdGlvbiBBdXRob3JpdHkwHhcNMTgx\n" \
27 | "MTAyMDAwMDAwWhcNMzAxMjMxMjM1OTU5WjCBjzELMAkGA1UEBhMCR0IxGzAZBgNV\n" \
28 | "BAgTEkdyZWF0ZXIgTWFuY2hlc3RlcjEQMA4GA1UEBxMHU2FsZm9yZDEYMBYGA1UE\n" \
29 | "ChMPU2VjdGlnbyBMaW1pdGVkMTcwNQYDVQQDEy5TZWN0aWdvIFJTQSBEb21haW4g\n" \
30 | "VmFsaWRhdGlvbiBTZWN1cmUgU2VydmVyIENBMIIBIjANBgkqhkiG9w0BAQEFAAOC\n" \
31 | "AQ8AMIIBCgKCAQEA1nMz1tc8INAA0hdFuNY+B6I/x0HuMjDJsGz99J/LEpgPLT+N\n" \
32 | "TQEMgg8Xf2Iu6bhIefsWg06t1zIlk7cHv7lQP6lMw0Aq6Tn/2YHKHxYyQdqAJrkj\n" \
33 | "eocgHuP/IJo8lURvh3UGkEC0MpMWCRAIIz7S3YcPb11RFGoKacVPAXJpz9OTTG0E\n" \
34 | "oKMbgn6xmrntxZ7FN3ifmgg0+1YuWMQJDgZkW7w33PGfKGioVrCSo1yfu4iYCBsk\n" \
35 | "Haswha6vsC6eep3BwEIc4gLw6uBK0u+QDrTBQBbwb4VCSmT3pDCg/r8uoydajotY\n" \
36 | "uK3DGReEY+1vVv2Dy2A0xHS+5p3b4eTlygxfFQIDAQABo4IBbjCCAWowHwYDVR0j\n" \
37 | "BBgwFoAUU3m/WqorSs9UgOHYm8Cd8rIDZsswHQYDVR0OBBYEFI2MXsRUrYrhd+mb\n" \
38 | "+ZsF4bgBjWHhMA4GA1UdDwEB/wQEAwIBhjASBgNVHRMBAf8ECDAGAQH/AgEAMB0G\n" \
39 | "A1UdJQQWMBQGCCsGAQUFBwMBBggrBgEFBQcDAjAbBgNVHSAEFDASMAYGBFUdIAAw\n" \
40 | "CAYGZ4EMAQIBMFAGA1UdHwRJMEcwRaBDoEGGP2h0dHA6Ly9jcmwudXNlcnRydXN0\n" \
41 | "LmNvbS9VU0VSVHJ1c3RSU0FDZXJ0aWZpY2F0aW9uQXV0aG9yaXR5LmNybDB2Bggr\n" \
42 | "BgEFBQcBAQRqMGgwPwYIKwYBBQUHMAKGM2h0dHA6Ly9jcnQudXNlcnRydXN0LmNv\n" \
43 | "bS9VU0VSVHJ1c3RSU0FBZGRUcnVzdENBLmNydDAlBggrBgEFBQcwAYYZaHR0cDov\n" \
44 | "L29jc3AudXNlcnRydXN0LmNvbTANBgkqhkiG9w0BAQwFAAOCAgEAMr9hvQ5Iw0/H\n" \
45 | "ukdN+Jx4GQHcEx2Ab/zDcLRSmjEzmldS+zGea6TvVKqJjUAXaPgREHzSyrHxVYbH\n" \
46 | "7rM2kYb2OVG/Rr8PoLq0935JxCo2F57kaDl6r5ROVm+yezu/Coa9zcV3HAO4OLGi\n" \
47 | "H19+24rcRki2aArPsrW04jTkZ6k4Zgle0rj8nSg6F0AnwnJOKf0hPHzPE/uWLMUx\n" \
48 | "RP0T7dWbqWlod3zu4f+k+TY4CFM5ooQ0nBnzvg6s1SQ36yOoeNDT5++SR2RiOSLv\n" \
49 | "xvcRviKFxmZEJCaOEDKNyJOuB56DPi/Z+fVGjmO+wea03KbNIaiGCpXZLoUmGv38\n" \
50 | "sbZXQm2V0TP2ORQGgkE49Y9Y3IBbpNV9lXj9p5v//cWoaasm56ekBYdbqbe4oyAL\n" \
51 | "l6lFhd2zi+WJN44pDfwGF/Y4QA5C5BIG+3vzxhFoYt/jmPQT2BVPi7Fp2RBgvGQq\n" \
52 | "6jG35LWjOhSbJuMLe/0CjraZwTiXWTb2qHSihrZe68Zk6s+go/lunrotEbaGmAhY\n" \
53 | "LcmsJWTyXnW0OMGuf1pGg+pRyrbxmRE1a6Vqe8YAsOf4vmSyrcjC8azjUeqkk+B5\n" \
54 | "yOGBQMkKW+ESPMFgKuOXwIlCypTPRpgSabuY0MLTDXJLR27lk8QyKGOHQ+SwMj4K\n" \
55 | "00u/I5sUKUErmgQfky3xxzlIPK1aEn8=\n" \
56 | "-----END CERTIFICATE-----\n";
57 |
58 | #endif /* ifndef _CERTIFICATES_H_ */
59 |
--------------------------------------------------------------------------------
/examples/SSL/MQTTClient_SSL_Auth/MQTTClient_SSL_Auth.ino:
--------------------------------------------------------------------------------
1 | /****************************************************************************************************************************
2 | MQTTClient_SSL_Auth.ino - Dead simple SSL MQTT Client for Ethernet shields
3 |
4 | For Ethernet shields using WT32_ETH01 (ESP32 + LAN8720)
5 |
6 | WebServer_WT32_ETH01 is a library for the Ethernet LAN8720 in WT32_ETH01 to run WebServer
7 |
8 | Based on and modified from ESP8266 https://github.com/esp8266/Arduino/releases
9 | Built by Khoi Hoang https://github.com/khoih-prog/WebServer_WT32_ETH01
10 | Licensed under MIT license
11 | *****************************************************************************************************************************/
12 |
13 | /*
14 | Basic MQTT example (with 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 ("testuser" and "testpass")
18 | - publishes "Hello from MQTTClient_SSL_Auth on WT32-ETH01" to the topic "MQTT_Pub"
19 | - subscribes to the topic "MQTT_Sub", 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 | #define DEBUG_ETHERNET_WEBSERVER_PORT Serial
27 |
28 | // Debug Level from 0 to 4
29 | #define _ETHERNET_WEBSERVER_LOGLEVEL_ 3
30 |
31 | #include
32 |
33 | #include
34 |
35 | // Select the IP address according to your local network
36 | IPAddress myIP(192, 168, 2, 232);
37 | IPAddress myGW(192, 168, 2, 1);
38 | IPAddress mySN(255, 255, 255, 0);
39 |
40 | // Google DNS Server IP
41 | IPAddress myDNS(8, 8, 8, 8);
42 |
43 | #include "certificates.h"
44 | #include
45 |
46 | // Update these with values suitable for your network.
47 |
48 | const char* mqttServer = "broker.emqx.io"; // Broker address
49 |
50 | const char *TOPIC = "MQTT_Pub"; // Topic to subcribe to
51 | const char *subTopic = "MQTT_Sub"; // Topic to subcribe to
52 |
53 | void callback(char* topic, byte* payload, unsigned int length)
54 | {
55 | Serial.print("Message arrived [");
56 | Serial.print(topic);
57 | Serial.print("] ");
58 |
59 | for (unsigned int i = 0; i < length; i++)
60 | {
61 | Serial.print((char)payload[i]);
62 | }
63 |
64 | Serial.println();
65 | }
66 |
67 | WiFiClientSecure ethClientSSL;
68 |
69 | PubSubClient client(mqttServer, 8883, callback, ethClientSSL);
70 |
71 | void reconnect()
72 | {
73 | // Loop until we're reconnected
74 | while (!client.connected())
75 | {
76 | Serial.print("Attempting MQTTS connection to ");
77 | Serial.print(mqttServer);
78 |
79 | // Attempt to connect
80 | if (client.connect("arduinoClient", "testuser", "testpass"))
81 | {
82 | Serial.println("...connected");
83 |
84 | // Once connected, publish an announcement...
85 | client.publish(TOPIC, "Hello World from WT32_ETH01");
86 | // ... and resubscribe
87 | client.subscribe(subTopic);
88 | // for loopback testing
89 | client.subscribe(TOPIC);
90 | }
91 | else
92 | {
93 | Serial.print(" => failed, rc=");
94 | Serial.print(client.state());
95 | Serial.println(" try again in 5 seconds");
96 |
97 | // Wait 5 seconds before retrying
98 | delay(5000);
99 | }
100 | }
101 | }
102 |
103 | // Not sure if WiFiClientSecure checks the validity date of the certificate.
104 | // Setting clock just to be sure...
105 | void setClock()
106 | {
107 | configTime(0, 0, "pool.ntp.org");
108 |
109 | Serial.print(F("Waiting for NTP time sync: "));
110 | time_t nowSecs = time(nullptr);
111 |
112 | while (nowSecs < 8 * 3600 * 2)
113 | {
114 | delay(500);
115 | Serial.print(F("."));
116 | yield();
117 | nowSecs = time(nullptr);
118 | }
119 |
120 | Serial.println();
121 | struct tm timeinfo;
122 | gmtime_r(&nowSecs, &timeinfo);
123 | Serial.print(F("Current time: "));
124 | Serial.print(asctime(&timeinfo));
125 | }
126 |
127 | void setup()
128 | {
129 | // Open serial communications and wait for port to open:
130 | Serial.begin(115200);
131 |
132 | while (!Serial);
133 |
134 | // Using this if Serial debugging is not necessary or not using Serial port
135 | //while (!Serial && (millis() < 3000));
136 |
137 | Serial.print("\nStarting MQTTClient_SSL_Auth on " + String(ARDUINO_BOARD));
138 | Serial.println(" with " + String(SHIELD_TYPE));
139 | Serial.println(WEBSERVER_WT32_ETH01_VERSION);
140 |
141 | // To be called before ETH.begin()
142 | WT32_ETH01_onEvent();
143 |
144 | //bool begin(uint8_t phy_addr=ETH_PHY_ADDR, int power=ETH_PHY_POWER, int mdc=ETH_PHY_MDC, int mdio=ETH_PHY_MDIO,
145 | // eth_phy_type_t type=ETH_PHY_TYPE, eth_clock_mode_t clk_mode=ETH_CLK_MODE);
146 | //ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER, ETH_PHY_MDC, ETH_PHY_MDIO, ETH_PHY_TYPE, ETH_CLK_MODE);
147 | ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER);
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 | WT32_ETH01_waitForConnect();
154 |
155 | setClock();
156 |
157 | ethClientSSL.setCACert(rootCACertificate);
158 |
159 | // Note - the default maximum packet size is 128 bytes. If the
160 | // combined length of clientId, username and password exceed this use the
161 | // following to increase the buffer size:
162 | client.setBufferSize(255);
163 | }
164 |
165 | #define MQTT_PUBLISH_INTERVAL_MS 10000L
166 |
167 | String data = "Hello from MQTTClient_SSL_Auth on " + String(BOARD_NAME);
168 | const char *pubData = data.c_str();
169 |
170 | unsigned long lastMsg = 0;
171 |
172 | void loop()
173 | {
174 | static unsigned long now;
175 |
176 | if (!client.connected())
177 | {
178 | reconnect();
179 | }
180 |
181 | // Sending Data
182 | now = millis();
183 |
184 | if (now - lastMsg > MQTT_PUBLISH_INTERVAL_MS)
185 | {
186 | lastMsg = now;
187 |
188 | if (!client.publish(TOPIC, pubData))
189 | {
190 | Serial.println("Message failed to send.");
191 | }
192 |
193 | Serial.print("Message Send : " + String(TOPIC) + " => ");
194 | Serial.println(data);
195 | }
196 |
197 | client.loop();
198 | }
199 |
--------------------------------------------------------------------------------
/examples/SSL/MQTTClient_SSL_Auth/certificates.h:
--------------------------------------------------------------------------------
1 | /****************************************************************************************************************************
2 | certificates.h
3 |
4 | For Ethernet shields using WT32_ETH01 (ESP32 + LAN8720)
5 |
6 | WebServer_WT32_ETH01 is a library for the Ethernet LAN8720 in WT32_ETH01 to run WebServer
7 |
8 | Based on and modified from ESP8266 https://github.com/esp8266/Arduino/releases
9 | Built by Khoi Hoang https://github.com/khoih-prog/WebServer_WT32_ETH01
10 | Licensed under MIT license
11 | *****************************************************************************************************************************/
12 |
13 | #ifndef _CERTIFICATES_H_
14 | #define _CERTIFICATES_H_
15 |
16 | // This is the root Certificate Authority that signed the server certifcate
17 | // for the demo server broker.emqx.io in this example.
18 | // To generate the Root CA Cert, use the following command (in Linux / Ubuntu)
19 | // $ openssl s_client -showcerts -connect broker.emqx.io:8883
20 |
21 | const char* rootCACertificate =
22 | "-----BEGIN CERTIFICATE-----\n" \
23 | "MIIGEzCCA/ugAwIBAgIQfVtRJrR2uhHbdBYLvFMNpzANBgkqhkiG9w0BAQwFADCB\n" \
24 | "iDELMAkGA1UEBhMCVVMxEzARBgNVBAgTCk5ldyBKZXJzZXkxFDASBgNVBAcTC0pl\n" \
25 | "cnNleSBDaXR5MR4wHAYDVQQKExVUaGUgVVNFUlRSVVNUIE5ldHdvcmsxLjAsBgNV\n" \
26 | "BAMTJVVTRVJUcnVzdCBSU0EgQ2VydGlmaWNhdGlvbiBBdXRob3JpdHkwHhcNMTgx\n" \
27 | "MTAyMDAwMDAwWhcNMzAxMjMxMjM1OTU5WjCBjzELMAkGA1UEBhMCR0IxGzAZBgNV\n" \
28 | "BAgTEkdyZWF0ZXIgTWFuY2hlc3RlcjEQMA4GA1UEBxMHU2FsZm9yZDEYMBYGA1UE\n" \
29 | "ChMPU2VjdGlnbyBMaW1pdGVkMTcwNQYDVQQDEy5TZWN0aWdvIFJTQSBEb21haW4g\n" \
30 | "VmFsaWRhdGlvbiBTZWN1cmUgU2VydmVyIENBMIIBIjANBgkqhkiG9w0BAQEFAAOC\n" \
31 | "AQ8AMIIBCgKCAQEA1nMz1tc8INAA0hdFuNY+B6I/x0HuMjDJsGz99J/LEpgPLT+N\n" \
32 | "TQEMgg8Xf2Iu6bhIefsWg06t1zIlk7cHv7lQP6lMw0Aq6Tn/2YHKHxYyQdqAJrkj\n" \
33 | "eocgHuP/IJo8lURvh3UGkEC0MpMWCRAIIz7S3YcPb11RFGoKacVPAXJpz9OTTG0E\n" \
34 | "oKMbgn6xmrntxZ7FN3ifmgg0+1YuWMQJDgZkW7w33PGfKGioVrCSo1yfu4iYCBsk\n" \
35 | "Haswha6vsC6eep3BwEIc4gLw6uBK0u+QDrTBQBbwb4VCSmT3pDCg/r8uoydajotY\n" \
36 | "uK3DGReEY+1vVv2Dy2A0xHS+5p3b4eTlygxfFQIDAQABo4IBbjCCAWowHwYDVR0j\n" \
37 | "BBgwFoAUU3m/WqorSs9UgOHYm8Cd8rIDZsswHQYDVR0OBBYEFI2MXsRUrYrhd+mb\n" \
38 | "+ZsF4bgBjWHhMA4GA1UdDwEB/wQEAwIBhjASBgNVHRMBAf8ECDAGAQH/AgEAMB0G\n" \
39 | "A1UdJQQWMBQGCCsGAQUFBwMBBggrBgEFBQcDAjAbBgNVHSAEFDASMAYGBFUdIAAw\n" \
40 | "CAYGZ4EMAQIBMFAGA1UdHwRJMEcwRaBDoEGGP2h0dHA6Ly9jcmwudXNlcnRydXN0\n" \
41 | "LmNvbS9VU0VSVHJ1c3RSU0FDZXJ0aWZpY2F0aW9uQXV0aG9yaXR5LmNybDB2Bggr\n" \
42 | "BgEFBQcBAQRqMGgwPwYIKwYBBQUHMAKGM2h0dHA6Ly9jcnQudXNlcnRydXN0LmNv\n" \
43 | "bS9VU0VSVHJ1c3RSU0FBZGRUcnVzdENBLmNydDAlBggrBgEFBQcwAYYZaHR0cDov\n" \
44 | "L29jc3AudXNlcnRydXN0LmNvbTANBgkqhkiG9w0BAQwFAAOCAgEAMr9hvQ5Iw0/H\n" \
45 | "ukdN+Jx4GQHcEx2Ab/zDcLRSmjEzmldS+zGea6TvVKqJjUAXaPgREHzSyrHxVYbH\n" \
46 | "7rM2kYb2OVG/Rr8PoLq0935JxCo2F57kaDl6r5ROVm+yezu/Coa9zcV3HAO4OLGi\n" \
47 | "H19+24rcRki2aArPsrW04jTkZ6k4Zgle0rj8nSg6F0AnwnJOKf0hPHzPE/uWLMUx\n" \
48 | "RP0T7dWbqWlod3zu4f+k+TY4CFM5ooQ0nBnzvg6s1SQ36yOoeNDT5++SR2RiOSLv\n" \
49 | "xvcRviKFxmZEJCaOEDKNyJOuB56DPi/Z+fVGjmO+wea03KbNIaiGCpXZLoUmGv38\n" \
50 | "sbZXQm2V0TP2ORQGgkE49Y9Y3IBbpNV9lXj9p5v//cWoaasm56ekBYdbqbe4oyAL\n" \
51 | "l6lFhd2zi+WJN44pDfwGF/Y4QA5C5BIG+3vzxhFoYt/jmPQT2BVPi7Fp2RBgvGQq\n" \
52 | "6jG35LWjOhSbJuMLe/0CjraZwTiXWTb2qHSihrZe68Zk6s+go/lunrotEbaGmAhY\n" \
53 | "LcmsJWTyXnW0OMGuf1pGg+pRyrbxmRE1a6Vqe8YAsOf4vmSyrcjC8azjUeqkk+B5\n" \
54 | "yOGBQMkKW+ESPMFgKuOXwIlCypTPRpgSabuY0MLTDXJLR27lk8QyKGOHQ+SwMj4K\n" \
55 | "00u/I5sUKUErmgQfky3xxzlIPK1aEn8=\n" \
56 | "-----END CERTIFICATE-----\n";
57 |
58 | #endif /* ifndef _CERTIFICATES_H_ */
59 |
--------------------------------------------------------------------------------
/examples/SSL/MQTTClient_SSL_Complex/MQTTClient_SSL_Complex.ino:
--------------------------------------------------------------------------------
1 | /****************************************************************************************************************************
2 | MQTTClient_SSL_Complex.ino - Dead simple SSL MQTT Client for Ethernet shields
3 |
4 | For Ethernet shields using WT32_ETH01 (ESP32 + LAN8720)
5 |
6 | WebServer_WT32_ETH01 is a library for the Ethernet LAN8720 in WT32_ETH01 to run WebServer
7 |
8 | Based on and modified from ESP8266 https://github.com/esp8266/Arduino/releases
9 | Built by Khoi Hoang https://github.com/khoih-prog/WebServer_WT32_ETH01
10 | Licensed under MIT license
11 | *****************************************************************************************************************************/
12 |
13 | /*
14 | Basic MQTT example (with SSL!)
15 | This sketch demonstrates the basic capabilities of the library.
16 | It connects to an MQTT server then:
17 | - publishes {Hello from MQTTClient_SSL_Complex on WT32-ETH01, millis = xxxxx} to the topic [MQTT_Pub]
18 | - subscribes to the topic [MQTT_Sub], printing out any messages
19 | it receives. NB - it assumes the received payloads are strings not binary
20 | It will reconnect to the server if the connection is lost using a blocking
21 | reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to
22 | achieve the same result without blocking the main loop.
23 | */
24 |
25 | #define DEBUG_ETHERNET_WEBSERVER_PORT Serial
26 |
27 | // Debug Level from 0 to 4
28 | #define _ETHERNET_WEBSERVER_LOGLEVEL_ 3
29 |
30 | #include
31 |
32 | #include
33 |
34 | // Select the IP address according to your local network
35 | IPAddress myIP(192, 168, 2, 232);
36 | IPAddress myGW(192, 168, 2, 1);
37 | IPAddress mySN(255, 255, 255, 0);
38 |
39 | // Google DNS Server IP
40 | IPAddress myDNS(8, 8, 8, 8);
41 |
42 | #include "certificates.h" // This file must be regenerated at https://openslab-osu.github.io/bearssl-certificate-utility/
43 | #include
44 |
45 | const char* mqttServer = "broker.emqx.io"; // Broker address
46 |
47 | const char *ID = "MQTTClient_SSL-Client"; // Name of our device, must be unique
48 | const char *TOPIC = "MQTT_Pub"; // Topic to subcribe to
49 | const char *subTopic = "MQTT_Sub"; // Topic to subcribe to
50 |
51 | unsigned long lastMsg = 0;
52 |
53 | // Initialize the SSL client library
54 | // Arguments: EthernetClient, our trust anchors
55 |
56 | void callback(char* topic, byte* payload, unsigned int length)
57 | {
58 | Serial.print("Message arrived [");
59 | Serial.print(topic);
60 | Serial.print("] ");
61 |
62 | for (unsigned int i = 0; i < length; i++)
63 | {
64 | Serial.print((char)payload[i]);
65 | }
66 |
67 | Serial.println();
68 | }
69 |
70 | WiFiClientSecure ethClientSSL;
71 |
72 | PubSubClient client(mqttServer, 8883, callback, ethClientSSL);
73 |
74 | void reconnect()
75 | {
76 | // Loop until we're reconnected
77 | while (!client.connected())
78 | {
79 | Serial.print("Attempting MQTT connection to ");
80 | Serial.print(mqttServer);
81 |
82 | // Attempt to connect
83 | if (client.connect(ID))
84 | {
85 | Serial.println("...connected");
86 |
87 | // Once connected, publish an announcement...
88 | String data = "Hello from MQTTClient_SSL_Complex on " + String(BOARD_NAME);
89 |
90 | client.publish(TOPIC, data.c_str());
91 |
92 | //Serial.println("Published connection message successfully!");
93 | //Serial.print("Subcribed to: ");
94 | //Serial.println(subTopic);
95 |
96 | // ... and resubscribe
97 | client.subscribe(subTopic);
98 | // for loopback testing
99 | client.subscribe(TOPIC);
100 | }
101 | else
102 | {
103 | Serial.print("failed, rc=");
104 | Serial.print(client.state());
105 | Serial.println(" try again in 5 seconds");
106 |
107 | // Wait 5 seconds before retrying
108 | delay(5000);
109 | }
110 | }
111 | }
112 |
113 | // Not sure if WiFiClientSecure checks the validity date of the certificate.
114 | // Setting clock just to be sure...
115 | void setClock()
116 | {
117 | configTime(0, 0, "pool.ntp.org");
118 |
119 | Serial.print(F("Waiting for NTP time sync: "));
120 | time_t nowSecs = time(nullptr);
121 |
122 | while (nowSecs < 8 * 3600 * 2)
123 | {
124 | delay(500);
125 | Serial.print(F("."));
126 | yield();
127 | nowSecs = time(nullptr);
128 | }
129 |
130 | Serial.println();
131 | struct tm timeinfo;
132 | gmtime_r(&nowSecs, &timeinfo);
133 | Serial.print(F("Current time: "));
134 | Serial.print(asctime(&timeinfo));
135 | }
136 |
137 | void setup()
138 | {
139 | // Open serial communications and wait for port to open:
140 | Serial.begin(115200);
141 |
142 | while (!Serial);
143 |
144 | // Using this if Serial debugging is not necessary or not using Serial port
145 | //while (!Serial && (millis() < 3000));
146 |
147 | Serial.print("\nStarting MQTTClient_SSL_Complex on " + String(ARDUINO_BOARD));
148 | Serial.println(" with " + String(SHIELD_TYPE));
149 | Serial.println(WEBSERVER_WT32_ETH01_VERSION);
150 |
151 | // To be called before ETH.begin()
152 | WT32_ETH01_onEvent();
153 |
154 | //bool begin(uint8_t phy_addr=ETH_PHY_ADDR, int power=ETH_PHY_POWER, int mdc=ETH_PHY_MDC, int mdio=ETH_PHY_MDIO,
155 | // eth_phy_type_t type=ETH_PHY_TYPE, eth_clock_mode_t clk_mode=ETH_CLK_MODE);
156 | //ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER, ETH_PHY_MDC, ETH_PHY_MDIO, ETH_PHY_TYPE, ETH_CLK_MODE);
157 | ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER);
158 |
159 | // Static IP, leave without this line to get IP via DHCP
160 | //bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
161 | ETH.config(myIP, myGW, mySN, myDNS);
162 |
163 | WT32_ETH01_waitForConnect();
164 |
165 | setClock();
166 |
167 | ethClientSSL.setCACert(rootCACertificate);
168 |
169 | // Note - the default maximum packet size is 128 bytes. If the
170 | // combined length of clientId, username and password exceed this use the
171 | // following to increase the buffer size:
172 | client.setBufferSize(255);
173 | }
174 |
175 | #define MQTT_PUBLISH_INTERVAL_MS 5000L
176 |
177 | void loop()
178 | {
179 | static unsigned long now;
180 |
181 | if (!client.connected())
182 | {
183 | reconnect();
184 | }
185 |
186 | // Sending Data
187 | now = millis();
188 |
189 | if (now - lastMsg > MQTT_PUBLISH_INTERVAL_MS)
190 | {
191 | lastMsg = now;
192 |
193 | String data = "Hello from MQTTClient_SSL_Complex on " + String(BOARD_NAME) + ", millis = " + String(millis());
194 |
195 | if (!client.publish(TOPIC, data.c_str()))
196 | {
197 | Serial.println("Message failed to send.");
198 | }
199 |
200 | Serial.print("Message Send : " + String(TOPIC) + " => ");
201 | Serial.println(data);
202 | }
203 |
204 | client.loop();
205 | }
206 |
--------------------------------------------------------------------------------
/examples/SSL/MQTTClient_SSL_Complex/certificates.h:
--------------------------------------------------------------------------------
1 | /****************************************************************************************************************************
2 | certificates.h
3 |
4 | For Ethernet shields using WT32_ETH01 (ESP32 + LAN8720)
5 |
6 | WebServer_WT32_ETH01 is a library for the Ethernet LAN8720 in WT32_ETH01 to run WebServer
7 |
8 | Based on and modified from ESP8266 https://github.com/esp8266/Arduino/releases
9 | Built by Khoi Hoang https://github.com/khoih-prog/WebServer_WT32_ETH01
10 | Licensed under MIT license
11 | *****************************************************************************************************************************/
12 |
13 | #ifndef _CERTIFICATES_H_
14 | #define _CERTIFICATES_H_
15 |
16 | // This is the root Certificate Authority that signed the server certifcate
17 | // for the demo server broker.emqx.io in this example.
18 | // To generate the Root CA Cert, use the following command (in Linux / Ubuntu)
19 | // $ openssl s_client -showcerts -connect broker.emqx.io:8883
20 |
21 | const char* rootCACertificate =
22 | "-----BEGIN CERTIFICATE-----\n" \
23 | "MIIGEzCCA/ugAwIBAgIQfVtRJrR2uhHbdBYLvFMNpzANBgkqhkiG9w0BAQwFADCB\n" \
24 | "iDELMAkGA1UEBhMCVVMxEzARBgNVBAgTCk5ldyBKZXJzZXkxFDASBgNVBAcTC0pl\n" \
25 | "cnNleSBDaXR5MR4wHAYDVQQKExVUaGUgVVNFUlRSVVNUIE5ldHdvcmsxLjAsBgNV\n" \
26 | "BAMTJVVTRVJUcnVzdCBSU0EgQ2VydGlmaWNhdGlvbiBBdXRob3JpdHkwHhcNMTgx\n" \
27 | "MTAyMDAwMDAwWhcNMzAxMjMxMjM1OTU5WjCBjzELMAkGA1UEBhMCR0IxGzAZBgNV\n" \
28 | "BAgTEkdyZWF0ZXIgTWFuY2hlc3RlcjEQMA4GA1UEBxMHU2FsZm9yZDEYMBYGA1UE\n" \
29 | "ChMPU2VjdGlnbyBMaW1pdGVkMTcwNQYDVQQDEy5TZWN0aWdvIFJTQSBEb21haW4g\n" \
30 | "VmFsaWRhdGlvbiBTZWN1cmUgU2VydmVyIENBMIIBIjANBgkqhkiG9w0BAQEFAAOC\n" \
31 | "AQ8AMIIBCgKCAQEA1nMz1tc8INAA0hdFuNY+B6I/x0HuMjDJsGz99J/LEpgPLT+N\n" \
32 | "TQEMgg8Xf2Iu6bhIefsWg06t1zIlk7cHv7lQP6lMw0Aq6Tn/2YHKHxYyQdqAJrkj\n" \
33 | "eocgHuP/IJo8lURvh3UGkEC0MpMWCRAIIz7S3YcPb11RFGoKacVPAXJpz9OTTG0E\n" \
34 | "oKMbgn6xmrntxZ7FN3ifmgg0+1YuWMQJDgZkW7w33PGfKGioVrCSo1yfu4iYCBsk\n" \
35 | "Haswha6vsC6eep3BwEIc4gLw6uBK0u+QDrTBQBbwb4VCSmT3pDCg/r8uoydajotY\n" \
36 | "uK3DGReEY+1vVv2Dy2A0xHS+5p3b4eTlygxfFQIDAQABo4IBbjCCAWowHwYDVR0j\n" \
37 | "BBgwFoAUU3m/WqorSs9UgOHYm8Cd8rIDZsswHQYDVR0OBBYEFI2MXsRUrYrhd+mb\n" \
38 | "+ZsF4bgBjWHhMA4GA1UdDwEB/wQEAwIBhjASBgNVHRMBAf8ECDAGAQH/AgEAMB0G\n" \
39 | "A1UdJQQWMBQGCCsGAQUFBwMBBggrBgEFBQcDAjAbBgNVHSAEFDASMAYGBFUdIAAw\n" \
40 | "CAYGZ4EMAQIBMFAGA1UdHwRJMEcwRaBDoEGGP2h0dHA6Ly9jcmwudXNlcnRydXN0\n" \
41 | "LmNvbS9VU0VSVHJ1c3RSU0FDZXJ0aWZpY2F0aW9uQXV0aG9yaXR5LmNybDB2Bggr\n" \
42 | "BgEFBQcBAQRqMGgwPwYIKwYBBQUHMAKGM2h0dHA6Ly9jcnQudXNlcnRydXN0LmNv\n" \
43 | "bS9VU0VSVHJ1c3RSU0FBZGRUcnVzdENBLmNydDAlBggrBgEFBQcwAYYZaHR0cDov\n" \
44 | "L29jc3AudXNlcnRydXN0LmNvbTANBgkqhkiG9w0BAQwFAAOCAgEAMr9hvQ5Iw0/H\n" \
45 | "ukdN+Jx4GQHcEx2Ab/zDcLRSmjEzmldS+zGea6TvVKqJjUAXaPgREHzSyrHxVYbH\n" \
46 | "7rM2kYb2OVG/Rr8PoLq0935JxCo2F57kaDl6r5ROVm+yezu/Coa9zcV3HAO4OLGi\n" \
47 | "H19+24rcRki2aArPsrW04jTkZ6k4Zgle0rj8nSg6F0AnwnJOKf0hPHzPE/uWLMUx\n" \
48 | "RP0T7dWbqWlod3zu4f+k+TY4CFM5ooQ0nBnzvg6s1SQ36yOoeNDT5++SR2RiOSLv\n" \
49 | "xvcRviKFxmZEJCaOEDKNyJOuB56DPi/Z+fVGjmO+wea03KbNIaiGCpXZLoUmGv38\n" \
50 | "sbZXQm2V0TP2ORQGgkE49Y9Y3IBbpNV9lXj9p5v//cWoaasm56ekBYdbqbe4oyAL\n" \
51 | "l6lFhd2zi+WJN44pDfwGF/Y4QA5C5BIG+3vzxhFoYt/jmPQT2BVPi7Fp2RBgvGQq\n" \
52 | "6jG35LWjOhSbJuMLe/0CjraZwTiXWTb2qHSihrZe68Zk6s+go/lunrotEbaGmAhY\n" \
53 | "LcmsJWTyXnW0OMGuf1pGg+pRyrbxmRE1a6Vqe8YAsOf4vmSyrcjC8azjUeqkk+B5\n" \
54 | "yOGBQMkKW+ESPMFgKuOXwIlCypTPRpgSabuY0MLTDXJLR27lk8QyKGOHQ+SwMj4K\n" \
55 | "00u/I5sUKUErmgQfky3xxzlIPK1aEn8=\n" \
56 | "-----END CERTIFICATE-----\n";
57 |
58 | #endif /* ifndef _CERTIFICATES_H_ */
59 |
--------------------------------------------------------------------------------
/examples/SSL/MQTTS_ThingStream/certificates.h:
--------------------------------------------------------------------------------
1 | /****************************************************************************************************************************
2 | certificates.h
3 |
4 | For Ethernet shields using WT32_ETH01 (ESP32 + LAN8720)
5 |
6 | WebServer_WT32_ETH01 is a library for the Ethernet LAN8720 in WT32_ETH01 to run WebServer
7 |
8 | Based on and modified from ESP8266 https://github.com/esp8266/Arduino/releases
9 | Built by Khoi Hoang https://github.com/khoih-prog/WebServer_WT32_ETH01
10 | Licensed under MIT license
11 | *****************************************************************************************************************************/
12 |
13 | #ifndef _CERTIFICATES_H_
14 | #define _CERTIFICATES_H_
15 |
16 | #if USING_THINGSTREAM_IO
17 |
18 | // This is the root Certificate Authority that signed the server certifcate
19 | // for the demo server mqtt.thingstream.io in this example.
20 | // To generate the Root CA Cert, use the following command (in Linux / Ubuntu)
21 | // $ openssl s_client -showcerts -connect mqtt.thingstream.io:8883
22 |
23 | const char* rootCACertificate =
24 | "-----BEGIN CERTIFICATE-----\n" \
25 | "MIIESTCCAzGgAwIBAgITBn+UV4WH6Kx33rJTMlu8mYtWDTANBgkqhkiG9w0BAQsF\n" \
26 | "ADA5MQswCQYDVQQGEwJVUzEPMA0GA1UEChMGQW1hem9uMRkwFwYDVQQDExBBbWF6\n" \
27 | "b24gUm9vdCBDQSAxMB4XDTE1MTAyMjAwMDAwMFoXDTI1MTAxOTAwMDAwMFowRjEL\n" \
28 | "MAkGA1UEBhMCVVMxDzANBgNVBAoTBkFtYXpvbjEVMBMGA1UECxMMU2VydmVyIENB\n" \
29 | "IDFCMQ8wDQYDVQQDEwZBbWF6b24wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEK\n" \
30 | "AoIBAQDCThZn3c68asg3Wuw6MLAd5tES6BIoSMzoKcG5blPVo+sDORrMd4f2AbnZ\n" \
31 | "cMzPa43j4wNxhplty6aUKk4T1qe9BOwKFjwK6zmxxLVYo7bHViXsPlJ6qOMpFge5\n" \
32 | "blDP+18x+B26A0piiQOuPkfyDyeR4xQghfj66Yo19V+emU3nazfvpFA+ROz6WoVm\n" \
33 | "B5x+F2pV8xeKNR7u6azDdU5YVX1TawprmxRC1+WsAYmz6qP+z8ArDITC2FMVy2fw\n" \
34 | "0IjKOtEXc/VfmtTFch5+AfGYMGMqqvJ6LcXiAhqG5TI+Dr0RtM88k+8XUBCeQ8IG\n" \
35 | "KuANaL7TiItKZYxK1MMuTJtV9IblAgMBAAGjggE7MIIBNzASBgNVHRMBAf8ECDAG\n" \
36 | "AQH/AgEAMA4GA1UdDwEB/wQEAwIBhjAdBgNVHQ4EFgQUWaRmBlKge5WSPKOUByeW\n" \
37 | "dFv5PdAwHwYDVR0jBBgwFoAUhBjMhTTsvAyUlC4IWZzHshBOCggwewYIKwYBBQUH\n" \
38 | "AQEEbzBtMC8GCCsGAQUFBzABhiNodHRwOi8vb2NzcC5yb290Y2ExLmFtYXpvbnRy\n" \
39 | "dXN0LmNvbTA6BggrBgEFBQcwAoYuaHR0cDovL2NydC5yb290Y2ExLmFtYXpvbnRy\n" \
40 | "dXN0LmNvbS9yb290Y2ExLmNlcjA/BgNVHR8EODA2MDSgMqAwhi5odHRwOi8vY3Js\n" \
41 | "LnJvb3RjYTEuYW1hem9udHJ1c3QuY29tL3Jvb3RjYTEuY3JsMBMGA1UdIAQMMAow\n" \
42 | "CAYGZ4EMAQIBMA0GCSqGSIb3DQEBCwUAA4IBAQCFkr41u3nPo4FCHOTjY3NTOVI1\n" \
43 | "59Gt/a6ZiqyJEi+752+a1U5y6iAwYfmXss2lJwJFqMp2PphKg5625kXg8kP2CN5t\n" \
44 | "6G7bMQcT8C8xDZNtYTd7WPD8UZiRKAJPBXa30/AbwuZe0GaFEQ8ugcYQgSn+IGBI\n" \
45 | "8/LwhBNTZTUVEWuCUUBVV18YtbAiPq3yXqMB48Oz+ctBWuZSkbvkNodPLamkB2g1\n" \
46 | "upRyzQ7qDn1X8nn8N8V7YJ6y68AtkHcNSRAnpTitxBKjtKPISLMVCx7i4hncxHZS\n" \
47 | "yLyKQXhw2W2Xs0qLeC1etA+jTGDK4UfLeC0SF7FSi8o5LL21L8IzApar2pR/\n" \
48 | "-----END CERTIFICATE-----\n";
49 |
50 | #else
51 |
52 | // This is the root Certificate Authority that signed the server certifcate
53 | // for the demo server broker.emqx.io in this example.
54 | // To generate the Root CA Cert, use the following command (in Linux / Ubuntu)
55 | // $ openssl s_client -showcerts -connect broker.emqx.io:8883
56 |
57 | const char* rootCACertificate =
58 | "-----BEGIN CERTIFICATE-----\n" \
59 | "MIIGEzCCA/ugAwIBAgIQfVtRJrR2uhHbdBYLvFMNpzANBgkqhkiG9w0BAQwFADCB\n" \
60 | "iDELMAkGA1UEBhMCVVMxEzARBgNVBAgTCk5ldyBKZXJzZXkxFDASBgNVBAcTC0pl\n" \
61 | "cnNleSBDaXR5MR4wHAYDVQQKExVUaGUgVVNFUlRSVVNUIE5ldHdvcmsxLjAsBgNV\n" \
62 | "BAMTJVVTRVJUcnVzdCBSU0EgQ2VydGlmaWNhdGlvbiBBdXRob3JpdHkwHhcNMTgx\n" \
63 | "MTAyMDAwMDAwWhcNMzAxMjMxMjM1OTU5WjCBjzELMAkGA1UEBhMCR0IxGzAZBgNV\n" \
64 | "BAgTEkdyZWF0ZXIgTWFuY2hlc3RlcjEQMA4GA1UEBxMHU2FsZm9yZDEYMBYGA1UE\n" \
65 | "ChMPU2VjdGlnbyBMaW1pdGVkMTcwNQYDVQQDEy5TZWN0aWdvIFJTQSBEb21haW4g\n" \
66 | "VmFsaWRhdGlvbiBTZWN1cmUgU2VydmVyIENBMIIBIjANBgkqhkiG9w0BAQEFAAOC\n" \
67 | "AQ8AMIIBCgKCAQEA1nMz1tc8INAA0hdFuNY+B6I/x0HuMjDJsGz99J/LEpgPLT+N\n" \
68 | "TQEMgg8Xf2Iu6bhIefsWg06t1zIlk7cHv7lQP6lMw0Aq6Tn/2YHKHxYyQdqAJrkj\n" \
69 | "eocgHuP/IJo8lURvh3UGkEC0MpMWCRAIIz7S3YcPb11RFGoKacVPAXJpz9OTTG0E\n" \
70 | "oKMbgn6xmrntxZ7FN3ifmgg0+1YuWMQJDgZkW7w33PGfKGioVrCSo1yfu4iYCBsk\n" \
71 | "Haswha6vsC6eep3BwEIc4gLw6uBK0u+QDrTBQBbwb4VCSmT3pDCg/r8uoydajotY\n" \
72 | "uK3DGReEY+1vVv2Dy2A0xHS+5p3b4eTlygxfFQIDAQABo4IBbjCCAWowHwYDVR0j\n" \
73 | "BBgwFoAUU3m/WqorSs9UgOHYm8Cd8rIDZsswHQYDVR0OBBYEFI2MXsRUrYrhd+mb\n" \
74 | "+ZsF4bgBjWHhMA4GA1UdDwEB/wQEAwIBhjASBgNVHRMBAf8ECDAGAQH/AgEAMB0G\n" \
75 | "A1UdJQQWMBQGCCsGAQUFBwMBBggrBgEFBQcDAjAbBgNVHSAEFDASMAYGBFUdIAAw\n" \
76 | "CAYGZ4EMAQIBMFAGA1UdHwRJMEcwRaBDoEGGP2h0dHA6Ly9jcmwudXNlcnRydXN0\n" \
77 | "LmNvbS9VU0VSVHJ1c3RSU0FDZXJ0aWZpY2F0aW9uQXV0aG9yaXR5LmNybDB2Bggr\n" \
78 | "BgEFBQcBAQRqMGgwPwYIKwYBBQUHMAKGM2h0dHA6Ly9jcnQudXNlcnRydXN0LmNv\n" \
79 | "bS9VU0VSVHJ1c3RSU0FBZGRUcnVzdENBLmNydDAlBggrBgEFBQcwAYYZaHR0cDov\n" \
80 | "L29jc3AudXNlcnRydXN0LmNvbTANBgkqhkiG9w0BAQwFAAOCAgEAMr9hvQ5Iw0/H\n" \
81 | "ukdN+Jx4GQHcEx2Ab/zDcLRSmjEzmldS+zGea6TvVKqJjUAXaPgREHzSyrHxVYbH\n" \
82 | "7rM2kYb2OVG/Rr8PoLq0935JxCo2F57kaDl6r5ROVm+yezu/Coa9zcV3HAO4OLGi\n" \
83 | "H19+24rcRki2aArPsrW04jTkZ6k4Zgle0rj8nSg6F0AnwnJOKf0hPHzPE/uWLMUx\n" \
84 | "RP0T7dWbqWlod3zu4f+k+TY4CFM5ooQ0nBnzvg6s1SQ36yOoeNDT5++SR2RiOSLv\n" \
85 | "xvcRviKFxmZEJCaOEDKNyJOuB56DPi/Z+fVGjmO+wea03KbNIaiGCpXZLoUmGv38\n" \
86 | "sbZXQm2V0TP2ORQGgkE49Y9Y3IBbpNV9lXj9p5v//cWoaasm56ekBYdbqbe4oyAL\n" \
87 | "l6lFhd2zi+WJN44pDfwGF/Y4QA5C5BIG+3vzxhFoYt/jmPQT2BVPi7Fp2RBgvGQq\n" \
88 | "6jG35LWjOhSbJuMLe/0CjraZwTiXWTb2qHSihrZe68Zk6s+go/lunrotEbaGmAhY\n" \
89 | "LcmsJWTyXnW0OMGuf1pGg+pRyrbxmRE1a6Vqe8YAsOf4vmSyrcjC8azjUeqkk+B5\n" \
90 | "yOGBQMkKW+ESPMFgKuOXwIlCypTPRpgSabuY0MLTDXJLR27lk8QyKGOHQ+SwMj4K\n" \
91 | "00u/I5sUKUErmgQfky3xxzlIPK1aEn8=\n" \
92 | "-----END CERTIFICATE-----\n";
93 |
94 | #endif // USING_THINGSTREAM_IO
95 |
96 | #endif /* ifndef _CERTIFICATES_H_ */
97 |
--------------------------------------------------------------------------------
/examples/SSL/WebClientMulti_SSL/WebClientMulti_SSL.ino:
--------------------------------------------------------------------------------
1 | /****************************************************************************************************************************
2 | WebClientMulti_SSL.ino - Dead simple SSL WebClient for Ethernet shields
3 |
4 | For Ethernet shields using WT32_ETH01 (ESP32 + LAN8720)
5 |
6 | WebServer_WT32_ETH01 is a library for the Ethernet LAN8720 in WT32_ETH01 to run WebServer
7 |
8 | Based on and modified from ESP8266 https://github.com/esp8266/Arduino/releases
9 | Built by Khoi Hoang https://github.com/khoih-prog/WebServer_WT32_ETH01
10 | Licensed under MIT license
11 | *****************************************************************************************************************************/
12 |
13 | // This sketch connects to SSL websites (https://www.arduino.cc/asciilogo.txt) and (https://www.cloudflare.com/cdn-cgi/trace)
14 |
15 | #define DEBUG_ETHERNET_WEBSERVER_PORT Serial
16 |
17 | // Debug Level from 0 to 4
18 | #define _ETHERNET_WEBSERVER_LOGLEVEL_ 3
19 |
20 | #include
21 |
22 | #include
23 |
24 | // Select the IP address according to your local network
25 | IPAddress myIP(192, 168, 2, 232);
26 | IPAddress myGW(192, 168, 2, 1);
27 | IPAddress mySN(255, 255, 255, 0);
28 |
29 | // Google DNS Server IP
30 | IPAddress myDNS(8, 8, 8, 8);
31 |
32 | #include "certificates.h"
33 |
34 | // the two domains we want to query
35 | char server1[] = "www.arduino.cc";
36 | char server2[] = "www.cloudflare.com";
37 | // and the queries we want to send to them
38 | char query1[] = "GET /asciilogo.txt HTTP/1.1";
39 | char query2[] = "GET /cdn-cgi/trace HTTP/1.1";
40 |
41 | const uint16_t server_port = 443;
42 |
43 | // Initialize the SSL client library
44 | WiFiClientSecure sslClient;
45 |
46 | // Variables to measure
47 | unsigned long beginMillis;
48 | unsigned long byteCount = 0;
49 | unsigned long loopCount = 0;
50 |
51 | bool printWebData = true; // set to false for better speed measurement
52 |
53 | void connectSSL()
54 | {
55 | static bool r = true;
56 | // cycle the server we want to connect to back and forth
57 | char* server;
58 | char* query;
59 |
60 | if (r)
61 | {
62 | server = server1;
63 | query = query1;
64 | sslClient.setCACert(rootCACertificate1);
65 | }
66 | else
67 | {
68 | server = server2;
69 | query = query2;
70 | sslClient.setCACert(rootCACertificate2);
71 | }
72 |
73 | r = !r;
74 |
75 | Serial.print("Connecting to ");
76 | Serial.print(server);
77 | Serial.println("...");
78 |
79 | // if you get a connection, report back via serial:
80 | auto start = millis();
81 |
82 | if (sslClient.connect(server, server_port))
83 | {
84 | auto time = millis() - start;
85 |
86 | Serial.print("Took (ms): ");
87 | Serial.println(time);
88 |
89 | // Make a HTTP request:
90 | sslClient.println(query);
91 | sslClient.println("User-Agent: SSLClientOverEthernet");
92 | sslClient.print("Host: ");
93 | sslClient.println(server);
94 | sslClient.println("Connection: close");
95 | sslClient.println();
96 | sslClient.flush();
97 | }
98 | else
99 | {
100 | // if you didn't get a connection to the server:
101 | Serial.println("connection failed");
102 | }
103 |
104 | beginMillis = millis();
105 | }
106 |
107 | // Not sure if WiFiClientSecure checks the validity date of the certificate.
108 | // Setting clock just to be sure...
109 | void setClock()
110 | {
111 | configTime(0, 0, "pool.ntp.org");
112 |
113 | Serial.print(F("Waiting for NTP time sync: "));
114 | time_t nowSecs = time(nullptr);
115 |
116 | while (nowSecs < 8 * 3600 * 2)
117 | {
118 | delay(500);
119 | Serial.print(F("."));
120 | yield();
121 | nowSecs = time(nullptr);
122 | }
123 |
124 | Serial.println();
125 | struct tm timeinfo;
126 | gmtime_r(&nowSecs, &timeinfo);
127 | Serial.print(F("Current time: "));
128 | Serial.print(asctime(&timeinfo));
129 | }
130 |
131 | void setup()
132 | {
133 | // Open serial communications and wait for port to open:
134 | Serial.begin(115200);
135 |
136 | while (!Serial);
137 |
138 | // Using this if Serial debugging is not necessary or not using Serial port
139 | //while (!Serial && (millis() < 3000));
140 |
141 | Serial.print("\nStarting WebClientMulti_SSL on " + String(ARDUINO_BOARD));
142 | Serial.println(" with " + String(SHIELD_TYPE));
143 | Serial.println(WEBSERVER_WT32_ETH01_VERSION);
144 |
145 | // To be called before ETH.begin()
146 | WT32_ETH01_onEvent();
147 |
148 | //bool begin(uint8_t phy_addr=ETH_PHY_ADDR, int power=ETH_PHY_POWER, int mdc=ETH_PHY_MDC, int mdio=ETH_PHY_MDIO,
149 | // eth_phy_type_t type=ETH_PHY_TYPE, eth_clock_mode_t clk_mode=ETH_CLK_MODE);
150 | //ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER, ETH_PHY_MDC, ETH_PHY_MDIO, ETH_PHY_TYPE, ETH_CLK_MODE);
151 | ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER);
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 | WT32_ETH01_waitForConnect();
158 |
159 | setClock();
160 |
161 | // give the Ethernet shield a second to initialize:
162 | delay(2000);
163 |
164 | // connect!
165 | connectSSL();
166 | }
167 |
168 | #define BUFFER_SZ 2048
169 |
170 | byte buffer[BUFFER_SZ + 1];
171 |
172 | void loop()
173 | {
174 | // if there are incoming bytes available
175 | // from the server, read them and print them:
176 | int len = sslClient.available();
177 |
178 | if (len > 0)
179 | {
180 | if (len > BUFFER_SZ)
181 | len = BUFFER_SZ;
182 |
183 | sslClient.read(buffer, len);
184 |
185 | if (printWebData)
186 | {
187 | Serial.write(buffer, len); // show in the serial monitor (slows some boards)
188 | }
189 |
190 | byteCount = byteCount + len;
191 | }
192 | else
193 | {
194 | if (millis() - beginMillis > 10000)
195 | {
196 | Serial.print("LoopCount ");
197 | Serial.print(++loopCount);
198 |
199 | Serial.print(", Received ");
200 | Serial.print(byteCount);
201 | Serial.println(" bytes");
202 |
203 | // Reset
204 | byteCount = 0;
205 |
206 | Serial.println("Disconnecting.");
207 | sslClient.stop();
208 | }
209 | }
210 |
211 | // if the server's disconnected, stop the sslClient:
212 | if (!sslClient.connected())
213 | {
214 | //quick delay
215 | delay(10000);
216 |
217 | // connect again!
218 | connectSSL();
219 | }
220 | }
221 |
--------------------------------------------------------------------------------
/examples/SSL/WebClientMulti_SSL/certificates.h:
--------------------------------------------------------------------------------
1 | /****************************************************************************************************************************
2 | certificates.h
3 |
4 | For Ethernet shields using WT32_ETH01 (ESP32 + LAN8720)
5 |
6 | WebServer_WT32_ETH01 is a library for the Ethernet LAN8720 in WT32_ETH01 to run WebServer
7 |
8 | Based on and modified from ESP8266 https://github.com/esp8266/Arduino/releases
9 | Built by Khoi Hoang https://github.com/khoih-prog/WebServer_WT32_ETH01
10 | Licensed under MIT license
11 | *****************************************************************************************************************************/
12 |
13 | #ifndef _CERTIFICATES_H_
14 | #define _CERTIFICATES_H_
15 |
16 | // This is the root Certificate Authority that signed the server certifcate
17 | // for the demo server www.arduino.cc:443 in this example.
18 | // To generate the Root CA Cert, use the following command (in Linux / Ubuntu)
19 | // $ openssl s_client -showcerts -connect www.arduino.cc:443
20 |
21 | const char* rootCACertificate1 =
22 | "-----BEGIN CERTIFICATE-----\n" \
23 | "MIIDzTCCArWgAwIBAgIQCjeHZF5ftIwiTv0b7RQMPDANBgkqhkiG9w0BAQsFADBa\n" \
24 | "MQswCQYDVQQGEwJJRTESMBAGA1UEChMJQmFsdGltb3JlMRMwEQYDVQQLEwpDeWJl\n" \
25 | "clRydXN0MSIwIAYDVQQDExlCYWx0aW1vcmUgQ3liZXJUcnVzdCBSb290MB4XDTIw\n" \
26 | "MDEyNzEyNDgwOFoXDTI0MTIzMTIzNTk1OVowSjELMAkGA1UEBhMCVVMxGTAXBgNV\n" \
27 | "BAoTEENsb3VkZmxhcmUsIEluYy4xIDAeBgNVBAMTF0Nsb3VkZmxhcmUgSW5jIEVD\n" \
28 | "QyBDQS0zMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEua1NZpkUC0bsH4HRKlAe\n" \
29 | "nQMVLzQSfS2WuIg4m4Vfj7+7Te9hRsTJc9QkT+DuHM5ss1FxL2ruTAUJd9NyYqSb\n" \
30 | "16OCAWgwggFkMB0GA1UdDgQWBBSlzjfq67B1DpRniLRF+tkkEIeWHzAfBgNVHSME\n" \
31 | "GDAWgBTlnVkwgkdYzKz6CFQ2hns6tQRN8DAOBgNVHQ8BAf8EBAMCAYYwHQYDVR0l\n" \
32 | "BBYwFAYIKwYBBQUHAwEGCCsGAQUFBwMCMBIGA1UdEwEB/wQIMAYBAf8CAQAwNAYI\n" \
33 | "KwYBBQUHAQEEKDAmMCQGCCsGAQUFBzABhhhodHRwOi8vb2NzcC5kaWdpY2VydC5j\n" \
34 | "b20wOgYDVR0fBDMwMTAvoC2gK4YpaHR0cDovL2NybDMuZGlnaWNlcnQuY29tL09t\n" \
35 | "bmlyb290MjAyNS5jcmwwbQYDVR0gBGYwZDA3BglghkgBhv1sAQEwKjAoBggrBgEF\n" \
36 | "BQcCARYcaHR0cHM6Ly93d3cuZGlnaWNlcnQuY29tL0NQUzALBglghkgBhv1sAQIw\n" \
37 | "CAYGZ4EMAQIBMAgGBmeBDAECAjAIBgZngQwBAgMwDQYJKoZIhvcNAQELBQADggEB\n" \
38 | "AAUkHd0bsCrrmNaF4zlNXmtXnYJX/OvoMaJXkGUFvhZEOFp3ArnPEELG4ZKk40Un\n" \
39 | "+ABHLGioVplTVI+tnkDB0A+21w0LOEhsUCxJkAZbZB2LzEgwLt4I4ptJIsCSDBFe\n" \
40 | "lpKU1fwg3FZs5ZKTv3ocwDfjhUkV+ivhdDkYD7fa86JXWGBPzI6UAPxGezQxPk1H\n" \
41 | "goE6y/SJXQ7vTQ1unBuCJN0yJV0ReFEQPaA1IwQvZW+cwdFD19Ae8zFnWSfda9J1\n" \
42 | "CZMRJCQUzym+5iPDuI9yP+kHyCREU3qzuWFloUwOxkgAyXVjBYdwRVKD05WdRerw\n" \
43 | "6DEdfgkfCv4+3ao8XnTSrLE=\n" \
44 | "-----END CERTIFICATE-----\n";
45 |
46 | // This is the root Certificate Authority that signed the server certifcate
47 | // for the demo server www.cloudflare.com:443 in this example.
48 | // To generate the Root CA Cert, use the following command (in Linux / Ubuntu)
49 | // $ openssl s_client -showcerts -connect www.cloudflare.com:443
50 |
51 | const char* rootCACertificate2 =
52 | "-----BEGIN CERTIFICATE-----\n" \
53 | "MIIDzTCCArWgAwIBAgIQCjeHZF5ftIwiTv0b7RQMPDANBgkqhkiG9w0BAQsFADBa\n" \
54 | "MQswCQYDVQQGEwJJRTESMBAGA1UEChMJQmFsdGltb3JlMRMwEQYDVQQLEwpDeWJl\n" \
55 | "clRydXN0MSIwIAYDVQQDExlCYWx0aW1vcmUgQ3liZXJUcnVzdCBSb290MB4XDTIw\n" \
56 | "MDEyNzEyNDgwOFoXDTI0MTIzMTIzNTk1OVowSjELMAkGA1UEBhMCVVMxGTAXBgNV\n" \
57 | "BAoTEENsb3VkZmxhcmUsIEluYy4xIDAeBgNVBAMTF0Nsb3VkZmxhcmUgSW5jIEVD\n" \
58 | "QyBDQS0zMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEua1NZpkUC0bsH4HRKlAe\n" \
59 | "nQMVLzQSfS2WuIg4m4Vfj7+7Te9hRsTJc9QkT+DuHM5ss1FxL2ruTAUJd9NyYqSb\n" \
60 | "16OCAWgwggFkMB0GA1UdDgQWBBSlzjfq67B1DpRniLRF+tkkEIeWHzAfBgNVHSME\n" \
61 | "GDAWgBTlnVkwgkdYzKz6CFQ2hns6tQRN8DAOBgNVHQ8BAf8EBAMCAYYwHQYDVR0l\n" \
62 | "BBYwFAYIKwYBBQUHAwEGCCsGAQUFBwMCMBIGA1UdEwEB/wQIMAYBAf8CAQAwNAYI\n" \
63 | "KwYBBQUHAQEEKDAmMCQGCCsGAQUFBzABhhhodHRwOi8vb2NzcC5kaWdpY2VydC5j\n" \
64 | "b20wOgYDVR0fBDMwMTAvoC2gK4YpaHR0cDovL2NybDMuZGlnaWNlcnQuY29tL09t\n" \
65 | "bmlyb290MjAyNS5jcmwwbQYDVR0gBGYwZDA3BglghkgBhv1sAQEwKjAoBggrBgEF\n" \
66 | "BQcCARYcaHR0cHM6Ly93d3cuZGlnaWNlcnQuY29tL0NQUzALBglghkgBhv1sAQIw\n" \
67 | "CAYGZ4EMAQIBMAgGBmeBDAECAjAIBgZngQwBAgMwDQYJKoZIhvcNAQELBQADggEB\n" \
68 | "AAUkHd0bsCrrmNaF4zlNXmtXnYJX/OvoMaJXkGUFvhZEOFp3ArnPEELG4ZKk40Un\n" \
69 | "+ABHLGioVplTVI+tnkDB0A+21w0LOEhsUCxJkAZbZB2LzEgwLt4I4ptJIsCSDBFe\n" \
70 | "lpKU1fwg3FZs5ZKTv3ocwDfjhUkV+ivhdDkYD7fa86JXWGBPzI6UAPxGezQxPk1H\n" \
71 | "goE6y/SJXQ7vTQ1unBuCJN0yJV0ReFEQPaA1IwQvZW+cwdFD19Ae8zFnWSfda9J1\n" \
72 | "CZMRJCQUzym+5iPDuI9yP+kHyCREU3qzuWFloUwOxkgAyXVjBYdwRVKD05WdRerw\n" \
73 | "6DEdfgkfCv4+3ao8XnTSrLE=\n" \
74 | "-----END CERTIFICATE-----\n";
75 |
76 | #endif /* ifndef _CERTIFICATES_H_ */
77 |
--------------------------------------------------------------------------------
/examples/SSL/WebClient_SSL/WebClient_SSL.ino:
--------------------------------------------------------------------------------
1 | /****************************************************************************************************************************
2 | WebClient_SSL.ino - Dead simple SSL WebClient for Ethernet shields
3 |
4 | For Ethernet shields using WT32_ETH01 (ESP32 + LAN8720)
5 |
6 | WebServer_WT32_ETH01 is a library for the Ethernet LAN8720 in WT32_ETH01 to run WebServer
7 |
8 | Based on and modified from ESP8266 https://github.com/esp8266/Arduino/releases
9 | Built by Khoi Hoang https://github.com/khoih-prog/WebServer_WT32_ETH01
10 | Licensed under MIT license
11 | *****************************************************************************************************************************/
12 |
13 | // This sample sketch connects to SSL website (https://www.arduino.cc/asciilogo.txt)
14 |
15 | #define DEBUG_ETHERNET_WEBSERVER_PORT Serial
16 |
17 | // Debug Level from 0 to 4
18 | #define _ETHERNET_WEBSERVER_LOGLEVEL_ 3
19 |
20 | #include
21 |
22 | #include
23 |
24 | // Select the IP address according to your local network
25 | IPAddress myIP(192, 168, 2, 232);
26 | IPAddress myGW(192, 168, 2, 1);
27 | IPAddress mySN(255, 255, 255, 0);
28 |
29 | // Google DNS Server IP
30 | IPAddress myDNS(8, 8, 8, 8);
31 |
32 | #include "certificates.h"
33 |
34 | const char server_host[] = "www.arduino.cc"; // leave this alone, change only above two
35 | const uint16_t server_port = 443;
36 |
37 | // Initialize the SSL client library
38 | WiFiClientSecure sslClient;
39 |
40 | // Variables to measure
41 | unsigned long beginMillis;
42 | unsigned long byteCount = 0;
43 |
44 | bool printWebData = true; // set to false for better speed measurement
45 |
46 | void connectSSL()
47 | {
48 | Serial.print("Connecting to ");
49 | Serial.print(server_host);
50 | Serial.println("...");
51 |
52 | // if you get a connection, report back via serial:
53 | auto start = millis();
54 |
55 | sslClient.setCACert(rootCACertificate1);
56 |
57 | if (sslClient.connect(server_host, server_port))
58 | {
59 | auto time = millis() - start;
60 |
61 | Serial.print("Took: ");
62 | Serial.println(time);
63 |
64 | // Make a HTTP request:
65 | sslClient.println("GET /asciilogo.txt HTTP/1.1");
66 | sslClient.println("User-Agent: SSLClientOverEthernet");
67 | sslClient.print("Host: ");
68 | sslClient.println(server_host);
69 | sslClient.println("Connection: close");
70 | sslClient.println();
71 | sslClient.flush();
72 | }
73 | else
74 | {
75 | // if you didn't get a connection to the server:
76 | Serial.println("connection failed");
77 | }
78 |
79 | beginMillis = millis();
80 | }
81 |
82 | // Not sure if WiFiClientSecure checks the validity date of the certificate.
83 | // Setting clock just to be sure...
84 | void setClock()
85 | {
86 | configTime(0, 0, "pool.ntp.org");
87 |
88 | Serial.print(F("Waiting for NTP time sync: "));
89 | time_t nowSecs = time(nullptr);
90 |
91 | while (nowSecs < 8 * 3600 * 2)
92 | {
93 | delay(500);
94 | Serial.print(F("."));
95 | yield();
96 | nowSecs = time(nullptr);
97 | }
98 |
99 | Serial.println();
100 | struct tm timeinfo;
101 | gmtime_r(&nowSecs, &timeinfo);
102 | Serial.print(F("Current time: "));
103 | Serial.print(asctime(&timeinfo));
104 | }
105 |
106 | void setup()
107 | {
108 | // Open serial communications and wait for port to open:
109 | Serial.begin(115200);
110 |
111 | while (!Serial);
112 |
113 | // Using this if Serial debugging is not necessary or not using Serial port
114 | //while (!Serial && (millis() < 3000));
115 |
116 | Serial.print("\nStarting WebClient_SSL on " + String(ARDUINO_BOARD));
117 | Serial.println(" with " + String(SHIELD_TYPE));
118 | Serial.println(WEBSERVER_WT32_ETH01_VERSION);
119 |
120 | // To be called before ETH.begin()
121 | WT32_ETH01_onEvent();
122 |
123 | //bool begin(uint8_t phy_addr=ETH_PHY_ADDR, int power=ETH_PHY_POWER, int mdc=ETH_PHY_MDC, int mdio=ETH_PHY_MDIO,
124 | // eth_phy_type_t type=ETH_PHY_TYPE, eth_clock_mode_t clk_mode=ETH_CLK_MODE);
125 | //ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER, ETH_PHY_MDC, ETH_PHY_MDIO, ETH_PHY_TYPE, ETH_CLK_MODE);
126 | ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER);
127 |
128 | // Static IP, leave without this line to get IP via DHCP
129 | //bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
130 | ETH.config(myIP, myGW, mySN, myDNS);
131 |
132 | WT32_ETH01_waitForConnect();
133 |
134 | setClock();
135 |
136 | // give the Ethernet shield a second to initialize:
137 | delay(2000);
138 |
139 | // connect!
140 | connectSSL();
141 | }
142 |
143 | #define BUFFER_SZ 2048
144 |
145 | byte buffer[BUFFER_SZ + 1];
146 |
147 | void loop()
148 | {
149 | // if there are incoming bytes available
150 | // from the server, read them and print them:
151 | int len = sslClient.available();
152 |
153 | if (len > 0)
154 | {
155 | if (len > BUFFER_SZ)
156 | len = BUFFER_SZ;
157 |
158 | sslClient.read(buffer, len);
159 |
160 | if (printWebData)
161 | {
162 | Serial.write(buffer, len); // show in the serial monitor (slows some boards)
163 | }
164 |
165 | byteCount = byteCount + len;
166 | }
167 | else
168 | {
169 | if (millis() - beginMillis > 10000)
170 | {
171 | Serial.print("Received ");
172 | Serial.print(byteCount);
173 | Serial.println(" bytes");
174 |
175 | // Reset
176 | byteCount = 0;
177 |
178 | Serial.println("Disconnecting.");
179 | sslClient.stop();
180 |
181 | // do nothing forever:
182 | while (true)
183 | {
184 | delay(1);
185 | }
186 | }
187 | }
188 | }
189 |
--------------------------------------------------------------------------------
/examples/SSL/WebClient_SSL/certificates.h:
--------------------------------------------------------------------------------
1 | /****************************************************************************************************************************
2 | certificates.h
3 |
4 | For Ethernet shields using WT32_ETH01 (ESP32 + LAN8720)
5 |
6 | WebServer_WT32_ETH01 is a library for the Ethernet LAN8720 in WT32_ETH01 to run WebServer
7 |
8 | Based on and modified from ESP8266 https://github.com/esp8266/Arduino/releases
9 | Built by Khoi Hoang https://github.com/khoih-prog/WebServer_WT32_ETH01
10 | Licensed under MIT license
11 | *****************************************************************************************************************************/
12 |
13 | #ifndef _CERTIFICATES_H_
14 | #define _CERTIFICATES_H_
15 |
16 | // This is the root Certificate Authority that signed the server certifcate
17 | // for the demo server www.arduino.cc:443 in this example.
18 | // To generate the Root CA Cert, use the following command (in Linux / Ubuntu)
19 | // $ openssl s_client -showcerts -connect www.arduino.cc:443
20 |
21 | const char* rootCACertificate1 =
22 | "-----BEGIN CERTIFICATE-----\n" \
23 | "MIIDzTCCArWgAwIBAgIQCjeHZF5ftIwiTv0b7RQMPDANBgkqhkiG9w0BAQsFADBa\n" \
24 | "MQswCQYDVQQGEwJJRTESMBAGA1UEChMJQmFsdGltb3JlMRMwEQYDVQQLEwpDeWJl\n" \
25 | "clRydXN0MSIwIAYDVQQDExlCYWx0aW1vcmUgQ3liZXJUcnVzdCBSb290MB4XDTIw\n" \
26 | "MDEyNzEyNDgwOFoXDTI0MTIzMTIzNTk1OVowSjELMAkGA1UEBhMCVVMxGTAXBgNV\n" \
27 | "BAoTEENsb3VkZmxhcmUsIEluYy4xIDAeBgNVBAMTF0Nsb3VkZmxhcmUgSW5jIEVD\n" \
28 | "QyBDQS0zMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEua1NZpkUC0bsH4HRKlAe\n" \
29 | "nQMVLzQSfS2WuIg4m4Vfj7+7Te9hRsTJc9QkT+DuHM5ss1FxL2ruTAUJd9NyYqSb\n" \
30 | "16OCAWgwggFkMB0GA1UdDgQWBBSlzjfq67B1DpRniLRF+tkkEIeWHzAfBgNVHSME\n" \
31 | "GDAWgBTlnVkwgkdYzKz6CFQ2hns6tQRN8DAOBgNVHQ8BAf8EBAMCAYYwHQYDVR0l\n" \
32 | "BBYwFAYIKwYBBQUHAwEGCCsGAQUFBwMCMBIGA1UdEwEB/wQIMAYBAf8CAQAwNAYI\n" \
33 | "KwYBBQUHAQEEKDAmMCQGCCsGAQUFBzABhhhodHRwOi8vb2NzcC5kaWdpY2VydC5j\n" \
34 | "b20wOgYDVR0fBDMwMTAvoC2gK4YpaHR0cDovL2NybDMuZGlnaWNlcnQuY29tL09t\n" \
35 | "bmlyb290MjAyNS5jcmwwbQYDVR0gBGYwZDA3BglghkgBhv1sAQEwKjAoBggrBgEF\n" \
36 | "BQcCARYcaHR0cHM6Ly93d3cuZGlnaWNlcnQuY29tL0NQUzALBglghkgBhv1sAQIw\n" \
37 | "CAYGZ4EMAQIBMAgGBmeBDAECAjAIBgZngQwBAgMwDQYJKoZIhvcNAQELBQADggEB\n" \
38 | "AAUkHd0bsCrrmNaF4zlNXmtXnYJX/OvoMaJXkGUFvhZEOFp3ArnPEELG4ZKk40Un\n" \
39 | "+ABHLGioVplTVI+tnkDB0A+21w0LOEhsUCxJkAZbZB2LzEgwLt4I4ptJIsCSDBFe\n" \
40 | "lpKU1fwg3FZs5ZKTv3ocwDfjhUkV+ivhdDkYD7fa86JXWGBPzI6UAPxGezQxPk1H\n" \
41 | "goE6y/SJXQ7vTQ1unBuCJN0yJV0ReFEQPaA1IwQvZW+cwdFD19Ae8zFnWSfda9J1\n" \
42 | "CZMRJCQUzym+5iPDuI9yP+kHyCREU3qzuWFloUwOxkgAyXVjBYdwRVKD05WdRerw\n" \
43 | "6DEdfgkfCv4+3ao8XnTSrLE=\n" \
44 | "-----END CERTIFICATE-----\n";
45 |
46 | #endif /* ifndef _CERTIFICATES_H_ */
47 |
--------------------------------------------------------------------------------
/examples/SimpleAuthentication/SimpleAuthentication.ino:
--------------------------------------------------------------------------------
1 | /****************************************************************************************************************************
2 | SimpleAuthentication.ino - Dead simple web-server for Ethernet shields
3 |
4 | For Ethernet shields using WT32_ETH01 (ESP32 + LAN8720)
5 |
6 | WebServer_WT32_ETH01 is a library for the Ethernet LAN8720 in WT32_ETH01 to run WebServer
7 |
8 | Based on and modified from ESP8266 https://github.com/esp8266/Arduino/releases
9 | Built by Khoi Hoang https://github.com/khoih-prog/WebServer_WT32_ETH01
10 | Licensed under MIT license
11 | *****************************************************************************************************************************/
12 |
13 | #define DEBUG_ETHERNET_WEBSERVER_PORT Serial
14 |
15 | // Debug Level from 0 to 4
16 | #define _ETHERNET_WEBSERVER_LOGLEVEL_ 3
17 |
18 | #include
19 |
20 | WebServer server(80);
21 |
22 | // Select the IP address according to your local network
23 | IPAddress myIP(192, 168, 2, 232);
24 | IPAddress myGW(192, 168, 2, 1);
25 | IPAddress mySN(255, 255, 255, 0);
26 |
27 | // Google DNS Server IP
28 | IPAddress myDNS(8, 8, 8, 8);
29 |
30 | //Check if header is present and correct
31 | bool is_authenticated()
32 | {
33 | Serial.println(F("Enter is_authenticated"));
34 |
35 | if (server.hasHeader(F("Cookie")))
36 | {
37 | Serial.print(F("Found cookie: "));
38 | String cookie = server.header(F("Cookie"));
39 | Serial.println(cookie);
40 |
41 | if (cookie.indexOf(F("ESPSESSIONID=1")) != -1)
42 | {
43 | Serial.println(F("Authentication Successful"));
44 | return true;
45 | }
46 | }
47 |
48 | Serial.println(F("Authentication Failed"));
49 | return false;
50 | }
51 |
52 | //login page, also called for disconnect
53 | void handleLogin()
54 | {
55 | String msg;
56 |
57 | if (server.hasHeader(F("Cookie")))
58 | {
59 | Serial.print(F("Found cookie: "));
60 | String cookie = server.header(F("Cookie"));
61 | Serial.println(cookie);
62 | }
63 |
64 | if (server.hasArg(F("DISCONNECT")))
65 | {
66 | Serial.println(F("Disconnection"));
67 | server.sendHeader(F("Location"), F("/login"));
68 | server.sendHeader(F("Cache-Control"), F("no-cache"));
69 | server.sendHeader(F("Set-Cookie"), F("ESPSESSIONID=0"));
70 | server.send(301);
71 | return;
72 | }
73 |
74 | if (server.hasArg(F("USERNAME")) && server.hasArg(F("PASSWORD")))
75 | {
76 | if (server.arg(F("USERNAME")) == F("admin") && server.arg(F("PASSWORD")) == F("password"))
77 | {
78 | server.sendHeader(F("Location"), F("/"));
79 | server.sendHeader(F("Cache-Control"), F("no-cache"));
80 | server.sendHeader(F("Set-Cookie"), F("ESPSESSIONID=1"));
81 | server.send(301);
82 | Serial.println(F("Log in Successful"));
83 | return;
84 | }
85 |
86 | msg = F("Wrong username/password! try again.");
87 | Serial.println(F("Log in Failed"));
88 | }
89 |
90 | String content = F("");
94 | content += msg;
95 | content += F(" ");
96 | content += F("You also can go here");
97 | server.send(200, F("text/html"), content);
98 | }
99 |
100 | //root page can be accessed only if authentication is ok
101 | void handleRoot()
102 | {
103 | String header;
104 |
105 | Serial.println(F("Enter handleRoot"));
106 |
107 | if (!is_authenticated())
108 | {
109 | server.sendHeader(F("Location"), F("/login"));
110 | server.sendHeader(F("Cache-Control"), F("no-cache"));
111 | server.send(301);
112 | return;
113 | }
114 |
115 | String content = F("
Hello, you're connected to WebServer_WT32_ETH01 running on ");
116 |
117 | content += String(BOARD_NAME);
118 | content += F("!
");
119 |
120 | if (server.hasHeader(F("User-Agent")))
121 | {
122 | content += F("the user agent used is : ");
123 | content += server.header(F("User-Agent"));
124 | content += F("
");
125 | }
126 |
127 | content += F("You can access this page until you disconnect");
128 | server.send(200, F("text/html"), content);
129 | }
130 |
131 | //no need authentication
132 | void handleNotFound()
133 | {
134 | String message = F("File Not Found\n\n");
135 |
136 | message += F("URI: ");
137 | message += server.uri();
138 | message += F("\nMethod: ");
139 | message += (server.method() == HTTP_GET) ? F("GET") : F("POST");
140 | message += F("\nArguments: ");
141 | message += server.args();
142 | message += F("\n");
143 |
144 | for (uint8_t i = 0; i < server.args(); i++)
145 | {
146 | message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
147 | }
148 |
149 | server.send(404, F("text/plain"), message);
150 | }
151 |
152 | void setup()
153 | {
154 | Serial.begin(115200);
155 |
156 | while (!Serial);
157 |
158 | // Using this if Serial debugging is not necessary or not using Serial port
159 | //while (!Serial && (millis() < 3000));
160 |
161 | Serial.print("\nStarting SimpleAuthentication on " + String(ARDUINO_BOARD));
162 | Serial.println(" with " + String(SHIELD_TYPE));
163 | Serial.println(WEBSERVER_WT32_ETH01_VERSION);
164 |
165 | // To be called before ETH.begin()
166 | WT32_ETH01_onEvent();
167 |
168 | //bool begin(uint8_t phy_addr=ETH_PHY_ADDR, int power=ETH_PHY_POWER, int mdc=ETH_PHY_MDC, int mdio=ETH_PHY_MDIO,
169 | // eth_phy_type_t type=ETH_PHY_TYPE, eth_clock_mode_t clk_mode=ETH_CLK_MODE);
170 | //ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER, ETH_PHY_MDC, ETH_PHY_MDIO, ETH_PHY_TYPE, ETH_CLK_MODE);
171 | ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER);
172 |
173 | // Static IP, leave without this line to get IP via DHCP
174 | //bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
175 | ETH.config(myIP, myGW, mySN, myDNS);
176 |
177 | WT32_ETH01_waitForConnect();
178 |
179 | server.on(F("/"), handleRoot);
180 |
181 | server.on(F("/login"), handleLogin);
182 |
183 | server.on(F("/inline"), []()
184 | {
185 | server.send(200, F("text/plain"), F("This works without need of authentication"));
186 | });
187 |
188 | server.onNotFound(handleNotFound);
189 |
190 | //here the list of headers to be recorded
191 | const char * headerkeys[] = {"User-Agent", "Cookie"} ;
192 | size_t headerkeyssize = sizeof(headerkeys) / sizeof(char*);
193 |
194 | //ask server to track these headers
195 | server.collectHeaders(headerkeys, headerkeyssize);
196 | server.begin();
197 |
198 | Serial.print(F("HTTP SimpleAuthentication is @ IP : "));
199 | Serial.println(ETH.localIP());
200 | }
201 |
202 | void loop()
203 | {
204 | server.handleClient();
205 | }
206 |
--------------------------------------------------------------------------------
/examples/UdpNTPClient/UdpNTPClient.ino:
--------------------------------------------------------------------------------
1 | /****************************************************************************************************************************
2 | UdpNTPClient.ino - Simple Arduino web server sample for ESP8266 AT-command shield
3 |
4 | For Ethernet shields using WT32_ETH01 (ESP32 + LAN8720)
5 |
6 | WebServer_WT32_ETH01 is a library for the Ethernet LAN8720 in WT32_ETH01 to run WebServer
7 |
8 | Based on and modified from ESP8266 https://github.com/esp8266/Arduino/releases
9 | Built by Khoi Hoang https://github.com/khoih-prog/WebServer_WT32_ETH01
10 | Licensed under MIT license
11 | *****************************************************************************************************************************/
12 | /*
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 | #define DEBUG_ETHERNET_WEBSERVER_PORT Serial
19 |
20 | // Debug Level from 0 to 4
21 | #define _ETHERNET_WEBSERVER_LOGLEVEL_ 3
22 |
23 | #include
24 |
25 | // Select the IP address according to your local network
26 | IPAddress myIP(192, 168, 2, 232);
27 | IPAddress myGW(192, 168, 2, 1);
28 | IPAddress mySN(255, 255, 255, 0);
29 |
30 | // Google DNS Server IP
31 | IPAddress myDNS(8, 8, 8, 8);
32 |
33 | char timeServer[] = "time.nist.gov"; // NTP server
34 | unsigned int localPort = 2390; // local port to listen for UDP packets
35 |
36 | const int NTP_PACKET_SIZE = 48; // NTP timestamp is in the first 48 bytes of the message
37 | const int UDP_TIMEOUT = 2000; // timeout in miliseconds to wait for an UDP packet to arrive
38 |
39 | byte packetBuffer[NTP_PACKET_SIZE]; // buffer to hold incoming and outgoing packets
40 |
41 | // A UDP instance to let us send and receive packets over UDP
42 | WiFiUDP Udp;
43 |
44 | // send an NTP request to the time server at the given address
45 | void sendNTPpacket(char *ntpSrv)
46 | {
47 | // set all bytes in the buffer to 0
48 | memset(packetBuffer, 0, NTP_PACKET_SIZE);
49 | // Initialize values needed to form NTP request
50 | // (see URL above for details on the packets)
51 |
52 | packetBuffer[0] = 0b11100011; // LI, Version, Mode
53 | packetBuffer[1] = 0; // Stratum, or type of clock
54 | packetBuffer[2] = 6; // Polling Interval
55 | packetBuffer[3] = 0xEC; // Peer Clock Precision
56 | // 8 bytes of zero for Root Delay & Root Dispersion
57 | packetBuffer[12] = 49;
58 | packetBuffer[13] = 0x4E;
59 | packetBuffer[14] = 49;
60 | packetBuffer[15] = 52;
61 |
62 | // all NTP fields have been given values, now
63 | // you can send a packet requesting a timestamp:
64 | Udp.beginPacket(ntpSrv, 123); //NTP requests are to port 123
65 |
66 | Udp.write(packetBuffer, NTP_PACKET_SIZE);
67 |
68 | Udp.endPacket();
69 | }
70 |
71 | void setup()
72 | {
73 | Serial.begin(115200);
74 |
75 | while (!Serial);
76 |
77 | // Using this if Serial debugging is not necessary or not using Serial port
78 | //while (!Serial && (millis() < 3000));
79 |
80 | Serial.print("\nStarting UdpNTPClient on " + String(ARDUINO_BOARD));
81 | Serial.println(" with " + String(SHIELD_TYPE));
82 | Serial.println(WEBSERVER_WT32_ETH01_VERSION);
83 |
84 | // To be called before ETH.begin()
85 | WT32_ETH01_onEvent();
86 |
87 | //bool begin(uint8_t phy_addr=ETH_PHY_ADDR, int power=ETH_PHY_POWER, int mdc=ETH_PHY_MDC, int mdio=ETH_PHY_MDIO,
88 | // eth_phy_type_t type=ETH_PHY_TYPE, eth_clock_mode_t clk_mode=ETH_CLK_MODE);
89 | //ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER, ETH_PHY_MDC, ETH_PHY_MDIO, ETH_PHY_TYPE, ETH_CLK_MODE);
90 | ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER);
91 |
92 | // Static IP, leave without this line to get IP via DHCP
93 | //bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
94 | ETH.config(myIP, myGW, mySN, myDNS);
95 |
96 | WT32_ETH01_waitForConnect();
97 |
98 | Udp.begin(localPort);
99 | }
100 |
101 | void loop()
102 | {
103 | sendNTPpacket(timeServer); // send an NTP packet to a time server
104 |
105 | // wait for a reply for UDP_TIMEOUT miliseconds
106 | unsigned long startMs = millis();
107 |
108 | while (!Udp.available() && (millis() - startMs) < UDP_TIMEOUT) {}
109 |
110 | // if there's data available, read a packet
111 | int packetSize = Udp.parsePacket();
112 |
113 | if (packetSize)
114 | {
115 | Serial.print(F("UDP Packet received, size "));
116 | Serial.println(packetSize);
117 | Serial.print(F("From "));
118 | IPAddress remoteIp = Udp.remoteIP();
119 | Serial.print(remoteIp);
120 | Serial.print(F(", port "));
121 | Serial.println(Udp.remotePort());
122 |
123 | // We've received a packet, read the data from it into the buffer
124 | Udp.read(packetBuffer, NTP_PACKET_SIZE);
125 |
126 | // the timestamp starts at byte 40 of the received packet and is four bytes,
127 | // or two words, long. First, esxtract the two words:
128 |
129 | unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
130 | unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
131 |
132 | // combine the four bytes (two words) into a long integer
133 | // this is NTP time (seconds since Jan 1 1900):
134 | unsigned long secsSince1900 = highWord << 16 | lowWord;
135 |
136 | Serial.print(F("Seconds since Jan 1 1900 = "));
137 | Serial.println(secsSince1900);
138 |
139 | // now convert NTP time into )everyday time:
140 | Serial.print(F("Unix time = "));
141 | // Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
142 | const unsigned long seventyYears = 2208988800UL;
143 | // subtract seventy years:
144 | unsigned long epoch = secsSince1900 - seventyYears;
145 | // print Unix time:
146 | Serial.println(epoch);
147 |
148 | // print the hour, minute and second:
149 | Serial.print(F("The UTC time is ")); // UTC is the time at Greenwich Meridian (GMT)
150 | Serial.print((epoch % 86400L) / 3600); // print the hour (86400 equals secs per day)
151 | Serial.print(F(":"));
152 |
153 | if (((epoch % 3600) / 60) < 10)
154 | {
155 | // In the first 10 minutes of each hour, we'll want a leading '0'
156 | Serial.print(F("0"));
157 | }
158 |
159 | Serial.print((epoch % 3600) / 60); // print the minute (3600 equals secs per minute)
160 | Serial.print(F(":"));
161 |
162 | if ((epoch % 60) < 10)
163 | {
164 | // In the first 10 seconds of each minute, we'll want a leading '0'
165 | Serial.print(F("0"));
166 | }
167 |
168 | Serial.println(epoch % 60); // print the second
169 | }
170 |
171 | // wait ten seconds before asking for the time again
172 | delay(10000);
173 | }
174 |
--------------------------------------------------------------------------------
/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 WT32_ETH01 (ESP32 + LAN8720)
5 |
6 | WebServer_WT32_ETH01 is a library for the Ethernet LAN8720 in WT32_ETH01 to run WebServer
7 |
8 | Based on and modified from ESP8266 https://github.com/esp8266/Arduino/releases
9 | Built by Khoi Hoang https://github.com/khoih-prog/WebServer_WT32_ETH01
10 | Licensed under MIT license
11 | *****************************************************************************************************************************/
12 |
13 | #define DEBUG_ETHERNET_WEBSERVER_PORT Serial
14 |
15 | // Debug Level from 0 to 4
16 | #define _ETHERNET_WEBSERVER_LOGLEVEL_ 3
17 |
18 | #include
19 |
20 | // Select the IP address according to your local network
21 | IPAddress myIP(192, 168, 2, 232);
22 | IPAddress myGW(192, 168, 2, 1);
23 | IPAddress mySN(255, 255, 255, 0);
24 |
25 | // Google DNS Server IP
26 | IPAddress myDNS(8, 8, 8, 8);
27 |
28 | unsigned int localPort = 1883; //10002; // local port to listen on
29 |
30 | char packetBuffer[255]; // buffer to hold incoming packet
31 | byte ReplyBuffer[] = "ACK"; // a string to send back
32 |
33 | // A UDP instance to let us send and receive packets over UDP
34 | WiFiUDP Udp;
35 |
36 | void setup()
37 | {
38 | Serial.begin(115200);
39 |
40 | while (!Serial);
41 |
42 | // Using this if Serial debugging is not necessary or not using Serial port
43 | //while (!Serial && (millis() < 3000));
44 |
45 | Serial.print("\nStarting UDPSendReceive on " + String(ARDUINO_BOARD));
46 | Serial.println(" with " + String(SHIELD_TYPE));
47 | Serial.println(WEBSERVER_WT32_ETH01_VERSION);
48 |
49 | // To be called before ETH.begin()
50 | WT32_ETH01_onEvent();
51 |
52 | //bool begin(uint8_t phy_addr=ETH_PHY_ADDR, int power=ETH_PHY_POWER, int mdc=ETH_PHY_MDC, int mdio=ETH_PHY_MDIO,
53 | // eth_phy_type_t type=ETH_PHY_TYPE, eth_clock_mode_t clk_mode=ETH_CLK_MODE);
54 | //ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER, ETH_PHY_MDC, ETH_PHY_MDIO, ETH_PHY_TYPE, ETH_CLK_MODE);
55 | ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER);
56 |
57 | // Static IP, leave without this line to get IP via DHCP
58 | //bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
59 | ETH.config(myIP, myGW, mySN, myDNS);
60 |
61 | WT32_ETH01_waitForConnect();
62 |
63 | Serial.println(F("\nStarting connection to server..."));
64 | // if you get a connection, report back via serial:
65 | Udp.begin(localPort);
66 |
67 | Serial.print(F("Listening on port "));
68 | Serial.println(localPort);
69 | }
70 |
71 | void loop()
72 | {
73 | // if there's data available, read a packet
74 | int packetSize = Udp.parsePacket();
75 |
76 | if (packetSize)
77 | {
78 | Serial.print(F("Received packet of size "));
79 | Serial.println(packetSize);
80 | Serial.print(F("From "));
81 | IPAddress remoteIp = Udp.remoteIP();
82 | Serial.print(remoteIp);
83 | Serial.print(F(", port "));
84 | Serial.println(Udp.remotePort());
85 |
86 | // read the packet into packetBufffer
87 | int len = Udp.read(packetBuffer, 255);
88 |
89 | if (len > 0)
90 | {
91 | packetBuffer[len] = 0;
92 | }
93 |
94 | Serial.println(F("Contents:"));
95 | Serial.println(packetBuffer);
96 |
97 | // send a reply, to the IP address and port that sent us the packet we received
98 | Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
99 | Udp.write(ReplyBuffer, sizeof(ReplyBuffer));
100 | Udp.endPacket();
101 | }
102 | }
103 |
--------------------------------------------------------------------------------
/examples/WebClient/WebClient.ino:
--------------------------------------------------------------------------------
1 | /****************************************************************************************************************************
2 | WebClient.ino - Simple Arduino web server sample for Ethernet shield
3 |
4 | For Ethernet shields using WT32_ETH01 (ESP32 + LAN8720)
5 |
6 | WebServer_WT32_ETH01 is a library for the Ethernet LAN8720 in WT32_ETH01 to run WebServer
7 |
8 | Based on and modified from ESP8266 https://github.com/esp8266/Arduino/releases
9 | Built by Khoi Hoang https://github.com/khoih-prog/WebServer_WT32_ETH01
10 | Licensed under MIT license
11 | *****************************************************************************************************************************/
12 |
13 | #define DEBUG_ETHERNET_WEBSERVER_PORT Serial
14 |
15 | // Debug Level from 0 to 4
16 | #define _ETHERNET_WEBSERVER_LOGLEVEL_ 3
17 |
18 | #include
19 |
20 | // Select the IP address according to your local network
21 | IPAddress myIP(192, 168, 2, 232);
22 | IPAddress myGW(192, 168, 2, 1);
23 | IPAddress mySN(255, 255, 255, 0);
24 |
25 | // Google DNS Server IP
26 | IPAddress myDNS(8, 8, 8, 8);
27 |
28 | char server[] = "arduino.cc";
29 |
30 | // Initialize the Ethernet client object
31 | WiFiClient client;
32 |
33 | void setup()
34 | {
35 | Serial.begin(115200);
36 |
37 | while (!Serial);
38 |
39 | // Using this if Serial debugging is not necessary or not using Serial port
40 | //while (!Serial && (millis() < 3000));
41 |
42 | Serial.print("\nStarting WebClient on " + String(ARDUINO_BOARD));
43 | Serial.println(" with " + String(SHIELD_TYPE));
44 | Serial.println(WEBSERVER_WT32_ETH01_VERSION);
45 |
46 | // To be called before ETH.begin()
47 | WT32_ETH01_onEvent();
48 |
49 | //bool begin(uint8_t phy_addr=ETH_PHY_ADDR, int power=ETH_PHY_POWER, int mdc=ETH_PHY_MDC, int mdio=ETH_PHY_MDIO,
50 | // eth_phy_type_t type=ETH_PHY_TYPE, eth_clock_mode_t clk_mode=ETH_CLK_MODE);
51 | //ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER, ETH_PHY_MDC, ETH_PHY_MDIO, ETH_PHY_TYPE, ETH_CLK_MODE);
52 | ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER);
53 |
54 | // Static IP, leave without this line to get IP via DHCP
55 | //bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
56 | ETH.config(myIP, myGW, mySN, myDNS);
57 |
58 | WT32_ETH01_waitForConnect();
59 |
60 | Serial.println();
61 | Serial.println(F("Starting connection to server..."));
62 |
63 | // if you get a connection, report back via serial
64 | if (client.connect(server, 80))
65 | {
66 | Serial.println(F("Connected to server"));
67 | // Make a HTTP request
68 | client.println(F("GET /asciilogo.txt HTTP/1.1"));
69 | client.println(F("Host: arduino.cc"));
70 | client.println(F("Connection: close"));
71 | client.println();
72 | }
73 | }
74 |
75 | void printoutData()
76 | {
77 | // if there are incoming bytes available
78 | // from the server, read them and print them
79 | while (client.available())
80 | {
81 | char c = client.read();
82 | Serial.write(c);
83 | Serial.flush();
84 | }
85 | }
86 |
87 | void loop()
88 | {
89 | printoutData();
90 |
91 | // if the server's disconnected, stop the client
92 | if (!client.connected())
93 | {
94 | Serial.println();
95 | Serial.println(F("Disconnecting from server..."));
96 | client.stop();
97 |
98 | // do nothing forevermore
99 | while (true)
100 | yield();
101 | }
102 | }
103 |
--------------------------------------------------------------------------------
/examples/WebClientRepeating/WebClientRepeating.ino:
--------------------------------------------------------------------------------
1 | /****************************************************************************************************************************
2 | WebClientRepeating.ino - Simple Arduino web server sample for Ethernet shield
3 |
4 | For Ethernet shields using WT32_ETH01 (ESP32 + LAN8720)
5 |
6 | WebServer_WT32_ETH01 is a library for the Ethernet LAN8720 in WT32_ETH01 to run WebServer
7 |
8 | Based on and modified from ESP8266 https://github.com/esp8266/Arduino/releases
9 | Built by Khoi Hoang https://github.com/khoih-prog/WebServer_WT32_ETH01
10 | Licensed under MIT license
11 | *****************************************************************************************************************************/
12 | /*
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 | #define DEBUG_ETHERNET_WEBSERVER_PORT Serial
19 |
20 | // Debug Level from 0 to 4
21 | #define _ETHERNET_WEBSERVER_LOGLEVEL_ 3
22 |
23 | #include
24 |
25 | // Select the IP address according to your local network
26 | IPAddress myIP(192, 168, 2, 232);
27 | IPAddress myGW(192, 168, 2, 1);
28 | IPAddress mySN(255, 255, 255, 0);
29 |
30 | // Google DNS Server IP
31 | IPAddress myDNS(8, 8, 8, 8);
32 |
33 | char server[] = "arduino.cc";
34 |
35 | unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
36 | const unsigned long postingInterval = 10000L; // delay between updates, in milliseconds
37 |
38 | // Initialize the Web client object
39 | WiFiClient client;
40 |
41 | // this method makes a HTTP connection to the server
42 | void httpRequest()
43 | {
44 | Serial.println();
45 |
46 | // close any connection before send a new request
47 | // this will free the socket on the WiFi shield
48 | client.stop();
49 |
50 | // if there's a successful connection
51 | if (client.connect(server, 80))
52 | {
53 | Serial.println(F("Connecting..."));
54 |
55 | // send the HTTP PUT request
56 | client.println(F("GET /asciilogo.txt HTTP/1.1"));
57 | client.println(F("Host: arduino.cc"));
58 | client.println(F("Connection: close"));
59 | client.println();
60 |
61 | // note the time that the connection was made
62 | lastConnectionTime = millis();
63 | }
64 | else
65 | {
66 | // if you couldn't make a connection
67 | Serial.println(F("Connection failed"));
68 | }
69 | }
70 |
71 | void setup()
72 | {
73 | Serial.begin(115200);
74 |
75 | while (!Serial);
76 |
77 | // Using this if Serial debugging is not necessary or not using Serial port
78 | //while (!Serial && (millis() < 3000));
79 |
80 | Serial.print("\nStarting WebClientRepeating on " + String(ARDUINO_BOARD));
81 | Serial.println(" with " + String(SHIELD_TYPE));
82 | Serial.println(WEBSERVER_WT32_ETH01_VERSION);
83 |
84 | // To be called before ETH.begin()
85 | WT32_ETH01_onEvent();
86 |
87 | //bool begin(uint8_t phy_addr=ETH_PHY_ADDR, int power=ETH_PHY_POWER, int mdc=ETH_PHY_MDC, int mdio=ETH_PHY_MDIO,
88 | // eth_phy_type_t type=ETH_PHY_TYPE, eth_clock_mode_t clk_mode=ETH_CLK_MODE);
89 | //ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER, ETH_PHY_MDC, ETH_PHY_MDIO, ETH_PHY_TYPE, ETH_CLK_MODE);
90 | ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER);
91 |
92 | // Static IP, leave without this line to get IP via DHCP
93 | //bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
94 | ETH.config(myIP, myGW, mySN, myDNS);
95 |
96 | WT32_ETH01_waitForConnect();
97 | }
98 |
99 | void loop()
100 | {
101 | // if there's incoming data from the net connection send it out the serial port
102 | // this is for debugging purposes only
103 | while (client.available())
104 | {
105 | char c = client.read();
106 | Serial.write(c);
107 | Serial.flush();
108 | }
109 |
110 | // if 10 seconds have passed since your last connection,
111 | // then connect again and send data
112 | if (millis() - lastConnectionTime > postingInterval)
113 | {
114 | httpRequest();
115 | }
116 | }
117 |
--------------------------------------------------------------------------------
/examples/WebServer/WebServer.ino:
--------------------------------------------------------------------------------
1 | /****************************************************************************************************************************
2 | WebServer.ino - Simple Arduino web server sample for Ethernet shield
3 |
4 | For Ethernet shields using WT32_ETH01 (ESP32 + LAN8720)
5 |
6 | WebServer_WT32_ETH01 is a library for the Ethernet LAN8720 in WT32_ETH01 to run WebServer
7 |
8 | Based on and modified from ESP8266 https://github.com/esp8266/Arduino/releases
9 | Built by Khoi Hoang https://github.com/khoih-prog/WebServer_WT32_ETH01
10 | Licensed under MIT license
11 | *****************************************************************************************************************************/
12 |
13 |
14 | #define DEBUG_ETHERNET_WEBSERVER_PORT Serial
15 |
16 | // Debug Level from 0 to 4
17 | #define _ETHERNET_WEBSERVER_LOGLEVEL_ 3
18 |
19 | #include
20 |
21 | WiFiServer server(80);
22 |
23 | // Select the IP address according to your local network
24 | IPAddress myIP(192, 168, 2, 232);
25 | IPAddress myGW(192, 168, 2, 1);
26 | IPAddress mySN(255, 255, 255, 0);
27 |
28 | // Google DNS Server IP
29 | IPAddress myDNS(8, 8, 8, 8);
30 |
31 | int reqCount = 0; // number of requests received
32 |
33 | void setup()
34 | {
35 | Serial.begin(115200);
36 |
37 | while (!Serial);
38 |
39 | // Using this if Serial debugging is not necessary or not using Serial port
40 | //while (!Serial && (millis() < 3000));
41 |
42 | Serial.print("\nStarting WebServer on " + String(ARDUINO_BOARD));
43 | Serial.println(" with " + String(SHIELD_TYPE));
44 | Serial.println(WEBSERVER_WT32_ETH01_VERSION);
45 |
46 | // To be called before ETH.begin()
47 | WT32_ETH01_onEvent();
48 |
49 | //bool begin(uint8_t phy_addr=ETH_PHY_ADDR, int power=ETH_PHY_POWER, int mdc=ETH_PHY_MDC, int mdio=ETH_PHY_MDIO,
50 | // eth_phy_type_t type=ETH_PHY_TYPE, eth_clock_mode_t clk_mode=ETH_CLK_MODE);
51 | //ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER, ETH_PHY_MDC, ETH_PHY_MDIO, ETH_PHY_TYPE, ETH_CLK_MODE);
52 | ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER);
53 |
54 | // Static IP, leave without this line to get IP via DHCP
55 | //bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
56 | ETH.config(myIP, myGW, mySN, myDNS);
57 |
58 | WT32_ETH01_waitForConnect();
59 |
60 | // start the web server on port 80
61 | server.begin();
62 | }
63 |
64 | void loop()
65 | {
66 | // listen for incoming clients
67 | WiFiClient client = server.available();
68 |
69 | if (client)
70 | {
71 | Serial.println(F("New client"));
72 | // an http request ends with a blank line
73 | bool currentLineIsBlank = true;
74 |
75 | while (client.connected())
76 | {
77 | if (client.available())
78 | {
79 | char c = client.read();
80 | Serial.write(c);
81 |
82 | // if you've gotten to the end of the line (received a newline
83 | // character) and the line is blank, the http request has ended,
84 | // so you can send a reply
85 | if (c == '\n' && currentLineIsBlank)
86 | {
87 | Serial.println(F("Sending response"));
88 |
89 | // send a standard http response header
90 | // use \r\n instead of many println statements to speedup data send
91 | client.print(
92 | "HTTP/1.1 200 OK\r\n"
93 | "Content-Type: text/html\r\n"
94 | "Connection: close\r\n" // the connection will be closed after completion of the response
95 | "Refresh: 20\r\n" // refresh the page automatically every 20 sec
96 | "\r\n");
97 | client.print("\r\n");
98 | client.print("\r\n");
99 | client.print(String("
Hello World from ") + BOARD_NAME + "!
\r\n");
100 | client.print("Requests received: ");
101 | client.print(++reqCount);
102 | client.print(" \r\n");
103 | client.print(" \r\n");
104 | client.print("\r\n");
105 | break;
106 | }
107 |
108 | if (c == '\n')
109 | {
110 | // you're starting a new line
111 | currentLineIsBlank = true;
112 | }
113 | else if (c != '\r')
114 | {
115 | // you've gotten a character on the current line
116 | currentLineIsBlank = false;
117 | }
118 | }
119 | }
120 |
121 | // give the web browser time to receive the data
122 | delay(10);
123 |
124 | // close the connection:
125 | client.stop();
126 | Serial.println(F("Client disconnected"));
127 | }
128 | }
129 |
--------------------------------------------------------------------------------
/examples/multiFileProject/multiFileProject.cpp:
--------------------------------------------------------------------------------
1 | /****************************************************************************************************************************
2 | multiFileProject.cpp
3 | For Ethernet shields using WT32_ETH01 (ESP32 + LAN8720)
4 |
5 | WebServer_WT32_ETH01 is a library for the Ethernet LAN8720 in WT32_ETH01 to run WebServer
6 |
7 | Based on and modified from ESP8266 https://github.com/esp8266/Arduino/releases
8 | Built by Khoi Hoang https://github.com/khoih-prog/WebServer_WT32_ETH01
9 | Licensed under MIT 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 WT32_ETH01 (ESP32 + LAN8720)
4 |
5 | WebServer_WT32_ETH01 is a library for the Ethernet LAN8720 in WT32_ETH01 to run WebServer
6 |
7 | Based on and modified from ESP8266 https://github.com/esp8266/Arduino/releases
8 | Built by Khoi Hoang https://github.com/khoih-prog/WebServer_WT32_ETH01
9 | Licensed under MIT 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 | // Can be included as many times as necessary, without `Multiple Definitions` Linker Error
19 | #include "WebServer_WT32_ETH01.hpp"
20 |
--------------------------------------------------------------------------------
/examples/multiFileProject/multiFileProject.ino:
--------------------------------------------------------------------------------
1 | /****************************************************************************************************************************
2 | multiFileProject.ino
3 | For Ethernet shields using WT32_ETH01 (ESP32 + LAN8720)
4 |
5 | WebServer_WT32_ETH01 is a library for the Ethernet LAN8720 in WT32_ETH01 to run WebServer
6 |
7 | Based on and modified from ESP8266 https://github.com/esp8266/Arduino/releases
8 | Built by Khoi Hoang https://github.com/khoih-prog/WebServer_WT32_ETH01
9 | Licensed under MIT 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_WT32_ETH01.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_WT32_ETH01_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 |
--------------------------------------------------------------------------------
/examples/serveStatic/data/page1.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 | Hello from Ethernet HTTP Web Server ESP8266/ESP32 - Page1
10 |
11 |
12 |
13 |
14 |
ESP8266/ESP32 Ethernet HTTP Web Server
15 |
Hello from ESP8266/ESP32 Ethernet HTTP Web Server. This is Page1.