Skip to content

Commit

Permalink
TelnetPrint object
Browse files Browse the repository at this point in the history
  • Loading branch information
JAndrassy committed Oct 3, 2020
1 parent f78e3dd commit da6e2fc
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 31 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ The library creates a TelnetStream object, which can be used the same way as Ser

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, EthernetENC and UIPEthernet library, with WiFiNINA, WiFi101 and with WiFiEspAT library.
Version 1.2.0 introduced TelnetPrint object, a simpler and smaller alternative to TelnetStream. Basically it is only EthernetServer or WiFiServer instanced for use anywhere in your sketch or libraries.

TelnetStream/TelnetPrint works as it is with esp8266 and esp32 WiFi library, with the Ethernet, EthernetENC and UIPEthernet library, with WiFiNINA, WiFi101 and with WiFiEspAT library.

The library is in Library Manager. You can install it there.

Expand Down
55 changes: 55 additions & 0 deletions examples/TelnetPrintEthTest/TelnetPrintEthTest.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <Ethernet.h>
#include <TelnetPrint.h>
#include <TimeLib.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress myIP(192, 168, 1, 177);

void setup() {

Serial.begin(115200);

Ethernet.init(10);
Ethernet.begin(mac, myIP);

//TelnetPrint = NetServer(2323); // uncomment to change port
TelnetPrint.begin();

Serial.println("Test started");
}

void loop() {
NetClient client = TelnetPrint.available();
if (client) {
int c = client.read();
switch (c) {
case 'C':
client.println("bye bye");
client.flush();
client.stop();
break;
default:
Serial.write(c);
}
}

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());

TelnetPrint.print(i++);
TelnetPrint.print(" ");
TelnetPrint.print(timeStr);
TelnetPrint.print(" A0: ");
TelnetPrint.println(analogRead(A0));
}

5 changes: 3 additions & 2 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
name=TelnetStream
version=1.1.0
version=1.2.0
author=Juraj Andrassy
maintainer=Juraj Andrassy <[email protected]>
sentence=Stream implementation over telnet for OTA debuging
paragraph=
category=Communication
url=https://github.com/jandrassy/TelnetStream
architectures=*
includes=TelnetStream.h
includes=TelnetStream.h,TelnetPrint.h
dot_a_linkage=true
61 changes: 61 additions & 0 deletions src/NetTypes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
Copyright (C) 2020 Juraj Andrassy
repository https://github.com/jandrassy
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef _NETTYPES_H_
#define _NETTYPES_H_

#include <Arduino.h> // to include MCU specific includes for networking library

#if __has_include(<EthernetENC.h>)
#include <EthernetENC.h>
#define NetClient EthernetClient
#define NetServer EthernetServerPrint

#elif __has_include(<Ethernet.h>)
#include <Ethernet.h>
#define NetClient EthernetClient
#define NetServer EthernetServer

#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#include "ArduinoWiFiServer.h"
#define NetClient WiFiClient
#define NetServer ArduinoWiFiServer

#elif defined(ESP32)
#include <WiFi.h>
#include "ArduinoWiFiServer.h"
#define NetClient WiFiClient
#define NetServer ArduinoWiFiServer

#elif __has_include(<WiFi101.h>)
#include <WiFi101.h>
#define NetClient WiFiClient
#define NetServer WiFiServer

#elif __has_include(<WiFiEspAT.h>)
#include <WiFiEspAT.h>
#define NetClient WiFiClient
#define NetServer WiFiServerPrint
#else
#include <WiFi.h>
#define NetClient WiFiClient
#define NetServer WiFiServer
#endif

#endif
3 changes: 3 additions & 0 deletions src/TelnetPrint.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "TelnetPrint.h"

NetServer TelnetPrint(23);
8 changes: 8 additions & 0 deletions src/TelnetPrint.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef _TELNETPRINT_H_
#define _TELNETPRINT_H_

#include "NetTypes.h"

extern NetServer TelnetPrint;

#endif
3 changes: 1 addition & 2 deletions src/TelnetStream.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include <Arduino.h> // to include MCU specific includes for networking library
#include "TelnetStream.h"

TelnetStreamClass::TelnetStreamClass(uint16_t port) :server(port) {
Expand Down Expand Up @@ -28,7 +27,7 @@ boolean TelnetStreamClass::disconnected() {
if (!server)
return true;
#endif

if (!client || !client.available()) {
client = server.available(); // try to get next client with data
}
Expand Down
27 changes: 1 addition & 26 deletions src/TelnetStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,7 @@ repository https://github.com/jandrassy
#ifndef _TELNETSTREAM_H_
#define _TELNETSTREAM_H_

#if __has_include(<Ethernet.h>)
#include <Ethernet.h>
#define NetClient EthernetClient
#define NetServer EthernetServer
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#define NetClient WiFiClient
#elif __has_include(<WiFi101.h>)
#include <WiFi101.h>
#define NetClient WiFiClient
#define NetServer WiFiServer
#else
#include <WiFi.h>
#define NetClient WiFiClient
#define NetServer WiFiServer
#endif

// special server types
#if defined(ESP32) || defined(ESP8266)
#include "ArduinoWiFiServer.h"
#define NetServer ArduinoWiFiServer
#elif defined(_WIFI_ESP_AT_H_) // from WiFi.h
#define NetServer WiFiServerPrint
#elif __has_include(<EthernetENC.h>)
#define NetServer EthernetServerPrint
#endif
#include "NetTypes.h"

class TelnetStreamClass : public Stream {

Expand Down

0 comments on commit da6e2fc

Please sign in to comment.