-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathHelloGetArgument.ino
More file actions
94 lines (76 loc) · 2.86 KB
/
HelloGetArgument.ino
File metadata and controls
94 lines (76 loc) · 2.86 KB
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
#include <ArduinoHttpServer.h>
#include <Arduino.h>
#ifdef ESP8266 // This example is compatible with both, ATMega and ESP8266
#include <ESP8266WiFi.h>
#else
#include <SPI.h> //! \todo Temporary see fix: https://github.com/platformio/platformio/issues/48
// on older Arduinos use WiFi.h, on Arduino Uno R4 WiFi use WiFiS3.h
#include <WiFiS3.h>
#endif
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASS";
WiFiServer wifiServer(80);
void setup()
{
Serial.begin(115200);
Serial.println("Starting Wifi Connection...");
WiFi.begin(const_cast<char*>(ssid), password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
}
Serial.println("Connected!");
wifiServer.begin();
}
void loop()
{
WiFiClient client( wifiServer.available() );
if (client.connected())
{
Serial.println("client connected!");
// Connected to client. Allocate and initialize StreamHttpRequest object.
ArduinoHttpServer::StreamHttpRequest<1024> httpRequest(client);
if (httpRequest.readRequest())
{
Serial.println("Done read request!");
// Retrieve HTTP resource / URL requested
Serial.println( httpRequest.getResource().toString() );
// Retrieve 1st part of HTTP resource.
// E.g.: "api" from "/api/sensors/on"
Serial.println(httpRequest.getResource()[0]);
ArduinoHttpServer::StreamHttpReply httpReply(client, "text/html");
String textField = httpRequest.getResource().getArgument("textfield");
Serial.print("Textfield is: ");
Serial.println(textField);
String reply = "<!DOCTYPE html>\n"
"<html>\n"
"<head>\n"
" <title>Minimal HTML Page</title>\n"
"</head>\n"
"<body>\n";
if(textField.length() > 0) {
reply += "<h3> Hello, ";
reply += textField;
reply += "!</h3>";
}
reply += " <form>\n"
" <label for=\"textfield\">Enter text:</label><br>\n"
" <input type=\"text\" id=\"textfield\" name=\"textfield\"><br><br>\n"
" <input type=\"submit\" value=\"Submit\">\n"
" </form>\n"
"</body>\n"
"</html>";
httpReply.send(reply);
}
else
{
// HTTP parsing failed. Client did not provide correct HTTP data or
// client requested an unsupported feature.
ArduinoHttpServer::StreamHttpErrorReply httpReply(client, httpRequest.getContentType());
const char *pErrorStr( httpRequest.getError().cStr() );
String errorStr(pErrorStr); //! \todo Make HttpReply FixString compatible.
httpReply.send( errorStr );
}
client.stop();
}
}