-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesp32_telnet.ino
213 lines (162 loc) · 4.59 KB
/
esp32_telnet.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/* ------------------------------------------------- */
#include <Arduino.h>
#include "ESPTelnet.h"
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
#define SER_BUF_SIZE 1024
/* ------------------------------------------------- */
#define SERIAL_SPEED 9600
#define WIFI_SSID "YOUR_SSID"
#define WIFI_PASSWORD "YOUR_PASSWORD"
/* ------------------------------------------------- */
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);
Serial2.println(str);
delay(10);
}
}
/* ------------------------------------------------- */
void setup() {
lcd.init();
lcd.backlight();
// Print a message to the LCD.
lcd.setCursor(0,0);
lcd.print("Getting IP ...");
setupSerial(SERIAL_SPEED, "Telnet Test");
Serial2.begin(9600);
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);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("IP:");
lcd.setCursor(4, 0);
lcd.print(ip);
lcd.setCursor(0, 1);
lcd.print("Port:");
lcd.setCursor(6, 1);
lcd.print(port);
setupTelnet();
} else {
Serial.println();
errorMsg("Error connecting to WiFi");
}
}
/* ------------------------------------------------- */
void loop() {
telnet.loop();
// send serial input to telnet as output
String contentFromC = "";
char characterFromC;
if (Serial2.available() > 0)
{
//characterFromC = Serial2.read();
//contentFromC.concat(characterFromC);
//delay (10);
//Serial.println(contentFromC);
//telnet.println(contentFromC);
int inByte = Serial2.read();
//Serial.write(inByte);
telnet.write(inByte);
}
}
//* ------------------------------------------------- */