Go Down

Topic: arduino+mysql (help) (Read 6123 times) previous topic - next topic

MantasRS

Jan 13, 2011, 05:50 pm Last Edit: Jan 13, 2011, 05:52 pm by MantasRS Reason: 1
Hi I'm new here, and I faced with the problem. I found code witch allows  received sensor data sent to mysql. However, I have a 4 of sensors and want to transfer data to MySQL. Here is my code:
Code: [Select]
#include <Ethernet.h>
#include <SPI.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 77 };
byte gw[] = {192,168,1,83};
byte server[] = { 192, 168, 1, 83  }; // Server IP
byte subnet[] = { 255, 255, 255, 0 };
float data = 0;
float tempPin = 2;  // In This case the temperature is taken from pin 2 and sent to a sql server
float temperatura=0;
float sviesPin = 5;
float sviesa = 0;
void setup()
{

pinMode(tempPin, INPUT);
Serial.begin(9600);               // Used for serial debugging

}
void loop()
{

Serial.println("Program running...");

delay(500);
senddata();                                 // Data is sent every 5 seconds

}
void senddata()
{

data = analogRead(tempPin);           //Reading analog data
temperatura = ((data*500)/1024);
sviesa = analogRead(sviesPin);
Ethernet.begin(mac, ip, gw, subnet);
Client client(server, 80);
Serial.println();
Serial.println("ATE :)");
delay(1000);                                    //Keeps the connection from freezing

if (client.connect()) {
Serial.println("Connected");
client.print("GET /perkeliam.php?temperatura=");
client.print(temperatura);
Serial.print(temperatura);

client.println(" HTTP/1.1");
client.println("Host: 192.168.1.83");
client.println();
Serial.println();



client.print("GET /perkeliam.php?sviesa=");
Serial.print(sviesa);
client.print(sviesa);
client.println(" HTTP/1.1");
client.println("Host: 192.168.1.83");
client.println();
Serial.println();
                          }

else
{
Serial.println("Connection unsuccesfull");
}
//}
//stop client
client.stop();
while(client.status() != 0)
{
 delay(5000);
}
}

here's my PHP
Code: [Select]

<html>

<?php

//echo ("update_db.php indicator");
$DATA $_GET['temperatura'];
$DATA1 $_GET['sviesa'];



//Connect to database
$con mysql_connect("localhost""root""");
if(!
$con)
      &
#123;
      
die('Could not connect: ' .mysql_error());
      &
#125;
      
      
mysql_select_db("duomenys"$con);

mysql_query(" INSERT INTO temperatura (id, temp, sviesa, Data) VALUES ('NULL', $DATA$DATA1, NOW())");

mysql_close($con);

?>


</html>



It sends only the first value, while the other disappears who knows where. I would be very grateful for the help. Sorry for my English

PaulS

You can send all the data in one call:
GET /perkeliam.php?temperatura=valOne[glow]&nextName=valTwo&anotherName=anotherVal&lastName=lastValue[/glow]

Obviously, the GET request requires multiple client.print() statements (8 at least).

Then, the PHP script simply gets the 4 different values from the input string.

MantasRS

Thank you for fast answering. My knowledge is very poor could you show how it would look realized in the code?  :-[

maurobs

Can i ask a question?
You are using USB connect or ethernetshield?
Because i'm using php with USB connection but i have some trouble to read serial value
Thank a lot

MantasRS

im using ethernet shield

PaulS

Quote
could you show how it would look realized in the code?

Code: [Select]
client.print("GET /perkeliam.php?temperatura=");
client.print(temperatura);
client.print("&nextName=");
client.print(valTwo);
client.print("&anotherName=");
client.print(anotherVal);
client.print("&lastName=");
client.print(lastValue);

This assumes, of course, that the variables exist and are valued. And that the names mean something. Neither of these assumptions is true, but the structure is correct.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy