Using Teensy 4.1 as a Telnet Server

Status
Not open for further replies.

MichaelS

Member
Hey, guys!

I am trying to figure out, how to use the Teensy 4.1 as a telnet server. After long research I haven't found an example or solution for my problem, yet. I'm pretty new to programming in Arduino. So please don't judge me too fast.

My thoughts were to use the Teensy 4.1 as a telnet server, connecting via ethernetcable to a laptop. The laptop should simulate the client. At first I want to try to send simple messages from the Laptop to the Teensy (via Putty). I created some stuff the Teensy should do when I text him.
I already activated the Telnet Client on the laptop and deactivated the firewalls.
The project is a lot larger than the part I show here. So I created an example to show you my problem.

First of all: Is it possible to use Teensy 4.1 as a Telnet server? If not, is there another way I could get a communication?
Do I have to specify the software to use telnet?


The problem I have is, if I ask for the client IP (laptop) it always tells me that the IP was 0.0.0.0. So my program isn't even able to come as far that I can communicate.
I have no more ideas how to get the Teensy 4.1 to give me the right IP...


I use Teensyduino 1.53, Arduino 1.8.13, Teensy 4.1, the latest Putty version, the latest verisons of NativEthernet and Fnet and an old Windows 7 laptop.


Code:

Code:
#include <stdlib.h>
#include <TimerOne.h>
#include <SPI.h>
#include <fnet.h>
#include <NativeEthernet.h>

#define SPI_MOSI        11
#define SPI_MISO        12
#define SPI_SCK         13
#define SPI_FREQ  1000000                                 //SPI frequency (1 MHz)
static  SPISettings spi_settings                          //SPI settings
        = SPISettings(SPI_FREQ, MSBFIRST, SPI_MODE1);

byte mac[] = {
  0x92, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

IPAddress ip(169, 254, 86, 246);
IPAddress subnet(255, 255, 0, 0);
IPAddress serv(169, 254, 86, 215);
IPAddress gateway(169, 254, 86, 1);

EthernetServer server(23);

EthernetClient client = server.available();

boolean alreadyConnected = false;

String inputString;
String inputCommand;
byte buf[1024];

uint8_t msg_received = 0;
uint8_t msg_send = 0;

float temperatures[] = {5, 4.3, 1234, 23, 1.23, -5.0000034};

bool alarm = true;


void setup() {

  Serial.begin(115200);
  Serial.println("Server online!");


  SPI.setMISO(SPI_MISO);                                     //set MISO pin
  SPI.setMOSI(SPI_MOSI);                                     //set MOSI pin
  SPI.setSCK(SPI_SCK);                                       //set clock pin
  SPI.begin();                                            //start SPI bus

  Ethernet.begin(mac, ip, gateway, subnet);
  if(Ethernet.hardwareStatus() == EthernetNoHardware){
    Serial.println("Ethernet is not available. Check hardware status!");
    while(true){;}
  }
  if(Ethernet.linkStatus() == LinkOFF){
    Serial.println("Ethernet cable is not connected");
  }
  server.begin();
  Serial.print("Server IP-address: ");
  Serial.println(Ethernet.localIP());
}

void loop() {

  if(client){
    new_client();
    communication();
  }

  control_interface();
}


void print_command_list(){
  Serial.println("Command list:");
  Serial.println("");
}

void new_client(){
  if(!alreadyConnected){
    client.flush();
    Serial.println("New Client available.");
    Serial.print("Client IP: ");
    Serial.println(client.remoteIP());
    client.println("Hello, client!");
    client.print("Server IP: ");
    client.println(ip);
    client.println(Ethernet.localIP());
    alreadyConnected = true;
  }
}


void communication(){

  if(client.available() > 0){
    
    char thisChar = client.read();
    inputCommand = client.readString();
//    server.write(thisChar);
//    Serial.write(thisChar);


    switch(thisChar){
      
      case 'temp':
        for(int i=0; i<6; i++){
          server.write(temperatures[i]);
          client.print(temperatures[i]);
          client.print("  ");
        }
        Serial.println("All temperature values have been sent.");
        client.println();
      break;

      case 'reads':
        inputString = client.readString();
        Serial.print(inputString);
//        client.write(inputString);
        server.write("Danke für die Nachricht ;)");
      break;

      case 'h':
        print_command_list();
        server.write("Command list has been print to Serial\r\n");
      break;

      case 'a':
        server.write(alarm);
        client.print("Alarmstatus: ");
        client.print(alarm);
      break;

      case 'b':
        server.write("Achtung, ich gebe dir den Alarm-Status zurück!");
        thisChar = 'a';
      break;
      
      case 'runs':
        server.write("deep, run wild");
      break;

      case 'traffic':
        while(client.connected()){
          client.read(buf,1024);
        }
        client.stop();
        Serial.println("Client disconnected");
      break;

      default:
        server.write("No valid input");
      break;

    }

    }
}


void control_interface(){

  String input = "";

  if(Serial.available() > 0){
    input = Serial.readString();
    msg_send = (input.substring(0,4)).toInt();
    Serial.print("Zu sendende Nachricht: ");
    Serial.println(msg_send);
  }
  
}


This is the output in the serial monitor:

Code:
Server IP-address: 169.254.86.246

New Client available.

Client IP: 0.0.0.0



I saw that my variables in the switch-case are too long or have the wrong data typ, please ignore the fact, I will change that later on.

I really would appreciate if you guys could help me!
Thank you!
 
Please verify you have the latest download for NativeEthernet, there used to be a problem with client not returning an IP address until client.available() was called. I remember fixing that, but I may not have updated the version number since then.
 
Hi

I have Telnet operation on a Teensy 4.1 here: https://www.utasker.com/iMX/Teensy_4_1.html
together with FTP, HTTP and some other Internet protocols.

Here's a Telnet session connected to a command line menu which shows some of the Ethernet operations:

Main menu
===================
1 Configure LAN interface
2 Configure serial interface
3 Go to I/O menu
4 Go to administration menu
5 Go to overview/statistics menu
6 Go to USB menu
7 Go to I2C menu
8 Go to utFAT disk interface
9 FTP/TELNET/MQTT client commands
a CAN commands
help Display menu specific help
quit Leave command mode
1



LAN configuration
===================
up go to main menu
phy Dump PHY registers
set_phy <reg> <val>
estat Show Ethernet statistics
ipstat Show IP statistics
r_ipstat Reset Ethernet statistics
set_dhcp <enable/disable> DHCP
set_ip_add Set IPV4 address
set_ip_mask Set IP subnet mask
set_ip_gway Set default gateway
set_ip_dns Set default DNS
set_eth_speed Set LAN speed (10/100/AUTO)
show_config Show network configuration
show_config_o Show original network configuration
save Save configuration to FLASH
validate Validate temporary config. in FLASH
ping Ping IPv4 addr [cnt]/i (infinite)}
ping? Ping status
arp -a Request ARP table
arp -d Delete ARP table
prime Prime IP
hosts List multicast host groups
join [multicast] join group
leave [host ID] leave group
multi <ip> send test frame
help Display menu specific help
quit Leave command mode


estat

Ethernet counters
=======================================
Tx Rx
Packets 117 1112
Broadcast 0 63
Multicast 0 43
CRC-Align 0 0
<64 bytes 0 0
>MAX_FL 0 0
<64 CRC 0 0
>MAX_FL CRC 0 0
64 bytes 67 362
65-127 bytes 47 428
128-255 bytes 0 103
256-511 bytes 0 59
512-1023 bytes 3 39
1024-2047 bytes 2 121
>2048 bytes 0 0
Octets 11987 303544
Dropped 0 0
Frames OK 119 1112
FIFO under/over 0 0
Pause frames 0 0
Good octets 11987 303544
Collisions 0
Single collision 0
Multi-collision 0
Deferral delay 0
Late collision 0
Ex. collisions 0
Carrier sense 119
SQE error 0
CRC error 0
Alignment error 0



I am sorry that it is not Arduino but instead is built with GCC from MCUXpresso but it may be of interest to some people or as a reference to creating similar functionality in the Arduino environment.

Regards

Mark

P.S: Note that the same command line interface is available on the USB which will block Telnet sessions when in use - The command "quit" at the USB VCOM session will release it so that Telnet can then be connected to. The IP settings can be configured on the USB interface (LAN menu) and then "MAC 00-00-00-00-00-01" (or other valid MAC address) in the LAN menu (invisible - on-time command) will save a MAC address and restart so that it is all ready for use.
 
Thanks for the hint! For this case I downloaded the library on wednesday again, so it unluckily is the latest version. I also tried to call client.available() without success.
 
I noticed that the LEDs on the ethernet socket of my laptop are on/flash, but the ones on my teensy not. Is it possible, that the socket on the Teensy is defective?
 
If the LEDs on the Teensy socket aren’t on then something is wrong, only one of the two LEDs are connected and it’s setup to show activity when you call Ethernet.begin().
 
I tried to come along with a solution, but haven't find one. The strange thing about it is, that the socket on the Teensy doesn't light up in any way, but the socket on the laptop lights red...
But ping still works.
Does anyone of you have an idea what could be wrong? I already tried several ethernet cables.
 
Hi

The connector on the socket may be 180° reversed. Like this the LED won't work since its polarity is incorrect but the Ethernet still works (as I have heard) since it doesn't care about the line's polarity (coupled by transformer).

Regards

Mark
 
Status
Not open for further replies.
Back
Top