Inviare una GET a un server pubblico

Ciao a tutti!
stavo cercando ci creare un sistema con un server web dove Arduino con la sua Ethernet Shield invia i dati mediante comando GET. Sul server gira un semplice script php che ascolta le GET che arrivano e salva il valore in un file di testo.

Lo sketch l'ho ottenuto facendo poche modifiche all'esempio WebClient presente nel'IDE 1.0
eccolo...

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

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server(192,168,xxx,xxx); // mio server

// Initialize the Ethernet client library
// with the IP address and port of the server 
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

void setup() {
  // start the serial library:
  Serial.begin(9600);
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    for(;;)
      ;
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect(server, 8080)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET /ricezionedati.php?miotesto=testo_inviato HTTP/1.0");
    client.println();
  } 
  else {
    // kf you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

void loop()
{
  // if there are incoming bytes available 
  // from the server, read them and print them:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    for(;;)
      ;
  }
}

e questo è il php che gira sul webserver che ho installato sul mio pc

<?php
    $a=$_GET["miotesto"];
    if (strlen($_GET["miotesto"])){
        $write_file = fopen("file.txt","w");
        fwrite($write_file,$a);
        fclose($write_file);
        }
?>

Il tutto funziona benissimo!!! ma io vorrei poter inviare le GET con Arduino allo stesso script PHP che ho messo sul mio account che mi sono creato su Altervista.org!
Come faccio a far collegare Arduino a "miosito.altervista.org" anziché a 192.168.xxx.xxx (che è il server che gira sul mio pc)???

Grazie in anticipo e W ARDUINO!!!

devi usare un server DNS che trasmorma l'indirizzo HTTP (miosito.altervista.org) in indirizzo IP. dovresti trovare delle librerie già fatte.

Cerca l'esempio DNSWebClient.
Nell'IDE 1.0 l' trovi sotto File --> Examples --> Ethernet.

devi usare un server DNS che trasmorma l'indirizzo HTTP (miosito.altervista.org) in indirizzo IP. dovresti trovare delle librerie già fatte.

purtroppo non è sufficiente, anche qui se ne era parlato:

Il fatto è che su web gran parte degli host (gratuiti o meno che siano), utilizzano Virtual Server.
Questo significa che non c'è rapporto 1:1 tra indirizzo web e indirizzo ip.

Lato server infatti, in base al fqdn name presente nella chiamata (es xxxx.altervista.org), la richiesta viene indirizzata verso un server virtuale o un'altro.
La prova del 9:

$ nslookup xxxxxx.altervista.org
...
Non-authoritative answer:
Name:	xxxxxx.altervista.org
Address: 78.46.92.147

Quindi l'indirizzo ip di xxxxxx.altervista.org è 78.46.92.147
Se su web browser inserisco quell'ip, mi si aprirà però la pagina di altervista (http://it.altervista.org/)

Questo è un esempio con Altervista, perchè qui se ne parla e anche nel thread aperto da ratto93.
La stessa cosa però vale per la maggior parte di web hosting (gratuiti o meno che sia) - discorso diverso se si ha un server dedicato, ma dubito che qui ce l'abbiano i molti...

Dopo il thread i ratto93, mi son messo a ragionare sulla cosa, e mi son reso conto di avere la risposta sotto il naso.

La chiamata GET accetta come parametro nell'header anche Host, in specifico:

The "Host" header distinguishes between various DNS names sharing a single IP address, allowing name-based virtual hosting.

(HTTP - Wikipedia)

Quindi, rivedendo il nostro codice, come indirizzo del server andrà messo l'ip di altervista (esempio calato a pennello per il ns. caso), e nella richiesta basterà aggiungere nell'header il parametro Host, con il nostro fqdn (xxxxx.altervista.org)

Esempi pratico, adattato dall'esempio WebClient dell'IDE, con riferimenti al threadi di ratto93:

/*
  Web client
 
 This sketch connects to a website (http://www.google.com)
 using an Arduino Wiznet Ethernet shield. 
 
 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 
 created 18 Dec 2009
 by David A. Mellis
 
 */

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

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 10,0,10,5 };
byte subnet[] = { 255,255,0,0 };
byte gateway[] = { 10,0,3,79 };
byte server[] = { 176, 9, 63, 22}; //altervista ip, risultato del ping ratto93.altervista.org

// Initialize the Ethernet client library
// with the IP address and port of the server 
// that you want to connect to (port 80 is default for HTTP):
Client client(server, 80);

void setup() {
  // start the Ethernet connection:
  Ethernet.begin(mac,ip,gateway,subnet);
  // start the serial library:
  Serial.begin(115200);
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect()) {
    Serial.println("connected");
    // Make a HTTP request:

    client.println("GET /index.php?temperatura=22&umidita=22 HTTP/1.0");    //GET da effettuare
    client.println("Host: ratto93.altervista.org");                                           //parametro Host per gestione virtual server
    client.println();                                                                                  //riga vuota

  } 
  else {
    // kf you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

void loop()
{
  // if there are incoming bytes available 
  // from the server, read them and print them:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    for(;;)
      ;
  }
}

Questo metodo permette di utilizzare Arduino con praticamente tutti i servizi di web hosting su web, basati su Virtual Server (ovvero quasi tutti).

Grande ragazzi!!! funziona alla perfezione! Grazie mille! :slight_smile:

ciao ciao