From b1a24a033f84a9811a99d02389cc6b64c02c69c0 Mon Sep 17 00:00:00 2001 From: Juraj Andrassy Date: Sun, 19 Jul 2020 18:59:33 +0200 Subject: [PATCH] version 1.0.0 for Library Manager - added support for Ethernet, WiFiEspAT and WiFiNINA - added and reworked examples - change port with begin(port) - README --- README.md | 22 +++++- .../TelnetStreamEsp32Test.ino | 75 ++++++++++++++++++ .../TelnetStreamEsp32Test/arduino_secrets.h | 2 + .../TelnetStreamEsp8266Test.ino | 76 +++++++++++++++++++ .../TelnetStreamEsp8266Test/arduino_secrets.h | 2 + .../TelnetStreamEthTest.ino | 63 +++++++++++++++ .../TelnetStreamSPIWiFiTest.ino | 75 ++++++++++++++++++ .../TelnetStreamSPIWiFiTest/arduino_secrets.h | 2 + .../TelnetStreamSerialWiFiTest.ino | 74 ++++++++++++++++++ .../TelnetStreamTest/TelnetStreamTest.ino | 53 ------------- library.properties | 4 +- src/TelnetStream.cpp | 22 +++++- src/TelnetStream.h | 16 +++- 13 files changed, 423 insertions(+), 63 deletions(-) create mode 100644 examples/TelnetStreamEsp32Test/TelnetStreamEsp32Test.ino create mode 100644 examples/TelnetStreamEsp32Test/arduino_secrets.h create mode 100644 examples/TelnetStreamEsp8266Test/TelnetStreamEsp8266Test.ino create mode 100644 examples/TelnetStreamEsp8266Test/arduino_secrets.h create mode 100644 examples/TelnetStreamEthTest/TelnetStreamEthTest.ino create mode 100644 examples/TelnetStreamSPIWiFiTest/TelnetStreamSPIWiFiTest.ino create mode 100644 examples/TelnetStreamSPIWiFiTest/arduino_secrets.h create mode 100644 examples/TelnetStreamSerialWiFiTest/TelnetStreamSerialWiFiTest.ino delete mode 100644 examples/TelnetStreamTest/TelnetStreamTest.ino diff --git a/README.md b/README.md index 20447f6..e4077cd 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,22 @@ # TelnetStream -Stream implementation over Telnet for ESP8266 and ESP32 Arduino boards package + +The library creates a TelnetStream object, which can be used the same way as Serial, but the output is sent to a connected telnet client. It enables remote logging or debugging. + +TelnetStream.h can be included not only in the ino file, but in cpp files of the sketch or in libraries to add debug prints for troubleshooting. + +TelnetStream works as it is with esp8266 and esp32 WiFi library, with the Ethernet library, with WiFiNINA and with WiFiEspAT library. + +Output of example: + +``` +juraj@nuc ~ $ telnet 192.168.1.114 +Trying 192.168.1.114... +Connected to 192.168.1.114. +Escape character is '^]'. +54 2020-07-19 16:50:43 A0: 355 +55 2020-07-19 16:50:48 A0: 335 +56 2020-07-19 16:50:53 A0: 223 +C +bye bye +Connection closed by foreign host. +``` \ No newline at end of file diff --git a/examples/TelnetStreamEsp32Test/TelnetStreamEsp32Test.ino b/examples/TelnetStreamEsp32Test/TelnetStreamEsp32Test.ino new file mode 100644 index 0000000..9dcfe68 --- /dev/null +++ b/examples/TelnetStreamEsp32Test/TelnetStreamEsp32Test.ino @@ -0,0 +1,75 @@ +#include +#include +#include + +const long gmtOffset_sec = 3600; +const int daylightOffset_sec = 3600; + +#include "arduino_secrets.h" +///////please enter your sensitive data in the Secret tab/arduino_secrets.h +const char ssid[] = SECRET_SSID; // your network SSID (name) +const char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) + +void setup() { + Serial.begin(115200); + + Serial.print("Attempting to connect to WPA SSID: "); + Serial.println(ssid); + WiFi.begin(ssid, pass); + if (WiFi.waitForConnectResult() != WL_CONNECTED) { + Serial.println("Failed to connect."); + while (1) { + delay(10); + } + } + + configTime(gmtOffset_sec, daylightOffset_sec, "pool.ntp.org"); + time_t now = time(nullptr); + while (now < SECS_YR_2000) { + delay(100); + now = time(nullptr); + } + setTime(now); + + IPAddress ip = WiFi.localIP(); + Serial.println(); + Serial.println("Connected to WiFi network."); + Serial.print("Connect with Telnet client to "); + Serial.println(ip); + + TelnetStream.begin(); +} + +void loop() { + switch (TelnetStream.read()) { + case 'R': + TelnetStream.stop(); + delay(100); + ESP.restart(); + break; + case 'C': + TelnetStream.println("bye bye"); + TelnetStream.flush(); + TelnetStream.stop(); + break; + } + + static unsigned long next; + if (millis() - next > 5000) { + next = millis(); + log(); + } +} + +void log() { + static int i = 0; + + char timeStr[20]; + sprintf(timeStr, "%02d-%02d-%02d %02d:%02d:%02d", year(), month(), day(), hour(), minute(), second()); + + TelnetStream.print(i++); + TelnetStream.print(" "); + TelnetStream.print(timeStr); + TelnetStream.print(" A0: "); + TelnetStream.println(analogRead(A0)); +} diff --git a/examples/TelnetStreamEsp32Test/arduino_secrets.h b/examples/TelnetStreamEsp32Test/arduino_secrets.h new file mode 100644 index 0000000..0c9fdd5 --- /dev/null +++ b/examples/TelnetStreamEsp32Test/arduino_secrets.h @@ -0,0 +1,2 @@ +#define SECRET_SSID "" +#define SECRET_PASS "" diff --git a/examples/TelnetStreamEsp8266Test/TelnetStreamEsp8266Test.ino b/examples/TelnetStreamEsp8266Test/TelnetStreamEsp8266Test.ino new file mode 100644 index 0000000..57d142e --- /dev/null +++ b/examples/TelnetStreamEsp8266Test/TelnetStreamEsp8266Test.ino @@ -0,0 +1,76 @@ +#include +#include +#include +#include +#include + +#define TIME_ZONE TZ_Europe_London + +#include "arduino_secrets.h" +///////please enter your sensitive data in the Secret tab/arduino_secrets.h +const char ssid[] = SECRET_SSID; // your network SSID (name) +const char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) + +void setup() { + Serial.begin(115200); + + Serial.print("Attempting to connect to WPA SSID: "); + Serial.println(ssid); + WiFi.begin(ssid, pass); + if (WiFi.waitForConnectResult() != WL_CONNECTED) { + Serial.println("Failed to connect."); + while (1) { + delay(10); + } + } + + configTime(TIME_ZONE, "pool.ntp.org"); + time_t now = time(nullptr); + while (now < SECS_YR_2000) { + delay(100); + now = time(nullptr); + } + setTime(now); + + IPAddress ip = WiFi.localIP(); + Serial.println(); + Serial.println("Connected to WiFi network."); + Serial.print("Connect with Telnet client to "); + Serial.println(ip); + + TelnetStream.begin(); +} + +void loop() { + switch (TelnetStream.read()) { + case 'R': + TelnetStream.stop(); + delay(100); + ESP.reset(); + break; + case 'C': + TelnetStream.println("bye bye"); + TelnetStream.flush(); + TelnetStream.stop(); + break; + } + + static unsigned long next; + if (millis() - next > 5000) { + next = millis(); + log(); + } +} + +void log() { + static int i = 0; + + char timeStr[20]; + sprintf(timeStr, "%02d-%02d-%02d %02d:%02d:%02d", year(), month(), day(), hour(), minute(), second()); + + TelnetStream.print(i++); + TelnetStream.print(" "); + TelnetStream.print(timeStr); + TelnetStream.print(" A0: "); + TelnetStream.println(analogRead(A0)); +} diff --git a/examples/TelnetStreamEsp8266Test/arduino_secrets.h b/examples/TelnetStreamEsp8266Test/arduino_secrets.h new file mode 100644 index 0000000..0c9fdd5 --- /dev/null +++ b/examples/TelnetStreamEsp8266Test/arduino_secrets.h @@ -0,0 +1,2 @@ +#define SECRET_SSID "" +#define SECRET_PASS "" diff --git a/examples/TelnetStreamEthTest/TelnetStreamEthTest.ino b/examples/TelnetStreamEthTest/TelnetStreamEthTest.ino new file mode 100644 index 0000000..9e960d1 --- /dev/null +++ b/examples/TelnetStreamEthTest/TelnetStreamEthTest.ino @@ -0,0 +1,63 @@ +#include +#include +#include + +byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; + +void setup() { + + Serial.begin(115200); + + Serial.println("Initialize Ethernet with DHCP:"); + if (Ethernet.begin(mac) == 0) { + Serial.println("Failed to configure Ethernet using DHCP"); + // Check for Ethernet hardware present + if (Ethernet.hardwareStatus() == EthernetNoHardware) { + Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :("); + while (true) { + delay(1); // do nothing, no point running without Ethernet hardware + } + } + if (Ethernet.linkStatus() == LinkOFF) { + Serial.println("Ethernet cable is not connected."); + } + while (true) { + delay(1); // do nothing, no point running without Ethernet hardware + } + } else { + Serial.print(" DHCP assigned IP "); + Serial.println(Ethernet.localIP()); + } + + TelnetStream.begin(); + NTP.begin("pool.ntp.org", 1, false); +} + +void loop() { + + switch (TelnetStream.read()) { + case 'C': + TelnetStream.println("bye bye"); + TelnetStream.flush(); + TelnetStream.stop(); + break; + } + + static unsigned long next; + if (millis() - next > 5000) { + next = millis(); + log(); + } +} + +void log() { + static int i = 0; + TelnetStream.print(i++); + TelnetStream.print(" "); + TelnetStream.print(NTP.getTimeDateString()); + TelnetStream.print(" "); + TelnetStream.print("Uptime: "); + TelnetStream.print(NTP.getUptimeString()); + TelnetStream.print(" since "); + TelnetStream.println(NTP.getTimeDateString(NTP.getFirstSync()).c_str()); +} diff --git a/examples/TelnetStreamSPIWiFiTest/TelnetStreamSPIWiFiTest.ino b/examples/TelnetStreamSPIWiFiTest/TelnetStreamSPIWiFiTest.ino new file mode 100644 index 0000000..8805ab0 --- /dev/null +++ b/examples/TelnetStreamSPIWiFiTest/TelnetStreamSPIWiFiTest.ino @@ -0,0 +1,75 @@ +#include +#include +#include + +const int8_t TIME_ZONE = 2; // UTC + 2 + +#include "arduino_secrets.h" +///////please enter your sensitive data in the Secret tab/arduino_secrets.h +const char ssid[] = SECRET_SSID; // your network SSID (name) +const char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) + +void setup() { + Serial.begin(115200); + + if (WiFi.status() == WL_NO_MODULE) { + Serial.println(); + Serial.println("Communication with WiFi module failed!"); + // don't continue + while (true); + } + + // waiting for connection to Wifi network set with the SetupWiFiConnection sketch + Serial.println("Waiting for connection to WiFi"); + WiFi.begin(ssid, pass); + while (WiFi.status() != WL_CONNECTED) { + delay(1000); + Serial.print('.'); + } + Serial.println(); + + Serial.println("Waiting for SNTP"); + while (!WiFi.getTime()) { + delay(1000); + Serial.print('.'); + } + setTime(WiFi.getTime()); + + IPAddress ip = WiFi.localIP(); + Serial.println(); + Serial.println("Connected to WiFi network."); + Serial.print("Connect with Telnet client to "); + Serial.println(ip); + + TelnetStream.begin(); +} + +void loop() { + + switch (TelnetStream.read()) { + case 'C': + TelnetStream.println("bye bye"); + TelnetStream.flush(); + TelnetStream.stop(); + break; + } + + static unsigned long next; + if (millis() - next > 5000) { + next = millis(); + log(); + } +} + +void log() { + static int i = 0; + + char timeStr[20]; + sprintf(timeStr, "%02d-%02d-%02d %02d:%02d:%02d", year(), month(), day(), hour(), minute(), second()); + + TelnetStream.print(i++); + TelnetStream.print(" "); + TelnetStream.print(timeStr); + TelnetStream.print(" A0: "); + TelnetStream.println(analogRead(A0)); +} diff --git a/examples/TelnetStreamSPIWiFiTest/arduino_secrets.h b/examples/TelnetStreamSPIWiFiTest/arduino_secrets.h new file mode 100644 index 0000000..0c9fdd5 --- /dev/null +++ b/examples/TelnetStreamSPIWiFiTest/arduino_secrets.h @@ -0,0 +1,2 @@ +#define SECRET_SSID "" +#define SECRET_PASS "" diff --git a/examples/TelnetStreamSerialWiFiTest/TelnetStreamSerialWiFiTest.ino b/examples/TelnetStreamSerialWiFiTest/TelnetStreamSerialWiFiTest.ino new file mode 100644 index 0000000..f611c58 --- /dev/null +++ b/examples/TelnetStreamSerialWiFiTest/TelnetStreamSerialWiFiTest.ino @@ -0,0 +1,74 @@ +#include +#include +#include + +const int8_t TIME_ZONE = 2; // UTC + 2 + +void setup() { + Serial.begin(115200); + + Serial1.begin(115200); + WiFi.init(Serial1); + + if (WiFi.status() == WL_NO_MODULE) { + Serial.println(); + Serial.println("Communication with WiFi module failed!"); + // don't continue + while (true); + } + + // waiting for connection to Wifi network set with the SetupWiFiConnection sketch + Serial.println("Waiting for connection to WiFi"); + while (WiFi.status() != WL_CONNECTED) { + delay(1000); + Serial.print('.'); + } + Serial.println(); + + WiFi.sntp(TIME_ZONE, "us.pool.ntp.org"); + + Serial.println("Waiting for SNTP"); +// while (!WiFi.getTime()) { +// delay(1000); +// Serial.print('.'); +// } + setTime(WiFi.getTime()); + + IPAddress ip = WiFi.localIP(); + Serial.println(); + Serial.println("Connected to WiFi network."); + Serial.print("Connect with Telnet client to "); + Serial.println(ip); + + TelnetStream.begin(); +} + +void loop() { + + switch (TelnetStream.read()) { + case 'C': + TelnetStream.println("bye bye"); + TelnetStream.flush(); + TelnetStream.stop(); + break; + } + + static unsigned long next; + if (millis() - next > 5000) { + next = millis(); + log(); + } +} + +void log() { + static int i = 0; + + char timeStr[20]; + sprintf(timeStr, "%02d-%02d-%02d %02d:%02d:%02d", year(), month(), day(), hour(), minute(), second()); + + TelnetStream.print(i++); + TelnetStream.print(" "); + TelnetStream.print(timeStr); + TelnetStream.print(" A0: "); + TelnetStream.println(analogRead(A0)); +} diff --git a/examples/TelnetStreamTest/TelnetStreamTest.ino b/examples/TelnetStreamTest/TelnetStreamTest.ino deleted file mode 100644 index 237fbf4..0000000 --- a/examples/TelnetStreamTest/TelnetStreamTest.ino +++ /dev/null @@ -1,53 +0,0 @@ -#include -#include -#include -#include -#include - -void setup() -{ - Serial.begin(115200); - ArduinoOTA.begin(); - - WiFiManager wifiManager; - wifiManager.autoConnect(); - - TelnetStream.begin(); - NTP.begin("pool.ntp.org", 1, false); -} - -void loop() -{ - ArduinoOTA.handle(); - - switch (TelnetStream.read()) { - case 'R': - TelnetStream.stop(); - delay(100); - ESP.reset(); - break; - case 'C': - TelnetStream.println("bye bye"); - TelnetStream.flush(); - TelnetStream.stop(); - break; - } - - static unsigned long next = millis(); - if (millis() > next) { - next += 5000; - log(); - } -} - -void log() { - static int i = 0; - TelnetStream.print(i++); - TelnetStream.print(" "); - TelnetStream.print(NTP.getTimeDateString()); - TelnetStream.print(" "); - TelnetStream.print("Uptime: "); - TelnetStream.print(NTP.getUptimeString()); - TelnetStream.print(" since "); - TelnetStream.println(NTP.getTimeDateString(NTP.getFirstSync()).c_str()); -} diff --git a/library.properties b/library.properties index 7983fcf..67a50f4 100755 --- a/library.properties +++ b/library.properties @@ -1,10 +1,10 @@ name=TelnetStream -version=0.0.2 +version=1.0.0 author=Juraj Andrassy maintainer=Juraj Andrassy sentence=Stream implementation over telnet for OTA debuging paragraph= category=Communication url=https://github.com/jandrassy/TelnetStream -architectures=esp8266,esp32 +architectures=* includes=TelnetStream.h diff --git a/src/TelnetStream.cpp b/src/TelnetStream.cpp index 77ca1e7..2e58722 100644 --- a/src/TelnetStream.cpp +++ b/src/TelnetStream.cpp @@ -1,9 +1,17 @@ +#include // to include MCU specific includes for networking library #include "TelnetStream.h" TelnetStreamClass::TelnetStreamClass(uint16_t port) :server(port) { } -void TelnetStreamClass::begin() { +void TelnetStreamClass::begin(int port) { + if (port) { +#if USE_ETHERNET + server = EthernetServer(port); +#else + server = WiFiServer(port); +#endif + } server.begin(); client = server.available(); } @@ -13,21 +21,29 @@ void TelnetStreamClass::stop() { } boolean TelnetStreamClass::disconnected() { -#ifdef ESP32 +#if defined(ESP32) || defined(USE_ETHERNET) if (!server) return true; #else - if (server.status() == CLOSED) + if (server.status() == 0) // 0 is CLOSED return true; #endif if (!client) { +#if defined(USE_ETHERNET) + client = server.accept(); +#else client = server.available(); +#endif } if (client) { if (client.connected()) return false; client.stop(); +#if defined(USE_ETHERNET) + client = server.accept(); +#else client = server.available(); +#endif } return true; } diff --git a/src/TelnetStream.h b/src/TelnetStream.h index 151d146..2071155 100644 --- a/src/TelnetStream.h +++ b/src/TelnetStream.h @@ -19,7 +19,10 @@ repository https://github.com/jandrassy #ifndef _TELNETSTREAM_H_ #define _TELNETSTREAM_H_ -#ifdef ESP8266 +#if __has_include() +#include +#define USE_ETHERNET 1 +#elif defined(ESP8266) #include #else #include @@ -28,8 +31,13 @@ repository https://github.com/jandrassy class TelnetStreamClass : public Stream { private: +#if USE_ETHERNET + EthernetServer server; + EthernetClient client; +#else WiFiServer server; WiFiClient client; +#endif boolean disconnected(); @@ -37,7 +45,7 @@ class TelnetStreamClass : public Stream { TelnetStreamClass(uint16_t port); - void begin(); + void begin(int port = 0); void stop(); // Stream implementation @@ -46,9 +54,9 @@ class TelnetStreamClass : public Stream { int peek(); // Print implementation - size_t write(uint8_t val); + virtual size_t write(uint8_t val); using Print::write; // pull in write(str) and write(buf, size) from Print - void flush(); + virtual void flush(); };