This repository has been archived by the owner on Jan 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathPostServer_LAN8720.ino
155 lines (129 loc) · 4.27 KB
/
PostServer_LAN8720.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/****************************************************************************************************************************
PostServer_LAN8720.h - Dead simple web-server for Ethernet shields
For STM32 with built-in Ethernet LAN8742A (Nucleo-144, DISCOVERY, etc) or W5x00/ENC28J60 shield/module
EthernetWebServer_STM32 is a library for the STM32 running Ethernet WebServer
Based on and modified from ESP8266 https://github.com/esp8266/Arduino/releases
Built by Khoi Hoang https://github.com/khoih-prog/EthernetWebServer_STM32
Licensed under MIT license
*****************************************************************************************************************************/
/*
Currently support
1) STM32 boards with built-in Ethernet (to use USE_BUILTIN_ETHERNET = true) such as :
- Nucleo-144 (F429ZI, F767ZI)
- Discovery (STM32F746G-DISCOVERY)
- STM32 boards (STM32F/L/H/G/WB/MP1) with 32K+ Flash, with Built-in Ethernet,
- See How To Use Built-in Ethernet at (https://github.com/khoih-prog/EthernetWebServer_STM32/issues/1)
2) STM32F/L/H/G/WB/MP1 boards (with 32+K Flash) running ENC28J60 shields (to use USE_BUILTIN_ETHERNET = false)
3) STM32F/L/H/G/WB/MP1 boards (with 32+K Flash) running W5x00 shields
*/
#include "defines.h"
EthernetWebServer server(80);
const int led = 13;
const String postForms =
"<html>\
<head>\
<title>EthernetWebServer POST handling</title>\
<style>\
body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
</style>\
</head>\
<body>\
<h1>POST plain text to /postplain/</h1><br>\
<form method=\"post\" enctype=\"text/plain\" action=\"/postplain/\">\
<input type=\"text\" name=\'{\"hello\": \"world\", \"trash\": \"\' value=\'\"}\'><br>\
<input type=\"submit\" value=\"Submit\">\
</form>\
<h1>POST form data to /postform/</h1><br>\
<form method=\"post\" enctype=\"application/x-www-form-urlencoded\" action=\"/postform/\">\
<input type=\"text\" name=\"hello\" value=\"world\"><br>\
<input type=\"submit\" value=\"Submit\">\
</form>\
</body>\
</html>";
void handleRoot()
{
digitalWrite(led, 1);
server.send(200, "text/html", postForms);
digitalWrite(led, 0);
}
void handlePlain()
{
if (server.method() != HTTP_POST)
{
digitalWrite(led, 1);
server.send(405, "text/plain", "Method Not Allowed");
digitalWrite(led, 0);
}
else
{
digitalWrite(led, 1);
server.send(200, "text/plain", "POST body was:\n" + server.arg("plain"));
digitalWrite(led, 0);
}
}
void handleForm()
{
if (server.method() != HTTP_POST)
{
digitalWrite(led, 1);
server.send(405, "text/plain", "Method Not Allowed");
digitalWrite(led, 0);
}
else
{
digitalWrite(led, 1);
String message = "POST form was:\n";
for (uint8_t i = 0; i < server.args(); i++)
{
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(200, "text/plain", message);
digitalWrite(led, 0);
}
}
void handleNotFound()
{
digitalWrite(led, 1);
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++)
{
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
digitalWrite(led, 0);
}
void setup()
{
pinMode(led, OUTPUT);
digitalWrite(led, 0);
Serial.begin(115200);
delay(2000);
Serial.println("\nStart POSTServer_LAN8720 on " + String(BOARD_NAME) + ", using " + String(SHIELD_TYPE));
Serial.println(ETHERNET_WEBSERVER_STM32_VERSION);
// start the ethernet connection and the server:
// Use DHCP dynamic IP and random mac
uint16_t index = millis() % NUMBER_OF_MAC;
// Use Static IP
//Ethernet.begin(mac[index], ip);
Ethernet.begin(mac[index]);
Serial.print(F("Connected! IP address: "));
Serial.println(Ethernet.localIP());
server.on("/", handleRoot);
server.on("/postplain/", handlePlain);
server.on("/postform/", handleForm);
server.onNotFound(handleNotFound);
server.begin();
Serial.print(F("HTTP EthernetWebServer started @ IP : "));
Serial.println(Ethernet.localIP());
}
void loop()
{
server.handleClient();
}