Pages: [1]   Go Down
Author Topic: Using the Arduino Ethernet to connect to a website  (Read 487 times)
0 Members and 1 Guest are viewing this topic.
Franquelin
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi everyone, I've been busy making my Arduino Ethernet connect to my website and I finally made it work. So I giving you the working code in the hope that it will help out those in the need. I added my trials and notes within.

Code:

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

byte mac[] = { The mac adress of your Arduino };
//char server[] = "http://www.abc.com"; // Nothing happens
//char server[] = "www.abc.com"; // OK
//char server[] = "abc.com"; // OK
byte server[] = { ip of abc }; // OK

EthernetClient client;

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

  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
  }

  Serial.print("My IP address: ");
  Serial.println(Ethernet.localIP());
  Serial.println();
  Serial.println("connecting...");
  Serial.println();

  if (client.connect(server, 80)) {
    Serial.println("connected");
    Serial.println();
    client.println("GET /arduino.php?q=data HTTP/1.0");
    client.println("Host: abc.com"); // OK -> It solves the Arduino sending the query to abc.com instead of abc.com IP.
    //client.println("Host: www.abc.com"); // OK
    //client.println("User-Agent: arduino-ethernet"); // This doe'nt work.  You can get 403 - Forbidden using this
    client.println("User-Agent: Mozilla/5.0"); // OK -> This solve the 403 - Forbidden problem
    client.println("Connection: close");
    client.println();
  }
  else {
    Serial.println("connection failed");
    client.println();
  }
}

void loop()
{
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.println();
    client.stop();
    for(;;)
      ;
  }
}


Enjoy this Arduino script.
Logged

Pages: [1]   Go Up
Jump to: