Telnet sending codes problem

Hello
I have a remote power unit (NP-02E). My use will be to send a telnet on / off command to the unit by Arduino One with Ethernet. My problem:
The code works and I can connect the unit, I get the report from the NP-02 as connected. But the command in my "void loop" works only, when I push the ENTER on the computerkeyboart in the send line of the Serialwindow. I tried out to send a "\n", before the command, after the command, nothing helps.
Who tells me what to do.

Thanks thowiba

Testcode:

#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {  0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF} ;
IPAddress ip(192, 168, 1, 2);
IPAddress server(192, 168, 1, 3);

//command for the NP-02E: pset [port] [0=off, 1=on]

EthernetClient client;
const char* on1 = "pset 1 1";
const char* off1 = "pset 1 0";
const char* on2 = "pset 2 1";
const char* off2 = "pset 2 0";
int oneState = 1;
int twoState = 1;

void setup() {

   Ethernet.init(10); 
   Ethernet.begin(mac, ip);

   Serial.begin(9600);

  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
     }

  if (client.connect(server, 23)) {
    Serial.println("connected");
  } 
  else {
    Serial.println("connection failed");
  }
}

void loop() {

 if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  while (Serial.available() > 0) {
    char inChar = Serial.read();

     if (client.connected()) {
        if (oneState ==  0){
        client.println(on1);
        oneState = 1;
        }
      else {
        client.println(off1);
        oneState = 0;
        }
       delay (1000); 
      if (twoState ==  0){
        client.println(on2);
        twoState = 1;
        }
      else {
        client.println(off2);
        twoState = 0;
        }
         delay (20);
      }
   }
}
````````````````````````````````````````````````````````````````````````````````````

Carriage return is a single character (ASCII 13) so if you want send it you do not use "\n", which is a string. Rather you use '\n' which will send a value of 13. Send it after the command to terminate it or concatenate it on the end of the command

I guess you mean an Arduino UNO with an Ethernet Shield.

Your code doesn't react on what you send it simply wait until you send something. The serial monitor by default waits for a return/enter key before it sends anything to the Arduino.

Hello Pylon

This could be exactly the error. But where is the error? Can you help me please?

thowiba

------ Originalnachricht ------

No, because I have no clue what the intended behavior is or what you think is an error. The Serial Monitor of the IDE always waits for a return/enter key before it sends anything to the Arduino. If you don't want that you're free to use another serial terminal program.

My final goal is to send a command to the NP-02E with a push-button on the arduino. This scetch was only as test. I'm a beginner and I expected that the "void loop" works as loop just changes the state of the NP-02E on and off each 2 seconds. Should I take away the

 while (Serial.available() > 0) {
    char inChar = Serial.read();

for this?

Thank you
thowiba

I think I anderstand now this testcode.
Thanks everybody for your help!

thowiba

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.