-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathTelnetServerWithAutoconnect.ino
167 lines (132 loc) · 3.78 KB
/
TelnetServerWithAutoconnect.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
156
157
158
159
160
161
162
163
164
165
166
167
/* ------------------------------------------------- */
#include "ESPTelnet.h"
#include "AutoConnect.h"
/* ------------------------------------------------- */
#define SERIAL_SPEED 9600
// AP-PASSWORD 12345678
/* ------------------------------------------------- */
ESPTelnet telnet;
IPAddress ip;
WebServer server;
AutoConnect portal(server);
/* ------------------------------------------------- */
void setupSerial(long speed, String msg = "") {
Serial.begin(speed);
while (!Serial) {
}
delay(200);
Serial.println();
Serial.println();
if (msg != "") Serial.println(msg);
}
/* ------------------------------------------------- */
bool isConnected() {
return (WiFi.status() == WL_CONNECTED);
}
/* ------------------------------------------------- */
static void rootPage() {
char content[] = "Welcome to ESP Telnet";
server.send(200, "text/plain", content);
}
/* ------------------------------------------------- */
void errorMsg(String error, bool restart = true) {
Serial.println(error);
if (restart) {
Serial.println("Rebooting now...");
delay(2000);
ESP.restart();
delay(2000);
}
}
/* ------------------------------------------------- */
// Type your browser:http://{{local_ip_address}}/_ac
void useAutoConnect() {
server.on("/", rootPage);
if (portal.begin()) {
ip = WiFi.localIP();
Serial.println("WiFi connected: " + WiFi.localIP().toString());
setupTelnet();
} else {
Serial.println();
errorMsg("Error connecting to WiFi");
}
}
/* ------------------------------------------------- */
void setupTelnet() {
// passing on functions for various telnet events
telnet.onConnect(onTelnetConnect);
telnet.onConnectionAttempt(onTelnetConnectionAttempt);
telnet.onReconnect(onTelnetReconnect);
telnet.onDisconnect(onTelnetDisconnect);
telnet.onInputReceived(onInput);
Serial.print("- Telnet: ");
if (telnet.begin()) {
Serial.println("running");
} else {
Serial.println("error.");
errorMsg("Will reboot...");
}
}
/* ------------------------------------------------- */
void onInput(String str) {
// checks for a certain command
if (str == "ping") {
telnet.println("> pong");
Serial.println("- Telnet: pong");
// disconnect the client
} else if (str == "bye") {
telnet.println("> disconnecting you...");
telnet.disconnectClient();
}
}
/* ------------------------------------------------- */
// (optional) callback functions for telnet events
void onTelnetConnect(String ip) {
Serial.print("- Telnet: ");
Serial.print(ip);
Serial.println(" connected");
telnet.println("\nWelcome " + telnet.getIP());
telnet.println("(Use ^] + q to disconnect.)");
}
/* ------------------------------------------------- */
void onTelnetDisconnect(String ip) {
Serial.print("- Telnet: ");
Serial.print(ip);
Serial.println(" disconnected");
}
/* ------------------------------------------------- */
void onTelnetReconnect(String ip) {
Serial.print("- Telnet: ");
Serial.print(ip);
Serial.println(" reconnected");
}
/* ------------------------------------------------- */
void onTelnetConnectionAttempt(String ip) {
Serial.print("- Telnet: ");
Serial.print(ip);
Serial.println(" tried to connected");
}
/* ------------------------------------------------- */
void setup() {
setupSerial(SERIAL_SPEED, "Telnet Test");
Serial.print("- Wifi: ");
useAutoConnect();
if (isConnected()) {
ip = WiFi.localIP();
Serial.print(" ");
Serial.println(ip);
setupTelnet();
} else {
Serial.println();
errorMsg("Error connecting to WiFi");
}
}
/* ------------------------------------------------- */
void loop() {
portal.handleClient();
telnet.loop();
if (Serial.available()) {
telnet.print(Serial.read());
}
}
/* ------------------------------------------------- */