-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added line mode, as described in #17
- Loading branch information
1 parent
c55ce71
commit 68e336f
Showing
6 changed files
with
195 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
/* ------------------------------------------------- */ | ||
|
||
#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); | ||
#if defined(ARDUINO_ARCH_ESP8266) | ||
WiFi.forceSleepWake(); | ||
delay(200); | ||
#endif | ||
WiFi.begin(ssid, password); | ||
do { | ||
delay(pause); | ||
Serial.print("."); | ||
} 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); | ||
|
||
// passing a lambda function | ||
telnet.onInputReceived([](String str) { | ||
Serial.print(str); | ||
}); | ||
|
||
telnet.setLineMode(false); | ||
Serial.print("- Telnet Line Mode: "); Serial.println(telnet.isLineModeSet() ? "YES" : "NO"); | ||
|
||
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 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()); | ||
} | ||
} | ||
//* ------------------------------------------------- */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters