Arduino as a Telnet CLient

Hi All,

I just got my Ethernet Shield and started off with the Ethernet TelnetClient example. My goal is to write a program to let the arduino run a telnet command get the answer (but before that delete some parts of it) and then use the value for some 7 Segments and servos etc....

1.My first question is concerning the IP Settings: In out network we use DHCP to distribute the IPs and DNS, Gateway etc... On the Arduino I see I need to use a static IP address as I am using on the Telnet Server HOST. Now do I only need to set the IP and a random MAC-Address (I got the shield from DFROBOT with no default) and have the connection working with the network?

2.Now I connect to the telnet server by setting his IP and port. That works. But now there is question 2: how do i tell the Arduino to run the command "Arn.Preg:123:" ?

3.Now I would store the output with the read command in a variable and pass it on in my program. But first I need to get rid of the Arn.Resp and the ":" and only get the number. is there a way to do so? (probably there is but I didn't find it.)

I hope you can help me with my program, i really "love" my Arduino it is BRILLIANT!!!!

best regards,
Marc

1.My first question is concerning the IP Settings: In out network we use DHCP to distribute the IPs and DNS, Gateway etc... On the Arduino I see I need to use a static IP address as I am using on the Telnet Server HOST. Now do I only need to set the IP and a random MAC-Address (I got the shield from DFROBOT with no default) and have the connection working with the network?

Just start with a static IP address, there are DHCP libs for Arduino that can be added later. Sometimes the MAC address is on a sticker on the shield (or the bag/box it came in), you musttake care that the MAC and IP addresses are unique.

2.Now I connect to the telnet server by setting his IP and port. That works. But now there is question 2: how do i tell the Arduino to run the command "Arn.Preg:123:" ?

Please clarify, is this a string that the Arduino must send to the server, or something the Arduino gets back from the server.

3.Now I would store the output with the read command in a variable and pass it on in my program. But first I need to get rid of the Arn.Resp and the ":" and only get the number. is there a way to do so? (probably there is but I didn't find it.)

Yes, you should parse the string. If it is the same text allways, you can get the substring by copying only the relevant part. This can be done with strcpy, see snippet.

There are zillion examples about parsing, search for substring parsing split fields etc.

char out[10];
strcpy(&incoming[10], out);

succes
Rob

ok, so now question 1 and 3 is clear. With no2 I meant:

I want the Arduino to send the command "Arn.Preg:123:" to the server.

thanks a lot!!!
Marc

based upon - Ethernet - Arduino Reference -

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 10, 0, 0, 177 };  <<<<<<<<<<< fill in your own
byte telnetServer[] = { 64, 233, 187, 99 };   <<<<<<<<<<< fill in your own

Client client(telnetServer, 23);

void setup()
{
  Ethernet.begin(mac, ip);
  Serial.begin(115200);

  delay(1000);

  Serial.println("connecting...");

  if (client.connect()) {
    Serial.println("connected");
    client.println("Arn.Preg:123:");
    client.println();
  } else {
    Serial.println("connection failed");
  }
}

void loop()
{
  if (client.available() > 0) {
    char c = client.read();
    Serial.print(c);
  }

  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    for(;;)
      ;
  }
}

Ok that helped a lot^^but I still can't connect to the host. From Terminal (i'm using mac) I can connect easily with " telnet x.x.x.x 8090" and then it works but here it never gets a connection.... I also added some delay to it after Ethernet.begin and/or before the read, write....

marc

even from my iphone i can connect to the server so it definitely is a problem in the code

did you change this line ? - Client client(telnetServer, 8090); // 23 is the default telnet port

yes I did... I am a bit lost..........

Hypothesis:
Problem could be the subnetmask; as you use an 10.x.x.x network, the netmask should be 255.0.0.0 iso 255.255.255.0 (Class A network IIRC ) which is automatically made when the subnet and gateway are not passed to the Ethernet class = see sources of ethernet.lib

Could you try this.

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 10, 0, 0, 177 };  <<<<<<<<<<< fill in your own
byte sn[] = { 255, 0, 0, 0};  
byte gw[] = { 10, 0, 0, 1};  <<<<<<<<<<< fill in your own
byte telnetServer[] = { 64, 233, 187, 99 };   <<<<<<<<<<< fill in your own

Client client(telnetServer, 8090);

void setup()
{
  Ethernet.begin(mac, ip, nm, gw);
  Serial.begin(115200);

  delay(1000);

  Serial.println("connecting...");

  if (client.connect()) {
    Serial.println("connected");
    client.println("Arn.Preg:123:");
    client.println();
  } else {
    Serial.println("connection failed");
  }
}

void loop()
{
  if (client.available() > 0) {
    char c = client.read();
    Serial.print(c);
  }

  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    for(;;)
      ;
  }
}

Have you gotten your arduino to successfully act as a client using any of the sample codes?

ok, it still doesn't work, here is the code I use (I set the Servers Subnet and Gateway because he is in the same network)

//copyright by Ma®c (Marc Gyöngyösi) 2011

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 0, 111 };
byte sn[] = { 255, 255, 255, 0 };
byte gw[] = { 192, 168, 0, 254 };
byte IOCPServer[] = { 192, 168, 0, 227 };

Client client(IOCPServer, 8090);

void setup()
{
  Ethernet.begin(mac, ip, sn, gw);
  Serial.begin(9600);
  
  delay(1000);
  
  Serial.println("connecting...");
  
  if (client.connect()) 
  {
    Serial.println("connected");
  }
  
  else
  {
    Serial.println("connection failed");
  }
}

void loop()
{
 
 //main Information read from client
  if (client.available() > 0) 
  {
    client.println("Arn.Preg:724");
    client.println();
    delay (100);
    char c = client.read();
    Serial.print(c);
  }
  
  //turn OFF when disconnect
   if (!client.connected()) 
  {
    Serial.println();
    Serial.println("disconnecting");
    client.stop();
    for(;;);
  }
}

And the output it generates is ....?

Does the Telnet server returns any char on opening port 8090? If not the line

if (client.available() > 0)

will never be true...

small improvement...

void loop()
{
  if (client.connected()) 
  { 
    client.println("Arn.Preg:724");
    client.println();
    delay (100);
   //main Information read from client
    if (client.available() > 0) 
    {
      char c = client.read();
      Serial.print(c);
    }
    else
    {
      Serial.println(" no answer");
    }
  }
  
  //turn OFF when disconnect
  if (!client.connected()) 
  {
    Serial.println();
    Serial.println("disconnecting");
    client.stop();
    for(;;);
  }
}

robtillaart:
Problem could be the subnetmask; as you use an 10.x.x.x network, the netmask should be 255.0.0.0 iso 255.255.255.0 (Class A network IIRC )...

CIDR networks obsoleted class A networks a very long time ago. Very, very few people who use 10.x networks still use the entire 10.0/8 as a single broadcast domain.

nothing works for me... I tried it with this:

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 0, 111 };
byte sn[] = { 255, 255, 255, 0 };
byte gw[] = { 192, 168, 0, 254 };
byte IOCPServer[] = { 192, 168, 0, 227 };

Client client(IOCPServer, 8090);

void setup()
{
  Ethernet.begin(mac, ip, sn, gw);
  Serial.begin(9600);
  
  delay(1000);
  
  Serial.println("connecting...");
  

}

void loop()
{
 
 //main Information read from client
 
    client.println("Arn.Preg:724");
    client.println();
    delay (100);
    char c = client.read();
    Serial.print(c);
 
  
}

but all I get on the Serial Port is "connecting... ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ"

This must work somehow.... is there anything else I have to consider? PLEASE I NEED HELP ...

btw: can I use the MAC Address that is in the code or do I have to change it?

edit I just found something interesting - arduino tells me "disconnecting" and not connection failed, what means, that he probably gets over the setup function but then he can't communicate in the main function (i hope you know what I mean)

connecting... ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ

ÿ = char(255) also known as -1; => meaning no character is available.

cause: You call read() without checking available();
furthermore you send the command to the server every loop!
is the empty println() needed?

Q: Do you have a link to the specification of the commands to be send to the server

Q: Did you try my modified loop() posted earlier?

Please try sending the command once

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 0, 111 };
byte sn[] = { 255, 255, 255, 0 };
byte gw[] = { 192, 168, 0, 254 };
byte IOCPServer[] = { 192, 168, 0, 227 };

Client client(IOCPServer, 8090);

void setup()
{
  Ethernet.begin(mac, ip, sn, gw);
  Serial.begin(9600);
  
  delay(1000);
  
  Serial.println("connecting...");
   client.println("Arn.Preg:724");
   client.println();
}

void loop()
{
  delay (100);
  char c = client.read();
  Serial.print(c);
}

I tried the code and the only thing I get is connecting... and no output....

q1: you mean the developer of the program to run the server or the command itself?

here is the readme: http://www.fransedano.net/wp-content/uploads/2010/08/uipcx-doc-en.txt

the program is called UIPCX and runs as a PLUG in for Xplane ( a flight simulator) It distributes variables that are preset in a config over network, that way you can run various programs that use the IOCP server (that is what it is called) for example SIOC or PRosim737. In the readme of this plugin there is a short example to test if the server is running. It says you can connect with telnet and then can either call a variable with the command "Arn.Preg:number of variable:" (I corrected the ":" in your sample code) or subscribe to a feed for that variable that gets updatet
q2: I did and it gives me: connecting.... disconnecting

Strange,

I changed few lines again - see code - please give it a try ...

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 0, 111 };
byte sn[] = { 255, 255, 255, 0 };
byte gw[] = { 192, 168, 0, 254 };
byte IOCPServer[] = { 192, 168, 0, 227 };

Client client(IOCPServer, 8090);

void setup()
{
  Ethernet.begin(mac, ip, sn, gw);  <<< added sn & gw explicitely
  Serial.begin(115200);

  delay(1000);

  Serial.println("connecting...");

  if (client.connect()) 
  {
    Serial.println("connected");
    // catch any prompt or so.
    while (client.available() > 0)  Serial.print(client.read());
    Serial.println();

    // send command
    client.println("Arn.Preg:123:");
    // client.println();  <<<<<<<<<<<<<<<<<<<<< REMOVED as an empty commandstring might cause disconnect
  } 
  else 
  {
    Serial.println("connection failed");
  }
}

void loop()
{
  while (client.available() > 0)    // changed if to while to get all chars in buffer
  {
    char c = client.read();
    Serial.print(c);
  }

  if (!client.connected())
  {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    for(;;);
  }
}

nothing works for me. I just tried the other examples and it seems as if the Arduino doesn't connect to my network. Neither the TX/RX/LINK LEDS on the board or the leds on my switch or router light up when I connect it. Maybe it is defect???? I just emailed the vendor to see if I can get a new one.

Is there anything else I can try??? I just want to see if the shield works....

marc

mrmaster:
nothing works for me. I just tried the other examples and it seems as if the Arduino doesn't connect to my network. Neither the TX/RX/LINK LEDS on the board or the leds on my switch or router light up when I connect it. Maybe it is defect???? I just emailed the vendor to see if I can get a new one.

That's a layer-1 problem - you don't have an Ethernet link, and there's no point trying anything in sketches until you do.

How are you powering the Arduino and Ethernet shield? If it's USB powered, the voltage may not be high enough. I have a Freetronics Etherten which needs to be powered from an external 12V PSU for the Ethernet chip to initialise properly.