28-07-2020, 21:41
Witam witam, prosze o pomoc z połączeniem się arduino z serwerem mysql używając php do zapisania danych do bazy przy użyciu GET tutaj daje kod arduino i php nie wiem dlaczego ale nie chce mi to działać prosze pomóżcie bo już psychicznie wymiękam :| PS jak mam załączone arduino to client chyba się łączy z serwerem ale nie wiem czy dobrze jest to napisane i czy też nie muszę otwierać portów itp gdyż serwer jest na zewnętrznym hostingu.
Kod PHP:
<?php
// Prepare variables for database connection
require_once "connectconfig.php";
// Connect to your database
$conn = new mysqli($host, $db_user, $db_password, $db_name);
$variable = $_GET['variable'];
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO data (variable) VALUES ($variable)";
//sql = "INSERT INTO MyGuests (firstname, lastname, email)
//VALUES ('John', 'Doe', 'john@example.com')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
// Prepare the SQL statement
echo $variable;
// Execute SQL statement
?>
Kod:
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
// Enter the IP address for Arduino, as mentioned we will use 192.168.0.16
// Be careful to use , insetead of . when you enter the address here
IPAddress ip(192, 168, 1, 3);
int var1 = 1111; // Here we will place our reading
char server[] = "www.ardunoticz.5v.pl"; // IMPORTANT: If you are using XAMPP you will have to find out the IP address of your computer and put it here (it is explained in previous article). If you have a web page, enter its address (ie. "www.yourwebpage.com")
// Initialize the Ethernet server library
EthernetClient client;
void setup() {
// Serial.begin starts the serial connection between computer and Arduino
Serial.begin(9600);
// start the Ethernet connection
Ethernet.begin(mac, ip);
}
void loop() {
// Connect to the server (your computer or web page)
if (client.connect(server, 80)) {
client.println("GET /index.php?variable=11111111 HTTP/1.1"); // This
//client.print("variable="); // This
// client.print(var1); // And this is what we did in the testing section above. We are making a GET request just like we would from our browser but now with live data from the sensor
// client.println(""); // Part of the GET request
// client.println("www.ardunoticz.5v.pl"); // IMPORTANT: If you are using XAMPP you will have to find out the IP address of your computer and put it here (it is explained in previous article). If you have a web page, enter its address (ie.Host: "www.yourwebpage.com")
client.println("Connection: close"); // Part of the GET request telling the server that we are over transmitting the message
// client.println(); // Empty line
// client.println(); // Empty line
//
client.stop(); // Closing connection to server
Serial.println("Connection Sucessfull");
}
else {
// If Arduino can't connect to the server (your computer or web page)
Serial.println("--> connection failed\n");
}
// Give the server some time to recieve the data and store it. I used 10 seconds here. Be advised when delaying. If u use a short delay, the server might not capture data because of Arduino transmitting new data too soon.
delay(120000);
}