-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
TelnetServerExample.ino
executable file
·165 lines (132 loc) · 3.72 KB
/
TelnetServerExample.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
/* ------------------------------------------------- */
#include <Arduino.h>
#include "ESPTelnet.h"
/* ------------------------------------------------- */
#define SERIAL_SPEED 9600
#define WIFI_SSID "MY SSID"
#define WIFI_PASSWORD "MY PASS"
/* ------------------------------------------------- */
ESPTelnet telnet;
IPAddress ip;
uint16_t port = 23;
/* ------------------------------------------------- */
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);
}
/* ------------------------------------------------- */
bool connectToWiFi(const char* ssid, const char* password, int max_tries = 20, int pause = 500) {
int i = 0;
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
#if defined(ARDUINO_ARCH_ESP8266)
WiFi.forceSleepWake();
delay(200);
#endif
WiFi.begin(ssid, password);
do {
delay(pause);
Serial.print(".");
i++;
} while (!isConnected() && i < max_tries);
WiFi.setAutoReconnect(true);
WiFi.persistent(true);
return isConnected();
}
/* ------------------------------------------------- */
void errorMsg(String error, bool restart = true) {
Serial.println(error);
if (restart) {
Serial.println("Rebooting now...");
delay(2000);
ESP.restart();
delay(2000);
}
}
/* ------------------------------------------------- */
void setupTelnet() {
// passing on functions for various telnet events
telnet.onConnect(onTelnetConnect);
telnet.onConnectionAttempt(onTelnetConnectionAttempt);
telnet.onReconnect(onTelnetReconnect);
telnet.onDisconnect(onTelnetDisconnect);
telnet.onInputReceived(onTelnetInput);
Serial.print("- Telnet: ");
if (telnet.begin(port)) {
Serial.println("running");
} else {
Serial.println("error.");
errorMsg("Will reboot...");
}
}
/* ------------------------------------------------- */
// (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 onTelnetInput(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();
} else {
telnet.println(str);
}
}
/* ------------------------------------------------- */
void setup() {
setupSerial(SERIAL_SPEED, "Telnet Test");
Serial.print("- Wifi: ");
connectToWiFi(WIFI_SSID, WIFI_PASSWORD);
if (isConnected()) {
ip = WiFi.localIP();
Serial.println();
Serial.print("- Telnet: "); Serial.print(ip); Serial.print(":"); Serial.println(port);
setupTelnet();
} else {
Serial.println();
errorMsg("Error connecting to WiFi");
}
}
/* ------------------------------------------------- */
void loop() {
telnet.loop();
// send serial input to telnet as output
if (Serial.available()) {
telnet.print(Serial.read());
}
}
//* ------------------------------------------------- */