Arduino telnet server

2012-06-25 · 513 words · 3 minute read

Today I want to present you a little bit Arduino code because i’ve searched a lot for a working telnet server to control I/O’s over network. The code is based on the “Examples - Ethernet - ChatServer”. So here is my version of the server:

 1#include <SPI.h>
 2#include <Ethernet.h>
 3
 4#define MAX_CMD_LENGTH   25
 5
 6byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0xE3, 0x5B };
 7
 8IPAddress ip(192, 168, 1, 177);
 9IPAddress gateway(192, 168, 1, 1);
10IPAddress subnet(255, 255, 255, 0);
11
12EthernetServer server = EthernetServer(23);
13EthernetClient client;
14boolean connected = false; 
15
16String cmd;

First declaring all needed variables, configure the MAC address, IP address, gateway and subnet mask. The port is set to 23, which is the default telnet port.

1void setup()
2{
3  Ethernet.begin(mac, ip, gateway, subnet);
4  server.begin();
5  pinMode(2, OUTPUT);
6  pinMode(7, INPUT_PULLUP);
7}

Start the server, and configur one output and input pin.

 1void loop()
 2{
 3  client = server.available();
 4
 5  if (client == true) {
 6    if (!connected) {
 7      client.flush();   
 8      connected = true;
 9    }
10    
11    if (client.available() > 0) {
12      readTelnetCommand(client.read());
13    }
14    
15  }
16
17  // check for input
18  if(digitalRead(7) == LOW) {
19    while(digitalRead(7) == LOW);
20    server.println("Input triggered :-)");
21  }
22
23  delay(10);
24}

Here we wait that a clinet connects to the server. If that happens we flush the buffer and set a flag to make sure to not flush the buffer each loop cycle. Then we proof for received data each cycle and jump into the readTelnetCommand function if so. Furthermore we check for input signals and write a text to the client if an input signal is detected.

 1void readTelnetCommand(char c) {
 2
 3  if(cmd.length() == MAX_CMD_LENGTH) {
 4    cmd = "";
 5  }
 6  
 7  cmd += c;
 8  
 9  if(c == '\n') {
10    if(cmd.length() > 2) {
11      // remove \r and \n from the string
12      cmd = cmd.substring(0,cmd.length() - 2);
13      parseCommand();
14    }
15  }
16}

For each received char we check if the maximum command length is exceeded and clear the commad if that happens. Otherwise we append the char to the command. Then we check if the char was a newline. If so we know Return was hit and a commad is completely sent. Now we remove newline and carriage return and try to parse the command.

 1void parseCommand() {
 2  
 3  if(cmd.equals("quit")) {
 4      client.stop();
 5      connected = false;
 6  } else if(cmd.equals("help")) {
 7      server.println("--- Telnet Server Help ---");
 8      server.println("on    : switch on the Main Power");      
 9      server.println("off   : switch off the Main Power");      
10      server.println("quit  : close the connection");       
11  } else if(cmd.equals("on")) {
12      digitalWrite(2, HIGH);
13  } else if(cmd.equals("off")) {
14      digitalWrite(2, LOW);
15  } else {
16      server.println("Invalid command, type help");
17  }
18  cmd = "";
19}

The last step is to check if the received string is a valid command. Then we do the apropriate action for the command or send an error message back if the command was invalid.

Thats it :-)