ArduinoLibs
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Telnet server example

The Shell class provides a Unix-like shell for issuing commands to the Arduino. The LoginShell class extends Shell to also include login functionality. The user is prompted for username and password before access is granted to shell commands.

This example shows how to use LoginShell to provide command-line access over the Internet via the telnet protocol. The example has one command "led" for turning the status LED on D13 on and off.

The full source code for the example follows:

/*
This example demonstrates how to create a simple telnet server.
This example is placed into the public domain.
*/
#include <SPI.h>
#include <Ethernet.h>
#include <Shell.h>
#include <LoginShell.h>
byte macAddress[6] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
int ledPin = 13;
EthernetServer server(23);
EthernetClient client;
bool haveClient = false;
LoginShell shell;
void cmdLed(Shell &shell, int argc, const ShellArguments &argv)
{
if (argc > 1 && !strcmp(argv[1], "on"))
digitalWrite(ledPin, HIGH);
else
digitalWrite(ledPin, LOW);
}
ShellCommand(led, "Turns the status LED on or off", cmdLed);
void setup()
{
// Configure I/O.
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
// Start the serial port for status messages.
Serial.begin(9600);
Serial.println();
Serial.print("Acquiring IP address ... ");
// Start Ethernet running and get an IP address via DHCP.
if (Ethernet.begin(macAddress))
Serial.println(Ethernet.localIP());
else
Serial.println("failed");
// Listen on port 23 for incoming telnet connections.
server.begin();
shell.setMachineName("Arduino");
}
void loop()
{
// Maintain the DHCP lease over time.
Ethernet.maintain();
// Handle new/disconnecting clients.
if (!haveClient) {
// Check for new client connections.
client = server.available();
if (client) {
haveClient = true;
shell.begin(client, 5);
}
} else if (!client.connected()) {
// The current client has been disconnected. Shut down the shell.
shell.end();
client.stop();
client = EthernetClient();
haveClient = false;
}
// Perform periodic shell processing on the active client.
shell.loop();
}