I have problem with sendind data from arduino UNO with wifi into mySQL database. Actually I know how to send data because when I put in my php file something like this:
$sql = "INSERT INTO data (test1,test2) VALUES ('54', '32')";
My database receive this record and I see it. Problem comes when I want to send to database for example int value
from my Arduino sketch. I'm able to send record to database but value is always 0 and I dont know why.
Here is my PHP file and Arduino sketch:
#include <SPI.h>
#include <WiFiNINA.h>
#include <WiFi.h>
char ssid[] = "***"; // your network SSID (name)
char pass[] = "***"; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key Index number (needed only for WEP)
int status = WL_IDLE_STATUS;
WiFiServer server(80);
WiFiClient client;
IPAddress serverip(******************);
String data = "";
int t = 123;
void setup() {
Serial.begin(9600);
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
// don't continue
while (true);
}
String fv = WiFi.firmwareVersion();
if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
Serial.println("Please upgrade the firmware");
}
// attempt to connect to Wifi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to Network named: ");
Serial.println(ssid); // print the network name (SSID);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
printWifiStatus();
data = "temp1=" + t;
if (client.connect(serverip, 80)) {
Serial.println("--> connection ok\n");
client.println("POST /mydatabase/test.php HTTP/1.1");
client.println("Host: ******");
client.print("Content-Length: ");
client.println(data.length());
client.println();
client.print(data);
Serial.println("Successfull");
client.stop();
Serial.println("--> finished transmission\n");
}
else {
Serial.println("--> connection failed\n");
}
void loop() {
//XXXXX
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your board'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");
// print where to go in a browser:
Serial.print("To see this page in action, open a browser to http://");
Serial.println(ip);
}
And PHP :
<?php
$servername = "hostname";
$database = "databaseName";
$username = "user";
$password = "pass";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $database);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
$sql = "INSERT INTO data (test1) VALUES ('".$_POST["temp1"]."')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "
" . mysqli_error($conn);
}
mysqli_close($conn);
So when I use code like this, record which come to database have always value of 0 instead of value of "t" which is 123.
wildbill:
Hit your php page from a browser. What do you have to send it there to get data into the db?
When i hit php file from broweser i get something like this:
Parse error: syntax error, unexpected '"', expecting '-' or identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:\xampp\htdocs\**** on line 15
So line 15 refer to this:
$sql = "INSERT INTO data (test1) VALUES ('$_POST["temp1"]')";
What was the string you used in the browser? I'd suggest that you need to get that working before you worry about the Arduino. Much easier to debug too.
wildbill:
What was the string you used in the browser? I'd suggest that you need to get that working before you worry about the Arduino. Much easier to debug too.
I don't clearly understand what you want me to do. When i hit this php with my browser:
<?php
$servername = "localhost";
$database = "voik3";
$username = "voik3";
$password = "xxxxx";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $database);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
$sql = "INSERT INTO data (test1) VALUES ('54')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "
" . mysqli_error($conn);
}
mysqli_close($conn);
I recieve record in mydatabase in column test1 with value of 54. So it is working fine. But i want to send variable value from arduino into this database so i put something like in my first post but it send 0 instead of value of "t"
To be able to type URL on a browser command line, I believe the HTTP method has to be GET not POST. GET is certainly easier for exactly this kind of trouble shooting and, in this case, I would consider changing to GET.
6v6gt:
To be able to type URL on a browser command line, I believe the HTTP method has to be GET not POST. GET is certainly easier for exactly this kind of trouble shooting and, in this case, I would consider changing to GET.
wildbill:
You need to pass a parameter to the page. You've hard coded it to be 54. You need something like this stolen example from w3schools:
/test/demo_form.php?name1=value1&name2=value2
And then you'll need the corresponding PHP to grab the parameter & use it in your SQL.
After hitting this php in browser :
<?php
$servername = "hostname";
$database = "databaseName";
$username = "user";
$password = "pass";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $database);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
$sql = "INSERT INTO data (test1) VALUES ('".$_GET["temp1"]."')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "
" . mysqli_error($conn);
}
mysqli_close($conn);
I put in browser something like this:
http://xxxx/test/demo_form.php?temp1=4
And my record with value 4 has been added to database. But how I have to change my Arduino code to post it from there?