Ethernet shield + arduino as a CLIENT, please help!

hi guys, i've made my site (www.valerioantonangeli.it, hosted on a server in a webfarm) to control my arduino project, a model of an house, with sensors, servo, lights,...
the code that control all the project is perfect, i've finally adjusted it...
the website accesses to the database, log the users and sends the parameters (like: "?3=accendi") to the internet...

THE QUESTION IS... i've started this project making arduino server, but now all it's ready except the PART of the code that set ARDUINO AS A CLIENT which connects to my website and listen for incoming values...
i've tried the "WebClientRepeating" exapmle but i wasn't able to make it operate...
Could someone explain me how to do, or post me some code of what i need? (i've searched but i haven't found nothing...)

Thanks in advance, hope u will help me! :%

This works for me. Sends a request and reads the server response every 30 seconds.
http://playground.arduino.cc/Code/WebClient

thank you server, here at home i use a dhcp assignation and then i NAT the ip to the internet but when i will make it at school i'll have a static ip...
netherless i'll try this code and i will update you about "his" work...

if someone has other suggest, please, tell me!

If the school does not have a dhcp server, then you can assign a static ip if the Ethernet.begin(mac) call fails. Something like this.

if(!Ethernet.begin(mac)) {
  Serial.println(F("DHCP failed. Setting static ip."));
  Ethernet.begin(mac, ip, gateway, gateway, subnet);
}
Serial.println(Ethernet.localIP());

ok, thanks! i'll try to explain u the question...

the school gives me an ethernet cable with a static ip directly connected to the internet...

how can i do to try at home now?

the website page (html form) sends parameters using GET (if u wanna see enter with: username: "valeanto94" and psw:"ciao2013xx") and tell me which of the two codes u posted me i have to use cause i've tried the get method but it doesen't work...

the server (of the hosted website) is: 46.4.96.70
i really couldn't understand how to do! :~

REALLY THANKS for your help and ur patience! :smiley:

I am fortunate enough to have static public ips. If you have a static public ip available, then you can test it using your public ip. If not, you will probably be stuck with my code above. It should get you an ip address, etc.

Or you could set your router localnet ports "static" by disabling the dhcp server on that interface. It will be a private ip localnet, but the theory is the same. Once you have a working ip, all else is pretty much the same, with the exception that a static ip does not need to call Ethernet.maintain() to renew your ip.

thanks man, i've understand! i'm going to set my router (not static, only at school i will have it, so i couldn't understand how i will do) as a static ip for arduino and setting the nat parameters with the port 80.
i'll try to use the first sketch (GET METHOD) and i will update u!
8)

ok, setted my router with static ip, i am modifying the GET sketch (the first one u had sent to me) but it isn't commentated too much and it's quite difficult to understand...
...i have two different pages in my website which should commmunicate with arduino, should i put the directory of theme in some part of the code??

which parameters i have to change?

thx!

the server of my website returns a big html page and says that it couldn't understand...

WTF??

should i use a pc with my website loaded on it and attached to a static public ip AS A SERVER, and connect arduino to it??

PLEASE, could someone help me? i'm really confused!!

If it is a virtual hosting server, then it requires a more complex request. Try loading your website home page. I couldn't load that url in your first post (the Host below).

client.println(F("GET / HTTP/1.1"));
client.println(F("Host: www.valerioantonangeli.it"));
client.println(F("Connection: close\r\n"));

should i use a pc with my website loaded on it and attached to a static public ip AS A SERVER, and connect arduino to it??

PLEASE, could someone help me? i'm really confused!!

Below is some arduino client test code that connects to a server and prints the returned info to the serial monitor. If it works for you, then you might try it with your server. also, when you get to school, get a wireless router and connect it to your school internet connection. Then you can have numerous internet devices.

//zoomkat 9-22-12
//simple client test
//for use with IDE 1.0.1
//with DNS, DHCP, and Host
//open serial monitor and send an e to test
//for use with W5100 based ethernet shields
//remove SD card if inserted

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address

char serverName[] = "web.comporium.net"; // zoomkat's test web page server
EthernetClient client;

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

void setup(){

  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    while(true);
  }

  Serial.begin(9600); 
  Serial.println("Better client test 9/22/12"); // so I can keep track of what is loaded
  Serial.println("Send an e in serial monitor to test"); // what to do to test
}

void loop(){
  // check for serial input
  if (Serial.available() > 0) //if something in serial buffer
  {
    byte inChar; // sets inChar as a byte
    inChar = Serial.read(); //gets byte from buffer
    if(inChar == 'e') // checks to see byte is an e
    {
      sendGET(); // call sendGET function below when byte is an e
    }
  }  
} 

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

void sendGET() //client function to send/receive GET request data.
{
  if (client.connect(serverName, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    client.println("GET /~shb/arduino.txt HTTP/1.0"); //download text
    client.println("Host: web.comporium.net");
    client.println(); //end of get request
  } 
  else {
    Serial.println("connection failed"); //error message if no client connect
    Serial.println();
  }

  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read(); //gets byte from ethernet buffer
    Serial.print(c); //prints byte to serial monitor 
  }

  Serial.println();
  Serial.println("disconnecting.");
  Serial.println("==================");
  Serial.println();
  client.stop(); //stop client

}

Wtf? I don't know ehat's happened to my website! The file are all puloaded! :0

I'll try your method but it seems how if the server is protected and i couldn't access to it, it shows parallel desktop information about the server, i think that my get answer couldn't reach my server! -.-

I'll try the sketches you posted me, i hope one of the two will work!
Thks for supporting my project, guys!
@zoomkat @surfertim

I don't think it has anything to do with your files uploaded. I can't reach your site with a web browser. I used nslookup to determine the status of your domain name, and I get no dns resolution for your domain.

I had disabled the dns services trying to do something to open the server's door...

Now it's ok, if u want u can visit and check it!

I see it now. That is a virtual hosting server, so you will need to send the Host parameter with the GET request. See reply #9.

edit: If you want to know how I can tell if it is a virtual server or not, here is how.

C:\Documents and Settings\user>nslookup www.valerioantonangeli.it
Server: ip68-105-28-16.at.at.cox.net
Address: 68.105.28.16

Non-authoritative answer:
Name: valerioantonangeli.it
Address: 46.4.96.70
Aliases: www.valerioantonangeli.it

Then go to your web browser and try to load http://46.4.96.70 . It will not be your web site. Instead it is normally your virtual hosting company's website.

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!

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.

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

VALERiOSPQR:
could i marry u?

Not without dinner and several drinks first. :slight_smile:

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.

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! XD

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!

/*
   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;
}