i have small problem

excuse me >>i need help from you if you can…

i have no. of sensores and i want to read its values and store it to mysql database on apache server
but when i try my code there is problem in connection …

the result on serial monitor is ((disconnected))…

this is my code :

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

byte mac = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
IPAddress ip( 192,168,0,104 );
IPAddress server(192,168,0,102);
byte gateway={192,168,0,1};
byte subnet={255,255,255,0};

float sensor1 =0;
float sensor2 =0;
float sensor3 =0;

EthernetClient cliente;

void setup()
{
Serial.begin(9600);
Ethernet.begin(mac,ip,gateway,subnet);
Serial.println(Ethernet.localIP());
Serial.println(Ethernet.gatewayIP());
Serial.println(Ethernet.subnetMask());
}

void loop()
{
if( cliente.connect (server,8095))
{ Serial.println(“connected”);

sensor1 =sensor3 +5;
sensor2=sensor1+5;
sensor3=sensor2+5;

// Make a HTTP request:
cliente.print(“GET/sensores/insert.php?”);
cliente.print(“sensor1=”);
cliente.print(sensor1);
cliente.print("&sensor2=");
cliente.print(sensor2);
cliente.print("&sensor3=");
cliente.println(sensor3);
cliente.println(" HTTP/1.1") ;
cliente.println(“Host: 192,168,0,102”);
cliente.println(“Connection: close”);

Serial.print("sensor1= ");
Serial.println(sensor1);
Serial.print(“sensor2=”);
Serial.println(sensor2);
Serial.print(“sensor3=”);
Serial.println(sensor3);

// cliente.stop();

cliente.stop();

}else {Serial.println(“disconnected”);
cliente.stop();
}

delay(10000);
}

Ethernet.begin(mac,ip,gateway,subnet);

Why do you feel it necessary to use the 4 argument begin() method? For connecting to a local server, the two argument version is more usual.

cliente.print("GET/sensores/insert.php?");
    cliente.print("sensor1=");
    cliente.print(sensor1);
    cliente.print("&sensor2=");
    cliente.print(sensor2);
    cliente.print("&sensor3=");
    cliente.println(sensor3);
   cliente.println(" HTTP/1.1") ;
    cliente.println("Host: 192,168,0,102");
    cliente.println("Connection: close");

The " HTTP/1.1" bit needs to be part of the same record as the "GET", so the println() for sending the sensor3 value is wrong.

cliente.print("GET/sensores/insert.php?");

Need space after the GET. https://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html#sec5.1

thank you my dear paulS ... but the problem don't in send http request .. the problem in connection between the arduino and server ..

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks… Tom… :slight_smile:

Your HTTP request is not well formed

try building a string that looks like this

GET /sensores/insert.php?sensor1=[color=blue]125[/color]&sensor2=[color=blue]17[/color]&sensor3=[color=blue]228[/color] HTTP/1.0\r\nHost: [color=red]wallaa_alebady.com[/color]\r\nUser-Agent: Arduino\r\n

the User-Agent might not be needed but does not hurt with some web servers

the blue parts are your dynamic values the red part is whatever you want to represent your domain as - does not matter much probably

Your HTTP request is not well formed

Until OP can connect to the server, that doesn't really matter. It does need to be corrected, but it is the second priority.

the problem in connection between the arduino and server ..

Have you changed to the two argument begin() call? If so, that is a code change, and you need to post your modified code.

my dear <>
my problem in previous code is with a connection between arduino and my server

but when replace this instruction

IPAddress server(192,168,0,102);

with this

char server[] = "192,168,0,102";

the connection problem is solved but the data don’t inserted in mysql

tne code become as this

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

byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };

IPAddress ip( 192,168,0,104 );

char server[] = "192,168,0,102";
   
   
EthernetClient cliente;

void setup() 
{ 
 Serial.begin(9600);
 Ethernet.begin(mac,ip);
   Serial.println(Ethernet.localIP());    
}

void loop() 
{
 if( cliente.connect (server,8095))
{  Serial.println("connected");
    
    
cliente.print("GET /sensores/insert.php??sensor1=125&sensor2=17&sensor3=228 HTTP/1.0\r\nHost: 192.168.0.102\r\n");       
 

cliente.stop();


}else {Serial.println("disconnected");
cliente.stop();
} 

delay(10000);
}

with my respect

cliente.print("GET /sensores/insert.php??sensor1=125&sensor2=17&sensor3=228 HTTP/1.0\r\nHost: 192.168.0.102\r\n");

ONE question mark to separate the name = value pairs from the script name.

thank you <>
but also after remove one of question mark the data don’t inserted in mysql

cliente.print("GET /sensores/insert.php?sensor1=125&sensor2=17&sensor3=228 HTTP/1.0\r\nHost: 192.168.0.102\r\n");

but also after remove one of question mark the data don't inserted in mysql

So, that would indicate that there is a problem with the PHP script - either it is not at the specified location or it is not getting the data it expects or it can not access the base or the command to insert the data is wrong.

The server generates a response. Rather than ignoring the response, you should read and print it. There just might be a clue waiting to whack you.

well then show us the PHP code on the other side - the data won’t go magically into SQL…

before playing with the database, try using this instead of your insert.php on server side:

<?php
date_default_timezone_set('Europe/London');
setlocale(LC_ALL, 'en_GB');

$var1 = $_GET['sensor1'];
$var2 = $_GET['sensor2'];
$var3 = $_GET['sensor3'];

$currTime = time();
$fileContent =  $currTime ."\t" .$var1 ."\t" .$var2 ."\t" .$var3 ."\n";

$fileStatus = file_put_contents('myFile.txt',$fileContent,FILE_APPEND);

if ($fileStatus != false) {
echo "OK";
} else {
echo "FAIL";
}
?>

change the settings at the beginning to match your region / language (cf the links to doc)

[url=http://php.net/manual/en/timezones.php]date_default_timezone_set[/url]('Europe/London');
[url=http://php.net/manual/en/function.setlocale.php]setlocale[/url](LC_ALL, 'en_GB');

If your client works fine, that will create a file on the server side with 1 line per execution of your GET request with the time info and the values of the 3 sensors and return OK to the client.

before playing with the database, try using this instead of your insert.php on server side:

Where does that create the file?

this is my php file (insert.php)

<?php
 
 $server="localhost";
 $user="****";
 $pass="********";
$db="advphp";
	   	
		$connection = mysql_connect($server, $user, $pass);

		if (!$connection) {
	    	die('MySQL ERROR: ' . mysql_error());
		}
		
		mysql_select_db($db) or die( 'MySQL ERROR: '. mysql_error() );

$sensor1=$_GET['sensor1'];
$sensor2=$_GET['sensor2'];
$sensor3=$_GET['sensor3'];


$sql_insert= "INSERT INTO sensores (sensor1,sensor2,sensor3)VALUES ('$sensor1','$sensor2','$sensor3') ";

mysql_query($sql_insert ,$connection);

if($sql_insert)
{
    echo "the data inserted";
    /*echo $sensor1 ;
      echo $sensor2 ;
        echo $sensor3 ; */
       
}else
    {
        echo "there is error in insertion";
        
    }

?>

and it is work fine where when i try to insert data from browser the data is inserted
but the problem when insert try to insert data from arduino

PaulS: Where does that create the file?

$fileStatus = [color=green]file_put_contents[/color]('myFile.txt',$fileContent,[color=blue]FILE_APPEND[/color]);

Description=

int [color=green]file_put_contents[/color] ( string $filename , mixed $data [, int $flags = 0 [, resource $context ]] )

This function is identical to calling fopen(), fwrite() and fclose() successively to write data to a file. If filename does not exist, the file is created. Otherwise, the existing file is overwritten, unless the FILE_APPEND flag is set.

quick and easy way to do logging.

but the problem when insert try to insert data from arduino

Where do the echo statements go? The server returns all of the output from the script, along with other stuff, to the client. Why are you ignoring the server's response?

wallaa_alebady: this is my php file (insert.php)

...

and it is work fine where when i try to insert data from browser the data is inserted but the problem when insert try to insert data from arduino

OK then your problem is that you do not reach your server properly...

As PaulS suggested, listen to what's coming back from the server and print it. you'll get a clue

J-M-L: $fileStatus = [color=green]file_put_contents[/color]('myFile.txt',$fileContent,[color=blue]FILE_APPEND[/color]);

Description=

int [color=green]file_put_contents[/color] ( string $filename , mixed $data [, int $flags = 0 [, resource $context ]] )

This function is identical to calling fopen(), fwrite() and fclose() successively to write data to a file. If filename does not exist, the file is created. Otherwise, the existing file is overwritten, unless the FILE_APPEND flag is set.

I assume (yeah, I know what that means) then, that 'myFile.txt' should be something like 'C:\Temp\myFile.txt' or' \home\me\myFile.txt'.

PaulS: I assume (yeah, I know what that means) then, that 'myFile.txt' should be something like 'C:\Temp\myFile.txt' or' \home\me\myFile.txt'.

Well in theory it's better :) but not necessarily - it will search for myFile.txt in the include directories defined by the include_path = a list of directories where the functions such as include, fopen(), file(), etc look for files.

standard PHP include_path on Unix usually looks like this include_path=".:/php/includes" and using a . in the include path allows for relative includes as it means the current directory.

So file_put_contents will check the first path, and if it doesn't find myFile.txt, check the next path, until it either locates it or returns with a warning or an error. upon first call that's what happens and the file gets created locally. then subsequent calls find the file there and just a happened the data at the end.

So you are right - this is somewhat specific to my settings (could be more challenging if you have enabled safe mode or open_basedir)

@wallaa - sorry polluting a bit your thread…