Ethernet Sheild and GSM router

Hi, within my job i use industrial GSM routers (a GSM modem with thernet port) to connect computers on site to the internet and as far as my understanding of them, there a router with a simcard and act like any other router?

So..i have my ethernet shield on my arduino and i have set it to connect to a server i have, this works fine when i share my computers ethernet port and loop the arduino into there (so its using my internet) when i plug it into the GSM router however, it cannot make a connection, yet throws no ethernet connection errors.

in the router log though i do see this:
DHCPDISCOVER(lan0) de:ad:be:ef:fe:ed
info Oct 6 13:27:06 dnsmasq[782] DHCPOFFER(lan0) 192.168.104.146 de:ad:be:ef:fe:ed
info Oct 6 13:27:06 dnsmasq[782] DHCPDISCOVER(lan0) de:ad:be:ef:fe:ed
info Oct 6 13:27:06 dnsmasq[782] DHCPOFFER(lan0) 192.168.104.146 de:ad:be:ef:fe:ed
info Oct 6 13:27:06 dnsmasq[782] DHCPREQUEST(lan0) 192.168.104.146 de:ad:be:ef:fe:ed
info Oct 6 13:27:06 dnsmasq[782] DHCPACK(lan0) 192.168.104.146 de:ad:be:ef:fe:ed WIZnetEFFEED
info Oct 6 13:31:23 kernel lan0: link down
info Oct 6 13:31:25 kernel lan0: link up, 100Mbps, full-duplex, lpa 0x41E1
info Oct 6 13:31:30 dnsmasq[782] DHCPDISCOVER(lan0) de:ad:be:ef:fe:ed
info Oct 6 13:31:30 dnsmasq[782] DHCPOFFER(lan0) 192.168.104.146 de:ad:be:ef:fe:ed
info Oct 6 13:31:30 dnsmasq[782] DHCPREQUEST(lan0) 192.168.104.146 de:ad:be:ef:fe:ed
info Oct 6 13:31:30 dnsmasq[782] DHCPACK(lan0) 192.168.104.146 de:ad:be:ef:fe:ed WIZnetEFFEED
info Oct 6 13:32:59 kernel lan0: link down
info Oct 6 13:33:02 kernel lan0: link up, 100Mbps, full-duplex, lpa 0x41E1

This repeats and repeats, i assume WIZnetEFFEED is the ethernet shield?

Does anyone know what is happeneing above and to why it wont connect?

The GSm router does work, and i can plug it into my computer and browse sites using the GSM.

Note: Since playing ive come to the conclusion that the above is fine... and therefore is having no issue to why i can not connect to the server, could this simply be caused by the delay being over GSM?

Thanks
Andy

could this simply be caused by the delay being over GSM?

That depends on the used software. Show us your sketch, maybe you have an issue there.

Hi, no probs this is the code i have, its probably as basic as it can get as its taken from examples.

Im using the pusherapp library that can be found here:

Ive also tried using just the websocket library & my own server but get the same results:

This is my code, this works when using a normal insternet but not over my GSM router, yet my GSM router works fine and ive tried using my browser to make a connection using the GSM and that too works fine, but put the arduino and GSM together and its a no go.

The code:

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

byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
//byte ip[] = {192,168,2,1};
//byte gateway[] = {192,168,2,1};
//byte subnet[] = {255,255,255,0};

PusherClient client;

void setup() {
 
  Serial.begin(9600);
   Ethernet.begin(mac);

  Serial.print("My IP address: ");
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(Ethernet.localIP()[thisByte], DEC);
    Serial.print(".");
  } 
    Serial.println();
    delay(1000);
    
    Serial.print("Connecting to server... ");
    Serial.println();
    while(client.connect("8bce0224d7dae3e8e75e") == 0){
      Serial.println("Cannont Connect, retyring after 2 seconds...");
      delay(2000);
    }
    
    //connected
    Serial.println("connected");
    client.bind("forward", moveForward);
    client.bind("backward", moveBackward);
    client.subscribe("my_channel");
  
  
}

void loop() {
  if (client.connected()) {
    client.monitor();
  }
  else {

  }
}

void moveForward(String data) {
Serial.println("forward");
}

void moveBackward(String data) {
Serial.println("backward");
}

Many thanks
Andy

In your code there are Serial.print() statements, what output do you get, where does it stop?

Do you get an IP address?

Have you tried a simpler example from Ethernet.h examples, like WebClient? Connecting to a host like www.google.com is a much better test than to a service which may be down while you're trying to connect or may have decided to not answer requests from certain networks.

Hi pylon, yes i get an IP and it tells me its tryign to connect but then obviously doesnt for whatever reason and prints "cannot connect, retrying in 2 seconds..." and it keeps doing this, as you can see in the while loop.

I have tried a simple one and it can connect to www.google.com and can print the get request in the serial monitor, so the connection through the GSM router works. But so does the client to server as, if I plug the arduino into my normal internet it connects to the websocket server fine?

I have also wrote a simple script in js that connects to the server and attatched the GSM to laptop (instead of the arduino) and i can connect to the server fine, its only when the arduino tries to connect to the server using the GSM router that i cannot make a connection?

Any ideas?

Thanks
Andy

Have you tried doing the same connection (to the websocket server) on your laptop using the telnet command? I guess it's not an Arduino problem but one on the network, somewhere between your GSM router and the server.

Have you tried doing the same connection (to the websocket server) on your laptop using the telnet command?

Yes and it works fine.

I guess it's not an Arduino problem but one on the network, somewhere between your GSM router and the server.

But i can plug my laptop into the GSM router and connect to the server fine? Unplug laptop, plug in arduino and cant connect.

thanks
Andy

The error is in the library you're using:

    while(_client.available() == 0 && attempts < maxAttempts)
    {
        delay(100);
        attempts++;
    }
    
    while((line = readLine()) != "") {
        handshake += line + '\n';
    }

The code is waiting for up to 30 seconds for one character to arrive but then the whole answer has to be there completely. I'd guess that the GSM modem is splitting the packets into smaller packets and the library code isn't prepared for that.

Ahh ok, ive looked at this code for while thinking this is where the problem (if any) must lie.

I will give it ago despite my C++ not being to good, but i have to ask, do you have any idea how to impliment a workaround to wait for all packets before proceeding?

Thanks
Andy

Although this is an ugly hack, it might be enough for your needs:

    while(_client.available() < 14 && attempts < maxAttempts)
    {
        delay(100);
        attempts++;
    }
    
    while((line = readLine()) != "") {
        handshake += line + '\n';
    }

This way you're waiting for at least the string to be arrived you're testing for afterwards.

BTW: the author of the library did a lot of work to move the constant strings to the flash memory (PROGMEM) but then uses the String class which wastes more memory than he saved with the first action but making the code much more complex.

Why don't you wait for the server to close the connection? That is the signal it is finished sending packets. If it is like the ethernet library, you should be able to use this. It should wait ten seconds for a packet before aborting the connection.

while(_client.connected()) {
  while(_client.available()) {
    // if a packet arrives, reset the attempts counter
    attempts = 0;
    Serial.write(_client.read());
  }
  // increment counter and wait 1ms
  attempts++;
  delay(1);

  // if the counter has reached the timeout value, close the connection
  if(attempts > 10000) _client.stop();
}
// close the connection on your end after the server closes the connection
// This would be the second stop if there is a timeout, 
// but does not affect the ethernet shield.
_client.stop();

My apology in advance for errors or typos.

edit: I had the wrong delay value. It is now corrected.

HI pylon, ive tried doing what you did, but unfortunatly it hasnt wroked as of yet. Where di you pluck the 14 from? (just trying to understand?)

SurferTim is that code a modification of the c++ library? (which is what im trying to do)

Thanks
Andy

Look here. Is this the code you are using?

All those functions are available with that library also. This is pretty standard TCP stuff. The transport should be transparent.

Yes that is what im using, but its not working when used with a GSM router and we think this is becasue the GSM might be splitting up packets and the library therefore isnt interpreting the handshake correctly.

Andy

The code I posted above does not matter how many packets the response is sent in. It could be one packet with a thousand characters, or a thousand packets with one character. It waits for the server to close the connection. That is why the "while(client.connected())" loop.

Ok, but im not sure if the code you posted was a modification to the "websocket library" code or for use in the arduino IDE, if its for arduino IDE then its pointless as i cant event connect to the server, as ive stated in my above posts.

when i plug the arduino into the GSM router that has a internet connect i cannot connect to the server, yet plugging the arduino into a normal intenet router (home internet) it connects fine.

Andy

I would use my code in your sketch. Where are you using your current code now?

Ok, i dont want to rewrite it all as its all in the above posts, my code is, the problem is and what libaray im using is.

As i said, if you read the early posts you will see that your code to use in my sketch is insignificant as im not trying to download bytes using my sketch aim tryign to make a connection to a pusher-app server or my custom websocket server using a GSM router. The code pylon was referring to was the websocket library code.

Andy

@SurferTim: He cannot take your code because the connection is not closed by the server after the reception of the handshake lines. It just reads the first few lines from the server and checks for the connection OK string, then returns a success flag.

@OP: The code I posted is a replacement for the appropriate code in the library, not for use in your sketch. The library is not prepared for a result not being delivered in one packet. The length of 14 is the length of the string the library is testing for plus carriage return and linefeed. If you get more characters: OK but you have to get at least this much to be able to successfully check for a proper connection initialization.

Cheers for the clearup pylon, i was aware you meant the library code and i have tried changing it but to still no avail.

Any other ideas? Im sure the response string is large than 14 bytes?

Thanks
Andy