alejho / Arduino-Telnet-Client

a minimal (but real) telnet client for Arduino, actually able to login and send commands to a generic telnet server
GNU General Public License v3.0
24 stars 24 forks source link

Login Hang #7

Closed 3xpL closed 5 years ago

3xpL commented 5 years ago

Here I am again, I encountered Hang in Login phase. Here's the output:

Initializing : 5
Initializing : 4
Initializing : 3
Initializing : 2
Initializing : 1

connecting...
Connected, Logging in..

a

Basically, I'm making delays for the arduino to initialize the ethernet shield. (5 seconds in my case). after 5 seconds delay. I will try to connect to Mikrotik using this if (client.connect(mikrotikRouterIp, 23)) if connection is successful then arduino will run this code. `delay(5000);
if(tc.login(mikrotikRouterIp, "admin", "MyPass", 23)){ } after executing login code, I got "a" in my serial monitor.

alejho commented 5 years ago

The output you posted is not coming from the library, I have no idea of what you're trying to do and you didn't shared your sketch...I'm sorry but in this way it's nearly impossible to help you...I'm not a wizard!

3xpL commented 5 years ago

void setup () {            
Serial.begin(9600, SERIAL_8N1);        // Setup for Ethernet - 115200 Baudrate, 8bits Parity none.
Ethernet.begin (clientMAC, clientIp);    //
Initialize();
}
void Initialize(){  
int i=5;
while(i != 0)
{ // Start of While Loop
  Serial.println(i);
  delay(1000);
  i -=1;
} // End of While Loop
ConnectToMT(); // if timer goes to zero, try to connect
}
void ConnectToMT{
  int ConnectTry = 1;

  Here:

 delay(1000);
   Serial.println("");
  Serial.println("connecting...");
  if (client.connect(mikrotikRouterIp, 23))
  {
Serial.println("Connected, Logging in..");
login(); // Go to Void Login
  }
       else {
    // if you didn't get a connection to the server:
    ConnectTry += 1;
    Serial.println("connection failed");
    if(ConnectTry == 10){
      delay(1000);
       Serial.println("Rebooting ...");
       delay(1000);
       Serial.println(" ");
      resetFunc(); //Reboot or rerun the whole process
    }
goto Here;
            }
}

void login (){                                            //Step3
    //PUT HERE YOUR USERNAME/PASSWORD
    int LoginTry =1;
ReLogin:  
delay(5000);  
  if(tc.login(mikrotikRouterIp, "admin", "MyPass", 23)){        //tc.login(mikrotikRouterIp, "admin", "", 1234) if you want to specify a port different than 23  
delay(1000);
tc.sendCommand("ip hotspot user");

delay(500);

  }
  else{
    LoginTry +=1;
    if (LoginTry==3){
      resetFunc(); 
    }
    Serial.println("Trying to relogin...");  
    goto ReLogin; 
  }
}

Sorry I forgot to put my codes.

alejho commented 5 years ago

I suggest you to place some more debug "Serial.println" to bettere detect where is the problem (specially in the "login" function). Why do you use "client.connect(mikrotikRouterIp, 23)" in "ConnectToMT" function?...it's not needed since the library itself should manage the connection! With the basic example you can normally login in your router?

3xpL commented 5 years ago

With the basic example you can normally login in your router? Not really, I mean in 10 tries, I can only login once. That's why I put delay and "ConnectToMT" function before logging in because my ethernet shield needs atleast 5-10 seconds to initialize, "ConnectToMT" function serves as "Go Signal" for the Arduino to try logging in.

alejho commented 5 years ago

ok, put a delay as long as you need...(this sound quite strange to me, are you using a standard Ethernet shield?). Once again: why do you use "client.connect(mikrotikRouterIp, 23)" in "ConnectToMT" function?...it's not needed since the library itself should manage the connection...did you try to remove it? I suggest you to investigate using the basic connection sketch, adding the delay you need after the shield initialization.

alejho commented 5 years ago

mt_test.zip

customize this sketch and test it! You can also try to uncomment //#define TNDBG 1 in the header file...it will produce more output that could be useful to me in case of problems!

3xpL commented 5 years ago

Initializing : 5 Initializing : 4 Initializing : 3 Initializing : 2 Initializing : 1

connecting... Connected, Logging in.. Logging in..

login|connecting... login|connected! negotiate|server:IAC negotiate|server:DO negotiate|server opt: 24 negotiate|client:IAC negotiate|client:WONT negotiate|server:IAC negotiate|server:DO negotiate|server opt: 32 negotiate|client:IAC negotiate|client:WONT negotiate|server:IAC negotiate|server:DO negotiate|server opt: 32 negotiate|client:IAC negotiate|client:WONT negotiate|server:IAC negotiate|server:DO negotiate|server opt: 39 negotiate|client:IAC negotiate|client:WONT login|sending username a

Using the code that I dropped (my 2nd comment), this is the output. ^ Btw, I'm using "client.connect(mikrotikRouterIp, 23)" in ConnectToMt function to let Arduino know that the connection is up and ready to login. Since my Ethernet Shield Initializing time was 5-10 seconds and my code is only for 5 seconds initializing, I give my Arduino upto 10 more seconds to initialize.

alejho commented 5 years ago

It seems that the library is stuck on line 99:

https://github.com/alejho/Arduino-Telnet-Client/blob/8bb4a94975261cc04647adce80051a967172c352/TelnetClient.cpp#L99

this means that there's no answer from the router. "client.connect(mikrotikRouterIp, 23)" is not needed since it's already managed by the library here (line 36):

https://github.com/alejho/Arduino-Telnet-Client/blob/8bb4a94975261cc04647adce80051a967172c352/TelnetClient.cpp#L36

Please try to remove it from your code. Did you try the skecth I sent you?can you post its output? Even if this platform is for specific bug reporting and not for application support I'm happy to help you, but please...try to do what I ask you to avoid waste of time. Thank you!

3xpL commented 5 years ago

login|connecting...
login|connection failed!
login failed

login|connecting...
login|connected!
negotiate|server:IAC
negotiate|server:DO
negotiate|server opt: 
24
negotiate|client:IAC
negotiate|client:WONT
negotiate|server:IAC
negotiate|server:DO
negotiate|server opt: 
32
negotiate|client:IAC
negotiate|client:WONT
negotiate|server:IAC
negotiate|server:DO
negotiate|server opt: 
35
negotiate|client:IAC
negotiate|client:WONT
negotiate|server:IAC
negotiate|server:DO
negotiate|server opt: 
39
negotiate|client:IAC
negotiate|client:WONT
negotiate|server:IAC
negotiate|server:WILL
negotiate|server opt: 
3
negotiate|client:IAC
negotiate|client:DO
negotiate|server:IAC
negotiate|server:DO
negotiate|server opt: 
1
negotiate|client:IAC
negotiate|client:WONT
negotiate|server:IAC
negotiate|server:DO
negotiate|server opt: 
31
negotiate|client:IAC
negotiate|client:WONT
negotiate|server:IAC
negotiate|server:WILL
negotiate|server opt: 
5
negotiate|client:IAC
negotiate|client:DO
negotiate|server:IAC
negotiate|server:DO
negotiate|server opt: 
33
negotiate|client:IAC
negotiate|client:WONT
login|sending username
asend|WRONG ECHO FROM SERVER
login failed

This is the output from the sketch file you sent to me. since the first try is failed, I reset the arduino from reset button and it printed this output

alejho commented 5 years ago

this is quite strange since here the behaviour is different (the router is aswering but with a wrong echo). At the first try the connection failed (this is also curious) and you told me you need a long delay for shield init...are you using a standard Ethernet shield?, are you sure that there's no connection problems?

I suggest you to change this line of code in the library:

https://github.com/alejho/Arduino-Telnet-Client/blob/8bb4a94975261cc04647adce80051a967172c352/TelnetClient.cpp#L42

putting "false" in place of "true"...just to see what happens if we force the login to continue.

3xpL commented 5 years ago

I'm using HanRun 18/20 HR911105A ethernet shield, Idk if this is standard. I just ordered it from online store. I tried putting false and replace true and this is the output. Just like earlier, first try failed. I adjusted the delay to 15 seconds.


login|connecting...
login|connection failed!
login failed

login|connecting...
login|connected!
negotiate|server:IAC
negotiate|server:DO
negotiate|server opt: 
24
negotiate|client:IAC
negotiate|client:WONT
negotiate|server:IAC
negotiate|server:DO
negotiate|server opt: 
32
negotiate|client:IAC
negotiate|client:WONT
negotiate|server:IAC
negotiate|server:DO
negotiate|server opt: 
35
negotiate|client:IAC
negotiate|client:WONT
negotiate|server:IAC
negotiate|server:DO
negotiate|server opt: 
39
negotiate|client:IAC
negotiate|client:WONT
negotiate|server:IAC
negotiate|server:WILL
negotiate|server opt: 
3
negotiate|client:IAC
negotiate|client:DO
negotiate|server:IAC
negotiate|server:WILL
negotiate|server opt: 
5
negotiate|client:IAC
negotiate|client:DO
negotiate|server:IAC
negotiate|server:WILL
negotiate|server opt: 
1
negotiate|client:IAC
negotiate|client:DO
negotiate|server:IAC
negotiate|server:DO
negotiate|server opt: 
1
negotiate|client:IAC
negotiate|client:WONT
negotiate|server:IAC
negotiate|server:DO
negotiate|server opt: 
31
negotiate|client:IAC
negotiate|client:WONT
negotiate|server:IAC
negotiate|server:DO
negotiate|server opt: 
33
negotiate|client:IAC
negotiate|client:WONT

MikroTik v6.42.11 (long-term)
login|sending username
alejho commented 5 years ago

You're using an ENC28J60 ethernet shield, I think you must use a specific Ethernet library: Take a look here:

https://github.com/alejho/Arduino-Telnet-Client/issues/1

3xpL commented 5 years ago

I tried installing uipethernet library but it's not working. Ethernet Library works well with my ethernet shield.

alejho commented 5 years ago

did you make this change?: https://github.com/alejho/Arduino-Telnet-Client/issues/1#issuecomment-455781379

In the next days I'm going to set up a virtual machine to run the image of a mikrotik router to replicate your issue!

3xpL commented 5 years ago

Yes, and it doesn't connect. Tried to search some solutions but no luck.

alejho commented 5 years ago

Surprisingly it seems a problem of the Ethernet library! Evern if I try to connect with the "simple" ethernet client (without my library) I'm unable even to connect reliably to the mikrotik router. It's a very strange situation since sometimes it works, sometimes it doesn't...the only acceptable workaround I found is to use a dhcp server. Don't ask me why but if I doesn't assign a static ip to arduino but I get it from a dhcp server everything seems working. I guess you can setup your router as dhcp server and eventually (if you absolutely need a static ip for your arduino) assign always the same ip to your arduino basing on its mac address (dhcp server usually support this feature). I will update the example and the home page of this repo about this issue...thank you for your feedback!

In attachment the sketch I used for my test...could you test it? mt_test_dhcp.zip

alejho commented 5 years ago

I released a new version of this library. I was wrong about the dchp...it's not there the problem. I few words it seems that attaching the serial monitor while the library is negotiating with the server can cause problems at the Ethernet shield/library (to fix this I updated the examples...now the login must be triggered manually ) Also login continuously resetting the board many times in a short time can cause problems you don't have if you wait 20/30 seconds to reset. In both cases the shield stop to work properly and only solution seems to be disconnect/reconnect the power.

3xpL commented 4 years ago

Hi alejho ! I'm back again and i'm using your latest version of library. I'm still encountering this Login Hang. I'm stuck here in logging in. This is the printed output.

`Logging in..

login|connecting... login|connected! `

I'm stuck here. Hoping that you're still working with this. Thank you !