Hello,
i have created a database in mysql, and i'm trying to send data from the database to my arduino, but i'm not able to make it work.
The code i'm using in my arduino function is:
int readSessionId() {
client.stop();
char idSes=0;
if (client.connect(serverObj, port)) { // connection to the SERVER address and PORT specified
Serial.println("\nConnected to the server!");
client.println("GET /AWSR/retrySessionID.php HTTP/1.1");
if (client.connected()) {
char c = client.read();
Serial.print("session id received: ");
Serial.print(c); // to see the value
}
idSes=client.read();
Serial.print("session id received: ");
Serial.println(idSes);
} else { Serial.println("\nFailed when connecting to the server");}
int temp=idSes - '0';
client.stop();
return temp;
}
and the code in retrySessionID.php is:
<?php
//function that creates new session and sends the id to the client
include("connect.php"); //file that manages the connection settings to the database
$link=Connection(); //function that connects with the database (specified at connect.php)
$timestamp = date('Y-m-d G:i:s');
$query = "INSERT INTO `sessions` VALUES (default,'".$timestamp."')";
$result=mysqli_query($link,$query) or die('Error:'.mysqli_error($link));
$idExp=mysqli_insert_id($link);
echo ("$idExp");
echo "\n";
?>
when I access to the php directly from the web browser it works, it shows the desired data and adds a row to the table in mysql. Nevertheless, when accessing from the arduino I get this in the terminal:
Connected to the server!
session id received: ÿsession id received: ÿ
And no new row is added to the database.
Can you help me with this issue?? If tried many things i've seen in different examples, but nothing works.
thanks
it shows the desired data
Which is?
client.println("GET /AWSR/retrySessionID.php HTTP/1.1");
if (client.connected()) {
char c = client.read();
There are TWO conditions that need to be satisfied BEFORE you read anything. Look more closely at the examples, if you can't figure out what the other condition is.
Is the "desired data" really one character?
PaulS,
i guess you are referring to the second condition client.available(), but it doesn't work either. If using:
int readSessionId() {
client.stop();
char idSes=0;
if (client.connect(serverObj, port)) { // connection to the SERVER address and PORT specified
Serial.println("\nConnected to the server!");
client.println("GET /AWSR/retrySessionID.php HTTP/1.1");
if (client.connected() && client.available()) {
char c = client.read();
Serial.print("session id received: ");
Serial.print(c); // to see the value
}
idSes=client.read();
Serial.print("session id received: ");
Serial.println(idSes);
} else { Serial.println("\nFailed when connecting to the server");}
int temp=idSes - '0';
client.stop();
return temp;
}
what I see through the terminal is:
Connected to the server!
session id received: ÿ
So it seems it doesn't get into the loop...i don't know why...
I'm still not able to solve this problem. I've tried this:
Arduino code:
//function that connects to retrySessionID.php to request the session Id from the database
int readSessionId(void) {
client.stop();
char idSes=0;
char c[100];
if (client.connect(serverObj, port)) { // connection to the SERVER address and PORT specified
Serial.println("\nConnected to the server in function readSessionId!");
client.println("POST /AWSR/retrySessionID.php HTTP/1.1");
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
for (int k=0; k<100;k++) {
c[k+1] = client.read();
Serial.print(c[k+1]);
}
//idSes=client.read();
Serial.print("session id received: ");
for (int k=0; k<100;k++) {
Serial.print(c[k+1]);
}
//Serial.println(idSes);
}}
} else { Serial.println("\nFailed when connecting to the server");}
int temp=idSes - '0';
client.stop();
return temp;
}
retrySessionID.php file
<?php
//function that creates new session and sends the id to the client
include("connect.php"); //file that manages the connection settings to the database
$link=Connection(); //function that connects with the database (specified at connect.php)
$timestamp = date('Y-m-d G:i:s');
$query = "INSERT INTO `sessions` VALUES (default,'".$timestamp."')";
$result=mysqli_query($link,$query) or die('Error:'.mysqli_error($link));
$idExp=mysqli_insert_id($link);
echo ("$idExp");
echo "\n";
?>
i've created a secnod php, that by accessing from the web browser, i'm able to see the variable $idExp, so I would say that the php code is right...but maybe is there the problem.
On the Arduino, what I see printed in the terminal is:
Connected to the server in function readSessionId!
but the systems gets blocked in the loop...so nothing is receiving from the webserver. So, my question is: using the echo in php, am i sending a post or get message? or none of them? in that case, how should I use it? i've found a lot of examples to send from the arduino to the webserver, but not the opposite...and i'm completely blocked...
Finally, I could figured out how to solve the problem. When sending the request it has to contain th whole HTTP header. So the code that Works is:
int readSessionId(void) {
client.stop();
int idSes=0;
char c[90];
if (client.connect(serverObj, port)) { // connection to the SERVER address and PORT specified
Serial.println("\nConnected to the server in function readSessionId!");
client.println("GET /AWSR/retrySessionID.php HTTP/1.1");
client.print("Host: "); // SERVER ADDRESS
client.println(serverObj);
client.print("Connection:close\r\n\r\n");
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
for (int k=0; k<90;k++) {
c[k+1] = client.read();
Serial.print(c[k+1]);
}
Serial.print("session id received: ");
for (int k=0; k<90;k++) {
if (c[k]=='i' && c[k+1]=='d' && c[k+2]=='E' && c[k+3]=='x' && c[k+4]=='p') { //looking for the chain idExp=''
if (c[k+8]==39) { //when idExp contains one digit notice that char(39)='
idSes=c[k+7]-'0'; //idSes takes the value of the session
} else if (c[k+9]==39){ //if idExp contains two digits notice that char(39)='
idSes=(c[k+7]-'0')*10 + (c[k+8]-'0'); //idSes takes the value of the session considering is two digits
} else if (c[k+10]==39){ //if idExp contains three digits notice that char(39)='
idSes=(c[k+7]-'0')*100 + (c[k+8]-'0')*10+(c[k+9]-'0');
}
}}
Serial.println(idSes);
}}
} else { Serial.println("\nFailed when connecting to the server");}
client.stop();
return idSes;
}
thanks.
if (client.available()) { // if there's bytes to read from the client,
for (int k=0; k<90;k++) {
c[k+1] = client.read();
Serial.print(c[k+1]);
}
If the client has one byte available to read, you look pretty silly reading, and storing, all 90 of them.