Hello everyone,I'm a newbie in Arduino and php.I have some problems in my project. I want to send my Arduino sensor value to my mysql and use a display php to show it on browser.It looks like success but it doesn't get any value from my Arduino.
Here are the codes I've been using:
Arduino:
#include <WiFiRM04.h>
char ssid[] = "TP......";
char password[] = "1......";
int keyIndex = 0;
int status = WL_IDLE_STATUS;
WiFiRM04Client client;
char server[] = "120......";
String yourdatacolumn = "yourdata=";
String yourdata;
void setup() {
Serial.begin(115200);
Serial.println("Wait for RM04 to be ready");
int count = 40;
do {
Serial.println(count);
delay(1000);
}while(--count);
connectWifi();
// You're connected now, so print out the status
printWifiStatus();
gas();
}
void loop() {
}
void connectWifi() {
// Attempt to connect to wifi network
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid, password);
// Wait 10 seconds for connection
delay(10000);
}
}
void printWifiStatus() {
// Print the SSID of the network you're attached to
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// Print your WiFi shield's IP address
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// Print the received signal strength
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
// This method makes a HTTP connection to the server and POSTs data
void gas(){
// Combine yourdatacolumn header (yourdata=) with the data recorded from your arduino
// (val) and package them into the String yourdata which is what will be
// sent in your POST request
int val;
Serial.print("Value:");
val=analogRead(0);//Read Gas value from analog 0
Serial.println(val,DEC);//Print the value to serial port
delay(1000);
yourdata = yourdatacolumn + val;
if (client.connect(server, 80)) {
Serial.println("connecting...");
// EDIT: The POST 'URL' to the location of your insert_mysql.php on your web-host
client.println("POST /ArduinoData/insert_mysql.php HTTP/1.1");
// EDIT: 'Host' to match your domain
client.println("Host: 120......");
client.println("User-Agent: Arduino/1.0");
client.println("Connection: close");
client.println("Content-Type: application/x-www-form-urlencoded;");
client.print("Content-Length: ");
client.println(yourdata.length());
client.println();
client.println(yourdata);
}
else {
// If you couldn't make a connection:
Serial.println("Connection failed");
Serial.println("Disconnecting.");
client.stop();
}
}
This is an insert php to add value to mySQL
<?php
foreach ($_REQUEST as $key => $value)
{
if($key == "yourdata") {
$yourdata = $value;
}
}
include ("mysql_connect.php");
$yourdata = $_POST['yourdata'];
$date_test=date ("Y-m-j H:i:s");
echo $yourdata;
echo $date_test;
// Check Connection to Database
$sjj = "INSERT INTO arduinotest (yourdata,timestamp) values ('$yourdata','$date_test')";
$skk=mysql_query ("$sjj")or die ("mysql_query error2");
?>
I POST to my insert php seem successful but I show the value on display php and it shows zero.It seems I successfully POST to the insert php but mysql doesn't get the value from Arduino.
I have no idea how to deal with it.I would really appreciate some help from anyone who has experience of these kind of things.
POST is generally used when you don't want anyone sniffing the data to see the data. Why is your temperature data secret? A GET request is far simpler.
When you send data to a script on a server, you get a response. Ignoring the response is stupid. Perhaps there's a clue-by-four waiting to whack you.
Debugging by guesswork sucks. Use the available information to help you.
Not only is a GET request simpler, you can create one in your browser and "bookmark" it for testing later - just create the link to the page you are trying to send data to, and append "?" and the variable data you expect to send (look at a GET example - I have been cleaning my room here and I'm not sure just where I filed my examples :o ). That allows you to test the server without the Arduino involved. Once you get that working, then get the Arduino to send the same stuff. That is what I am going to do with my temperature data when I send it from my little heater controllers. Log it on the server with a timestamp.
gpsmikey:
Not only is a GET request simpler, you can create one in your browser and "bookmark" it for testing later - just create the link to the page you are trying to send data to, and append "?" and the variable data you expect to send (look at a GET example - I have been cleaning my room here and I'm not sure just where I filed my examples :o ). That allows you to test the server without the Arduino involved. Once you get that working, then get the Arduino to send the same stuff. That is what I am going to do with my temperature data when I send it from my little heater controllers. Log it on the server with a timestamp.
When I key a value in the browser , the insert php actually get the value.And the display php show the right value what I key. So I'm not sure that the POST is not easily to get a value than GET.Or just my data transfer between my Arduino and insert php can't communicate .
stone090104:
When I key a value in the browser , the insert php actually get the value.And the display php show the right value what I key. So I'm not sure that the POST is not easily to get a value than GET.Or just my data transfer between my Arduino and insert php can't communicate .
When you send data to a script on a server, you get a response. Ignoring the response is stupid. Perhaps there's a clue-by-four waiting to whack you.
Why are you not reading the response from the server, or the advice you are getting here? Why do you keep posting if you are not going to accept the advice offered?
PaulS:
POST is generally used when you don't want anyone sniffing the data to see the data. Why is your temperature data secret? A GET request is far simpler.
When you send data to a script on a server, you get a response. Ignoring the response is stupid. Perhaps there's a clue-by-four waiting to whack you.
Debugging by guesswork sucks. Use the available information to help you.
Thanks for your reply first.
I had seen the article which you reply PHP question - sending to mysql - Programming Questions - Arduino Forum
I used the same example with that questioner.In fact I didn't get the value like the example,but I think I actually POST to the insert php because the display php form show the zero in it.I'm confusing that whether is my variable between insert php and Arduino get wrong.But if it wrong why it show zero in the display php?Is the data pattern wrong?
I think I should try another way like you advice.I will try it ,thanks for your advice,PaulS.
PaulS:
Why would you want to send an array of pointers to MySQL?
I mean that I cant sent any string/char pointer variables using this mysql-connector (doubles, floats etc works fine). Only if I determine them first. Normally sprintf '%s' should do this but there is some kind of problem.
Normally sprintf '%s' should do this but there is some kind of problem.
You'll have to post some code that demonstrates that you are writing correct code but that still fails. I'm going to guess that you can't clear the first hurdle.