-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTAMUHackathon.ino
More file actions
172 lines (149 loc) · 3.65 KB
/
TAMUHackathon.ino
File metadata and controls
172 lines (149 loc) · 3.65 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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
TAMU Hackathon 2018
ESP8266 based webserver and Webform
*/
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
// Fill in your WiFi router SSID and password
const char* ssid = "esptest";
const char* password = "12345678";
MDNSResponder mdns;
ESP8266WebServer server(80);
const char INDEX_HTML[] =
"<!DOCTYPE HTML>"
"<html>"
"<head>"
"<meta name = \"viewport\" content = \"width = device-width, initial-scale = 1.0, maximum-scale = 1.0, user-scalable=0\">"
"<title>ESP8266 Web Form Demo</title>"
"<style>"
"\"body { background-color: #808080; font-family: Arial, Helvetica, Sans-Serif; Color: #000000; }\""
"</style>"
"</head>"
"<body>"
"<h1>ESP8266 Web Form Demo</h1>"
"<FORM action=\"/\" method=\"post\">"
"<P>"
"LED<br>"
"<INPUT type=\"text\" name=\"LED\" value=\"\">On<br>"
"<INPUT type=\"submit\" value=\"Send\"> <INPUT type=\"reset\">"
"</P>"
"</FORM>"
"</body>"
"</html>";
// GPIO#0 is for Adafruit ESP8266 HUZZAH board. Your board LED might be on 13.
const int LEDPIN = 4;
void handleRoot()
{
if (server.hasArg("LED")) {
handleSubmit();
}
else {
server.send(200, "text/html", INDEX_HTML);
}
}
void returnFail(String msg)
{
server.sendHeader("Connection", "close");
server.sendHeader("Access-Control-Allow-Origin", "*");
server.send(500, "text/plain", msg + "\r\n");
}
void handleSubmit()
{
String LEDvalue;
if (!server.hasArg("LED")) return returnFail("BAD ARGS");
LEDvalue = server.arg("LED");
if (LEDvalue == "on") {
writeLED(true);
server.send(200, "text/html", INDEX_HTML);
}
else if (LEDvalue == "off") {
writeLED(false);
server.send(200, "text/html", INDEX_HTML);
}
else {
returnFail("Bad LED value");
}
}
void returnOK()
{
server.sendHeader("Connection", "close");
server.sendHeader("Access-Control-Allow-Origin", "*");
server.send(200, "text/plain", "OK\r\n");
}
/*
* Imperative to turn the LED on using a non-browser http client.
* For example, using wget.
* $ wget http://esp8266webform/ledon
*/
void handleLEDon()
{
writeLED(true);
returnOK();
}
/*
* Imperative to turn the LED off using a non-browser http client.
* For example, using wget.
* $ wget http://esp8266webform/ledoff
*/
void handleLEDoff()
{
writeLED(false);
returnOK();
}
void handleNotFound()
{
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);
}
void writeLED(bool LEDon)
{
// Note inverted logic for Adafruit HUZZAH board
if (LEDon)
digitalWrite(LEDPIN, 0);
else
digitalWrite(LEDPIN, 1);
}
void setup(void)
{
pinMode(LEDPIN, OUTPUT);
writeLED(false);
Serial.begin(115200);
WiFi.softAP(ssid, password);
Serial.println("");
// Wait for connection
// while (WiFi.status() != WL_CONNECTED) {
// delay(500);
// Serial.print(".");
// }
Serial.println("");
Serial.print("Connect to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.softAPIP());
// if (mdns.begin("esp8266WebForm", WiFi.softAPIP())) {
// Serial.println("MDNS responder started");
// }
server.on("/", handleRoot);
server.on("/ledon", handleLEDon);
server.on("/ledoff", handleLEDoff);
server.onNotFound(handleNotFound);
server.begin();
Serial.print("Connect to http://esp8266WebForm.local or http://");
Serial.println(WiFi.localIP());
}
void loop(void)
{
server.handleClient();
}