Pages: 1 [2] 3   Go Down
Author Topic: client.connect  (Read 990 times)
0 Members and 1 Guest are viewing this topic.
Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 139
Posts: 5852
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I tested your code on a busy network with a busy server. No instability issues, just some funny ys in the output when the network and the server are busy. On a localnet with no other server traffic, it seems to work fine as long as the server doesn't stall.

edit: If the connection breaks or the server stalls during the download, your code locks up until you restart the Arduino.   smiley-sad
« Last Edit: July 02, 2014, 03:04:55 pm by SurferTim » Logged

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I tested your code on a busy network with a busy server. No instability issues, just some funny ys in the output when the network and the server are busy. On a localnet with no other server traffic, it seems to work fine as long as the server doesn't stall.

edit: If the connection breaks or the server stalls during the download, your code locks up until you restart the Arduino.   smiley-sad

For this  simple test, the server seems fairly robust and I've not needed to add timeout code or other workarounds for bad connections and such. If the posted code does not work for the OP, then maybe he has network issues too.
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Emsworth, UK
Offline Offline
Full Member
***
Karma: 8
Posts: 199
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

If the posted code does not work for the OP, then maybe he has network issues too.
The OP has network issues.

1. Subnet mask has not been assigned.
2. A default gateway has not been assigned.  

1. I haven't checked what happens when a subnet mask is not assigned to the shield.  If it defaults to 0.0.0.0, it could still respond to pings on the local subnet.  The OP has  indicated the mask needs to be no more than 19 bits, for the subnet to encompass the router and the IP specified for the Arduino shield.

2. If you don't set a gateway address, traffic cannot be routed off the local subnet, onto the wide area (Internet).  This will definitely stop you accessing a google server.

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

byte mac[] = { 0x90, 0xa2, 0xDA, 0x0F, 0x45, 0x1A };
byte ip[] = { 172, 20, 30, 50 };
byte subnet[] = {255, 255, 224, 0};  // <-- add these
byte gateway[] = {172, 20, 10, 59};  // <-- parameters
byte dns[] = {8.8.8.8};  //google dns server

byte server[] = { 212, 56, 71, 154 }; // www.google

EthernetClient client;
void setup(void) {
  Ethernet.begin(mac, ip, dns, gateway, subnet); // <--- should fix it
  Serial.begin(9600);
  Serial.println("connecting...");

  if (client.connect(server, 80)) {
    Serial.println("connected");
    client.println("GET /search?q=arduino HTTP/1.0");
    client.println();
  }
  else {
    Serial.println("connection failed");
  }
}
// etc.




« Last Edit: July 03, 2014, 03:26:02 am by MattS-UK » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, it worked.

Code:
Better client test 9/22/12
Send an e in serial monitor to test
connected
HTTP/1.1 200 OK
Date: Thu, 03 Jul 2014 11:55:11 GMT
Server: Apache/2.2.15 (CentOS)
Last-Modified: Sat, 13 Nov 2010 16:31:40 GMT
Accept-Ranges: bytes
Content-Length: 51
Connection: close
Content-Type: text/plain; charset=UTF-8

Woohoo! Your arduino ethernet client works!
zoomkat
disconnecting.
==================

Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 139
Posts: 5852
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If the posted code does not work for the OP, then maybe he has network issues too.
The OP has network issues.

1. Subnet mask has not been assigned.
2. A default gateway has not been assigned.  

1. I haven't checked what happens when a subnet mask is not assigned to the shield.  If it defaults to 0.0.0.0, it could still respond to pings on the local subnet.  The OP has  indicated the mask needs to be no more than 19 bits, for the subnet to encompass the router and the IP specified for the Arduino shield.

2. If you don't set a gateway address, traffic cannot be routed off the local subnet, onto the wide area (Internet).  This will definitely stop you accessing a google server.
1) If you do not assign a subnet mask, the library uses 255.255.255.0 by default.
2) If you do not assign a dns server or gateway, the library uses the assigned IP first three octets and sets the last octet to .1 by default.

edit: It is probably a network setting problem as I stated in replies #5 and #8.
« Last Edit: July 03, 2014, 07:20:04 am by SurferTim » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, that well.

Now, I have a problem that i can not explain.

In this code, sometimes it work, but after several times that i run it, it stop working.
I need to do a initialization to the modem, or I am making here a mistake? Becase when it stops it stop on the line

"if (client) {"

So, can you help me one more time please?

Code:
#include <GSM.h>
#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address and IP address for your controller below.
byte mac[] = {
  0x90, 0xa2, 0xDA, 0x0F, 0x45, 0x1A };
IPAddress ip(172, 20, 30, 50);

EthernetServer server(80);
GSM gsmAccess;
GPRS gprsAccess;
GSMVoiceCall vcs;
GSM_SMS sms;
GSMClient client;

#define PINNUMBER ""
String Number1 = "+31635118920"; 
const char* Nr2 = "+31635118920";
String txtMsg = "Hello from Arduino GSM Shield.";
GSM3ShieldV1DirectModemProvider modemAccess;

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////

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

  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());

  // start the GSM shield
  boolean notConnected = true;
  while(notConnected)
  {
    if(gsmAccess.begin(PINNUMBER)==GSM_READY)
      notConnected = false;
    else
    {
      Serial.println("Not connected");
      delay(1000);
    }
  }
  Serial.println("GSM initialized");


 // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
       
        if (c == '\n' && currentLineIsBlank) {
           //send a standard http response header
           client.println("HTTP/1.1 200 OK");
           client.println("Content-Type: text/html");
           client.println("Connection: close");  // the connection will be closed after completion of the response
           client.println("Refresh: 5");  // refresh the page automatically every 5 sec
           client.println();
           client.println("<!DOCTYPE HTML>");
           client.println("<html>");

          // output the value of each analog input pin
            client.print("Network Test ");
            client.println("<br />");
           
       
          //////////////////////////////////

          Serial.println("SMS From +31635118921 to +31635118920");
          Serial.print("SMS: Hello from Arduino GSM Shield.");
                   
          // send the message   
          sms.beginSMS(Nr2);
          sms.print(txtMsg);
          sms.endSMS();
         
                 
         client.println("SMS Complete");
          //////////////////////////////////


          client.println("</html>");
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        }
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
    Serial.println("client disonnected");
  }

}


void loop() {

}


Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 139
Posts: 5852
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Does it show "new client" on the serial monitor before it stops?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No no, when it doesn't work it stops before it prints "new client"

The last thing he print is "GSM initialized"

I already add a "test lines" and he never gets in on the "if (client)"

And I don't understand why sometimes it works others doesn't...

Normally the first times it work, after some "runs" it stop working, because of that I asked if I can reset the modem each time I run the program or something like that to clear it from "trash".

Thank you

Does it show "new client" on the serial monitor before it stops?

Logged

Emsworth, UK
Offline Offline
Full Member
***
Karma: 8
Posts: 199
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hmm.   Your sketch seems to be suffering from Copy / Paste.

A couple obvious issues
Code:
client.println("Refresh: 5");  // refresh the page automatically every 5 sec
/* So what happens if it takes > 5 seconds to send an SMS? */

// give the web browser time to receive the data
delay(1);
/* 1 ms is no time at all. */
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It is my first time with arduino...

The refresh was used when it was in "void loop()" and I was refresh and presenting the time, to see the seconds passing, now it is not necessary anymore.

the delay I already put "delay(1000);" and makes no difference :/

Hmm.   Your sketch seems to be suffering from Copy / Paste.

A couple obvious issues
Code:
client.println("Refresh: 5");  // refresh the page automatically every 5 sec
/* So what happens if it takes > 5 seconds to send an SMS? */

// give the web browser time to receive the data
delay(1);
/* 1 ms is no time at all. */

Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 139
Posts: 5852
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is it possible that server.available() is returning FALSE every call?
Code:
  EthernetClient client = server.available();
  if (client) {
Maybe you have a socket problem? Add this function to your code and call it just before the server.available() call. If none of the sockets have a status of 0x0, you have a socket problem.
Code:
#include <utility/w5100.h>

byte socketStat[MAX_SOCK_NUM];

void ShowSockStatus()
{
  for (int i = 0; i < MAX_SOCK_NUM; i++) {
    Serial.print(F("Socket#"));
    Serial.print(i);
    uint8_t s = W5100.readSnSR(i);
    socketStat[i] = s;
    Serial.print(F(":0x"));
    Serial.print(s,16);
    Serial.print(F(" "));
    Serial.print(W5100.readSnPORT(i));
    Serial.print(F(" D:"));
    uint8_t dip[4];
    W5100.readSnDIPR(i, dip);
    for (int j=0; j<4; j++) {
      Serial.print(dip[j],10);
      if (j<3) Serial.print(".");
    }
    Serial.print(F("("));
    Serial.print(W5100.readSnDPORT(i));
    Serial.println(F(")"));
  }
}
A socket status list:
0x0 = available.
0x14 = socket waiting for a connection
0x17 = socket connected to a client if a server, or a server if a client.
0x22 = UDP socket.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello again,
sorry I was out for the weekend,

Code:
Socket#0:0x14 80 D:0.0.0.0(0)
Socket#1:0x0 0 D:0.0.0.0(0)
Socket#2:0x0 0 D:0.0.0.0(0)
Socket#3:0x0 0 D:0.0.0.0(0)

So I suppose it is ok. since I have sockets available.
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 139
Posts: 5852
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That is what it should display before any clients connect.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

But the program blocks here.
Is the same question he doesn't pass the next part.

A "new client" is to access to the ip address in the browser?


So this supposed to work? Why it doesn't work? smiley-confuse
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 139
Posts: 5852
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Are you certain your network settings are correct? Here is a test sketch that uses dhcp to get the network settings. Upload and run it. Post the serial monitor output.
Code:
#include <SPI.h>
#include <Ethernet.h>

byte mac[] = {  0x00, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

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

  // disable SD SPI
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

  Serial.print(F("Starting ethernet..."));
  if(!Ethernet.begin(mac)) Serial.println(F("failed"));
  else {
      Serial.print(F("IP: "));
      Serial.println(Ethernet.localIP());
      Serial.print(F("Subnet: "));
      Serial.println(Ethernet.subnetMask());
      Serial.print(F("Gateway: "));
      Serial.println(Ethernet.gatewayIP());
      Serial.print(F("DNS server: "));
      Serial.println(Ethernet.dnsServerIP());
}

void loop() {
}

edit: What do you mean by "blocks here"? Does the code actually quit running, or does it just not execute the "if(client)" part?
« Last Edit: July 08, 2014, 07:45:47 am by SurferTim » Logged

Pages: 1 [2] 3   Go Up
Jump to: