Arduino Ethernet Shield Telnet Client reads nothing (-1)

Hi everybody,

The server is working on the IOCP protocol and i am able to use it from my Iphone and other computers - the arduino ethernet shield works with other programs but with this code here I only get a -1 where I should get

Arn.Resp:1=1:2=1:3=0:4=0:5=0:6=0:7=0:8=0:9=0:10=0:11=0:12=0:13=0:14=0:15=0:16=0:17=0:18=0:19=0:20=0:21=0:22=0:23=0:24=1:25=1:26=0:27=1:28=0:29=0:30=0:31=1:32=0:33=0:34=0:35=0:36=0:37=1:38=0:39=0:40=0:41=0:42=0:43=0:44=0:45=0:46=1:47=0:48=0:49=0:50=0:51=0:52=0:53=0:54=0:55=0:56=0:57=0:58=0:59=0:60=0:61=0:62=0:63=1:64=1:65=0:66=0:67=0:68=0:69=0:70=0:71=1:72=1:73=0:74=0:75=1:76=0:77=0:78=0:79=0:80=0:81=0:82=1:83=1:84=0:85=0:86=0:87=0:88=0:89=0:90=0:91=0:92=0:93=0:94=0:95=0:96=0:97=0:98=0:99=0:100=1:101=1:102=1:103=1:104=1:105=1:106=1:107=1:108=1:109=0:110=0:111=0:112=0:113=0:114=0:115=1:116=1:117=0:118=1:119=0:120=1:121=1:122=1:123=1:124=1:125=1:126=0:127=0:128=0:129=0:130=0:131=0:132=0:133=0:134=0:135=0:136=0:137=0:138=0:139=0:140=0:141=1:142=1:143=0:144=1:145=1:146=0:147=0:148=0:149=0:150=0:151=1:152=0:153=0:154=0:155=1:156=1:157=0:158=0:159=0:160=0:161=0:162=0:163=0:164=0:165=0:166=0:167=0:168=0:169=0:170=0:171=0:172=0:173=0:174=0:175=0:176=0:177=0:178=0:179=0:180=0:181=0:182=0:183=1:184=1:185=1:186=1:187=0:188=0:189=0:190=0:191=0:192=0:193=1:194=0:195=1:196=1:197=0:198=0:199=1:200=1:201=0:202=0:203=1:204=0:205=1:206=1:207=1:208=0:209=1:210=0:211=0:212=1:213=0:214=1:215=0:216=1:217=0:218=1:219=0:220=0:221=1:222=0:223=0:224=0:225=1:226=0:227=0:

(That is for LEDS in a Cockpit)

I know that with client read I only get the first byte (that is the A) but I only get -1

here is the code, I hope you can help me :slight_smile:

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 0, 248 };

byte IOCPServer[] = { 192, 168, 0, 214 };

Client client(IOCPServer, 8092);

void setup()
{
  Ethernet.begin(mac, ip);
  Serial.begin(9600);
  
  delay(1000);
  
  Serial.println("connecting...");
  if (client.connected());
  {
    Serial.println("connected");
  }
  

    client.println(); //now the server usually responds with Arn.Vivo:
    
    //now I subscribe to 227 Variables where the server usually responds with "Arn.Resp:1=0:2=1:3=0 ......... but I get nothing
    client.println("Arn.Inicio:1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:22:23:24:25:26:27:28:29:30:31:32:33:34:35:36:37:38:39:40:41:42:43:44:45:46:47:48:49:50:51:52:53:54:55:56:57:58:59:60:61:62:63:64:65:66:67:68:69:70:71:72:73:74:75:76:77:78:79:80:81:82:83:84:85:86:87:88:89:90:91:92:93:94:95:96:97:98:99:100:101:102:103:104:105:106:107:108:109:110:111:112:113:114:115:116:117:118:119:120:121:122:123:124:125:126:127:128:129:130:131:132:133:134:135:136:137:138:139:140:141:142:143:144:145:146:147:148:149:150:151:152:153:154:155:156:157:158:159:160:161:162:163:164:165:166:167:168:169:170:171:172:173:174:175:176:177:178:179:180:181:182:183:184:185:186:187:188:189:190:191:192:193:194:195:196:197:198:199:200:201:202:203:204:205:206:207:208:209:210:211:212:213:214:215:216:217:218:219:220:221:222:223:224:225:226:227");


    
    Serial.print(client.read());
    
   
}

void loop()
{
 


}
    client.println(); //now the server usually responds with Arn.Vivo:

Do you have any proof of that? How long are you waiting for that response to be generated and returned?

    client.println("Arn.Inicio:1:2:3:4:5:6:<snip>")

Ah, I see. A few nanoseconds. And how long are you waiting for this request to be processed?

    Serial.print(client.read());

Ah, that same few nanoseconds.

I know that with client read I only get the first byte (that is the A) but I only get -1

I wonder why.

it works now ;D

/*
  Telnet client
 
 This sketch connects to a a telnet server (http://www.google.com)
 using an Arduino Wiznet Ethernet shield.  You'll need a telnet server 
 to test this with.
 Processing's ChatServer example (part of the network library) works well, 
 running on port 10002. It can be found as part of the examples
 in the Processing application, available at 
 http://processing.org/
 
 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 
 created 14 Sep 2010
 by Tom Igoe
 
 */

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

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {  
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 
  192,168,0,219 };

// Enter the IP address of the server you're connecting to:
byte server[] = { 
  192,168,0,214 }; 

// Initialize the Ethernet client library
// with the IP address and port of the server 
// that you want to connect to (port 23 is default for telnet;
// if you're using Processing's ChatServer, use  port 10002):
Client client(server, 8092);

void setup() {
  // start the Ethernet connection:
  Ethernet.begin(mac, ip);
  // start the serial library:
  Serial.begin(9600);
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect()) {
    Serial.println("connected");
  } 
  else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
    client.println("Arn.Inicio:1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:22:23:24:25:26:27:28:29:30:31:32:33:34:35:36:37:38:39:40:41:42:43:44:45:46:47:48:49:50:51:52:53:54:55:56:57:58:59:60:61:62:63:64:65:66:67:68:69:70:71:72:73:74:75:76:77:78:79:80:81:82:83:84:85:86:87:88:89:90:91:92:93:94:95:96:97:98:99:100:101:102:103:104:105:106:107:108:109:110:111:112:113:114:115:116:117:118:119:120:121:122:123:124:125:126:127:128:129:130:131:132:133:134:135:136:137:138:139:140:141:142:143:144:145:146:147:148:149:150:151:152:153:154:155:156:157:158:159:160:161:162:163:164:165:166:167:168:169:170:171:172:173:174:175:176:177:178:179:180:181:182:183:184:185:186:187:188:189:190:191:192:193:194:195:196:197:198:199:200:201:202:203:204:205:206:207:208:209:210:211:212:213:214:215:216:217:218:219:220:221:222:223:224:225:226:227");
    char d = client.read();
    Serial.print(d);

}

void loop()
{
  char c;
  
  // if there are incoming bytes available 
  // from the server, read them and print them:
  if (client.available()) {
   
    char c = client.read();
    Serial.print(c);
   
  }

  // as long as there are bytes in the serial queue,
  // read them and send them out the socket if it's open:
  while (Serial.available() > 0) {
    char inChar = Serial.read();
    if (client.connected()) {
      client.print(inChar); 
    }
  }

 }

I have a question: usually you would store the output of client.read in a string right? But how comes that I now get an ouput of the complete response? And how can I use that response (look for 1= or 6= etc.....) to control leds?

marc

I have a question: usually you would store the output of client.read in a string right?

I would, yes.

But how comes that I now get an ouput of the complete response?

Because now you send the request once, and read and print the entire response - one character at a time, over possible millions of executions of loop.

And how can I use that response (look for 1= or 6= etc...

The strtok() function comes to mind.

Notice that you don't need to store the whole response in an array. If you are getting something like:
"Arn.Resp:1=1:2=1:3=0:4=0:5=0:6=0:..." as a response, you don't need to start storing the data until you get a ':'. Then, store the rest of the data, checking each character.

If you get another ':', use strtok() to extract two tokens from the string (lower case s - that's important) - the value before the = and the value after the =. Use atoi() on each token to get numbers. Do something with the numbers, reset the array, and resume reading.

I didn't see your post so I tried a few other things and one way I found was to create a String "String stringAnswer[13] = {""};" which has only 13 positions because the responses I want to get are "Arn.Resp:x=1" or "Arn.Resp:x=0" (everytime something happens I get a single message^^so that is what I tried but when trying to append char c to the string with "stringAnswer += c;" but I get a error message with "incompatible types in assignment of 'char' to 'String[13]'" I don't know what I am doing wrong can you please help me here i am just a hobbyist who "has to program" for his main hobby (building a cockpit)

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


byte mac[] = {  
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 
  192,168,0,219 };


byte server[] = { 
  192,168,0,214 }; 


Client client(server, 8092);
String stringAnswer[13] = {""};
int count = 0;

void setup() {
  // start the Ethernet connection:
  Ethernet.begin(mac, ip);
  // start the serial library:
  Serial.begin(9600);
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect()) {
    Serial.println("connected");
  } 
  else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
    client.println("Arn.Inicio:1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:22:23:24:25:26:27:28:29:30:31:32:33:34:35:36:37:38:39:40:41:42:43:44:45:46:47:48:49:50:51:52:53:54:55:56:57:58:59:60:61:62:63:64:65:66:67:68:69:70:71:72:73:74:75:76:77:78:79:80:81:82:83:84:85:86:87:88:89:90:91:92:93:94:95:96:97:98:99:100:101:102:103:104:105:106:107:108:109:110:111:112:113:114:115:116:117:118:119:120:121:122:123:124:125:126:127:128:129:130:131:132:133:134:135:136:137:138:139:140:141:142:143:144:145:146:147:148:149:150:151:152:153:154:155:156:157:158:159:160:161:162:163:164:165:166:167:168:169:170:171:172:173:174:175:176:177:178:179:180:181:182:183:184:185:186:187:188:189:190:191:192:193:194:195:196:197:198:199:200:201:202:203:204:205:206:207:208:209:210:211:212:213:214:215:216:217:218:219:220:221:222:223:224:225:226:227");
   

}

void loop()
{
 
  
 
  if (client.available()) {
   while(count != 13)
{
  char c = client.read();
  stringAnswer += c;
  count++;
}


if(count == 13);
{
  Serial.print(stringAnswer)
  stringAnswer = {""};
  count = 0;
}
    
  }   
 
   
  }

  // as long as there are bytes in the serial queue,
  // read them and send them out the socket if it's open:
  while (Serial.available() > 0) {
    char inChar = Serial.read();
    if (client.connected()) {
      client.print(inChar); 
    }
  }

 }
String stringAnswer[13] = {""};

This is creating an array of 13 String objects, not one String object capable of holding 13 characters.

thank ALOT!!!! Now I am going to try to use the string XD

Now I am going to try to use the string

I mentioned in an earlier response that it was important to recognize the difference between a String (a class dealing with characters) and a string (a NULL terminated array of characters).