Do a arduino client + server

Hi,
I get data on website which is on my localhost, this data provide to the arduino and its work, but now how can i get data from the website to the arduino ethernet?

Marinelamoche:
this data provide to the arduino and its work, but now how can i get data from the website to the arduino ethernet?

You say you can get data to the Arduino and then you ask how to use ethernet to get data to the Arduino. That seems contradictory.

Tell us more about what works and what exactly you want to do.

...R

In made early I get an ASCII code in a variable "code" that is an array. Then I make a request to a PHP page that she will check in the database if this code is correct or not. Now once the code is processed by the database, the site will return a response to the Arduino (1 or 0) that then depending on the response or not he opens a door.

So I do not know how to do, I can do the Arduino as a client and server, but not both ie send information and then retrieve via internet

If you are planning on retrieving the data from the PHP server, why do you need a server on the Arduino? As a client, you can send data, and receive data.

Here is a basic client sketch. It sends a request and retrieves a response every 30 seconds, but you can change that timing.
http://playground.arduino.cc/Code/WebClient

I dont how this tutorial can help me

<?php
	require 'app/autoload.php';
	$db = App::getDatabase();

	$badge_read = $_GET['badge'];

	if(isset($badge_read))
	{
		$req = $db->query("SELECT badge FROM users WHERE badge = $badge_read");
		$badge = $req->fetch();
		if($badge)
		{
?>
			<script type="text/javascript">

                var xhr = new XMLHttpRequest();
                xhr.open('GET', '172.17.2.30/?1');
                xhr.send(null);
            
        </script>
<?php
		}else{
?>
			<script type="text/javascript">
                var xhr = new XMLHttpRequest();
                xhr.open('GET', '172.17.2.30/?0');
                xhr.send(null);
        </script>
<?php
		}
	}
	//$req = $db->query("UPDATE bike SET status = ?", [$_GET["tension1"]]);
?>

Thats the code php and js which get a request HTTP to the arduino which will be the server and read.

You want the php computer to be the client? So the computer might move around, but the Arduino will remain stationary on the same network?

If that is the case, then you should use the second example sketch here.
http://playground.arduino.cc/Code/WebServerST

All my projects use the PC/computer as the LAMP server, and the Arduino/ethernet shield as the client. I want the more powerful machine as the server. Besides, if I move the Arduino, it is easier for the Arduino to connect to my server than the other way around. Just my opinion...

 EthernetClient client = server.available();
    if(client){
        boolean currentLineIsBlank = true;
        boolean sentHeader = false;
        while(client.connected()){
            if(client.available()){
                if(!sentHeader){
                    client.println("HTTP/1.1 200 OK");
                    client.println("Content-Type: text/html");
                    client.println();
                    sentHeader = true;
                }
                char c = client.read();
                if(reading && c == ' '){reading = false;}
                if(c == '?'){reading = true;}
                if(reading){
                    Serial.print(c);
                    switch(c){
                        case '1':
                            lChambre = !lChambre;
                            digitalWrite(7, lChambre);
                            break;
                        case '0':
                            eBouton = !eBouton; //acitver/désactiver les interrupteurs
                            break;
                    }
                }
                if(c == '\n' && currentLineIsBlank){break;}
                if(c == '\n'){
                    currentLineIsBlank = true;
                }else if(c != '\r'){
                    currentLineIsBlank = false;
                }
            }
        }
        delay(1); //Permettre au navigateur de tout recevoir
        client.stop();
    }

This code read data on http://172.17.2.30/ when we do

var xhr = new XMLHttpRequest();
                xhr.open('GET', 'http://192.168.1.10/?1');
                xhr.send(null);

but in main program its not work

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

byte mac[] = {0x90, 0xA2, 0xDA, 0x0D, 0x3D, 0x04};
char code[23]; // Tableau de 22 bits qui comportera le code du badge.

// Attribution des broches à des variables qui leur correspond.

int commandePorte = 8;
int commandeBuzzer = 9;
int commandeCapteur = 7;
int accessInterdit = 6;
int accessAutorise = 5;

boolean accessBadge;
boolean etatPorte;

EthernetServer serveur = EthernetServer(80);
IPAddress ip(172,17,2,30);

void setup()
{
  Serial.begin(9600); // Met en place le nombre de bits par secondes pour les transimission d'information par série
  
  // Mise en place des différentes entrées/sorties de la carte.
  pinMode(commandeCapteur, OUTPUT);
  pinMode(commandePorte, OUTPUT);
  pinMode(commandeBuzzer, OUTPUT);
  pinMode(accessInterdit, OUTPUT);
  pinMode(accessAutorise, OUTPUT);
  
  // Si les broches sont configurées en sortie alors on peut mettre 5v ou 0v (HIGH , LOW).
  digitalWrite(commandeCapteur, LOW);
  digitalWrite(commandeBuzzer, LOW);
  digitalWrite(commandePorte, LOW);
  digitalWrite(accessInterdit, LOW);
  digitalWrite(accessAutorise, LOW);
  
  Ethernet.begin(mac, ip); // Initialisation de la librairie ethernet et des paramètres
  serveur.begin();
  delay(1000);
  Serial.println(Ethernet.localIP());
  Serial.println("En cours de conexion..");
}

void loop()
{
  lireBadge();
  EthernetClient client = serveur.available();
   if(client){Serial.print("c");}
  if(accessBadge)
  {
    digitalWrite(commandeBuzzer, HIGH); 
    tone(commandeBuzzer, NOTE_F4, 1000); 
    digitalWrite(commandeBuzzer, LOW); 
    
    digitalWrite(accessAutorise, HIGH);
    delay(1000); // On attend 1s
    digitalWrite(accessAutorise, LOW); 
    
    ouvrirPorte(); 
    if(etatPorte) 
    {
      digitalWrite(commandeCapteur, HIGH); 
      lireCapteur(); 
      delay(2000);
      digitalWrite(commandePorte, LOW);
      delay(3000);
    }
  }else{
    digitalWrite(commandeBuzzer, HIGH);
    tone(commandeBuzzer, NOTE_C6, 1000);
    digitalWrite(commandeBuzzer, LOW);
    
    digitalWrite(accessInterdit, HIGH);
    delay(1000);
    digitalWrite(accessInterdit, LOW);
  }
}
/*************************************************************************/
void ouvrirPorte()
{
  digitalWrite(commandePorte, HIGH);
  delay(5000);
  etatPorte = true;
}

void lireCapteur()
{
   EthernetClient client;
  int valeurCapteur = analogRead(A0);
  Serial.println(valeurCapteur);
  delay(500);
  if(valeurCapteur <= 1000)
  {
    if(client.connect("172.17.2.5", 80))
    {
       client.println("GET /arduino.php?capteur=1 HTTP/1.1");
       client.println("Host: 172.17.2.5");
       client.println("Connection: close");
    }
  }else if(valeurCapteur > 1000){
    if(client.connect("172.17.2.5", 80))
    {
       client.println("GET /arduino.php?capteur=0 HTTP/1.1");
       client.println("Host: 172.17.2.5");
       client.println("Connection: close");
    }
  }
}

void lireBadge()
{
  EthernetClient client;
  while(Serial.available() < 22){}
  for(int i = 0; i < 22; i++)
  {
     code[i] = Serial.read();
  }
  Serial.print(code);
  if(client.connect("172.17.2.5",80))
  {       
      client.print( "GET /arduino.php?");
      client.print("badge=");
      client.print(code);
      client.println( " HTTP/1.1");
      client.println( "Host: 172.17.2.5" );
      client.println( "Content-Type: application/x-www-form-urlencoded" );
      client.println( "Connection: close" );
      client.println();
      client.println();
      client.stop();
   }

   EthernetClient client = server.available();
    if(client){
        boolean currentLineIsBlank = true;
        boolean sentHeader = false;
        while(client.connected()){
            if(client.available()){
                if(!sentHeader){
                    client.println("HTTP/1.1 200 OK");
                    client.println("Content-Type: text/html");
                    client.println();
                    sentHeader = true;
                }
                char c = client.read();
                if(reading && c == ' '){reading = false;}
                if(c == '?'){reading = true;}
                if(reading){
                    Serial.print(c);
                    switch(c){
                        case '1':
                            lChambre = !lChambre;
                            digitalWrite(7, lChambre);
                            break;
                        case '0':
                            eBouton = !eBouton; //acitver/désactiver les interrupteurs
                            break;
                    }
                }
                if(c == '\n' && currentLineIsBlank){break;}
                if(c == '\n'){
                    currentLineIsBlank = true;
                }else if(c != '\r'){
                    currentLineIsBlank = false;
                }
            }
        }
        delay(1); //Permettre au navigateur de tout recevoir
        client.stop();
}

@Marinelamoche, I am not an expert on Arduino Ethernet stuff but I do know that you are not explaining clearly what you want to do.

Try to explain the requirement without using any program code.

...R