Pages: 1 [2] 3 4   Go Down
Author Topic: Ethernet shield + arduino as a CLIENT, please help!  (Read 3761 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 95
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok, @surfer, i'm gonna understanding right now...
so u say that if i print (client.print) the parameters in the #9 reply i'll be able to send/recieve GET request?
if it is, please tell me which sketch should i use and where i hve to put those lines..

tomorrow i'll aks to my school if i can user their server to host my site, but i prefer your solution with MY website, if u think it's possible...

thx, man!
Logged

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

You should be able to use your website. Use the code in reply #9. Your web hosting company server requires the Host parameter, or it will load your web hosting company's page.

This is the code I use. You will need to modify the GET code to use the Host parameter.
http://playground.arduino.cc/Code/WebClient
It has all the bug fixes I know to prevent lockups. The good part about that code is if you have a question or problem, ask me.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 95
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

could i marry u? i'll try and i'll update you soon! thx for help!
Logged

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

could i marry u?
Not without dinner and several drinks first.  smiley

edit: I changed the GET code to HTTP/1.1. Set the global char[] serverName to your domain name, and it will send the Host and Connection:close for you.
« Last Edit: June 17, 2013, 08:17:22 am by SurferTim » Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 95
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i modified this codewith my domain and ip but the part of the GET it's a little bit difficult, i don't know which parts i have to delete or replace (THE PART IS: "this part |||||||||...")
if you could add in the right method this part of the code u've sent me i'll promise, we go out for dinner! smiley-lol

Code:
client.println(F("GET / HTTP/1.1"));
client.println(F("Host: www.valerioantonangeli.it"));
client.println(F("Connection: close\r\n"));
you have reason, i know, i'm a little bit retarted but in the sketch there are something like: outBuf, sprintf, %s, and something like these and i don't wanna go wrong another one!


Code:
/*
   Web client sketch for IDE v1.0.1 and w5100/w5200
   Uses GET method.
   Posted October 2012 by SurferTim
   Last modified June 17, 2013
*/

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

// this must be unique
byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// change to your network settings
IPAddress ip(192,168,1,2);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);

// change to your server
IPAddress server(46,4,96,70); // Google

//Change to your domain name for virtual servers
char serverName[] = "www.valerioantonangeli.it";
// If no domain name, use the ip address above
// char serverName[] = "74.125.227.16";

// change to your server's port
int serverPort = 80;

EthernetClient client;
int totalCount = 0;
int loopCount = 0;
char pageAdd[32];

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

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

  // Start ethernet
  Serial.println(F("provo a connettermi"));
  Ethernet.begin(mac, ip, gateway, gateway, subnet);

  // If using dhcp, comment out the line above
  // and uncomment the next 2 lines

  // if(!Ethernet.begin(mac)) Serial.println(F("failed"));
  // else Serial.println(F("ok"));
  digitalWrite(10,HIGH);

  Serial.println(Ethernet.localIP());

  delay(2000);
  Serial.println(F("connesso"));
}

void loop()
{
  if(loopCount < 30)
  {
    // if loopCount is less than 30, just delay a second
    delay(1000);
  }
  else
  {
    // every thirty seconds this runs
    loopCount = 0;

    // Modify next line to load different page
    // or pass values to server
    sprintf(pageAdd,"/",totalCount);

    // sprintf(pageAdd,"/arduino.php?test=%u",totalCount);

    if(!getPage(server,serverPort,pageAdd)) Serial.print(F("Fail "));
    else Serial.print(F("Pass "));
    totalCount++;
    Serial.println(totalCount,DEC);
  }   

  loopCount++;
}

byte getPage(IPAddress ipBuf,int thisPort, char *page)
{
  int inChar;
  char outBuf[128];

  Serial.print(F("connecting..."));

  if(client.connect(ipBuf,thisPort))
  {
    Serial.println(F("connected"));
//this part |||||||||||||||||||||||||||||||||||||||||||||
    sprintf(outBuf,"GET %s HTTP/1.1",page);
    client.println(outBuf);
    sprintf(outBuf,"Host: %s",serverName);
    client.println(outBuf);
    client.println(F("Connection: close\r\n"));
//this part||||||||||||||||||||||||||||||||||||||||||||||| 
}
  else
  {
    Serial.println(F("failed"));
    return 0;
  }

  // connectLoop controls the hardware fail timeout
  int connectLoop = 0;

  while(client.connected())
  {
    while(client.available())
    {
      inChar = client.read();
      Serial.write(inChar);
      // set connectLoop to zero if a packet arrives
      connectLoop = 0;
    }

    connectLoop++;

    // if more than 10000 milliseconds since the last packet
    if(connectLoop > 10000)
    {
      // then close the connection from this end.
      Serial.println();
      Serial.println(F("Timeout"));
      client.stop();
    }
    // this is a delay for the connectLoop timing
    delay(1);
  }

  Serial.println();

  Serial.println(F("disconnecting."));
  // close client end
  client.stop();

  return 1;
}
« Last Edit: June 18, 2013, 06:32:13 am by VALERiOSPQR » Logged

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

I changed the ethernet client GET code in the playground to send the Host and Connection parameters.

Feel free to change it any way that fits your requirements. That code fits mine, because I can use it to access multiple servers at different ports and different pages, or the same page with different parameters. You can hard code those values into your getPage() function if your code requires only one server and page.

It is a demo of how to contact a server with the client code using a basic trigger (30 seconds in my demo) and the ability to pass parameters to the server.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 95
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i've tried this one but it doesn't connect... now i'll try directly the sketche u've modified!

Code:
/*
   Web client sketch for IDE v1.0.1 and w5100/w5200
   Uses GET method.
   Posted October 2012 by SurferTim
   Last modified June 17, 2013
*/

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

// this must be unique
byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// change to your network settings
IPAddress ip(192,168,1,2);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);

// change to your server
IPAddress server(46,4,96,70); // Google

//Change to your domain name for virtual servers
char serverName[] = "www.valerioantonangeli.it";
// If no domain name, use the ip address above
// char serverName[] = "74.125.227.16";

// change to your server's port
int serverPort = 80;

EthernetClient client;
int totalCount = 0;
int loopCount = 0;
char pageAdd[32];

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

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

  // Start ethernet
  Serial.println(F("provo a connettermi"));
  Ethernet.begin(mac, ip, gateway, gateway, subnet);

  // If using dhcp, comment out the line above
  // and uncomment the next 2 lines

  // if(!Ethernet.begin(mac)) Serial.println(F("failed"));
  // else Serial.println(F("ok"));
  digitalWrite(10,HIGH);

  Serial.println(Ethernet.localIP());

  delay(2000);
  Serial.println(F("connesso"));
}

void loop()
{
  if(loopCount < 30)
  {
    // if loopCount is less than 30, just delay a second
    delay(1000);
  }
  else
  {
    // every thirty seconds this runs
    loopCount = 0;

    // Modify next line to load different page
    // or pass values to server
    sprintf(pageAdd,"/",totalCount);

    // sprintf(pageAdd,"/arduino.php?test=%u",totalCount);

    if(!getPage(server,serverPort,pageAdd)) Serial.print(F("Fail "));
    else Serial.print(F("Pass "));
    totalCount++;
    Serial.println(totalCount,DEC);
  }   

  loopCount++;
}

byte getPage(IPAddress ipBuf,int thisPort, char *page)
{
  int inChar;
  char outBuf[128];

  Serial.print(F("connecting..."));

  if(client.connect(ipBuf,thisPort))
  {
    Serial.println(F("connected"));
//this part |||||||||||||||||||||||||||||||||||||||||||||
    sprintf(outBuf,"GET / HTTP/1.1",page);
    client.println(outBuf);
    sprintf(outBuf,"Host: www.valerioantonangeli.it);
    client.println(outBuf);
    client.println(F("Connection: close\r\n"));
//this part||||||||||||||||||||||||||||||||||||||||||||||| 
}
  else
  {
    Serial.println(F("failed"));
    return 0;
  }

  // connectLoop controls the hardware fail timeout
  int connectLoop = 0;

  while(client.connected())
  {
    while(client.available())
    {
      inChar = client.read();
      Serial.write(inChar);
      // set connectLoop to zero if a packet arrives
      connectLoop = 0;
    }

    connectLoop++;

    // if more than 10000 milliseconds since the last packet
    if(connectLoop > 10000)
    {
      // then close the connection from this end.
      Serial.println();
      Serial.println(F("Timeout"));
      client.stop();
    }
    // this is a delay for the connectLoop timing
    delay(1);
  }

  Serial.println();

  Serial.println(F("disconnecting."));
  // close client end
  client.stop();

  return 1;
}
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 95
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

it couldn't be possible... the code above this post doesen't function and the code u've modified "for me" on the playgroundarduino doesn't funztion too...

i'm thinking to go in local, it couldn't be possible, i think ther's no solution but really thax for your help, @SurferTim, u're the best! smiley-cool
Logged

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

I downloaded the sketch for the new GET client code from the playground link above. Those are my localnet settings, so I changed only these
Code:
char serverName[] = "www.valerioantonangeli.it";
IPAddress server(46,4,96,70);
I'm now on pass 10 and doing fine.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 95
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

impossible, changing only the srrver name and ipadress and adjusting network settings it doesen't connect.... for me...
Logged

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

Not impossible. Are you required to use a proxy by your ISP? Sometimes that will interfere with the connect/send/receive unless you adjust your ethernet shield settings for a proxy.

edit: Insure you are not using an ip that is already being used by another device. That will cause connection problems.
« Last Edit: June 18, 2013, 10:45:13 am by SurferTim » Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 95
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

no proxy and no doubled ip on my network, thx for the information...
sorry, but u've tried with my parameters and u're able to recieve something like "3=accendi"? from the administration page??
Logged

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

What page do you want me to check? I was downloading your home page. Indirectly this:
Code:
client.println("GET / HTTP/1.1");

edit: If you are using an Uno, try reducing the size of outBuf. Maybe you are getting close to running out of SRAM.
Code:
  char outBuf[64];
« Last Edit: June 18, 2013, 11:25:21 am by SurferTim » Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 95
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thx surfertim, i have to "download", to listen to the amministratore.php orto the portiere.php depending by which  account're logged in!
Ps: arduino mega 2560 r3 + eth shield r3
Logged

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

Then you have to login to access those pages? I cannot access either of those pages from here. If so, then you need to modify your code to login first. It appears you need to send this page these parameters in a POST format to login.
Code:
<FORM ACTION="verifica_credenziali.php" METHOD="POST">
 Username: <INPUT TYPE="text" NAME="username" SIZE=20>
 <br>
 Password:  <INPUT TYPE="password" NAME="password" SIZE=20>
 <br><br>
 <INPUT TYPE="submit" VALUE="Login">
 <br>
 </form>
Logged

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