Go Down

Topic: i have small problem (Read 1 time) previous topic - next topic

wallaa_alebady

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);
}

PaulS

Code: [Select]
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.

Code: [Select]
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.
The art of getting good answers lies in asking good questions.

PaulMurrayCbr

Quote
cliente.print("GET/sensores/insert.php?");
Need space after the GET.  https://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html#sec5.1
http://paulmurraycbr.github.io/ArduinoTheOOWay.html

wallaa_alebady

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

TomGeorge

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.. :)
Everything runs on smoke, let the smoke out, it stops running....

J-M-L

#5
Oct 12, 2016, 02:07 pm Last Edit: Oct 12, 2016, 03:25 pm by J-M-L
Your HTTP request is not well formed

try building a string that looks like this

GET /sensores/insert.php?sensor1=125&sensor2=17&sensor3=228 HTTP/1.0\r\nHost: wallaa_alebady.com\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
Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums
Pas de messages privés SVP

PaulS

Quote
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.

Quote
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.
The art of getting good answers lies in asking good questions.

wallaa_alebady

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

but when replace this instruction
Code: [Select]
IPAddress server(192,168,0,102);
with this
Code: [Select]
char server[] = "192,168,0,102";

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

tne code become as this

Code: [Select]

#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

PaulS

Code: [Select]
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.
The art of getting good answers lies in asking good questions.

wallaa_alebady

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

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

PaulS

Quote
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.
The art of getting good answers lies in asking good questions.

J-M-L

#11
Oct 12, 2016, 03:36 pm Last Edit: Oct 12, 2016, 03:38 pm by J-M-L
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:

Code: [Select]
<?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)

date_default_timezone_set('Europe/London');
setlocale(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.
Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums
Pas de messages privés SVP

PaulS

Quote
before playing with the database, try using this instead of your insert.php on server side:
Where does that create the file?
The art of getting good answers lies in asking good questions.

wallaa_alebady

#13
Oct 12, 2016, 04:20 pm Last Edit: Oct 12, 2016, 04:25 pm by wallaa_alebady
this is my php file (insert.php)
Code: [Select]

<?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



J-M-L

#14
Oct 12, 2016, 04:24 pm Last Edit: Oct 12, 2016, 04:28 pm by J-M-L
Where does that create the file?
$fileStatus = file_put_contents('myFile.txt',$fileContent,FILE_APPEND);

Description=

int file_put_contents ( 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.
Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums
Pas de messages privés SVP

Go Up