diff --git a/README.md b/README.md
index b04de91..d33bfdf 100644
--- a/README.md
+++ b/README.md
@@ -1,46 +1,6 @@
-# Arduino-Telnet-Client
-a minimal (but real) telnet client for Arduino, actually able to login and send commands to a generic telnet server.
+# ESP8266-Telnet-Client
-
Getting Started:
-It's a normal Arduino library...so just install it and take a look at the example provided to understand how to use it!
-how to install an Arduino library
+Based on the excellent work of @alejho. (https://github.com/alejho)
-Useful tips:
+https://github.com/alejho/Arduino-Telnet-Client
-By default the connection is made on port 23, but you can specify a different port passing it as optional argument to the login method.
-
-By default the answer received from the server is printed on serial port, but you can edit just the "print()" method in TelnetClient.cpp to redirect the chars received from the server as you prefer.
-
-
-
-The only thing you have to configure in your sketch is the char that represent the "prompt" for the server you're connecting to:
-
-
-
-for this use this function in your sketch:
-
-
-
-There are some other configuration parameters you can modify in the header file (Telnet.h).
-They are quite self explanatory:
-
-
-
-Messing with a Mikrotik router?
-For some reason I don't understand you're not the first here for this!
-I didn't find an explanation for this, but the telnet server implemented on Mikrotik routers asnwers twice...not a big deal, everything works, but your output can be a little messy:
-
-
-
-Known issues:
-It seems that login continuously resetting arduino many times in a short time cause problems at the Ethernet shields...it simply begin to work not properly or to not connect at all.
-In this case the only solution seems to disconnect/reconnect the power supply...hope to find a solution for this.
-
-Troubleshooting:
-
-For any issue you want to report about this library please uncomment this macro (TNDBG) in the header file
-
-
-
-then post the output or your sketch in the Issues section being as specific as possible (share your code, and explain what you're trying to do!).
-In general I'm very happy to receive feedback and to help people with their projects...but remember that is just an hobby for me, I'm doing it for free...so words as "please" and "thank you" are really appreciated.
diff --git a/examples/telnetDraytekRouter/telnetDraytekRouter.ino b/examples/telnetDraytekRouter/telnetDraytekRouter.ino
new file mode 100644
index 0000000..fd0e9b0
--- /dev/null
+++ b/examples/telnetDraytekRouter/telnetDraytekRouter.ino
@@ -0,0 +1,86 @@
+#include
+#include
+#include
+
+//Enter your Wifi details here (multiple SSIDs possible)
+#ifndef STASSID
+#define STASSID "**********"
+#define STAPSK "**********"
+#define STASSID2 "*********"
+#define STAPSK2 "*********"
+#endif
+
+//put here your router ip address, and login details
+IPAddress draytekRouterIp (192, 168, 1, 2);
+const char* user = "************";
+const char* pwd = "***********";
+
+const char* ssid = STASSID;
+const char* password = STAPSK;
+const char* ssid2 = STASSID2;
+const char* password2 = STAPSK2;
+
+
+ESP8266WiFiMulti WiFiMulti;
+
+WiFiClient client;
+
+ESP8266telnetClient tc(client);
+
+
+
+void setup () {
+
+ Serial.begin (9600);
+
+// We start by connecting to a WiFi network
+ WiFi.mode(WIFI_STA);
+ WiFiMulti.addAP(ssid, password);
+ WiFiMulti.addAP(ssid2,password2);
+
+ Serial.println();
+ Serial.println();
+ Serial.print("Wait for WiFi... ");
+
+ while (WiFiMulti.run() != WL_CONNECTED) {
+ Serial.print(".");
+ delay(500);
+ }
+
+ Serial.println("");
+ Serial.println("WiFi connected");
+ Serial.print("IP address: ");
+ Serial.println(WiFi.localIP());
+ Serial.println("Connecting.... ");
+
+
+
+
+ //WHICH CHARACTER SHOULD BE INTERPRETED AS "PROMPT"?
+ tc.setPromptChar('>');
+
+ //this is to trigger manually the login
+ //since it could be a problem to attach the serial monitor while negotiating with the server (it cause the board reset)
+ //remove it or replace it with a delay/wait of a digital input in case you're not using the serial monitors
+
+// char key = 0;
+// Serial.println("\r\npress Enter to begin:");
+// do{
+// key = Serial.read();
+// }while(key<=0);
+
+ //PUT HERE YOUR USERNAME/PASSWORD
+ if(tc.login(draytekRouterIp, user, pwd)){ //tc.login(RouterIp, "admin", "", 1234) if you want to specify a port different than 23
+ tc.sendCommand("ipf view -V");
+ tc.sendCommand("exit");
+ }
+ else{
+ Serial.println("login failed");
+ }
+ tc.disconnect();
+
+}
+
+void loop () { // run your loop routine
+
+}
diff --git a/examples/telnetMikrotikRouter/telnetMikrotikRouter.ino b/examples/telnetMikrotikRouter/telnetMikrotikRouter.ino
index 70487ca..1450432 100644
--- a/examples/telnetMikrotikRouter/telnetMikrotikRouter.ino
+++ b/examples/telnetMikrotikRouter/telnetMikrotikRouter.ino
@@ -1,31 +1,59 @@
-#include
-#include
-#include
+#include
+#include
+#include
+//Enter your Wifi details here (multiple SSIDs possible)
+#ifndef STASSID
+#define STASSID "**********"
+#define STAPSK "**********"
+#define STASSID2 "*********"
+#define STAPSK2 "*********"
+#endif
-EthernetClient client;
-telnetClient tc(client);
+//put here your raspi ip address, and login details
+IPAddress mikrotikRouterIp (192, 168, 1, 2);
+const char* user = "************";
+const char* pwd = "***********";
-byte clientMAC[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
-IPAddress clientIp (192, 168, 1, 177);
+const char* ssid = STASSID;
+const char* password = STAPSK;
+const char* ssid2 = STASSID2;
+const char* password2 = STAPSK2;
-//put here your router's ip address
-IPAddress mikrotikRouterIp (192, 168, 1, 2);
+
+ESP8266WiFiMulti WiFiMulti;
+
+WiFiClient client;
+
+ESP8266telnetClient tc(client);
+
+
void setup () {
Serial.begin (9600);
- Ethernet.begin (clientMAC, clientIp);
-
- //want to use dhcp?
- //if (!Ethernet.begin (clientMAC)){
- //Serial.println("\r\nDHCP error");
- //while(1);
- //}
+// We start by connecting to a WiFi network
+ WiFi.mode(WIFI_STA);
+ WiFiMulti.addAP(ssid, password);
+ WiFiMulti.addAP(ssid2,password);
+
+ Serial.println();
+ Serial.println();
+ Serial.print("Wait for WiFi... ");
+
+ while (WiFiMulti.run() != WL_CONNECTED) {
+ Serial.print(".");
+ delay(500);
+ }
+ Serial.println("");
+ Serial.println("WiFi connected");
+ Serial.print("IP address: ");
+ Serial.println(WiFi.localIP());
+ Serial.println("Connecting.... ");
- //WICH CHARACTER SHOULD BE INTERPRETED AS "PROMPT"?
+ //WHICH CHARACTER SHOULD BE INTERPRETED AS "PROMPT"?
tc.setPromptChar('>');
//this is to trigger manually the login
diff --git a/examples/telnetRaspberry/telnetRaspberry.ino b/examples/telnetRaspberry/telnetRaspberry.ino
index 5c13d52..15aa47e 100644
--- a/examples/telnetRaspberry/telnetRaspberry.ino
+++ b/examples/telnetRaspberry/telnetRaspberry.ino
@@ -1,29 +1,58 @@
-#include
-#include
-#include
+#include
+#include
+#include
+//Enter your Wifi details here (multiple SSIDs possible)
+#ifndef STASSID
+#define STASSID "**********"
+#define STAPSK "**********"
+#define STASSID2 "*********"
+#define STAPSK2 "*********"
+#endif
-EthernetClient client;
-telnetClient tc(client);
+//put here your raspi ip address, and login details
+IPAddress raspberryIp (192, 168, 1, 1);
+const char* user = "************";
+const char* pwd = "***********";
-byte clientMAC[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
-IPAddress clientIp (192, 168, 1, 177);
+const char* ssid = STASSID;
+const char* password = STAPSK;
+const char* ssid2 = STASSID2;
+const char* password2 = STAPSK2;
-//put here your raspberry's ip address
-IPAddress raspberryIp (192, 168, 1, 11);
+
+ESP8266WiFiMulti WiFiMulti;
+
+WiFiClient client;
+
+ESP8266telnetClient tc(client);
+
+
void setup () {
Serial.begin (9600);
- Ethernet.begin (clientMAC, clientIp);
-
- //want to use dhcp?
- //if (!Ethernet.begin (clientMAC)){
- //Serial.println("\r\nDHCP error");
- //while(1);
- //}
+// We start by connecting to a WiFi network
+ WiFi.mode(WIFI_STA);
+ WiFiMulti.addAP(ssid, password);
+ WiFiMulti.addAP(ssid2,password);
+
+ Serial.println();
+ Serial.println();
+ Serial.print("Wait for WiFi... ");
+
+ while (WiFiMulti.run() != WL_CONNECTED) {
+ Serial.print(".");
+ delay(500);
+ }
+
+ Serial.println("");
+ Serial.println("WiFi connected");
+ Serial.print("IP address: ");
+ Serial.println(WiFi.localIP());
+ Serial.println("Connecting.... ");
- //WICH CHARACTER SHOULD BE INTERPRETED AS "PROMPT"?
+ //WHICH CHARACTER SHOULD BE INTERPRETED AS "PROMPT"?
tc.setPromptChar('$');
//this is to trigger manually the login
diff --git a/library.properties b/library.properties
new file mode 100644
index 0000000..97180be
--- /dev/null
+++ b/library.properties
@@ -0,0 +1,9 @@
+name=ESP8266TelnetClient
+version=0.1
+author=RJB
+maintainer=RJB
+sentence=ESP8266 real Telnet Client
+paragraph=Allows esp8266 telnet access with login over multiple wifi ssid
+category=Communication
+url=https://github.com/videojedi/ESP8266-Telnet-Client
+architectures=*
diff --git a/TelnetClient.cpp b/src/ESP8266TelnetClient.cpp
similarity index 83%
rename from TelnetClient.cpp
rename to src/ESP8266TelnetClient.cpp
index c2ac1e4..aafe970 100644
--- a/TelnetClient.cpp
+++ b/src/ESP8266TelnetClient.cpp
@@ -20,14 +20,14 @@
*
*/
-#include "TelnetClient.h"
+#include "ESP8266telnetClient.h"
-telnetClient::telnetClient(EthernetClient& client){
+ESP8266telnetClient::ESP8266telnetClient(WiFiClient& client){
this->client = &client;
}
-bool telnetClient::login(IPAddress serverIpAddress, const char* username, const char* password, uint8_t port){
+bool ESP8266telnetClient::login(IPAddress serverIpAddress, const char* username, const char* password, uint8_t port){
this->print('\n');
this->print('\r');
@@ -36,8 +36,10 @@ bool telnetClient::login(IPAddress serverIpAddress, const char* username, const
if(this->client->connect(serverIpAddress, port)){
DEBUG_PRINT(F("login|connected!"));
//here there will be the initial negotiation
- //listenUntil(':');
- listen();
+ if(!listenUntil(':')){
+ return false;
+ }
+ //listen();
DEBUG_PRINT(F("login|sending username"));
if (!this->send(username, false)) return false;
listenUntil(':');
@@ -62,7 +64,7 @@ bool telnetClient::login(IPAddress serverIpAddress, const char* username, const
}
}
-bool telnetClient::sendCommand(const char* cmd){
+bool ESP8266telnetClient::sendCommand(const char* cmd){
this->send(cmd);
//negotiation until the server show the command prompt again
@@ -75,11 +77,11 @@ bool telnetClient::sendCommand(const char* cmd){
}
-void telnetClient::disconnect(){
+void ESP8266telnetClient::disconnect(){
this->client->stop();
}
-bool telnetClient::send(const char* buf, bool waitEcho){
+bool ESP8266telnetClient::send(const char* buf, bool waitEcho){
uint8_t l_size = strnlen(buf, MAX_OUT_BUFFER_LENGTH);
if(l_size == MAX_OUT_BUFFER_LENGTH){
@@ -110,7 +112,7 @@ bool telnetClient::send(const char* buf, bool waitEcho){
return true;
}
-void telnetClient::negotiate(){
+void ESP8266telnetClient::negotiate(){
byte verb, opt;
byte outBuf[3] = {255, 0, 0};
@@ -164,7 +166,7 @@ void telnetClient::negotiate(){
}
-void telnetClient::listen(){
+void ESP8266telnetClient::listen(){
while (this->client->available() == 0) delay (1);
@@ -193,12 +195,13 @@ void telnetClient::listen(){
}
}
-bool telnetClient::listenUntil(char c){
+bool ESP8266telnetClient::listenUntil(char c){
byte inByte;
+ unsigned long startMillis = millis();
//listen incoming bytes untile one char in the array arrive
while (this->client->available() == 0) delay (1);
-
+ startMillis = millis();
do {
if(this->client->available() > 0){
inByte = this->client->read();
@@ -216,12 +219,16 @@ bool telnetClient::listenUntil(char c){
DEBUG_PRINT(F("listenUntil|TERMINATOR RECEIVED"));
return true;
}
+ }
+ else if (millis() - startMillis > LISTEN_TOUT){
+ DEBUG_PRINT(F("listen|TIMEOUT!!!"));
+ return false;
}
}while (1);
}
-bool telnetClient::waitPrompt(){
+bool ESP8266telnetClient::waitPrompt(){
bool l_bLoop = false;
unsigned long startMillis = millis();
@@ -249,12 +256,12 @@ bool telnetClient::waitPrompt(){
return true;
}
-void telnetClient::print(char c){
+void ESP8266telnetClient::print(char c){
//edit this function if you want a different output!
Serial.print(c);
}
-void telnetClient::setPromptChar(char c){
+void ESP8266telnetClient::setPromptChar(char c){
m_promptChar = c;
}
diff --git a/TelnetClient.h b/src/ESP8266TelnetClient.h
similarity index 91%
rename from TelnetClient.h
rename to src/ESP8266TelnetClient.h
index 1fe2c94..9e2f511 100644
--- a/TelnetClient.h
+++ b/src/ESP8266TelnetClient.h
@@ -22,7 +22,8 @@
#ifndef TELNETC_H
#define TELNETC_H
-#include
+#include
+
//#define TNDBG 1
//#define MT_VM 1//for me to work with a virtual machine running a mikrotik router
@@ -45,11 +46,11 @@ const unsigned int LISTEN_TOUT = 5000;
const uint16_t PROMPT_REC_TOUT = 300;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-class telnetClient{
+class ESP8266telnetClient{
public:
- telnetClient(EthernetClient& client);
+ ESP8266telnetClient(WiFiClient& client);
bool login(IPAddress serverIpAddress, const char* username, const char* password, uint8_t port = 23);
bool sendCommand(const char* cmd);
@@ -58,7 +59,7 @@ class telnetClient{
private:
- EthernetClient* client;
+ WiFiClient* client;
char m_promptChar = '>';
bool send(const char* buf, bool waitEcho = true);