Was this code helpful for you?

Yes
1
100%
No
0
No votes
 
Total votes : 1

Using the Arduino Ethernet to connect to a website

by dimbeault on Mon Mar 25, 2013 8:44 pm

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: Select all
#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.
Last edited by dimbeault on Thu May 09, 2013 7:14 pm, edited 1 time in total.
User avatar
dimbeault
 
Posts: 51
Joined: Sun Dec 30, 2012 6:15 pm
Location: Franquelin, Québec, Canada

Re: Using the Arduino Ethernet to connect to a website

by adafruit_support_bill on Tue Mar 26, 2013 5:42 am

Thanks for posting your results!
User avatar
adafruit_support_bill
 
Posts: 25601
Joined: Sat Feb 07, 2009 9:11 am

Using the Arduino Ethernet to connect to a website and retreive the data

by dimbeault on Sat Mar 30, 2013 8:34 pm

Hi, this is a modification of the previous code. In a situation where you want to query a PHP script hosted somewhere and retreive the responses.

First build you PHP page :

Code: Select all

<?php
if(isset($_GET['q']))
{
   echo "<arduino>".$_GET['q']."</arduino>";
}
else
{
   echo "No data was received from the arduino...";
}
?>



The $_GET[] will receive the data sended by the Arduino. At this moment you can do your query to sql or anything you want. Me I only resend it back to Arduino. You noticed that I put <arduino></arduino> around my answer. This will simplify getting the answer back.

The Arduino code :

Code: Select all

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

byte mac[] = { The mac adress of your Arduino };
byte server[] = { ip of abc }; // OK

String http_response  = "";
int    response_start = 0;
int    response_end   = 0;

EthernetClient client;

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

  Ethernet.begin(mac, ip);

  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.1");    // Here we send the 'q' field to PHP
    client.println("Host: abc.com");
    client.println("User-Agent: Mozilla/5.0");
    client.println("Connection: close");
    client.println();
  }
  else {
    Serial.println("Connection failed");
    Serial.println();
  }
}

void loop()
{
  if (client.available()) {
    char c = client.read();
    http_response += c;   // We store the response in a string
    Serial.print(c);
  }
  Serial.println();

  if (!client.connected()) {
    Serial.println("Disconnecting.");
    Serial.println();
    client.stop();

    if((http_response.indexOf("<arduino>") > -1 )&&(http_response.indexOf("</arduino>") > -1 )){
                                                                     // We check if the <arduino></arduino> are there.
      response_start = http_response.indexOf("<arduino>")+9;
                                                                     // Where it begins, the 9 is the length of ' <arduino> ',
                                                                     // since we dont want it to appear in the response.
      response_end = http_response.indexOf("</arduino>");
                                                                     // Where it ends
      http_response = http_response.substring(response_start,response_end);
                                                                     // We keep only the response of the website
      Serial.print("Website response : ");
      Serial.println(http_response);
      Serial.println();
    }
    else {
      Serial.println("No response detected...");
      Serial.println();
    }   
    for(;;)
      ;
  }
}



Hope this helps out those of you who want to interact with a website!
User avatar
dimbeault
 
Posts: 51
Joined: Sun Dec 30, 2012 6:15 pm
Location: Franquelin, Québec, Canada