From 7570d14130443e6cb05056f2cf5a9cf4f34cbfa9 Mon Sep 17 00:00:00 2001 From: Richard Burford Date: Sat, 4 Jan 2020 09:34:56 +0000 Subject: [PATCH 1/8] ESP8266 fork changed to support ESP8266 with WiFi added draytek router example --- TelnetClient.cpp => ESP8266TelnetClient.cpp | 37 ++++---- TelnetClient.h => ESP8266TelnetClient.h | 9 +- .../telnetDraytek/telnetDraytekRouter.ino | 86 +++++++++++++++++++ .../telnetMikrotikRouter.ino | 62 +++++++++---- examples/telnetRaspberry/telnetRaspberry.ino | 63 ++++++++++---- 5 files changed, 204 insertions(+), 53 deletions(-) rename TelnetClient.cpp => ESP8266TelnetClient.cpp (83%) rename TelnetClient.h => ESP8266TelnetClient.h (91%) create mode 100644 examples/telnetDraytek/telnetDraytekRouter.ino diff --git a/TelnetClient.cpp b/ESP8266TelnetClient.cpp similarity index 83% rename from TelnetClient.cpp rename to ESP8266TelnetClient.cpp index c2ac1e4..aafe970 100644 --- a/TelnetClient.cpp +++ b/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/ESP8266TelnetClient.h similarity index 91% rename from TelnetClient.h rename to ESP8266TelnetClient.h index 1fe2c94..9e2f511 100644 --- a/TelnetClient.h +++ b/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); diff --git a/examples/telnetDraytek/telnetDraytekRouter.ino b/examples/telnetDraytek/telnetDraytekRouter.ino new file mode 100644 index 0000000..dee35ad --- /dev/null +++ b/examples/telnetDraytek/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 raspi 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,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.... "); + + + + + //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 From 15f253a70eb1d344ebb0690b56d50216a041cd56 Mon Sep 17 00:00:00 2001 From: Richard Burford Date: Sat, 4 Jan 2020 09:38:04 +0000 Subject: [PATCH 2/8] Update README.md --- README.md | 45 ++------------------------------------------- 1 file changed, 2 insertions(+), 43 deletions(-) diff --git a/README.md b/README.md index b04de91..9b11ed6 100644 --- a/README.md +++ b/README.md @@ -1,46 +1,5 @@ -# 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. -

Useful tips:

-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. - -print - -The only thing you have to configure in your sketch is the char that represent the "prompt" for the server you're connecting to: - -prompt-char - -for this use this function in your sketch: - -prompt-char2 - -There are some other configuration parameters you can modify in the header file (Telnet.h). -They are quite self explanatory: - -param - -

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: - -doubleprompt - -

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 - -tndbg - -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. From 464c3bb7167f43b623069b9f1bf25b039cdfd030 Mon Sep 17 00:00:00 2001 From: Richard Burford Date: Sat, 4 Jan 2020 09:40:55 +0000 Subject: [PATCH 3/8] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 9b11ed6..9a740da 100644 --- a/README.md +++ b/README.md @@ -2,4 +2,5 @@ Based on the excellent work of @alejho. +https://github.com/alejho/Arduino-Telnet-Client From 12eda3da6a2a70112a8cf67e87b5ed6c029c8f55 Mon Sep 17 00:00:00 2001 From: Richard Burford Date: Sat, 4 Jan 2020 09:49:58 +0000 Subject: [PATCH 4/8] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 9a740da..d33bfdf 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # ESP8266-Telnet-Client -Based on the excellent work of @alejho. +Based on the excellent work of @alejho. (https://github.com/alejho) https://github.com/alejho/Arduino-Telnet-Client From 2408852a715feb38a1fb5ecb17a72dbd4b33ed7e Mon Sep 17 00:00:00 2001 From: Richard Burford Date: Sat, 4 Jan 2020 10:12:45 +0000 Subject: [PATCH 5/8] library properties added --- library.properties | 9 +++++++++ ESP8266TelnetClient.cpp => src/ESP8266TelnetClient.cpp | 0 ESP8266TelnetClient.h => src/ESP8266TelnetClient.h | 0 3 files changed, 9 insertions(+) create mode 100644 library.properties rename ESP8266TelnetClient.cpp => src/ESP8266TelnetClient.cpp (100%) rename ESP8266TelnetClient.h => src/ESP8266TelnetClient.h (100%) 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/ESP8266TelnetClient.cpp b/src/ESP8266TelnetClient.cpp similarity index 100% rename from ESP8266TelnetClient.cpp rename to src/ESP8266TelnetClient.cpp diff --git a/ESP8266TelnetClient.h b/src/ESP8266TelnetClient.h similarity index 100% rename from ESP8266TelnetClient.h rename to src/ESP8266TelnetClient.h From caef615fae4ddd612aa43915a468b66654f53a95 Mon Sep 17 00:00:00 2001 From: Richard Burford Date: Sat, 4 Jan 2020 10:20:04 +0000 Subject: [PATCH 6/8] fix example folder --- .../telnetDraytekRouter.ino | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename examples/{telnetDraytek => telnetDraytekRouter}/telnetDraytekRouter.ino (100%) diff --git a/examples/telnetDraytek/telnetDraytekRouter.ino b/examples/telnetDraytekRouter/telnetDraytekRouter.ino similarity index 100% rename from examples/telnetDraytek/telnetDraytekRouter.ino rename to examples/telnetDraytekRouter/telnetDraytekRouter.ino From f21ed04678ae01cfe271ac079d4d72801491f253 Mon Sep 17 00:00:00 2001 From: Richard Burford Date: Mon, 6 Jan 2020 20:39:50 +0000 Subject: [PATCH 7/8] fixed second password --- examples/telnetDraytekRouter/telnetDraytekRouter.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/telnetDraytekRouter/telnetDraytekRouter.ino b/examples/telnetDraytekRouter/telnetDraytekRouter.ino index dee35ad..a3657a6 100644 --- a/examples/telnetDraytekRouter/telnetDraytekRouter.ino +++ b/examples/telnetDraytekRouter/telnetDraytekRouter.ino @@ -36,7 +36,7 @@ void setup () { // We start by connecting to a WiFi network WiFi.mode(WIFI_STA); WiFiMulti.addAP(ssid, password); - WiFiMulti.addAP(ssid2,password); + WiFiMulti.addAP(ssid2,password2); Serial.println(); Serial.println(); From 51ee1b040e76e484d364d9acfb9a4219f46323a9 Mon Sep 17 00:00:00 2001 From: Richard Burford Date: Mon, 6 Jan 2020 20:40:25 +0000 Subject: [PATCH 8/8] Update telnetDraytekRouter.ino --- examples/telnetDraytekRouter/telnetDraytekRouter.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/telnetDraytekRouter/telnetDraytekRouter.ino b/examples/telnetDraytekRouter/telnetDraytekRouter.ino index a3657a6..fd0e9b0 100644 --- a/examples/telnetDraytekRouter/telnetDraytekRouter.ino +++ b/examples/telnetDraytekRouter/telnetDraytekRouter.ino @@ -10,7 +10,7 @@ #define STAPSK2 "*********" #endif -//put here your raspi ip address, and login details +//put here your router ip address, and login details IPAddress draytekRouterIp (192, 168, 1, 2); const char* user = "************"; const char* pwd = "***********";