-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathturtle-esp8266.ino
113 lines (98 loc) · 2.24 KB
/
turtle-esp8266.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
#include <ESP8266WebServer.h>
#include <ESP8266WiFi.h>
#define DEBUG 0
ESP8266WebServer web_server(80);
bool web_client_is_sta() {
return web_server.client().localIP() == WiFi.localIP();
}
bool web_client_is_ap() {
return web_server.client().localIP() == WiFi.softAPIP();
}
bool str_isprint(String s) {
for (int i = 0; i < s.length(); i++)
if (!isprint(s[i]))
return false;
return true;
}
void handle_http_not_found() {
web_server.send(404, "text/plain", "Not Found");
}
const char html_root[] = { "\
<html\
<head>\
<title>ESP8266 Turtle</title>\
</head>\
<body>\
<form method=\"POST\" action=\"/\">\
Command:\
<br/>\
<textarea name=\"command\" rows=\"20\" cols=\"80\"></textarea>\
<br/>\
<input type=\"submit\" value=\"Execute\"/>\
</form>\
</body>\
</html>\
"
};
void handle_http_root() {
#if DEBUG
Serial.println("handle_http_root()");
#endif
if (web_server.hasArg("command")) {
String command = web_server.arg("command");
if (command.length()) {
command.replace("\r", "");
if (!command.endsWith("\n"))
command.concat("\n");
const char *c = command.c_str();
while (*c) {
Serial.print(*c);
if (*c == '\n')
Serial.readStringUntil(':');
c++;
}
}
}
web_server.send(200, "text/html", html_root);
#if DEBUG
Serial.println("handle_http_root() DONE");
#endif
}
void setup() {
Serial.begin(57600);
#if DEBUG
delay(250);
Serial.println("");
Serial.println("setup()");
#endif
WiFi.mode(WIFI_AP);
WiFi.softAPConfig(IPAddress(192, 168, 50, 1), IPAddress(192, 168, 50, 1), IPAddress(255, 255, 255, 0));
WiFi.softAP("ESP-turtle", "ESP-turtle");
Serial.setTimeout(30000);
while (Serial.available())
Serial.read();
Serial.print("0\n");
Serial.readStringUntil(':');
web_server.onNotFound(handle_http_not_found);
web_server.on("/", HTTP_ANY, handle_http_root);
web_server.begin();
#if DEBUG
Serial.println("setup() DONE");
#endif
}
void loop() {
#if DEBUG
{
static unsigned long tl;
unsigned long t;
t = millis();
if (t - tl > 1000) {
tl = t;
Serial.println("loop()");
Serial.print("AP IP: ");
Serial.println(WiFi.softAPIP().toString());
}
}
#endif
web_server.handleClient();
}