Reading this article:
http://www.icreateproject.info/uncategorized/arduino-save-data-to-database/
I have the following issues...
-
What is the use of 192.168.0.16 IF I use xampp?
-
I managed to send data to the databave via write_data.php :
http://localhost/write_data.php?value=5555 (the value 55 is completely random just for example)
and display them via get_data.php :
http://localhost/get_data.php (here I see the value 555 in ID and timestamps)
Very well up to here.
- My problem is that I can't send data(the status of digital pin 3) via the Arduino sketch to write_data.php, instead of writing in the URL: localhost/write_data.php?value=555 . (In the Serial monitor says: "failed connection", meaning that something is going wrong in my sketch)
My Arduino sketch simply reads the digitalPin 3. So I want to store in the db the on/off (0/1) values
My IP4v is: 192.168.1.2 Should I use it? How should my Arduino sketch be??
My skecth:
#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,178); // WHAT FOR???????????????????????????
int readingPin = 3;
int ledStatus;
EthernetClient client;
char server[] = "localhost"; // 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”)
void setup() {
Serial.begin(9600);
Ethernet.begin(mac);
}
void loop() {
ledStatus = digitalRead(readingPin);
if (client.connect(server, 80)) {
client.print("GET /write_data.php?");
client.print("value=");
client.print(ledStatus);
client.println(" HTTP/1.1"); // Part of the GET request
client.println(" Host:"); // 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("localhost"); // Is it right???
client.println("Connection: close");
client.println(); // Empty line
client.println(); // Empty line
client.stop(); // Closing connection to server
}
else {
// If Arduino can’t connect to the server (your computer or web page)
Serial.println("–> connection failed/n");
}
delay(10000);
}
My write_data.php file:
<?php // Prepare variables for database connection $dbusername = "arduino"; // enter database username, I used “arduino” in step 2.2 $dbpassword = "arduinotest"; // enter database password, I used “arduinotest1” in step 2.2 $server = "localhost"; // IMPORTANT: if you are using XAMPP enter “localhost”, but if you have an online website enter its address, ie.”www.yourwebsite.com” // Connect to your database $dbconnect = mysql_pconnect($server, $dbusername, $dbpassword); $dbselect = mysql_select_db("test",$dbconnect); // Prepare the SQL statement $sql = "INSERT INTO test.sensor (value) VALUES ('".$_GET["value"]."')"; // Execute SQL statement mysql_query($sql); echo 'GET value = ' . htmlspecialchars($_GET["value"]) . ''; ?>My get_data.php file:
$url=$_SERVER['REQUEST_URI'];
header("Refresh: 5; URL=$url"); // Refresh the webpage every 5 seconds
?>
Led Status readings
<?php // Connect to database $con=mysqli_connect("localhost","arduino","arduinotest","test"); // Retrieve all records and display them $result = mysqli_query($con,'SELECT * FROM sensor ORDER BY id DESC'); // Process every record while($row = mysqli_fetch_array($result)) { echo ""; echo ""; echo ""; echo ""; echo ""; } If anyone can help... :( Trying hours reading many posts but nothing...ID | Timestamp | Value |
" . $row['id'] . " | " . $row['time'] . " | " . $row['value'] . " |