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

Commit 3226dc8

Browse files
authored
Add examples for WT32_ETH01
1 parent d2d0b98 commit 3226dc8

39 files changed

+4084
-0
lines changed
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
/**
2+
Example for the WT32_ETH01 HTTP(S) Webserver
3+
4+
IMPORTANT NOTE:
5+
To run this script, your need to
6+
1) Make sure to have certificate data available. You will find a
7+
shell script (create_cert.sh) and instructions to do so in the library folder
8+
under extras/
9+
10+
This script will install an HTTPS Server on your WT32_ETH01 with the following
11+
functionalities:
12+
- Show simple page on web server root
13+
- 404 for everything else
14+
The server will be run in a separate task, so that you can do your own stuff
15+
in the loop() function.
16+
Everything else is just like the Static-Page example
17+
*/
18+
19+
/** Check if we have multiple cores */
20+
#if CONFIG_FREERTOS_UNICORE
21+
#define ARDUINO_RUNNING_CORE 0
22+
#else
23+
#define ARDUINO_RUNNING_CORE 1
24+
#endif
25+
26+
// Include certificate data (see note above)
27+
#include "cert.h"
28+
#include "private_key.h"
29+
30+
//////////////////////////////////////////////////
31+
32+
// For WT32_ETH01
33+
#define DEBUG_ETHERNET_WEBSERVER_PORT Serial
34+
35+
// Debug Level from 0 to 4
36+
#define _ETHERNET_WEBSERVER_LOGLEVEL_ 3
37+
38+
#include <WebServer_WT32_ETH01.h>
39+
40+
// Select the IP address according to your local network
41+
IPAddress myIP(192, 168, 2, 232);
42+
IPAddress myGW(192, 168, 2, 1);
43+
IPAddress mySN(255, 255, 255, 0);
44+
45+
// Google DNS Server IP
46+
IPAddress myDNS(8, 8, 8, 8);
47+
48+
//////////////////////////////////////////////////
49+
50+
// Includes for the server
51+
#include <HTTPSServer.hpp>
52+
#include <SSLCert.hpp>
53+
#include <HTTPRequest.hpp>
54+
#include <HTTPResponse.hpp>
55+
56+
// The HTTPS Server comes in a separate namespace. For easier use, include it here.
57+
using namespace httpsserver;
58+
59+
// Create an SSL certificate object from the files included above
60+
SSLCert cert = SSLCert(
61+
example_crt_DER, example_crt_DER_len,
62+
example_key_DER, example_key_DER_len
63+
);
64+
65+
// Create an SSL-enabled server that uses the certificate
66+
HTTPSServer secureServer = HTTPSServer(&cert);
67+
68+
void serverTask(void *params)
69+
{
70+
// In the separate task we first do everything that we would have done in the
71+
// setup() function, if we would run the server synchronously.
72+
73+
// Note: The second task has its own stack, so you need to think about where
74+
// you create the server's resources and how to make sure that the server
75+
// can access everything it needs to access. Also make sure that concurrent
76+
// access is no problem in your sketch or implement countermeasures like locks
77+
// or mutexes.
78+
79+
// Create nodes
80+
ResourceNode * nodeRoot = new ResourceNode("/", "GET", &handleRoot);
81+
ResourceNode * node404 = new ResourceNode("", "GET", &handle404);
82+
83+
// Add nodes to the server
84+
secureServer.registerNode(nodeRoot);
85+
secureServer.setDefaultNode(node404);
86+
87+
Serial.println("Starting server...");
88+
secureServer.start();
89+
90+
if (secureServer.isRunning())
91+
{
92+
Serial.println("Server ready.");
93+
94+
// "loop()" function of the separate task
95+
while (true)
96+
{
97+
// This call will let the server do its work
98+
secureServer.loop();
99+
100+
// Other code would go here...
101+
delay(1);
102+
}
103+
}
104+
}
105+
106+
void handleRoot(HTTPRequest * req, HTTPResponse * res)
107+
{
108+
// Status code is 200 OK by default.
109+
// We want to deliver a simple HTML page, so we send a corresponding content type:
110+
res->setHeader("Content-Type", "text/html");
111+
112+
// The response implements the Print interface, so you can use it just like
113+
// you would write to Serial etc.
114+
res->println("<!DOCTYPE html>");
115+
res->println("<html>");
116+
res->println("<head><title>Hello World!</title></head>");
117+
res->println("<body>");
118+
res->println("<h1>Hello World!</h1>");
119+
res->print("<p>Your server is running for ");
120+
// A bit of dynamic data: Show the uptime
121+
res->print((int)(millis() / 1000), DEC);
122+
res->println(" seconds.</p>");
123+
res->println("</body>");
124+
res->println("</html>");
125+
}
126+
127+
void handle404(HTTPRequest * req, HTTPResponse * res)
128+
{
129+
// Discard request body, if we received any
130+
// We do this, as this is the default node and may also server POST/PUT requests
131+
req->discardRequestBody();
132+
133+
// Set the response status
134+
res->setStatusCode(404);
135+
res->setStatusText("Not Found");
136+
137+
// Set content type of the response
138+
res->setHeader("Content-Type", "text/html");
139+
140+
// Write a tiny HTTP page
141+
res->println("<!DOCTYPE html>");
142+
res->println("<html>");
143+
res->println("<head><title>Not Found</title></head>");
144+
res->println("<body><h1>404 Not Found</h1><p>The requested resource was not found on this server.</p></body>");
145+
res->println("</html>");
146+
}
147+
148+
void setup()
149+
{
150+
// For logging
151+
Serial.begin(115200);
152+
while (!Serial && millis() < 5000);
153+
154+
///////////////////////////////////////////////
155+
156+
Serial.print("\nStarting Async_Server on " + String(ARDUINO_BOARD));
157+
Serial.println(" with " + String(SHIELD_TYPE));
158+
Serial.println(WEBSERVER_WT32_ETH01_VERSION);
159+
160+
// To be called before ETH.begin()
161+
WT32_ETH01_onEvent();
162+
163+
//bool begin(uint8_t phy_addr=ETH_PHY_ADDR, int power=ETH_PHY_POWER, int mdc=ETH_PHY_MDC, int mdio=ETH_PHY_MDIO,
164+
// eth_phy_type_t type=ETH_PHY_TYPE, eth_clock_mode_t clk_mode=ETH_CLK_MODE);
165+
//ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER, ETH_PHY_MDC, ETH_PHY_MDIO, ETH_PHY_TYPE, ETH_CLK_MODE);
166+
ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER);
167+
168+
// Static IP, leave without this line to get IP via DHCP
169+
//bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
170+
ETH.config(myIP, myGW, mySN, myDNS);
171+
172+
WT32_ETH01_waitForConnect();
173+
174+
Serial.print(F("HTTP EthernetWebServer is @ IP : "));
175+
Serial.println(ETH.localIP());
176+
177+
///////////////////////////////////////////////
178+
179+
// Setup the server as a separate task.
180+
Serial.println("Creating server task... ");
181+
// We pass:
182+
// serverTask - the function that should be run as separate task
183+
// "https443" - a name for the task (mainly used for logging)
184+
// 6144 - stack size in byte. If you want up to four clients, you should
185+
// not go below 6kB. If your stack is too small, you will encounter
186+
// Panic and stack canary exceptions, usually during the call to
187+
// SSL_accept.
188+
xTaskCreatePinnedToCore(serverTask, "https443", 6144, NULL, 1, NULL, ARDUINO_RUNNING_CORE);
189+
}
190+
191+
void loop()
192+
{
193+
Serial.println("loop()");
194+
delay(5000);
195+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#ifndef CERT_H_
2+
#define CERT_H_
3+
#error You have to run the srcipt extras/create_cert.sh to recreate these files
4+
#endif
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#ifndef PRIVATE_KEY_H_
2+
#define PRIVATE_KEY_H_
3+
#error You have to run the srcipt extras/create_cert.sh to recreate these files
4+
#endif

0 commit comments

Comments
 (0)