CALL PHP FILE

Hi everyone ,
I’m trying to call a php file to change a Field in my database hosted in www.hostinger.fr but i’ve tried many topics but no way.
Here is my code :

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

 
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = {192,168,1,65};
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(80); //server port
Servo porteservo;  // create servo object to control a servo 
Servo garageservo;  // create servo object to control a servo 


String readString;
 int vent = 3 ;
 int motporte = 2 ;
 int motfenetre = 5 ;
 int lampe = 6 ;
 int motgarage = 7 ;
 int capteurgas = 8 ;
 int capteurfumee = 9 ;
 int ledgas = 13 ;
 const int analogInPingas = A0 ;
 int gassensorValue = 0;
//////////////////////
 
void setup(){
  porteservo.attach(motporte);  // attaches the servo on pin 9 to the servo object
  garageservo.attach(motgarage);  // attaches the servo on pin 9 to the servo object
  pinMode(vent, OUTPUT); //pin selected to control
  pinMode(motporte, OUTPUT); //pin selected to control
  pinMode(motfenetre, OUTPUT); //pin selected to control
  pinMode(lampe, OUTPUT); //pin selected to control
  pinMode(motgarage, OUTPUT); //pin selected to control
  pinMode(ledgas, OUTPUT); 
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
  //the pin for the servo co
  //enable serial data print
  Serial.begin(9600);
  Serial.println("server LED test 1.0"); // so I can keep track of what is loaded
}
 
void loop(){
  // read the analog in value:
  gassensorValue = analogRead(analogInPingas);
  if (gassensorValue >= 750)
  {
    digitalWrite(ledgas, HIGH);   // sets the LED on
  }
  else
  {
  digitalWrite(ledgas, LOW);    // sets the LED off
  }

  // print the results to the serial monitor:
  Serial.print("sensor = " );                       
  Serial.println(gassensorValue);     

  // wait 10 milliseconds before the next loop
  // for the analog-to-digital converter to settle
  // after the last reading:
  delay(5000);
  
  
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
 
        //read char by char HTTP request
        if (readString.length() < 100) {
 
          //store characters to string
          readString += c;
          //Serial.print(c);
        }
 
        //if HTTP request has ended
        if (c == '\n') {
 
          ///////////////
          Serial.println(readString); //print to serial monitor for debuging
 
          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: text/html");
          client.println();
 
          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<meta name='apple-mobile-web-app-capable' content='yes' />");
          client.println("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />");
          client.println("<link rel='stylesheet' type='text/css' href='http://homeautocss.net84.net/a.css' />");
          client.println("<TITLE>Home Automation</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");
          client.println("<H1>Home Automation</H1>");
          client.println("<hr />");
          client.println("
");
         
          client.println("<a href=\"/?venton\"\">Ouvrir ventilateur</a>");
          client.println("<a href=\"/?ventoff\"\">Fermer ventilateur</a>

");
          client.println("

");        
          client.println("<a href=\"/?porteon\"\">Ouvrir porte</a>");
          client.println("<a href=\"/?porteoff\"\">Fermer porte</a>
");
          client.println("

");
          client.println("<a href=\"/?feneton\"\">Ouvrir fenetre</a>");
          client.println("<a href=\"/?fenetreoff\"\">Fermer fenetre</a>
"); 
          client.println("

");
          client.println("<a href=\"/?lighton\"\">Ouvrir lampe</a>");
          client.println("<a href=\"/?lightoff\"\">Eteindre lampe</a>
"); 
          client.println("

");
          client.println("<a href=\"/?garageon\"\">Ouvrir porte garage</a>");
          client.println("<a href=\"/?garageoff\"\">Fermer porte garage</a>
");
          client.println("

");
          client.println("<a href=\"/?entree\"\">Scénario d'entrée</a>");
          client.println("<a href=\"/?sortie\"\">Scénario de sortie</a>
");  
 
          client.println("</BODY>");
          client.println("</HTML>");
 
          delay(1);
          //stopping client
          client.stop();
 
          ///////////////////// control arduino pin
          
                               
          
          
          
          
          
          
                    if(readString.indexOf("?venton") >0)//checks for on
          {
            digitalWrite(vent, HIGH);    // set pin 4 high
            Serial.println("Ventilateur En Marche");
            
            
            
            
            
            
          }
          else{
          if(readString.indexOf("?ventoff") >0)//checks for off
          {
            digitalWrite(vent, LOW);    // set pin 4 low
            Serial.println("Ventilateur En Arrêt");
          }
          else{ if(readString.indexOf("?porteon") >0)//checks for off
          {
            
            porteservo.write(90);    // set pin 4 low
            
            delay(3);
           
            
            Serial.println("Porte Ouverte");
          }
          else{ if(readString.indexOf("?porteoff") >0)//checks for off
          {
            
            porteservo.write(1);    // set pin 4 low
            delay(3);
            
            Serial.println("Porte Fermée");
          }
          else{ if(readString.indexOf("?feneton") >0)//checks for off
          {
            digitalWrite(motfenetre, HIGH);    // set pin 4 low
            Serial.println("Fenetre ouverte");
          }
          else{ if(readString.indexOf("?fenetoff") >0)//checks for off
          {
            digitalWrite(motfenetre, LOW);    // set pin 4 low
            Serial.println("Fenetre Fermée");
          }
           else{ if(readString.indexOf("?lighton") >0)//checks for off
          {
            digitalWrite(lampe, HIGH);    // set pin 4 low
            Serial.println("Lampe Allumée");
          }
           else{ if(readString.indexOf("?lightoff") >0)//checks for off
          {
            digitalWrite(lampe, LOW);    // set pin 4 low
            Serial.println("Lampe Eteinte");
          }
             else{ if(readString.indexOf("?garageon") >0)//checks for off
          {
            garageservo.write(150);    
            delay(3);
            Serial.println("Porte Garage Ouverte");
          }
             else{ if(readString.indexOf("?garageoff") >0)//checks for off
          {
            garageservo.write(0);
            delay(3);
            Serial.println("Porte Garage Fermée");
          }
            else{ if(readString.indexOf("?entree") >0)//checks for off
          {
            digitalWrite(vent,HIGH);
            Serial.println("Ventilateur En Marche");
            digitalWrite(motgarage, HIGH);    // set pin 4 low
            Serial.println("Porte Garage Ouverte");
            digitalWrite(lampe,HIGH);
            Serial.println("Lampe Allumée");
            delay(10000);
            digitalWrite(lampe , LOW);
            digitalWrite(motgarage , LOW);
            
          }
            else{ if(readString.indexOf("?sortie") >0)//checks for off
          {
            digitalWrite(vent, LOW);   // set pin 4 low
            Serial.println("Ventilateur Fermée");
            digitalWrite(lampe,HIGH);
            Serial.println("Lampe Allumée");
            digitalWrite(motgarage,HIGH);
            Serial.println("Porte Garage Ouverte");
            delay(10000);
            digitalWrite(lampe,LOW);
            digitalWrite(motgarage , LOW);
          }
          
          
          
            }}}}}}}}}}}
          
          
          
          
          //clearing string for next read
          readString="";
 
        }
      }
    }
  }
}

The code i want to search for solution is :

{
            digitalWrite(vent, HIGH);    // set pin 4 high
            Serial.println("Ventilateur En Marche");
            if (client.connected()) {
              client.println("smarthousesfax.bl.ee/venton.php");
              client.println("Host smarthousesfax.bl.ee");
            }

And my other problem is with android
How to make my application listening to every changing in the field of database
it means if
if(readString.indexOf("?venton") >0)
a notification will be sent to the application
Think you

I can’t help with the Android part of the question, but here is a working example I have of using a Mega with an Ethernet shield to regularly (twice a minute) send a HTTP GET statement to my local web server.

If you are using a 328 or 32u4 chip, the first thing I would check is to see if you are running out of RAM. When using the Ethernet board I have found RAM on the smaller chips to be very tight and the errors are not easy to find.

#include <Time.h>
#include <Wire.h>
#include <SPI.h>
#include <Ethernet.h>
#include <Adafruit_BMP085.h>
#include "DHT.h"
#include "Adafruit_MCP9808.h"
#include <MCP79412RTC.h>
#include <SFE_TSL2561.h>

#define DHTTYPE DHT22  

const int DHTPIN=5;
const int ssPin = 53;      
const int http_port = 80;

byte mac[] = { 0xAC, 0x03, 0xFB, 0xB4, 0xEE, 0x00 };
char memoria[] = "192.168.0.100";
char location[] = "INDOOR";
//char location[] = "OUTDOOR";
unsigned char gain;     // Gain setting, 0 = X1, 1 = X16;
unsigned int ms;  // Integration ("shutter") time in milliseconds
static long lastPrint=0;

IPAddress ip(192,168,0,199);
IPAddress myDns(192,168,0,1);

DHT dht(DHTPIN, DHTTYPE);
Adafruit_BMP085 bmp;
Adafruit_MCP9808 tempsensor = Adafruit_MCP9808();
SFE_TSL2561 light;
EthernetClient client;

void httpRequest(char *query) {
  // if there's a successful connection:
  if (client.connect(memoria, http_port)) {
    // send the HTTP GET request:
    client.println(query);
    Serial.println(query);
    client.println("Host: 192.168.0.100");
    client.println("User-Agent: arduino");
    client.println("Connection: close");    
    client.println();
    delay(50);
    client.stop();
  } 
  else {
    // if you couldn't make a connection:
    Serial.println("connection failed");
    Serial.println("disconnecting.");
    client.stop();
  }
}

void setup() {
  Serial.begin(115200);
  
  delay(1000);
  // start the Ethernet connection using a fixed IP address and DNS server:
  Ethernet.begin(mac, ip, myDns);

  setSyncProvider(RTC.get);
  if (timeStatus() != timeSet)
    Serial.println("Unable to sync with the RTC");

  if (!tempsensor.begin()) {
    Serial.println("Couldn't find MCP9808!");
    while (1);
  }
  if (!bmp.begin()) {
	Serial.println("Could not find a valid BMP085 sensor, check wiring!");
	while (1) {}
  }
  dht.begin();
  light.begin();
  gain = 0;
  unsigned char time = 2;
  light.setTiming(gain,time,ms);
  light.setPowerUp();
}
  
void loop() 
{
  if ((millis() - lastPrint) > 30000L)
  {
    lastPrint = millis();
    char buffer[250];
    long dht_h = dht.readHumidity() * 10;
    long mcp_c = tempsensor.readTempC() * 100;
    long mcp_f = ((float) (tempsensor.readTempC() * 9.0 / 5.0 + 32.0))*100;
    long bmp_p = ((bmp.readPressure() / 1000.0) * 0.3)*100;
    unsigned int data0;
    unsigned int data1;
    light.getData(data0, data1);
    double lux;
    boolean good = light.getLux(gain,ms,data0,data1,lux);
    long llux = lux*100;

    sprintf(buffer, "GET /apps/add_env.php?rec_date=%04d-%02d-%02d%c20%02d:%02d:%02d&location=%s&temperature=%ld&pressure=%ld&humidity=%ld&illumination=%ld HTTP/1.1", 
      year(), month(), day(), '%', hour(), minute(), second(), location, mcp_f, bmp_p, dht_h, llux);
    Serial.println(buffer);
    httpRequest(buffer);
  }
}

My other suggestion is that before trying to debug your Arduino code, you TELNET into your web server and manually type in the same text you think you are trying to send via your Arduino to ensure the problem is not in the header/text you are sending to the server.

xSeriaLx: I'm trying to call a php file to change a Field in my database hosted in www.hostinger.fr

That seems reasonable. What's the problem?

i can't connect into my host when i do " http://smarthousesfax.bl.ee/porteon.php " the field is changed in the database but i can't do it directly from arduino how can i connect to porteon.php

Using the Arduino to use a domain name is more complicated that you really need (have to do a DNS lookup), so I suggest you use the sites IP address. Below is how I obtained that address for your site.

wandrson@delphi:~$ ping smarthousesfax.bl.ee
PING smarthousesfax.bl.ee (31.170.164.54) 56(84) bytes of data.
64 bytes from 31.170.164.54: icmp_seq=1 ttl=49 time=154 ms
64 bytes from 31.170.164.54: icmp_seq=2 ttl=49 time=154 ms
64 bytes from 31.170.164.54: icmp_seq=3 ttl=49 time=153 ms
64 bytes from 31.170.164.54: icmp_seq=4 ttl=49 time=152 ms
--- smarthousesfax.bl.ee ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 7408ms
rtt min/avg/max/mdev = 152.203/153.780/154.857/1.147 ms

In the example code I provided earlier I demonstrated how to use this IP address to build a HTTP request to that PHP page.

...

char memoria[] = "192.168.0.100";   // <-- Change this to 31.170.164.54

...

void httpRequest(char *query) {
  // if there's a successful connection:
  if (client.connect(memoria, http_port)) {
    // send the HTTP GET request:
    client.println(query);
    Serial.println(query);
    client.println("Host: 192.168.0.100");  // <-- Change this to 31.170.164.54
    client.println("User-Agent: arduino");
    client.println("Connection: close");    
    client.println();
    delay(50);
    client.stop();
  } 
  else {
    // if you couldn't make a connection:
    Serial.println("connection failed");
    Serial.println("disconnecting.");
    client.stop();
  }

...

// Change this sprintf to use your php file and provide it with whatever parameters it expects.
// like "GET porteon.php?P1=...

sprintf(buffer, "GET /apps/add_env.php?rec_date=%04d-%02d-%02d%c20%02d:%02d:%02d&location=%s&temperature=%ld&pressure=%ld&humidity=%ld&illumination=%ld HTTP/1.1", 
      year(), month(), day(), '%', hour(), minute(), second(), location, mcp_f, bmp_p, dht_h, llux);
    Serial.println(buffer);
    httpRequest(buffer);

The OP sent me an IM asking about some problems they had with my suggestion. I think my response may prove helpful to others, so I am copying it here:

You made a couple of mistakes in your code, it should look like this:

// if there's a successful connection:
  if (client.connect(serveur, 80)) {
    // send the HTTP GET request:
    Serial.println("Connected");    
    client.println("GET /porteon.php HTTP/1.1");
    client.println("Host: 31.170.164.54");  // <-- Change this to 31.170.164.54
    client.println("User-Agent: arduino");
    client.println("Connection: close");    
    client.println();
    delay(50);
    client.stop();
  } 
  else {
    // if you couldn't make a connection:
    Serial.println("connection failed");
    Serial.println("disconnecting.");
    client.stop();

And as I mentioned earlier, you can use telnet to test the text exchange. Here is a telnet session I just did using the same text sequence that the above code sends.

wandrson@delphi:~$ telnet 31.170.164.54 80
Trying 31.170.164.54...
Connected to 31.170.164.54.
Escape character is '^]'.
GET /porteon.php HTTP/1.1
Host: 31.170.164.54
User-Agent: arduino
Connection: close

Connection closed by foreign host.
wandrson@delphi:~$

I also tested the same process with the php script you used in your email

wandrson@delphi:~$ telnet 31.170.164.54 80
Trying 31.170.164.54...
Connected to 31.170.164.54.
Escape character is '^]'.
GET /venton.php HTTP/1.1
Host: 31.170.164.54
User-Agent: arduino
Connection: close

Connection closed by foreign host.
wandrson@delphi:~$

As you can see, both are successful. Using telnet us a great way to test the connection protocol, since it can give more feedback than you get when programming it through the Arduino.

For instance if you were to try it with the text sequence you used you get the following result:

wandrson@delphi:~$ telnet 31.170.164.54 80
Trying 31.170.164.54...
Connected to 31.170.164.54.
Escape character is '^]'.
Host: 31.170.164.54
HTTP/1.1 400 Bad Request
Server: nginx
Date: Fri, 23 May 2014 22:31:01 GMT
Content-Type: text/html
Content-Length: 166
Connection: close

<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx</center>
</body>
</html>
Connection closed by foreign host.
wandrson@delphi:~$

In spending some time this evening trying to help OP we have narrowed down the problem to something not with the Arduino code, but rather his server. I have never seen the problem before so I thought I would present it and see if anyone has any suggestions.

In a web browser if we type this:

http://smarthousesfax.bl.ee/porteon.php

it maps the address to:

smarthousesfax.bl.ee/porteon.php

and returns

connected
changement avec succes

however, if we instead use the ip address we get from pinging smarthousesfax.bl.ee

wandrson@delphi:~$ ping smarthousesfax.bl.ee
PING smarthousesfax.bl.ee (31.170.164.54) 56(84) bytes of data.
64 bytes from 31.170.164.54: icmp_seq=1 ttl=49 time=154 ms
64 bytes from 31.170.164.54: icmp_seq=2 ttl=49 time=154 ms
64 bytes from 31.170.164.54: icmp_seq=3 ttl=49 time=153 ms
64 bytes from 31.170.164.54: icmp_seq=4 ttl=49 time=152 ms
--- smarthousesfax.bl.ee ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 7408ms
rtt min/avg/max/mdev = 152.203/153.780/154.857/1.147 ms

and enter
http://31.170.164.54/porteon.php
it again drops the http:// and returns what you can see in the attached image

I have no idea what might be causing this problem